3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
12 git subtree add --prefix=<prefix> <commit>
13 git subtree add --prefix=<prefix> <repository> <ref>
14 git subtree merge --prefix=<prefix> <commit>
15 git subtree pull --prefix=<prefix> <repository> <ref>
16 git subtree push --prefix=<prefix> <repository> <ref>
17 git subtree split --prefix=<prefix> <commit...>
22 P,prefix= the name of the subdir to split out
23 m,message= use the given message as the commit message for the merge commit
25 annotate= add a prefix to commit message of new commits
26 b,branch= create a new branch from the split subtree
27 ignore-joins ignore prior --rejoin commits
28 onto= try connecting new tree to an existing one
29 rejoin merge the new branch back into HEAD
30 options for 'add', 'merge', and 'pull'
31 squash merge subtree changes as a single commit
33 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
35 PATH=$PATH:$(git --exec-path)
56 printf "%s\n" "$*" >&2
64 printf "%s\n" "$*" >&2
72 printf "%s\r" "$*" >&2
80 die "assertion failed: " "$@"
148 die "Unexpected option: $opt"
161 default="--default HEAD"
164 die "Unknown command '$command'"
170 die "You must provide the --prefix option."
176 die "prefix '$prefix' already exists."
180 die "'$prefix' does not exist; use 'git subtree add'"
184 dir="$(dirname "$prefix/.")"
186 if test "$command" != "pull" &&
187 test "$command" != "add" &&
188 test "$command" != "push"
190 revs=$(git rev-parse $default --revs-only "$@") || exit $?
191 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
194 die "Error: Use --prefix instead of bare filenames."
198 debug "command: {$command}"
199 debug "quiet: {$quiet}"
200 debug "revs: {$revs}"
207 cachedir="$GIT_DIR/subtree-cache/$$"
208 rm -rf "$cachedir" ||
209 die "Can't delete old cachedir: $cachedir"
210 mkdir -p "$cachedir" ||
211 die "Can't create new cachedir: $cachedir"
212 mkdir -p "$cachedir/notree" ||
213 die "Can't create new cachedir: $cachedir/notree"
214 debug "Using cachedir: $cachedir" >&2
221 if test -r "$cachedir/$oldrev"
223 read newrev <"$cachedir/$oldrev"
233 if ! test -r "$cachedir/$oldrev"
242 missed=$(cache_miss "$@")
245 if ! test -r "$cachedir/notree/$miss"
247 debug " incorrect order: $miss"
254 echo "1" > "$cachedir/notree/$1"
261 if test "$oldrev" != "latest_old" &&
262 test "$oldrev" != "latest_new" &&
263 test -e "$cachedir/$oldrev"
265 die "cache for $oldrev already exists!"
267 echo "$newrev" >"$cachedir/$oldrev"
272 if git rev-parse "$1" >/dev/null 2>&1
280 rev_is_descendant_of_branch()
284 branch_hash=$(git rev-parse "$branch")
285 match=$(git rev-list -1 "$branch_hash" "^$newrev")
295 # if a commit doesn't have a parent, this might not work. But we only want
296 # to remove the parent from the rev-list, and since it doesn't exist, it won't
297 # be there anyway, so do nothing in that case.
298 try_remove_previous()
308 debug "Looking for latest squash ($dir)..."
313 git log --grep="^git-subtree-dir: $dir/*\$" \
314 --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
318 debug "{{$sq/$main/$sub}}"
323 git-subtree-mainline:)
327 sub="$(git rev-parse "$b^0")" ||
328 die "could not rev-parse split hash $b from commit $sq"
336 # Pretend its sub was a squash.
339 debug "Squash found: $sq $sub"
351 find_existing_splits()
353 debug "Looking for prior splits..."
358 git log --grep="^git-subtree-dir: $dir/*\$" \
359 --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
366 git-subtree-mainline:)
370 sub="$(git rev-parse "$b^0")" ||
371 die "could not rev-parse split hash $b from commit $sq"
374 debug " Main is: '$main'"
375 if test -z "$main" -a -n "$sub"
377 # squash commits refer to a subtree
378 debug " Squash: $sq from $sub"
379 cache_set "$sq" "$sub"
381 if test -n "$main" -a -n "$sub"
383 debug " Prior: $main -> $sub"
386 try_remove_previous "$main"
387 try_remove_previous "$sub"
398 # We're going to set some environment vars here, so
399 # do it in a subshell to get rid of them safely later
400 debug copy_commit "{$1}" "{$2}" "{$3}"
401 git log -1 --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
404 read GIT_AUTHOR_EMAIL
406 read GIT_COMMITTER_NAME
407 read GIT_COMMITTER_EMAIL
408 read GIT_COMMITTER_DATE
409 export GIT_AUTHOR_NAME \
413 GIT_COMMITTER_EMAIL \
416 printf "%s" "$annotate"
419 git commit-tree "$2" $3 # reads the rest of stdin
420 ) || die "Can't copy commit $1"
428 if test -n "$message"
430 commit_message="$message"
432 commit_message="Add '$dir/' from commit '$latest_new'"
437 git-subtree-dir: $dir
438 git-subtree-mainline: $latest_old
439 git-subtree-split: $latest_new
445 if test -n "$message"
449 echo "Merge commit '$1' as '$2'"
458 if test -n "$message"
460 commit_message="$message"
462 commit_message="Split '$dir/' into commit '$latest_new'"
467 git-subtree-dir: $dir
468 git-subtree-mainline: $latest_old
469 git-subtree-split: $latest_new
478 newsub_short=$(git rev-parse --short "$newsub")
482 oldsub_short=$(git rev-parse --short "$oldsub")
483 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
485 git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
486 git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
488 echo "Squashed '$dir/' content from commit $newsub_short"
492 echo "git-subtree-dir: $dir"
493 echo "git-subtree-split: $newsub"
499 git log -1 --pretty=format:'%T' "$commit" -- || exit $?
506 git ls-tree "$commit" -- "$dir" |
507 while read mode type tree name
509 assert test "$name" = "$dir"
510 assert test "$type" = "tree" -o "$type" = "commit"
511 test "$type" = "commit" && continue # ignore submodules
523 return 0 # weird parents, consider it changed
525 ptree=$(toptree_for_commit $1)
526 if test "$ptree" != "$tree"
530 return 1 # not changed
540 tree=$(toptree_for_commit $newsub) || exit $?
543 squash_msg "$dir" "$oldsub" "$newsub" |
544 git commit-tree "$tree" -p "$old" || exit $?
546 squash_msg "$dir" "" "$newsub" |
547 git commit-tree "$tree" || exit $?
556 assert test -n "$tree"
562 for parent in $newparents
564 ptree=$(toptree_for_commit $parent) || exit $?
565 test -z "$ptree" && continue
566 if test "$ptree" = "$tree"
568 # an identical parent could be used in place of this rev.
571 nonidentical="$parent"
574 # sometimes both old parents map to the same newparent;
575 # eliminate duplicates
577 for gp in $gotparents
579 if test "$gp" = "$parent"
587 gotparents="$gotparents $parent"
593 if test -n "$identical" && test -n "$nonidentical"
595 extras=$(git rev-list --count $identical..$nonidentical)
596 if test "$extras" -ne 0
598 # we need to preserve history along the other branch
602 if test -n "$identical" && test -z "$copycommit"
606 copy_commit "$rev" "$tree" "$p" || exit $?
612 if ! git diff-index HEAD --exit-code --quiet 2>&1
614 die "Working tree has modifications. Cannot add."
616 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
618 die "Index has modifications. Cannot add."
622 ensure_valid_ref_format()
624 git check-ref-format "refs/heads/$1" ||
625 die "'$1' does not look like a ref"
632 die "'$dir' already exists. Cannot add."
639 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
640 die "'$1' does not refer to a commit"
646 # Technically we could accept a refspec here but we're
647 # just going to turn around and add FETCH_HEAD under the
648 # specified directory. Allowing a refspec might be
649 # misleading because we won't do anything with any other
650 # branches fetched via the refspec.
651 ensure_valid_ref_format "$2"
653 cmd_add_repository "$@"
655 say "error: parameters were '$@'"
656 die "Provide either a commit or a repository and commit."
662 echo "git fetch" "$@"
665 git fetch "$@" || exit $?
673 revs=$(git rev-parse $default --revs-only "$@") || exit $?
677 debug "Adding $dir as '$rev'..."
678 git read-tree --prefix="$dir" $rev || exit $?
679 git checkout -- "$dir" || exit $?
680 tree=$(git write-tree) || exit $?
682 headrev=$(git rev-parse HEAD) || exit $?
683 if test -n "$headrev" && test "$headrev" != "$rev"
692 rev=$(new_squash_commit "" "" "$rev") || exit $?
693 commit=$(add_squashed_msg "$rev" "$dir" |
694 git commit-tree "$tree" $headp -p "$rev") || exit $?
696 revp=$(peel_committish "$rev") &&
697 commit=$(add_msg "$dir" $headrev "$rev" |
698 git commit-tree "$tree" $headp -p "$revp") || exit $?
700 git reset "$commit" || exit $?
702 say "Added dir '$dir'"
707 debug "Splitting $dir..."
708 cache_setup || exit $?
712 debug "Reading history for --onto=$onto..."
716 # the 'onto' history is already just the subdir, so
717 # any parent we find there can be used verbatim
719 cache_set "$rev" "$rev"
723 if test -n "$ignore_joins"
727 unrevs="$(find_existing_splits "$dir" "$revs")"
730 # We can't restrict rev-list to only $dir here, because some of our
731 # parents have the $dir contents the root, and those won't match.
732 # (and rev-list --follow doesn't seem to solve this)
733 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
734 revmax=$(eval "$grl" | wc -l)
738 while read rev parents
740 revcount=$(($revcount + 1))
741 progress "$revcount/$revmax ($createcount)"
742 debug "Processing commit: $rev"
743 exists=$(cache_get "$rev")
746 debug " prior: $exists"
749 createcount=$(($createcount + 1))
750 debug " parents: $parents"
751 newparents=$(cache_get $parents)
752 debug " newparents: $newparents"
754 tree=$(subtree_for_commit "$rev" "$dir")
755 debug " tree is: $tree"
757 check_parents $parents
759 # ugly. is there no better way to tell if this is a subtree
760 # vs. a mainline commit? Does it matter?
764 if test -n "$newparents"
766 cache_set "$rev" "$rev"
771 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
772 debug " newrev is: $newrev"
773 cache_set "$rev" "$newrev"
774 cache_set latest_new "$newrev"
775 cache_set latest_old "$rev"
778 latest_new=$(cache_get latest_new)
779 if test -z "$latest_new"
781 die "No new revisions were found"
786 debug "Merging split branch into HEAD..."
787 latest_old=$(cache_get latest_old)
789 --allow-unrelated-histories \
790 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
791 "$latest_new" >&2 || exit $?
795 if rev_exists "refs/heads/$branch"
797 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
799 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
805 git update-ref -m 'subtree split' \
806 "refs/heads/$branch" "$latest_new" || exit $?
807 say "$action branch '$branch'"
815 revs=$(git rev-parse $default --revs-only "$@") || exit $?
821 die "You must provide exactly one revision. Got: '$revs'"
827 first_split="$(find_latest_squash "$dir")"
828 if test -z "$first_split"
830 die "Can't squash-merge: '$dir' was never added."
835 if test "$sub" = "$rev"
837 say "Subtree is already at commit $rev."
840 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
841 debug "New squash commit: $new"
845 version=$(git version)
846 if test "$version" \< "git version 1.7"
848 if test -n "$message"
850 git merge -s subtree --message="$message" "$rev"
852 git merge -s subtree "$rev"
855 if test -n "$message"
857 git merge -Xsubtree="$prefix" \
858 --message="$message" "$rev"
860 git merge -Xsubtree="$prefix" $rev
869 die "You must provide <repository> <ref>"
872 ensure_valid_ref_format "$2"
873 git fetch "$@" || exit $?
883 die "You must provide <repository> <ref>"
885 ensure_valid_ref_format "$2"
890 echo "git push using: " "$repository" "$refspec"
891 localrev=$(git subtree split --prefix="$prefix") || die
892 git push "$repository" "$localrev":"refs/heads/$refspec"
894 die "'$dir' must already exist. Try 'git subtree add'."