3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
8 if test -z "$GIT_EXEC_PATH" ||
! test -f "$GIT_EXEC_PATH/git-sh-setup" ||
{
9 test "${PATH#"${GIT_EXEC_PATH}:"}" = "$PATH" &&
10 test ! "$GIT_EXEC_PATH" -ef "${PATH%%:*}" 2>/dev
/null
14 echo >&2 'It looks like either your git installation or your'
15 echo >&2 'git-subtree installation is broken.'
18 echo >&2 " - If \`git --exec-path\` does not print the correct path to"
19 echo >&2 " your git install directory, then set the GIT_EXEC_PATH"
20 echo >&2 " environment variable to the correct directory."
21 echo >&2 " - Make sure that your \`$basename\` file is either in your"
22 echo >&2 " PATH or in your git exec path (\`$(git --exec-path)\`)."
23 echo >&2 " - You should run git-subtree as \`git ${basename#git-}\`,"
24 echo >&2 " not as \`$basename\`." >&2
29 git subtree add --prefix=<prefix> <commit>
30 git subtree add --prefix=<prefix> <repository> <ref>
31 git subtree merge --prefix=<prefix> <commit>
32 git subtree split --prefix=<prefix> [<commit>]
33 git subtree pull --prefix=<prefix> <repository> <ref>
34 git subtree push --prefix=<prefix> <repository> <refspec>
39 P,prefix= the name of the subdir to split out
40 options for 'split' (also: 'push')
41 annotate= add a prefix to commit message of new commits
42 b,branch= create a new branch from the split subtree
43 ignore-joins ignore prior --rejoin commits
44 onto= try connecting new tree to an existing one
45 rejoin merge the new branch back into HEAD
46 options for 'add' and 'merge' (also: 'pull', 'split --rejoin', and 'push --rejoin')
47 squash merge subtree changes as a single commit
48 m,message= use the given message as the commit message for the merge commit
53 # Usage: debug [MSG...]
55 if test -n "$arg_debug"
57 printf "%$(($indent * 2))s%s\n" '' "$*" >&2
61 # Usage: progress [MSG...]
63 if test -z "$GIT_QUIET"
65 if test -z "$arg_debug"
69 # Print one progress line that we keep updating (use
70 # "\r" to return to the beginning of the line, rather
71 # than "\n" to start a new line). This only really
72 # works when stderr is a terminal.
73 printf "%s\r" "$*" >&2
75 # Debug mode is on. The `debug` function is regularly
78 # Don't do the one-line-with-"\r" thing, because on a
79 # terminal the debug output would overwrite and hide the
80 # progress output. Add a "progress:" prefix to make the
81 # progress output and the debug output easy to
82 # distinguish. This ensures maximum readability whether
83 # stderr is a terminal or a file.
84 printf "progress: %s\n" "$*" >&2
89 # Usage: assert CMD...
93 die
"assertion failed: $*"
102 set_args
="$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@
" || echo exit $?)"
107 # First figure out the command and whether we use --rejoin, so
108 # that we can provide more helpful validation when we do the
109 # "real" flag parsing.
118 --annotate|
-b|
-P|
-m|
--onto)
133 case "$arg_command" in
139 allow_addmerge
=$arg_split_rejoin
142 die
"Unknown command '$arg_command'"
145 # Reset the arguments array for "real" flag parsing.
148 # Begin "real" flag parsing.
153 arg_split_ignore_joins
=
156 arg_addmerge_message
=
170 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
171 arg_split_annotate
="$1"
175 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
179 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
180 arg_split_branch
="$1"
188 test -n "$allow_addmerge" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
189 arg_addmerge_message
="$1"
196 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
201 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
205 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
208 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
211 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
212 arg_split_ignore_joins
=1
215 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
216 arg_split_ignore_joins
=
219 test -n "$allow_addmerge" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
220 arg_addmerge_squash
=1
223 test -n "$allow_addmerge" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
230 die
"Unexpected option: $opt"
236 if test -z "$arg_prefix"
238 die
"You must provide the --prefix option."
241 case "$arg_command" in
243 test -e "$arg_prefix" &&
244 die
"prefix '$arg_prefix' already exists."
247 test -e "$arg_prefix" ||
248 die
"'$arg_prefix' does not exist; use 'git subtree add'"
252 dir
="$(dirname "$arg_prefix/.
")"
254 debug
"command: {$arg_command}"
255 debug
"quiet: {$GIT_QUIET}"
260 "cmd_$arg_command" "$@"
266 cachedir
="$GIT_DIR/subtree-cache/$$"
267 rm -rf "$cachedir" ||
268 die
"Can't delete old cachedir: $cachedir"
269 mkdir
-p "$cachedir" ||
270 die
"Can't create new cachedir: $cachedir"
271 mkdir
-p "$cachedir/notree" ||
272 die
"Can't create new cachedir: $cachedir/notree"
273 debug
"Using cachedir: $cachedir" >&2
276 # Usage: cache_get [REVS...]
280 if test -r "$cachedir/$oldrev"
282 read newrev
<"$cachedir/$oldrev"
288 # Usage: cache_miss [REVS...]
292 if ! test -r "$cachedir/$oldrev"
299 # Usage: check_parents [REVS...]
301 missed
=$
(cache_miss
"$@") ||
exit $?
302 local indent
=$
(($indent + 1))
305 if ! test -r "$cachedir/notree/$miss"
307 debug
"incorrect order: $miss"
308 process_split_commit
"$miss" ""
313 # Usage: set_notree REV
316 echo "1" > "$cachedir/notree/$1"
319 # Usage: cache_set OLDREV NEWREV
324 if test "$oldrev" != "latest_old" &&
325 test "$oldrev" != "latest_new" &&
326 test -e "$cachedir/$oldrev"
328 die
"cache for $oldrev already exists!"
330 echo "$newrev" >"$cachedir/$oldrev"
333 # Usage: rev_exists REV
336 if git rev-parse
"$1" >/dev
/null
2>&1
344 # Usage: try_remove_previous REV
346 # If a commit doesn't have a parent, this might not work. But we only want
347 # to remove the parent from the rev-list, and since it doesn't exist, it won't
348 # be there anyway, so do nothing in that case.
349 try_remove_previous
() {
357 # Usage: find_latest_squash DIR
358 find_latest_squash
() {
360 debug
"Looking for latest squash ($dir)..."
361 local indent
=$
(($indent + 1))
367 git log
--grep="^git-subtree-dir: $dir/*\$" \
368 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
372 debug
"{{$sq/$main/$sub}}"
377 git-subtree-mainline
:)
381 sub
="$(git rev-parse "$b^
{commit
}")" ||
382 die
"could not rev-parse split hash $b from commit $sq"
390 # Pretend its sub was a squash.
391 sq=$
(git rev-parse
--verify "$sq^2") ||
394 debug
"Squash found: $sq $sub"
406 # Usage: find_existing_splits DIR REV
407 find_existing_splits
() {
409 debug
"Looking for prior splits..."
410 local indent
=$
(($indent + 1))
416 local grep_format
="^git-subtree-dir: $dir/*\$"
417 if test -n "$arg_split_ignore_joins"
419 grep_format
="^Add '$dir/' from commit '"
421 git log
--grep="$grep_format" \
422 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' "$rev" |
429 git-subtree-mainline
:)
433 sub
="$(git rev-parse "$b^
{commit
}")" ||
434 die
"could not rev-parse split hash $b from commit $sq"
437 debug
"Main is: '$main'"
438 if test -z "$main" -a -n "$sub"
440 # squash commits refer to a subtree
441 debug
" Squash: $sq from $sub"
442 cache_set
"$sq" "$sub"
444 if test -n "$main" -a -n "$sub"
446 debug
" Prior: $main -> $sub"
449 try_remove_previous
"$main"
450 try_remove_previous
"$sub"
459 # Usage: copy_commit REV TREE FLAGS_STR
462 # We're going to set some environment vars here, so
463 # do it in a subshell to get rid of them safely later
464 debug copy_commit
"{$1}" "{$2}" "{$3}"
465 git log
-1 --no-show-signature --pretty=format
:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
468 read GIT_AUTHOR_EMAIL
470 read GIT_COMMITTER_NAME
471 read GIT_COMMITTER_EMAIL
472 read GIT_COMMITTER_DATE
473 export GIT_AUTHOR_NAME \
477 GIT_COMMITTER_EMAIL \
480 printf "%s" "$arg_split_annotate"
483 git commit-tree
"$2" $3 # reads the rest of stdin
484 ) || die
"Can't copy commit $1"
487 # Usage: add_msg DIR LATEST_OLD LATEST_NEW
493 if test -n "$arg_addmerge_message"
495 commit_message
="$arg_addmerge_message"
497 commit_message
="Add '$dir/' from commit '$latest_new'"
499 if test -n "$arg_split_rejoin"
501 # If this is from a --rejoin, then rejoin_msg has
502 # already inserted the `git-subtree-xxx:` tags
503 echo "$commit_message"
509 git-subtree-dir: $dir
510 git-subtree-mainline: $latest_old
511 git-subtree-split: $latest_new
515 # Usage: add_squashed_msg REV DIR
516 add_squashed_msg
() {
518 if test -n "$arg_addmerge_message"
520 echo "$arg_addmerge_message"
522 echo "Merge commit '$1' as '$2'"
526 # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
532 if test -n "$arg_addmerge_message"
534 commit_message
="$arg_addmerge_message"
536 commit_message
="Split '$dir/' into commit '$latest_new'"
541 git-subtree-dir: $dir
542 git-subtree-mainline: $latest_old
543 git-subtree-split: $latest_new
547 # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
553 newsub_short
=$
(git rev-parse
--short "$newsub")
557 oldsub_short
=$
(git rev-parse
--short "$oldsub")
558 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
560 git log
--no-show-signature --pretty=tformat
:'%h %s' "$oldsub..$newsub"
561 git log
--no-show-signature --pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
563 echo "Squashed '$dir/' content from commit $newsub_short"
567 echo "git-subtree-dir: $dir"
568 echo "git-subtree-split: $newsub"
571 # Usage: toptree_for_commit COMMIT
572 toptree_for_commit
() {
575 git rev-parse
--verify "$commit^{tree}" ||
exit $?
578 # Usage: subtree_for_commit COMMIT DIR
579 subtree_for_commit
() {
583 git ls-tree
"$commit" -- "$dir" |
584 while read mode
type tree name
586 assert
test "$name" = "$dir"
587 assert
test "$type" = "tree" -o "$type" = "commit"
588 test "$type" = "commit" && continue # ignore submodules
594 # Usage: tree_changed TREE [PARENTS...]
601 return 0 # weird parents, consider it changed
603 ptree
=$
(toptree_for_commit
$1) ||
exit $?
604 if test "$ptree" != "$tree"
608 return 1 # not changed
613 # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
614 new_squash_commit
() {
619 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
622 squash_msg
"$dir" "$oldsub" "$newsub" |
623 git commit-tree
"$tree" -p "$old" ||
exit $?
625 squash_msg
"$dir" "" "$newsub" |
626 git commit-tree
"$tree" ||
exit $?
630 # Usage: copy_or_skip REV TREE NEWPARENTS
636 assert
test -n "$tree"
643 for parent
in $newparents
645 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
646 test -z "$ptree" && continue
647 if test "$ptree" = "$tree"
649 # an identical parent could be used in place of this rev.
650 if test -n "$identical"
652 # if a previous identical parent was found, check whether
653 # one is already an ancestor of the other
654 mergebase
=$
(git merge-base
$identical $parent)
655 if test "$identical" = "$mergebase"
657 # current identical commit is an ancestor of parent
659 elif test "$parent" != "$mergebase"
661 # no common history; commit must be copied
665 # first identical parent detected
669 nonidentical
="$parent"
672 # sometimes both old parents map to the same newparent;
673 # eliminate duplicates
675 for gp
in $gotparents
677 if test "$gp" = "$parent"
685 gotparents
="$gotparents $parent"
690 if test -n "$identical" && test -n "$nonidentical"
692 extras
=$
(git rev-list
--count $identical..
$nonidentical)
693 if test "$extras" -ne 0
695 # we need to preserve history along the other branch
699 if test -n "$identical" && test -z "$copycommit"
703 copy_commit
"$rev" "$tree" "$p" ||
exit $?
707 # Usage: ensure_clean
710 if ! git diff-index HEAD
--exit-code --quiet 2>&1
712 die
"Working tree has modifications. Cannot add."
714 if ! git diff-index
--cached HEAD
--exit-code --quiet 2>&1
716 die
"Index has modifications. Cannot add."
720 # Usage: ensure_valid_ref_format REF
721 ensure_valid_ref_format
() {
723 git check-ref-format
"refs/heads/$1" ||
724 die
"'$1' does not look like a ref"
727 # Usage: process_split_commit REV PARENTS
728 process_split_commit
() {
733 if test $indent -eq 0
735 revcount
=$
(($revcount + 1))
737 # processing commit without normal parent information;
739 parents
=$
(git rev-parse
"$rev^@")
740 extracount
=$
(($extracount + 1))
743 progress
"$revcount/$revmax ($createcount) [$extracount]"
745 debug
"Processing commit: $rev"
746 local indent
=$
(($indent + 1))
747 exists
=$
(cache_get
"$rev") ||
exit $?
750 debug
"prior: $exists"
753 createcount
=$
(($createcount + 1))
754 debug
"parents: $parents"
755 check_parents
$parents
756 newparents
=$
(cache_get
$parents) ||
exit $?
757 debug
"newparents: $newparents"
759 tree
=$
(subtree_for_commit
"$rev" "$dir") ||
exit $?
760 debug
"tree is: $tree"
762 # ugly. is there no better way to tell if this is a subtree
763 # vs. a mainline commit? Does it matter?
767 if test -n "$newparents"
769 cache_set
"$rev" "$rev"
774 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
775 debug
"newrev is: $newrev"
776 cache_set
"$rev" "$newrev"
777 cache_set latest_new
"$newrev"
778 cache_set latest_old
"$rev"
782 # Or: cmd_add REPOSITORY REF
789 git rev-parse
-q --verify "$1^{commit}" >/dev
/null ||
790 die
"'$1' does not refer to a commit"
796 # Technically we could accept a refspec here but we're
797 # just going to turn around and add FETCH_HEAD under the
798 # specified directory. Allowing a refspec might be
799 # misleading because we won't do anything with any other
800 # branches fetched via the refspec.
801 ensure_valid_ref_format
"$2"
803 cmd_add_repository
"$@"
805 say
>&2 "error: parameters were '$*'"
806 die
"Provide either a commit or a repository and commit."
810 # Usage: cmd_add_repository REPOSITORY REFSPEC
811 cmd_add_repository
() {
813 echo "git fetch" "$@"
816 git fetch
"$@" ||
exit $?
817 cmd_add_commit FETCH_HEAD
820 # Usage: cmd_add_commit REV
822 # The rev has already been validated by cmd_add(), we just
823 # need to normalize it.
825 rev=$
(git rev-parse
--verify "$1^{commit}") ||
exit $?
827 debug
"Adding $dir as '$rev'..."
828 if test -z "$arg_split_rejoin"
830 # Only bother doing this if this is a genuine 'add',
831 # not a synthetic 'add' from '--rejoin'.
832 git read-tree
--prefix="$dir" $rev ||
exit $?
834 git checkout
-- "$dir" ||
exit $?
835 tree
=$
(git write-tree
) ||
exit $?
837 headrev
=$
(git rev-parse HEAD
) ||
exit $?
838 if test -n "$headrev" && test "$headrev" != "$rev"
845 if test -n "$arg_addmerge_squash"
847 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
848 commit
=$
(add_squashed_msg
"$rev" "$dir" |
849 git commit-tree
"$tree" $headp -p "$rev") ||
exit $?
851 revp
=$
(peel_committish
"$rev") ||
exit $?
852 commit
=$
(add_msg
"$dir" $headrev "$rev" |
853 git commit-tree
"$tree" $headp -p "$revp") ||
exit $?
855 git
reset "$commit" ||
exit $?
857 say
>&2 "Added dir '$dir'"
860 # Usage: cmd_split [REV]
864 rev=$
(git rev-parse HEAD
)
867 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
868 die
"'$1' does not refer to a commit"
870 die
"You must provide exactly one revision. Got: '$*'"
873 if test -n "$arg_split_rejoin"
878 debug
"Splitting $dir..."
879 cache_setup ||
exit $?
881 if test -n "$arg_split_onto"
883 debug
"Reading history for --onto=$arg_split_onto..."
884 git rev-list
$arg_split_onto |
887 # the 'onto' history is already just the subdir, so
888 # any parent we find there can be used verbatim
890 cache_set
"$rev" "$rev"
894 unrevs
="$(find_existing_splits "$dir" "$rev")" ||
exit $?
896 # We can't restrict rev-list to only $dir here, because some of our
897 # parents have the $dir contents the root, and those won't match.
898 # (and rev-list --follow doesn't seem to solve this)
899 grl
='git rev-list --topo-order --reverse --parents $rev $unrevs'
900 revmax
=$
(eval "$grl" |
wc -l)
905 while read rev parents
907 process_split_commit
"$rev" "$parents"
910 latest_new
=$
(cache_get latest_new
) ||
exit $?
911 if test -z "$latest_new"
913 die
"No new revisions were found"
916 if test -n "$arg_split_rejoin"
918 debug
"Merging split branch into HEAD..."
919 latest_old
=$
(cache_get latest_old
) ||
exit $?
920 arg_addmerge_message
="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" ||
exit $?
921 if test -z "$(find_latest_squash "$dir")"
923 cmd_add
"$latest_new" >&2 ||
exit $?
925 cmd_merge
"$latest_new" >&2 ||
exit $?
928 if test -n "$arg_split_branch"
930 if rev_exists
"refs/heads/$arg_split_branch"
932 if ! git merge-base
--is-ancestor "$arg_split_branch" "$latest_new"
934 die
"Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
940 git update-ref
-m 'subtree split' \
941 "refs/heads/$arg_split_branch" "$latest_new" ||
exit $?
942 say
>&2 "$action branch '$arg_split_branch'"
948 # Usage: cmd_merge REV
951 die
"You must provide exactly one revision. Got: '$*'"
952 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
953 die
"'$1' does not refer to a commit"
956 if test -n "$arg_addmerge_squash"
958 first_split
="$(find_latest_squash "$dir")" ||
exit $?
959 if test -z "$first_split"
961 die
"Can't squash-merge: '$dir' was never added."
966 if test "$sub" = "$rev"
968 say
>&2 "Subtree is already at commit $rev."
971 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
972 debug
"New squash commit: $new"
976 if test -n "$arg_addmerge_message"
978 git merge
--no-ff -Xsubtree="$arg_prefix" \
979 --message="$arg_addmerge_message" "$rev"
981 git merge
--no-ff -Xsubtree="$arg_prefix" $rev
985 # Usage: cmd_pull REPOSITORY REMOTEREF
989 die
"You must provide <repository> <ref>"
992 ensure_valid_ref_format
"$2"
993 git fetch
"$@" ||
exit $?
997 # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF
1001 die
"You must provide <repository> <refspec>"
1007 remoteref
=${refspec#*:}
1008 if test "$remoteref" = "$refspec"
1010 localrevname_presplit
=HEAD
1012 localrevname_presplit
=${refspec%%:*}
1014 ensure_valid_ref_format
"$remoteref"
1015 localrev_presplit
=$
(git rev-parse
-q --verify "$localrevname_presplit^{commit}") ||
1016 die
"'$localrevname_presplit' does not refer to a commit"
1018 echo "git push using: " "$repository" "$refspec"
1019 localrev
=$
(cmd_split
"$localrev_presplit") || die
1020 git push
"$repository" "$localrev":"refs/heads/$remoteref"
1022 die
"'$dir' must already exist. Try 'git subtree add'."