update-third-party.bash 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #=============================================================================
  2. # Copyright 2015-2016 Kitware, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #=============================================================================
  16. ########################################################################
  17. # Script for updating third party packages.
  18. #
  19. # This script should be sourced in a project-specific script which sets
  20. # the following variables:
  21. #
  22. # name
  23. # The name of the project.
  24. # ownership
  25. # A git author name/email for the commits.
  26. # subtree
  27. # The location of the thirdparty package within the main source
  28. # tree.
  29. # repo
  30. # The git repository to use as upstream.
  31. # tag
  32. # The tag, branch or commit hash to use for upstream.
  33. # shortlog
  34. # Optional. Set to 'true' to get a shortlog in the commit message.
  35. #
  36. # Additionally, an "extract_source" function must be defined. It will be
  37. # run within the checkout of the project on the requested tag. It should
  38. # should place the desired tree into $extractdir/$name-reduced. This
  39. # directory will be used as the newest commit for the project.
  40. #
  41. # For convenience, the function may use the "git_archive" function which
  42. # does a standard "git archive" extraction using the (optional) "paths"
  43. # variable to only extract a subset of the source tree.
  44. ########################################################################
  45. ########################################################################
  46. # Utility functions
  47. ########################################################################
  48. git_archive () {
  49. git archive --worktree-attributes --prefix="$name-reduced/" HEAD -- $paths | \
  50. tar -C "$extractdir" -x
  51. }
  52. die () {
  53. echo >&2 "$@"
  54. exit 1
  55. }
  56. warn () {
  57. echo >&2 "warning: $@"
  58. }
  59. readonly regex_date='20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
  60. readonly basehash_regex="$name $regex_date ([0-9a-f]*)"
  61. readonly basehash="$( git rev-list --author="$ownership" --grep="$basehash_regex" -n 1 HEAD )"
  62. readonly upstream_old_short="$( git cat-file commit "$basehash" | sed -n '/'"$basehash_regex"'/ {s/.*(//;s/)//;p}' | egrep '^[0-9a-f]+$' )"
  63. ########################################################################
  64. # Sanity checking
  65. ########################################################################
  66. [ -n "$name" ] || \
  67. die "'name' is empty"
  68. [ -n "$ownership" ] || \
  69. die "'ownership' is empty"
  70. [ -n "$subtree" ] || \
  71. die "'subtree' is empty"
  72. [ -n "$repo" ] || \
  73. die "'repo' is empty"
  74. [ -n "$tag" ] || \
  75. die "'tag' is empty"
  76. [ -n "$basehash" ] || \
  77. warn "'basehash' is empty; performing initial import"
  78. readonly do_shortlog="${shortlog-false}"
  79. readonly workdir="$PWD/work"
  80. readonly upstreamdir="$workdir/upstream"
  81. readonly extractdir="$workdir/extract"
  82. [ -d "$workdir" ] && \
  83. die "error: workdir '$workdir' already exists"
  84. trap "rm -rf '$workdir'" EXIT
  85. # Get upstream
  86. git clone "$repo" "$upstreamdir"
  87. if [ -n "$basehash" ]; then
  88. # Use the existing package's history
  89. git worktree add "$extractdir" "$basehash"
  90. # Clear out the working tree
  91. pushd "$extractdir"
  92. git ls-files | xargs rm -v
  93. find . -type d -empty -delete
  94. popd
  95. else
  96. # Create a repo to hold this package's history
  97. mkdir -p "$extractdir"
  98. git -C "$extractdir" init
  99. fi
  100. # Extract the subset of upstream we care about
  101. pushd "$upstreamdir"
  102. git checkout "$tag"
  103. readonly upstream_hash="$( git rev-parse HEAD )"
  104. readonly upstream_hash_short="$( git rev-parse --short=8 "$upstream_hash" )"
  105. readonly upstream_datetime="$( git rev-list "$upstream_hash" --format='%ci' -n 1 | grep -e "^$regex_date" )"
  106. readonly upstream_date="$( echo "$upstream_datetime" | grep -o -e "$regex_date" )"
  107. if $do_shortlog && [ -n "$basehash" ]; then
  108. readonly commit_shortlog="
  109. Upstream Shortlog
  110. -----------------
  111. $( git shortlog --no-merges --abbrev=8 --format='%h %s' "$upstream_old_short".."$upstream_hash" )"
  112. else
  113. readonly commit_shortlog=""
  114. fi
  115. extract_source || \
  116. die "failed to extract source"
  117. popd
  118. [ -d "$extractdir/$name-reduced" ] || \
  119. die "expected directory to extract does not exist"
  120. readonly commit_summary="$name $upstream_date ($upstream_hash_short)"
  121. # Commit the subset
  122. pushd "$extractdir"
  123. mv -v "$name-reduced/"* .
  124. rmdir "$name-reduced/"
  125. git add -A .
  126. git commit -n --author="$ownership" --date="$upstream_datetime" -F - <<-EOF
  127. $commit_summary
  128. Code extracted from:
  129. $repo
  130. at commit $upstream_hash ($tag).$commit_shortlog
  131. EOF
  132. git branch -f "upstream-$name"
  133. popd
  134. # Merge the subset into this repository
  135. if [ -n "$basehash" ]; then
  136. git merge --log -s recursive "-Xsubtree=$subtree/" --no-commit "upstream-$name"
  137. else
  138. unrelated_histories_flag=""
  139. if git merge --help | grep -q -e allow-unrelated-histories; then
  140. unrelated_histories_flag="--allow-unrelated-histories "
  141. fi
  142. readonly unrelated_histories_flag
  143. git fetch "$extractdir" "upstream-$name:upstream-$name"
  144. git merge --log -s ours --no-commit $unrelated_histories_flag "upstream-$name"
  145. git read-tree -u --prefix="$subtree/" "upstream-$name"
  146. fi
  147. git commit --no-edit
  148. git branch -d "upstream-$name"