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: process_split_commit REV PARENTS
791 process_split_commit
() {
796 if test $indent -eq 0
798 revcount
=$
(($revcount + 1))
800 # processing commit without normal parent information;
802 parents
=$
(git rev-parse
"$rev^@")
803 extracount
=$
(($extracount + 1))
806 progress
"$revcount/$revmax ($createcount) [$extracount]"
808 debug
"Processing commit: $rev"
809 local indent
=$
(($indent + 1))
810 exists
=$
(cache_get
"$rev") ||
exit $?
813 debug
"prior: $exists"
816 createcount
=$
(($createcount + 1))
817 debug
"parents: $parents"
818 check_parents
$parents
819 newparents
=$
(cache_get
$parents) ||
exit $?
820 debug
"newparents: $newparents"
822 tree
=$
(subtree_for_commit
"$rev" "$dir") ||
exit $?
823 debug
"tree is: $tree"
825 # ugly. is there no better way to tell if this is a subtree
826 # vs. a mainline commit? Does it matter?
830 if test -n "$newparents"
832 cache_set
"$rev" "$rev"
837 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
838 debug
"newrev is: $newrev"
839 cache_set
"$rev" "$newrev"
840 cache_set latest_new
"$newrev"
841 cache_set latest_old
"$rev"
845 # Or: cmd_add REPOSITORY REF
852 git rev-parse
-q --verify "$1^{commit}" >/dev
/null ||
853 die
"fatal: '$1' does not refer to a commit"
859 # Technically we could accept a refspec here but we're
860 # just going to turn around and add FETCH_HEAD under the
861 # specified directory. Allowing a refspec might be
862 # misleading because we won't do anything with any other
863 # branches fetched via the refspec.
864 ensure_valid_ref_format
"$2"
866 cmd_add_repository
"$@"
868 say
>&2 "fatal: parameters were '$*'"
869 die
"Provide either a commit or a repository and commit."
873 # Usage: cmd_add_repository REPOSITORY REFSPEC
874 cmd_add_repository
() {
876 echo "git fetch" "$@"
879 git fetch
"$@" ||
exit $?
880 cmd_add_commit FETCH_HEAD
883 # Usage: cmd_add_commit REV
885 # The rev has already been validated by cmd_add(), we just
886 # need to normalize it.
888 rev=$
(git rev-parse
--verify "$1^{commit}") ||
exit $?
890 debug
"Adding $dir as '$rev'..."
891 if test -z "$arg_split_rejoin"
893 # Only bother doing this if this is a genuine 'add',
894 # not a synthetic 'add' from '--rejoin'.
895 git read-tree
--prefix="$dir" $rev ||
exit $?
897 git checkout
-- "$dir" ||
exit $?
898 tree
=$
(git write-tree
) ||
exit $?
900 headrev
=$
(git rev-parse
--verify HEAD
) ||
exit $?
901 if test -n "$headrev" && test "$headrev" != "$rev"
908 if test -n "$arg_addmerge_squash"
910 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
911 commit
=$
(add_squashed_msg
"$rev" "$dir" |
912 git commit-tree
"$tree" $headp -p "$rev") ||
exit $?
914 revp
=$
(peel_committish
"$rev") ||
exit $?
915 commit
=$
(add_msg
"$dir" $headrev "$rev" |
916 git commit-tree
"$tree" $headp -p "$revp") ||
exit $?
918 git
reset "$commit" ||
exit $?
920 say
>&2 "Added dir '$dir'"
923 # Usage: cmd_split [REV] [REPOSITORY]
927 rev=$
(git rev-parse HEAD
)
928 elif test $# -eq 1 ||
test $# -eq 2
930 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
931 die
"fatal: '$1' does not refer to a commit"
933 die
"fatal: you must provide exactly one revision, and optionnally a repository. Got: '$*'"
941 if test -n "$arg_split_rejoin"
946 debug
"Splitting $dir..."
947 cache_setup ||
exit $?
949 if test -n "$arg_split_onto"
951 debug
"Reading history for --onto=$arg_split_onto..."
952 git rev-list
$arg_split_onto |
955 # the 'onto' history is already just the subdir, so
956 # any parent we find there can be used verbatim
958 cache_set
"$rev" "$rev"
962 unrevs
="$(find_existing_splits "$dir" "$rev" "$repository")" ||
exit $?
964 # We can't restrict rev-list to only $dir here, because some of our
965 # parents have the $dir contents the root, and those won't match.
966 # (and rev-list --follow doesn't seem to solve this)
967 grl
='git rev-list --topo-order --reverse --parents $rev $unrevs'
968 revmax
=$
(eval "$grl" |
wc -l)
973 while read rev parents
975 process_split_commit
"$rev" "$parents"
978 latest_new
=$
(cache_get latest_new
) ||
exit $?
979 if test -z "$latest_new"
981 die
"fatal: no new revisions were found"
984 if test -n "$arg_split_rejoin"
986 debug
"Merging split branch into HEAD..."
987 latest_old
=$
(cache_get latest_old
) ||
exit $?
988 arg_addmerge_message
="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" ||
exit $?
989 if test -z "$(find_latest_squash "$dir")"
991 cmd_add
"$latest_new" >&2 ||
exit $?
993 cmd_merge
"$latest_new" >&2 ||
exit $?
996 if test -n "$arg_split_branch"
998 if rev_exists
"refs/heads/$arg_split_branch"
1000 if ! git merge-base
--is-ancestor "$arg_split_branch" "$latest_new"
1002 die
"fatal: branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
1008 git update-ref
-m 'subtree split' \
1009 "refs/heads/$arg_split_branch" "$latest_new" ||
exit $?
1010 say
>&2 "$action branch '$arg_split_branch'"
1016 # Usage: cmd_merge REV [REPOSITORY]
1018 if test $# -lt 1 ||
test $# -gt 2
1020 die
"fatal: you must provide exactly one revision, and optionally a repository. Got: '$*'"
1023 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
1024 die
"fatal: '$1' does not refer to a commit"
1032 if test -n "$arg_addmerge_squash"
1034 first_split
="$(find_latest_squash "$dir" "$repository")" ||
exit $?
1035 if test -z "$first_split"
1037 die
"fatal: can't squash-merge: '$dir' was never added."
1042 if test "$sub" = "$rev"
1044 say
>&2 "Subtree is already at commit $rev."
1047 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
1048 debug
"New squash commit: $new"
1052 if test -n "$arg_addmerge_message"
1054 git merge
--no-ff -Xsubtree="$arg_prefix" \
1055 --message="$arg_addmerge_message" "$rev"
1057 git merge
--no-ff -Xsubtree="$arg_prefix" $rev
1061 # Usage: cmd_pull REPOSITORY REMOTEREF
1065 die
"fatal: you must provide <repository> <ref>"
1070 ensure_valid_ref_format
"$ref"
1071 git fetch
"$repository" "$ref" ||
exit $?
1072 cmd_merge FETCH_HEAD
"$repository"
1075 # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF
1079 die
"fatal: you must provide <repository> <refspec>"
1085 remoteref
=${refspec#*:}
1086 if test "$remoteref" = "$refspec"
1088 localrevname_presplit
=HEAD
1090 localrevname_presplit
=${refspec%%:*}
1092 ensure_valid_ref_format
"$remoteref"
1093 localrev_presplit
=$
(git rev-parse
-q --verify "$localrevname_presplit^{commit}") ||
1094 die
"fatal: '$localrevname_presplit' does not refer to a commit"
1096 echo "git push using: " "$repository" "$refspec"
1097 localrev
=$
(cmd_split
"$localrev_presplit" "$repository") || die
1098 git push
"$repository" "$localrev":"refs/heads/$remoteref"
1100 die
"fatal: '$dir' must already exist. Try 'git subtree add'."