update-third-party.bash 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. disable_custom_gitattributes() {
  53. pushd "${extractdir}/${name}-reduced"
  54. # Git does not allow custom attributes in a subdirectory where we
  55. # are about to merge the `.gitattributes` file, so disable them.
  56. sed -i '/^\[attr\]/ {s/^/#/}' .gitattributes
  57. popd
  58. }
  59. die () {
  60. echo >&2 "$@"
  61. exit 1
  62. }
  63. warn () {
  64. echo >&2 "warning: $@"
  65. }
  66. readonly regex_date='20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
  67. readonly basehash_regex="$name $regex_date ([0-9a-f]*)"
  68. readonly basehash="$( git rev-list --author="$ownership" --grep="$basehash_regex" -n 1 HEAD )"
  69. readonly upstream_old_short="$( git cat-file commit "$basehash" | sed -n '/'"$basehash_regex"'/ {s/.*(//;s/)//;p}' | egrep '^[0-9a-f]+$' )"
  70. ########################################################################
  71. # Sanity checking
  72. ########################################################################
  73. [ -n "$name" ] || \
  74. die "'name' is empty"
  75. [ -n "$ownership" ] || \
  76. die "'ownership' is empty"
  77. [ -n "$subtree" ] || \
  78. die "'subtree' is empty"
  79. [ -n "$repo" ] || \
  80. die "'repo' is empty"
  81. [ -n "$tag" ] || \
  82. die "'tag' is empty"
  83. [ -n "$basehash" ] || \
  84. warn "'basehash' is empty; performing initial import"
  85. readonly do_shortlog="${shortlog-false}"
  86. readonly workdir="$PWD/work"
  87. readonly upstreamdir="$workdir/upstream"
  88. readonly extractdir="$workdir/extract"
  89. [ -d "$workdir" ] && \
  90. die "error: workdir '$workdir' already exists"
  91. trap "rm -rf '$workdir'" EXIT
  92. # Get upstream
  93. git clone "$repo" "$upstreamdir"
  94. if [ -n "$basehash" ]; then
  95. # Use the existing package's history
  96. git worktree add "$extractdir" "$basehash"
  97. # Clear out the working tree
  98. pushd "$extractdir"
  99. git ls-files | xargs rm -v
  100. find . -type d -empty -delete
  101. popd
  102. else
  103. # Create a repo to hold this package's history
  104. mkdir -p "$extractdir"
  105. git -C "$extractdir" init
  106. fi
  107. # Extract the subset of upstream we care about
  108. pushd "$upstreamdir"
  109. git checkout "$tag"
  110. readonly upstream_hash="$( git rev-parse HEAD )"
  111. readonly upstream_hash_short="$( git rev-parse --short=8 "$upstream_hash" )"
  112. readonly upstream_datetime="$( git rev-list "$upstream_hash" --format='%ci' -n 1 | grep -e "^$regex_date" )"
  113. readonly upstream_date="$( echo "$upstream_datetime" | grep -o -e "$regex_date" )"
  114. if $do_shortlog && [ -n "$basehash" ]; then
  115. readonly commit_shortlog="
  116. Upstream Shortlog
  117. -----------------
  118. $( git shortlog --no-merges --abbrev=8 --format='%h %s' "$upstream_old_short".."$upstream_hash" )"
  119. else
  120. readonly commit_shortlog=""
  121. fi
  122. extract_source || \
  123. die "failed to extract source"
  124. popd
  125. [ -d "$extractdir/$name-reduced" ] || \
  126. die "expected directory to extract does not exist"
  127. readonly commit_summary="$name $upstream_date ($upstream_hash_short)"
  128. # Commit the subset
  129. pushd "$extractdir"
  130. mv -v "$name-reduced/"* .
  131. rmdir "$name-reduced/"
  132. git add -A .
  133. git commit -n --author="$ownership" --date="$upstream_datetime" -F - <<-EOF
  134. $commit_summary
  135. Code extracted from:
  136. $repo
  137. at commit $upstream_hash ($tag).$commit_shortlog
  138. EOF
  139. git branch -f "upstream-$name"
  140. popd
  141. # Merge the subset into this repository
  142. if [ -n "$basehash" ]; then
  143. git merge --log -s recursive "-Xsubtree=$subtree/" --no-commit "upstream-$name"
  144. else
  145. unrelated_histories_flag=""
  146. if git merge --help | grep -q -e allow-unrelated-histories; then
  147. unrelated_histories_flag="--allow-unrelated-histories "
  148. fi
  149. readonly unrelated_histories_flag
  150. git fetch "$extractdir" "upstream-$name:upstream-$name"
  151. git merge --log -s ours --no-commit $unrelated_histories_flag "upstream-$name"
  152. git read-tree -u --prefix="$subtree/" "upstream-$name"
  153. fi
  154. git commit --no-edit
  155. git branch -d "upstream-$name"