123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- git_archive () {
- git archive --worktree-attributes --prefix="$name-reduced/" HEAD -- $paths | \
- tar -C "$extractdir" -x
- }
- disable_custom_gitattributes() {
- pushd "${extractdir}/${name}-reduced"
-
-
- sed -i '/^\[attr\]/ {s/^/#/}' .gitattributes
- popd
- }
- die () {
- echo >&2 "$@"
- exit 1
- }
- warn () {
- echo >&2 "warning: $@"
- }
- readonly regex_date='20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
- readonly basehash_regex="$name $regex_date ([0-9a-f]*)"
- readonly basehash="$( git rev-list --author="$ownership" --grep="$basehash_regex" -n 1 HEAD )"
- readonly upstream_old_short="$( git cat-file commit "$basehash" | sed -n '/'"$basehash_regex"'/ {s/.*(//;s/)//;p}' | egrep '^[0-9a-f]+$' )"
- [ -n "$name" ] || \
- die "'name' is empty"
- [ -n "$ownership" ] || \
- die "'ownership' is empty"
- [ -n "$subtree" ] || \
- die "'subtree' is empty"
- [ -n "$repo" ] || \
- die "'repo' is empty"
- [ -n "$tag" ] || \
- die "'tag' is empty"
- [ -n "$basehash" ] || \
- warn "'basehash' is empty; performing initial import"
- readonly do_shortlog="${shortlog-false}"
- readonly workdir="$PWD/work"
- readonly upstreamdir="$workdir/upstream"
- readonly extractdir="$workdir/extract"
- [ -d "$workdir" ] && \
- die "error: workdir '$workdir' already exists"
- trap "rm -rf '$workdir'" EXIT
- git clone "$repo" "$upstreamdir"
- if [ -n "$basehash" ]; then
-
- git worktree add "$extractdir" "$basehash"
-
- pushd "$extractdir"
- git ls-files | xargs rm -v
- find . -type d -empty -delete
- popd
- else
-
- mkdir -p "$extractdir"
- git -C "$extractdir" init
- fi
- pushd "$upstreamdir"
- git checkout "$tag"
- readonly upstream_hash="$( git rev-parse HEAD )"
- readonly upstream_hash_short="$( git rev-parse --short=8 "$upstream_hash" )"
- readonly upstream_datetime="$( git rev-list "$upstream_hash" --format='%ci' -n 1 | grep -e "^$regex_date" )"
- readonly upstream_date="$( echo "$upstream_datetime" | grep -o -e "$regex_date" )"
- if $do_shortlog && [ -n "$basehash" ]; then
- readonly commit_shortlog="
- Upstream Shortlog
- -----------------
- $( git shortlog --no-merges --abbrev=8 --format='%h %s' "$upstream_old_short".."$upstream_hash" )"
- else
- readonly commit_shortlog=""
- fi
- extract_source || \
- die "failed to extract source"
- popd
- [ -d "$extractdir/$name-reduced" ] || \
- die "expected directory to extract does not exist"
- readonly commit_summary="$name $upstream_date ($upstream_hash_short)"
- pushd "$extractdir"
- mv -v "$name-reduced/"* .
- rmdir "$name-reduced/"
- git add -A .
- git commit -n --author="$ownership" --date="$upstream_datetime" -F - <<-EOF
- $commit_summary
- Code extracted from:
- $repo
- at commit $upstream_hash ($tag).$commit_shortlog
- EOF
- git branch -f "upstream-$name"
- popd
- if [ -n "$basehash" ]; then
- git merge --log -s recursive "-Xsubtree=$subtree/" --no-commit "upstream-$name"
- else
- unrelated_histories_flag=""
- if git merge --help | grep -q -e allow-unrelated-histories; then
- unrelated_histories_flag="--allow-unrelated-histories "
- fi
- readonly unrelated_histories_flag
- git fetch "$extractdir" "upstream-$name:upstream-$name"
- git merge --log -s ours --no-commit $unrelated_histories_flag "upstream-$name"
- git read-tree -u --prefix="$subtree/" "upstream-$name"
- fi
- git commit --no-edit
- git branch -d "upstream-$name"
|