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>
38 d,debug! show debug messages
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
55 if test -z "$arg_quiet"
61 # Usage: debug [MSG...]
63 if test -n "$arg_debug"
65 printf "%$(($indent * 2))s%s\n" '' "$*" >&2
69 # Usage: progress [MSG...]
71 if test -z "$arg_quiet"
73 if test -z "$arg_debug"
77 # Print one progress line that we keep updating (use
78 # "\r" to return to the beginning of the line, rather
79 # than "\n" to start a new line). This only really
80 # works when stderr is a terminal.
81 printf "%s\r" "$*" >&2
83 # Debug mode is on. The `debug` function is regularly
86 # Don't do the one-line-with-"\r" thing, because on a
87 # terminal the debug output would overwrite and hide the
88 # progress output. Add a "progress:" prefix to make the
89 # progress output and the debug output easy to
90 # distinguish. This ensures maximum readability whether
91 # stderr is a terminal or a file.
92 printf "progress: %s\n" "$*" >&2
97 # Usage: assert CMD...
101 die
"fatal: assertion failed: $*"
105 # Usage: die_incompatible_opt OPTION COMMAND
106 die_incompatible_opt
() {
110 die
"fatal: the '$opt' flag does not make sense with 'git subtree $arg_command'."
118 set_args
="$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@
" || echo exit $?)"
123 # First figure out the command and whether we use --rejoin, so
124 # that we can provide more helpful validation when we do the
125 # "real" flag parsing.
134 --annotate|
-b|
-P|
-m|
--onto)
149 case "$arg_command" in
155 allow_addmerge
=$arg_split_rejoin
158 die
"fatal: unknown command '$arg_command'"
161 # Reset the arguments array for "real" flag parsing.
164 # Begin "real" flag parsing.
170 arg_split_ignore_joins
=
173 arg_addmerge_message
=
187 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
188 arg_split_annotate
="$1"
192 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
196 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
197 arg_split_branch
="$1"
205 test -n "$allow_addmerge" || die_incompatible_opt
"$opt" "$arg_command"
206 arg_addmerge_message
="$1"
213 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
218 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
222 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
225 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
228 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
229 arg_split_ignore_joins
=1
232 test -n "$allow_split" || die_incompatible_opt
"$opt" "$arg_command"
233 arg_split_ignore_joins
=
236 test -n "$allow_addmerge" || die_incompatible_opt
"$opt" "$arg_command"
237 arg_addmerge_squash
=1
240 test -n "$allow_addmerge" || die_incompatible_opt
"$opt" "$arg_command"
247 die
"fatal: unexpected option: $opt"
253 if test -z "$arg_prefix"
255 die
"fatal: you must provide the --prefix option."
258 case "$arg_command" in
260 test -e "$arg_prefix" &&
261 die
"fatal: prefix '$arg_prefix' already exists."
264 test -e "$arg_prefix" ||
265 die
"fatal: '$arg_prefix' does not exist; use 'git subtree add'"
269 dir
="$(dirname "$arg_prefix/.
")"
271 debug
"command: {$arg_command}"
272 debug
"quiet: {$arg_quiet}"
277 "cmd_$arg_command" "$@"
283 cachedir
="$GIT_DIR/subtree-cache/$$"
284 rm -rf "$cachedir" ||
285 die
"fatal: can't delete old cachedir: $cachedir"
286 mkdir
-p "$cachedir" ||
287 die
"fatal: can't create new cachedir: $cachedir"
288 mkdir
-p "$cachedir/notree" ||
289 die
"fatal: can't create new cachedir: $cachedir/notree"
290 debug
"Using cachedir: $cachedir" >&2
293 # Usage: cache_get [REVS...]
297 if test -r "$cachedir/$oldrev"
299 read newrev
<"$cachedir/$oldrev"
305 # Usage: cache_miss [REVS...]
309 if ! test -r "$cachedir/$oldrev"
316 # Usage: check_parents [REVS...]
318 missed
=$
(cache_miss
"$@") ||
exit $?
319 local indent
=$
(($indent + 1))
322 if ! test -r "$cachedir/notree/$miss"
324 debug
"incorrect order: $miss"
325 process_split_commit
"$miss" ""
330 # Usage: set_notree REV
333 echo "1" > "$cachedir/notree/$1"
336 # Usage: cache_set OLDREV NEWREV
341 if test "$oldrev" != "latest_old" &&
342 test "$oldrev" != "latest_new" &&
343 test -e "$cachedir/$oldrev"
345 die
"fatal: cache for $oldrev already exists!"
347 echo "$newrev" >"$cachedir/$oldrev"
350 # Usage: rev_exists REV
353 if git rev-parse
"$1" >/dev
/null
2>&1
361 # Usage: try_remove_previous REV
363 # If a commit doesn't have a parent, this might not work. But we only want
364 # to remove the parent from the rev-list, and since it doesn't exist, it won't
365 # be there anyway, so do nothing in that case.
366 try_remove_previous
() {
374 # Usage: process_subtree_split_trailer SPLIT_HASH MAIN_HASH [REPOSITORY]
375 process_subtree_split_trailer
() {
385 fail_msg
="fatal: could not rev-parse split hash $b from commit $sq"
386 if ! sub
="$(git rev-parse --verify --quiet "$b^
{commit
}")"
388 # if 'repository' was given, try to fetch the 'git-subtree-split' hash
389 # before 'rev-parse'-ing it again, as it might be a tag that we do not have locally
390 if test -n "${repository}"
392 git fetch
"$repository" "$b"
393 sub
="$(git rev-parse --verify --quiet "$b^
{commit
}")" ||
396 hint1
=$
(printf "hint: hash might be a tag, try fetching it from the subtree repository:")
397 hint2
=$
(printf "hint: git fetch <subtree-repository> $b")
398 fail_msg
=$
(printf "$fail_msg\n$hint1\n$hint2")
404 # Usage: find_latest_squash DIR [REPOSITORY]
405 find_latest_squash
() {
414 debug
"Looking for latest squash (dir=$dir, repository=$repository)..."
415 local indent
=$
(($indent + 1))
420 git log
--grep="^git-subtree-dir: $dir/*\$" \
421 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
425 debug
"{{$sq/$main/$sub}}"
430 git-subtree-mainline
:)
434 process_subtree_split_trailer
"$b" "$sq" "$repository"
442 # Pretend its sub was a squash.
443 sq=$
(git rev-parse
--verify "$sq^2") ||
446 debug
"Squash found: $sq $sub"
458 # Usage: find_existing_splits DIR REV [REPOSITORY]
459 find_existing_splits
() {
462 debug
"Looking for prior splits..."
463 local indent
=$
(($indent + 1))
474 local grep_format
="^git-subtree-dir: $dir/*\$"
475 if test -n "$arg_split_ignore_joins"
477 grep_format
="^Add '$dir/' from commit '"
479 git log
--grep="$grep_format" \
480 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' "$rev" |
487 git-subtree-mainline
:)
491 process_subtree_split_trailer
"$b" "$sq" "$repository"
494 debug
"Main is: '$main'"
495 if test -z "$main" && test -n "$sub"
497 # squash commits refer to a subtree
498 debug
" Squash: $sq from $sub"
499 cache_set
"$sq" "$sub"
501 if test -n "$main" && test -n "$sub"
503 debug
" Prior: $main -> $sub"
506 try_remove_previous
"$main"
507 try_remove_previous
"$sub"
516 # Usage: copy_commit REV TREE FLAGS_STR
519 # We're going to set some environment vars here, so
520 # do it in a subshell to get rid of them safely later
521 debug copy_commit
"{$1}" "{$2}" "{$3}"
522 git log
-1 --no-show-signature --pretty=format
:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
525 read GIT_AUTHOR_EMAIL
527 read GIT_COMMITTER_NAME
528 read GIT_COMMITTER_EMAIL
529 read GIT_COMMITTER_DATE
530 export GIT_AUTHOR_NAME \
534 GIT_COMMITTER_EMAIL \
537 printf "%s" "$arg_split_annotate"
540 git commit-tree
"$2" $3 # reads the rest of stdin
541 ) || die
"fatal: can't copy commit $1"
544 # Usage: add_msg DIR LATEST_OLD LATEST_NEW
550 if test -n "$arg_addmerge_message"
552 commit_message
="$arg_addmerge_message"
554 commit_message
="Add '$dir/' from commit '$latest_new'"
556 if test -n "$arg_split_rejoin"
558 # If this is from a --rejoin, then rejoin_msg has
559 # already inserted the `git-subtree-xxx:` tags
560 echo "$commit_message"
566 git-subtree-dir: $dir
567 git-subtree-mainline: $latest_old
568 git-subtree-split: $latest_new
572 # Usage: add_squashed_msg REV DIR
573 add_squashed_msg
() {
575 if test -n "$arg_addmerge_message"
577 echo "$arg_addmerge_message"
579 echo "Merge commit '$1' as '$2'"
583 # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
589 if test -n "$arg_addmerge_message"
591 commit_message
="$arg_addmerge_message"
593 commit_message
="Split '$dir/' into commit '$latest_new'"
598 git-subtree-dir: $dir
599 git-subtree-mainline: $latest_old
600 git-subtree-split: $latest_new
604 # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
610 newsub_short
=$
(git rev-parse
--short "$newsub")
614 oldsub_short
=$
(git rev-parse
--short "$oldsub")
615 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
617 git log
--no-show-signature --pretty=tformat
:'%h %s' "$oldsub..$newsub"
618 git log
--no-show-signature --pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
620 echo "Squashed '$dir/' content from commit $newsub_short"
624 echo "git-subtree-dir: $dir"
625 echo "git-subtree-split: $newsub"
628 # Usage: toptree_for_commit COMMIT
629 toptree_for_commit
() {
632 git rev-parse
--verify "$commit^{tree}" ||
exit $?
635 # Usage: subtree_for_commit COMMIT DIR
636 subtree_for_commit
() {
640 git ls-tree
"$commit" -- "$dir" |
641 while read mode
type tree name
643 assert
test "$name" = "$dir"
647 continue;; # ignore submodules
652 die
"fatal: tree entry is of type ${type}, expected tree or commit";;
657 # Usage: tree_changed TREE [PARENTS...]
664 return 0 # weird parents, consider it changed
666 ptree
=$
(toptree_for_commit
$1) ||
exit $?
667 if test "$ptree" != "$tree"
671 return 1 # not changed
676 # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
677 new_squash_commit
() {
682 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
685 squash_msg
"$dir" "$oldsub" "$newsub" |
686 git commit-tree
"$tree" -p "$old" ||
exit $?
688 squash_msg
"$dir" "" "$newsub" |
689 git commit-tree
"$tree" ||
exit $?
693 # Usage: copy_or_skip REV TREE NEWPARENTS
699 assert
test -n "$tree"
706 for parent
in $newparents
708 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
709 test -z "$ptree" && continue
710 if test "$ptree" = "$tree"
712 # an identical parent could be used in place of this rev.
713 if test -n "$identical"
715 # if a previous identical parent was found, check whether
716 # one is already an ancestor of the other
717 mergebase
=$
(git merge-base
$identical $parent)
718 if test "$identical" = "$mergebase"
720 # current identical commit is an ancestor of parent
722 elif test "$parent" != "$mergebase"
724 # no common history; commit must be copied
728 # first identical parent detected
732 nonidentical
="$parent"
735 # sometimes both old parents map to the same newparent;
736 # eliminate duplicates
738 for gp
in $gotparents
740 if test "$gp" = "$parent"
748 gotparents
="$gotparents $parent"
753 if test -n "$identical" && test -n "$nonidentical"
755 extras
=$
(git rev-list
--count $identical..
$nonidentical)
756 if test "$extras" -ne 0
758 # we need to preserve history along the other branch
762 if test -n "$identical" && test -z "$copycommit"
766 copy_commit
"$rev" "$tree" "$p" ||
exit $?
770 # Usage: ensure_clean
773 if ! git diff-index HEAD
--exit-code --quiet 2>&1
775 die
"fatal: working tree has modifications. Cannot add."
777 if ! git diff-index
--cached HEAD
--exit-code --quiet 2>&1
779 die
"fatal: index has modifications. Cannot add."
783 # Usage: ensure_valid_ref_format REF
784 ensure_valid_ref_format
() {
786 git check-ref-format
"refs/heads/$1" ||
787 die
"fatal: '$1' does not look like a ref"
790 # Usage: check if a commit from another subtree should be
791 # ignored from processing for splits
792 should_ignore_subtree_split_commit
() {
795 if test -n "$(git log -1 --grep="git-subtree-dir
:" $rev)"
797 if test -z "$(git log -1 --grep="git-subtree-mainline
:" $rev)" &&
798 test -z "$(git log -1 --grep="git-subtree-dir
: $arg_prefix$
" $rev)"
806 # Usage: process_split_commit REV PARENTS
807 process_split_commit
() {
812 if test $indent -eq 0
814 revcount
=$
(($revcount + 1))
816 # processing commit without normal parent information;
818 parents
=$
(git rev-parse
"$rev^@")
819 extracount
=$
(($extracount + 1))
822 progress
"$revcount/$revmax ($createcount) [$extracount]"
824 debug
"Processing commit: $rev"
825 local indent
=$
(($indent + 1))
826 exists
=$
(cache_get
"$rev") ||
exit $?
829 debug
"prior: $exists"
832 createcount
=$
(($createcount + 1))
833 debug
"parents: $parents"
834 check_parents
$parents
835 newparents
=$
(cache_get
$parents) ||
exit $?
836 debug
"newparents: $newparents"
838 tree
=$
(subtree_for_commit
"$rev" "$dir") ||
exit $?
839 debug
"tree is: $tree"
841 # ugly. is there no better way to tell if this is a subtree
842 # vs. a mainline commit? Does it matter?
846 if test -n "$newparents"
848 cache_set
"$rev" "$rev"
853 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
854 debug
"newrev is: $newrev"
855 cache_set
"$rev" "$newrev"
856 cache_set latest_new
"$newrev"
857 cache_set latest_old
"$rev"
861 # Or: cmd_add REPOSITORY REF
868 git rev-parse
-q --verify "$1^{commit}" >/dev
/null ||
869 die
"fatal: '$1' does not refer to a commit"
875 # Technically we could accept a refspec here but we're
876 # just going to turn around and add FETCH_HEAD under the
877 # specified directory. Allowing a refspec might be
878 # misleading because we won't do anything with any other
879 # branches fetched via the refspec.
880 ensure_valid_ref_format
"$2"
882 cmd_add_repository
"$@"
884 say
>&2 "fatal: parameters were '$*'"
885 die
"Provide either a commit or a repository and commit."
889 # Usage: cmd_add_repository REPOSITORY REFSPEC
890 cmd_add_repository
() {
892 echo "git fetch" "$@"
895 git fetch
"$@" ||
exit $?
896 cmd_add_commit FETCH_HEAD
899 # Usage: cmd_add_commit REV
901 # The rev has already been validated by cmd_add(), we just
902 # need to normalize it.
904 rev=$
(git rev-parse
--verify "$1^{commit}") ||
exit $?
906 debug
"Adding $dir as '$rev'..."
907 if test -z "$arg_split_rejoin"
909 # Only bother doing this if this is a genuine 'add',
910 # not a synthetic 'add' from '--rejoin'.
911 git read-tree
--prefix="$dir" $rev ||
exit $?
913 git checkout
-- "$dir" ||
exit $?
914 tree
=$
(git write-tree
) ||
exit $?
916 headrev
=$
(git rev-parse
--verify HEAD
) ||
exit $?
917 if test -n "$headrev" && test "$headrev" != "$rev"
924 if test -n "$arg_addmerge_squash"
926 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
927 commit
=$
(add_squashed_msg
"$rev" "$dir" |
928 git commit-tree
"$tree" $headp -p "$rev") ||
exit $?
930 revp
=$
(peel_committish
"$rev") ||
exit $?
931 commit
=$
(add_msg
"$dir" $headrev "$rev" |
932 git commit-tree
"$tree" $headp -p "$revp") ||
exit $?
934 git
reset "$commit" ||
exit $?
936 say
>&2 "Added dir '$dir'"
939 # Usage: cmd_split [REV] [REPOSITORY]
943 rev=$
(git rev-parse HEAD
)
944 elif test $# -eq 1 ||
test $# -eq 2
946 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
947 die
"fatal: '$1' does not refer to a commit"
949 die
"fatal: you must provide exactly one revision, and optionnally a repository. Got: '$*'"
957 if test -n "$arg_split_rejoin"
962 debug
"Splitting $dir..."
963 cache_setup ||
exit $?
965 if test -n "$arg_split_onto"
967 debug
"Reading history for --onto=$arg_split_onto..."
968 git rev-list
$arg_split_onto |
971 # the 'onto' history is already just the subdir, so
972 # any parent we find there can be used verbatim
974 cache_set
"$rev" "$rev"
978 unrevs
="$(find_existing_splits "$dir" "$rev" "$repository")" ||
exit $?
980 # We can't restrict rev-list to only $dir here, because some of our
981 # parents have the $dir contents the root, and those won't match.
982 # (and rev-list --follow doesn't seem to solve this)
983 grl
='git rev-list --topo-order --reverse --parents $rev $unrevs'
984 revmax
=$
(eval "$grl" |
wc -l)
989 while read rev parents
991 if should_ignore_subtree_split_commit
"$rev"
996 for parent
in $parents
998 if ! should_ignore_subtree_split_commit
"$parent"
1000 parsedparents
="$parsedparents$parent "
1003 process_split_commit
"$rev" "$parsedparents"
1006 latest_new
=$
(cache_get latest_new
) ||
exit $?
1007 if test -z "$latest_new"
1009 die
"fatal: no new revisions were found"
1012 if test -n "$arg_split_rejoin"
1014 debug
"Merging split branch into HEAD..."
1015 latest_old
=$
(cache_get latest_old
) ||
exit $?
1016 arg_addmerge_message
="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" ||
exit $?
1017 if test -z "$(find_latest_squash "$dir")"
1019 cmd_add
"$latest_new" >&2 ||
exit $?
1021 cmd_merge
"$latest_new" >&2 ||
exit $?
1024 if test -n "$arg_split_branch"
1026 if rev_exists
"refs/heads/$arg_split_branch"
1028 if ! git merge-base
--is-ancestor "$arg_split_branch" "$latest_new"
1030 die
"fatal: branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
1036 git update-ref
-m 'subtree split' \
1037 "refs/heads/$arg_split_branch" "$latest_new" ||
exit $?
1038 say
>&2 "$action branch '$arg_split_branch'"
1044 # Usage: cmd_merge REV [REPOSITORY]
1046 if test $# -lt 1 ||
test $# -gt 2
1048 die
"fatal: you must provide exactly one revision, and optionally a repository. Got: '$*'"
1051 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
1052 die
"fatal: '$1' does not refer to a commit"
1060 if test -n "$arg_addmerge_squash"
1062 first_split
="$(find_latest_squash "$dir" "$repository")" ||
exit $?
1063 if test -z "$first_split"
1065 die
"fatal: can't squash-merge: '$dir' was never added."
1070 if test "$sub" = "$rev"
1072 say
>&2 "Subtree is already at commit $rev."
1075 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
1076 debug
"New squash commit: $new"
1080 if test -n "$arg_addmerge_message"
1082 git merge
--no-ff -Xsubtree="$arg_prefix" \
1083 --message="$arg_addmerge_message" "$rev"
1085 git merge
--no-ff -Xsubtree="$arg_prefix" $rev
1089 # Usage: cmd_pull REPOSITORY REMOTEREF
1093 die
"fatal: you must provide <repository> <ref>"
1098 ensure_valid_ref_format
"$ref"
1099 git fetch
"$repository" "$ref" ||
exit $?
1100 cmd_merge FETCH_HEAD
"$repository"
1103 # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF
1107 die
"fatal: you must provide <repository> <refspec>"
1113 remoteref
=${refspec#*:}
1114 if test "$remoteref" = "$refspec"
1116 localrevname_presplit
=HEAD
1118 localrevname_presplit
=${refspec%%:*}
1120 ensure_valid_ref_format
"$remoteref"
1121 localrev_presplit
=$
(git rev-parse
-q --verify "$localrevname_presplit^{commit}") ||
1122 die
"fatal: '$localrevname_presplit' does not refer to a commit"
1124 echo "git push using: " "$repository" "$refspec"
1125 localrev
=$
(cmd_split
"$localrev_presplit" "$repository") || die
1126 git push
"$repository" "$localrev":"refs/heads/$remoteref"
1128 die
"fatal: '$dir' must already exist. Try 'git subtree add'."