| 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 |
######################################################################## |
| 18 |
# Script for updating third party packages. |
| 19 |
# |
| 20 |
# This script should be sourced in a project-specific script which sets |
| 21 |
# the following variables: |
| 22 |
# |
| 23 |
# name |
| 24 |
# The name of the project. |
| 25 |
# ownership |
| 26 |
# A git author name/email for the commits. |
| 27 |
# subtree |
| 28 |
# The location of the thirdparty package within the main source |
| 29 |
# tree. |
| 30 |
# repo |
| 31 |
# The git repository to use as upstream. |
| 32 |
# tag |
| 33 |
# The tag, branch or commit hash to use for upstream. |
| 34 |
# shortlog |
| 35 |
# Optional. Set to 'true' to get a shortlog in the commit message. |
| 36 |
# |
| 37 |
# Additionally, an "extract_source" function must be defined. It will be |
| 38 |
# run within the checkout of the project on the requested tag. It should |
| 39 |
# should place the desired tree into $extractdir/$name-reduced. This |
| 40 |
# directory will be used as the newest commit for the project. |
| 41 |
# |
| 42 |
# For convenience, the function may use the "git_archive" function which |
| 43 |
# does a standard "git archive" extraction using the (optional) "paths" |
| 44 |
# variable to only extract a subset of the source tree. |
| 45 |
######################################################################## |
| 46 |
|
| 47 |
######################################################################## |
| 48 |
# Utility functions |
| 49 |
######################################################################## |
| 50 |
git_archive () { |
| 51 |
git archive --worktree-attributes --prefix="$name-reduced/" HEAD -- $paths | \ |
| 52 |
tar -C "$extractdir" -x |
| 53 |
} |
| 54 |
|
| 55 |
die () { |
| 56 |
echo >&2 "$@" |
| 57 |
exit 1 |
| 58 |
} |
| 59 |
|
| 60 |
warn () { |
| 61 |
echo >&2 "warning: $@" |
| 62 |
} |
| 63 |
|
| 64 |
readonly regex_date='20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]' |
| 65 |
readonly basehash_regex="$name $regex_date ([0-9a-f]*)" |
| 66 |
readonly basehash="$( git rev-list --author="$ownership" --grep="$basehash_regex" -n 1 HEAD )" |
| 67 |
readonly upstream_old_short="$( git cat-file commit "$basehash" | sed -n '/'"$basehash_regex"'/ {s/.*(//;s/)//;p}' | egrep '^[0-9a-f]+$' )" |
| 68 |
|
| 69 |
######################################################################## |
| 70 |
# Sanity checking |
| 71 |
######################################################################## |
| 72 |
[ -n "$name" ] || \ |
| 73 |
die "'name' is empty" |
| 74 |
[ -n "$ownership" ] || \ |
| 75 |
die "'ownership' is empty" |
| 76 |
[ -n "$subtree" ] || \ |
| 77 |
die "'subtree' is empty" |
| 78 |
[ -n "$repo" ] || \ |
| 79 |
die "'repo' is empty" |
| 80 |
[ -n "$tag" ] || \ |
| 81 |
die "'tag' is empty" |
| 82 |
[ -n "$basehash" ] || \ |
| 83 |
warn "'basehash' is empty; performing initial import" |
| 84 |
readonly do_shortlog="${shortlog-false}" |
| 85 |
|
| 86 |
readonly workdir="$PWD/work" |
| 87 |
readonly upstreamdir="$workdir/upstream" |
| 88 |
readonly extractdir="$workdir/extract" |
| 89 |
|
| 90 |
[ -d "$workdir" ] && \ |
| 91 |
die "error: workdir '$workdir' already exists" |
| 92 |
|
| 93 |
trap "rm -rf '$workdir'" EXIT |
| 94 |
|
| 95 |
# Get upstream |
| 96 |
git clone "$repo" "$upstreamdir" |
| 97 |
|
| 98 |
if [ -n "$basehash" ]; then |
| 99 |
# Remove old worktrees |
| 100 |
git worktree prune |
| 101 |
# Use the existing package's history |
| 102 |
git worktree add "$extractdir" "$basehash" |
| 103 |
# Clear out the working tree |
| 104 |
pushd "$extractdir" |
| 105 |
git ls-files | xargs rm -v |
| 106 |
find . -type d -empty -delete |
| 107 |
popd |
| 108 |
else |
| 109 |
# Create a repo to hold this package's history |
| 110 |
mkdir -p "$extractdir" |
| 111 |
git -C "$extractdir" init |
| 112 |
fi |
| 113 |
|
| 114 |
# Extract the subset of upstream we care about |
| 115 |
pushd "$upstreamdir" |
| 116 |
git checkout "$tag" |
| 117 |
readonly upstream_hash="$( git rev-parse HEAD )" |
| 118 |
readonly upstream_hash_short="$( git rev-parse --short=8 "$upstream_hash" )" |
| 119 |
readonly upstream_datetime="$( git rev-list "$upstream_hash" --format='%ci' -n 1 | grep -e "^$regex_date" )" |
| 120 |
readonly upstream_date="$( echo "$upstream_datetime" | grep -o -e "$regex_date" )" |
| 121 |
if $do_shortlog && [ -n "$basehash" ]; then |
| 122 |
readonly commit_shortlog=" |
| 123 |
|
| 124 |
Upstream Shortlog |
| 125 |
----------------- |
| 126 |
|
| 127 |
$( git shortlog --no-merges --abbrev=8 --format='%h %s' "$upstream_old_short".."$upstream_hash" )" |
| 128 |
else |
| 129 |
readonly commit_shortlog="" |
| 130 |
fi |
| 131 |
extract_source || \ |
| 132 |
die "failed to extract source" |
| 133 |
popd |
| 134 |
|
| 135 |
[ -d "$extractdir/$name-reduced" ] || \ |
| 136 |
die "expected directory to extract does not exist" |
| 137 |
readonly commit_summary="$name $upstream_date ($upstream_hash_short)" |
| 138 |
|
| 139 |
# Commit the subset |
| 140 |
pushd "$extractdir" |
| 141 |
mv -v "$name-reduced/"* . |
| 142 |
rmdir "$name-reduced/" |
| 143 |
git add -A . |
| 144 |
git commit -n --author="$ownership" --date="$upstream_datetime" -F - <<-EOF |
| 145 |
$commit_summary |
| 146 |
|
| 147 |
Code extracted from: |
| 148 |
|
| 149 |
$repo |
| 150 |
|
| 151 |
at commit $upstream_hash ($tag).$commit_shortlog |
| 152 |
EOF |
| 153 |
git branch -f "upstream-$name" |
| 154 |
popd |
| 155 |
|
| 156 |
# Merge the subset into this repository |
| 157 |
if [ -n "$basehash" ]; then |
| 158 |
git merge --log -s recursive "-Xsubtree=$subtree/" --no-commit "upstream-$name" |
| 159 |
else |
| 160 |
git fetch "$extractdir" "upstream-$name:upstream-$name" |
| 161 |
git merge --log -s ours --no-commit "upstream-$name" |
| 162 |
git read-tree -u --prefix="$subtree/" "upstream-$name" |
| 163 |
fi |
| 164 |
git commit --no-edit |
| 165 |
git branch -d "upstream-$name" |