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 PARENTS_EXPR
302 missed
=$
(cache_miss
"$1") ||
exit $?
303 local indent
=$
(($indent + 1))
306 if ! test -r "$cachedir/notree/$miss"
308 debug
"incorrect order: $miss"
309 process_split_commit
"$miss" ""
314 # Usage: set_notree REV
317 echo "1" > "$cachedir/notree/$1"
320 # Usage: cache_set OLDREV NEWREV
325 if test "$oldrev" != "latest_old" &&
326 test "$oldrev" != "latest_new" &&
327 test -e "$cachedir/$oldrev"
329 die
"cache for $oldrev already exists!"
331 echo "$newrev" >"$cachedir/$oldrev"
334 # Usage: rev_exists REV
337 if git rev-parse
"$1" >/dev
/null
2>&1
345 # Usage: try_remove_previous REV
347 # If a commit doesn't have a parent, this might not work. But we only want
348 # to remove the parent from the rev-list, and since it doesn't exist, it won't
349 # be there anyway, so do nothing in that case.
350 try_remove_previous
() {
358 # Usage: find_latest_squash DIR
359 find_latest_squash
() {
361 debug
"Looking for latest squash ($dir)..."
362 local indent
=$
(($indent + 1))
368 git log
--grep="^git-subtree-dir: $dir/*\$" \
369 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
373 debug
"{{$sq/$main/$sub}}"
378 git-subtree-mainline
:)
382 sub
="$(git rev-parse "$b^
{commit
}")" ||
383 die
"could not rev-parse split hash $b from commit $sq"
391 # Pretend its sub was a squash.
392 sq=$
(git rev-parse
--verify "$sq^2") ||
395 debug
"Squash found: $sq $sub"
407 # Usage: find_existing_splits DIR REV
408 find_existing_splits
() {
410 debug
"Looking for prior splits..."
411 local indent
=$
(($indent + 1))
417 local grep_format
="^git-subtree-dir: $dir/*\$"
418 if test -n "$arg_split_ignore_joins"
420 grep_format
="^Add '$dir/' from commit '"
422 git log
--grep="$grep_format" \
423 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' "$rev" |
430 git-subtree-mainline
:)
434 sub
="$(git rev-parse "$b^
{commit
}")" ||
435 die
"could not rev-parse split hash $b from commit $sq"
438 debug
"Main is: '$main'"
439 if test -z "$main" -a -n "$sub"
441 # squash commits refer to a subtree
442 debug
" Squash: $sq from $sub"
443 cache_set
"$sq" "$sub"
445 if test -n "$main" -a -n "$sub"
447 debug
" Prior: $main -> $sub"
450 try_remove_previous
"$main"
451 try_remove_previous
"$sub"
460 # Usage: copy_commit REV TREE FLAGS_STR
463 # We're going to set some environment vars here, so
464 # do it in a subshell to get rid of them safely later
465 debug copy_commit
"{$1}" "{$2}" "{$3}"
466 git log
-1 --no-show-signature --pretty=format
:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
469 read GIT_AUTHOR_EMAIL
471 read GIT_COMMITTER_NAME
472 read GIT_COMMITTER_EMAIL
473 read GIT_COMMITTER_DATE
474 export GIT_AUTHOR_NAME \
478 GIT_COMMITTER_EMAIL \
481 printf "%s" "$arg_split_annotate"
484 git commit-tree
"$2" $3 # reads the rest of stdin
485 ) || die
"Can't copy commit $1"
488 # Usage: add_msg DIR LATEST_OLD LATEST_NEW
494 if test -n "$arg_addmerge_message"
496 commit_message
="$arg_addmerge_message"
498 commit_message
="Add '$dir/' from commit '$latest_new'"
500 if test -n "$arg_split_rejoin"
502 # If this is from a --rejoin, then rejoin_msg has
503 # already inserted the `git-subtree-xxx:` tags
504 echo "$commit_message"
510 git-subtree-dir: $dir
511 git-subtree-mainline: $latest_old
512 git-subtree-split: $latest_new
516 # Usage: add_squashed_msg REV DIR
517 add_squashed_msg
() {
519 if test -n "$arg_addmerge_message"
521 echo "$arg_addmerge_message"
523 echo "Merge commit '$1' as '$2'"
527 # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
533 if test -n "$arg_addmerge_message"
535 commit_message
="$arg_addmerge_message"
537 commit_message
="Split '$dir/' into commit '$latest_new'"
542 git-subtree-dir: $dir
543 git-subtree-mainline: $latest_old
544 git-subtree-split: $latest_new
548 # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
554 newsub_short
=$
(git rev-parse
--short "$newsub")
558 oldsub_short
=$
(git rev-parse
--short "$oldsub")
559 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
561 git log
--no-show-signature --pretty=tformat
:'%h %s' "$oldsub..$newsub"
562 git log
--no-show-signature --pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
564 echo "Squashed '$dir/' content from commit $newsub_short"
568 echo "git-subtree-dir: $dir"
569 echo "git-subtree-split: $newsub"
572 # Usage: toptree_for_commit COMMIT
573 toptree_for_commit
() {
576 git rev-parse
--verify "$commit^{tree}" ||
exit $?
579 # Usage: subtree_for_commit COMMIT DIR
580 subtree_for_commit
() {
584 git ls-tree
"$commit" -- "$dir" |
585 while read mode
type tree name
587 assert
test "$name" = "$dir"
588 assert
test "$type" = "tree" -o "$type" = "commit"
589 test "$type" = "commit" && continue # ignore submodules
595 # Usage: tree_changed TREE [PARENTS...]
602 return 0 # weird parents, consider it changed
604 ptree
=$
(toptree_for_commit
$1) ||
exit $?
605 if test "$ptree" != "$tree"
609 return 1 # not changed
614 # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
615 new_squash_commit
() {
620 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
623 squash_msg
"$dir" "$oldsub" "$newsub" |
624 git commit-tree
"$tree" -p "$old" ||
exit $?
626 squash_msg
"$dir" "" "$newsub" |
627 git commit-tree
"$tree" ||
exit $?
631 # Usage: copy_or_skip REV TREE NEWPARENTS
637 assert
test -n "$tree"
644 for parent
in $newparents
646 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
647 test -z "$ptree" && continue
648 if test "$ptree" = "$tree"
650 # an identical parent could be used in place of this rev.
651 if test -n "$identical"
653 # if a previous identical parent was found, check whether
654 # one is already an ancestor of the other
655 mergebase
=$
(git merge-base
$identical $parent)
656 if test "$identical" = "$mergebase"
658 # current identical commit is an ancestor of parent
660 elif test "$parent" != "$mergebase"
662 # no common history; commit must be copied
666 # first identical parent detected
670 nonidentical
="$parent"
673 # sometimes both old parents map to the same newparent;
674 # eliminate duplicates
676 for gp
in $gotparents
678 if test "$gp" = "$parent"
686 gotparents
="$gotparents $parent"
691 if test -n "$identical" && test -n "$nonidentical"
693 extras
=$
(git rev-list
--count $identical..
$nonidentical)
694 if test "$extras" -ne 0
696 # we need to preserve history along the other branch
700 if test -n "$identical" && test -z "$copycommit"
704 copy_commit
"$rev" "$tree" "$p" ||
exit $?
708 # Usage: ensure_clean
711 if ! git diff-index HEAD
--exit-code --quiet 2>&1
713 die
"Working tree has modifications. Cannot add."
715 if ! git diff-index
--cached HEAD
--exit-code --quiet 2>&1
717 die
"Index has modifications. Cannot add."
721 # Usage: ensure_valid_ref_format REF
722 ensure_valid_ref_format
() {
724 git check-ref-format
"refs/heads/$1" ||
725 die
"'$1' does not look like a ref"
728 # Usage: process_split_commit REV PARENTS
729 process_split_commit
() {
734 if test $indent -eq 0
736 revcount
=$
(($revcount + 1))
738 # processing commit without normal parent information;
740 parents
=$
(git rev-parse
"$rev^@")
741 extracount
=$
(($extracount + 1))
744 progress
"$revcount/$revmax ($createcount) [$extracount]"
746 debug
"Processing commit: $rev"
747 local indent
=$
(($indent + 1))
748 exists
=$
(cache_get
"$rev") ||
exit $?
751 debug
"prior: $exists"
754 createcount
=$
(($createcount + 1))
755 debug
"parents: $parents"
756 check_parents
"$parents"
757 newparents
=$
(cache_get
$parents) ||
exit $?
758 debug
"newparents: $newparents"
760 tree
=$
(subtree_for_commit
"$rev" "$dir") ||
exit $?
761 debug
"tree is: $tree"
763 # ugly. is there no better way to tell if this is a subtree
764 # vs. a mainline commit? Does it matter?
768 if test -n "$newparents"
770 cache_set
"$rev" "$rev"
775 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
776 debug
"newrev is: $newrev"
777 cache_set
"$rev" "$newrev"
778 cache_set latest_new
"$newrev"
779 cache_set latest_old
"$rev"
783 # Or: cmd_add REPOSITORY REF
790 git rev-parse
-q --verify "$1^{commit}" >/dev
/null ||
791 die
"'$1' does not refer to a commit"
797 # Technically we could accept a refspec here but we're
798 # just going to turn around and add FETCH_HEAD under the
799 # specified directory. Allowing a refspec might be
800 # misleading because we won't do anything with any other
801 # branches fetched via the refspec.
802 ensure_valid_ref_format
"$2"
804 cmd_add_repository
"$@"
806 say
>&2 "error: parameters were '$*'"
807 die
"Provide either a commit or a repository and commit."
811 # Usage: cmd_add_repository REPOSITORY REFSPEC
812 cmd_add_repository
() {
814 echo "git fetch" "$@"
817 git fetch
"$@" ||
exit $?
818 cmd_add_commit FETCH_HEAD
821 # Usage: cmd_add_commit REV
823 # The rev has already been validated by cmd_add(), we just
824 # need to normalize it.
826 rev=$
(git rev-parse
--verify "$1^{commit}") ||
exit $?
828 debug
"Adding $dir as '$rev'..."
829 if test -z "$arg_split_rejoin"
831 # Only bother doing this if this is a genuine 'add',
832 # not a synthetic 'add' from '--rejoin'.
833 git read-tree
--prefix="$dir" $rev ||
exit $?
835 git checkout
-- "$dir" ||
exit $?
836 tree
=$
(git write-tree
) ||
exit $?
838 headrev
=$
(git rev-parse HEAD
) ||
exit $?
839 if test -n "$headrev" && test "$headrev" != "$rev"
846 if test -n "$arg_addmerge_squash"
848 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
849 commit
=$
(add_squashed_msg
"$rev" "$dir" |
850 git commit-tree
"$tree" $headp -p "$rev") ||
exit $?
852 revp
=$
(peel_committish
"$rev") ||
exit $?
853 commit
=$
(add_msg
"$dir" $headrev "$rev" |
854 git commit-tree
"$tree" $headp -p "$revp") ||
exit $?
856 git
reset "$commit" ||
exit $?
858 say
>&2 "Added dir '$dir'"
861 # Usage: cmd_split [REV]
865 rev=$
(git rev-parse HEAD
)
868 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
869 die
"'$1' does not refer to a commit"
871 die
"You must provide exactly one revision. Got: '$*'"
874 if test -n "$arg_split_rejoin"
879 debug
"Splitting $dir..."
880 cache_setup ||
exit $?
882 if test -n "$arg_split_onto"
884 debug
"Reading history for --onto=$arg_split_onto..."
885 git rev-list
$arg_split_onto |
888 # the 'onto' history is already just the subdir, so
889 # any parent we find there can be used verbatim
891 cache_set
"$rev" "$rev"
895 unrevs
="$(find_existing_splits "$dir" "$rev")" ||
exit $?
897 # We can't restrict rev-list to only $dir here, because some of our
898 # parents have the $dir contents the root, and those won't match.
899 # (and rev-list --follow doesn't seem to solve this)
900 grl
='git rev-list --topo-order --reverse --parents $rev $unrevs'
901 revmax
=$
(eval "$grl" |
wc -l)
906 while read rev parents
908 process_split_commit
"$rev" "$parents"
911 latest_new
=$
(cache_get latest_new
) ||
exit $?
912 if test -z "$latest_new"
914 die
"No new revisions were found"
917 if test -n "$arg_split_rejoin"
919 debug
"Merging split branch into HEAD..."
920 latest_old
=$
(cache_get latest_old
) ||
exit $?
921 arg_addmerge_message
="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" ||
exit $?
922 if test -z "$(find_latest_squash "$dir")"
924 cmd_add
"$latest_new" >&2 ||
exit $?
926 cmd_merge
"$latest_new" >&2 ||
exit $?
929 if test -n "$arg_split_branch"
931 if rev_exists
"refs/heads/$arg_split_branch"
933 if ! git merge-base
--is-ancestor "$arg_split_branch" "$latest_new"
935 die
"Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
941 git update-ref
-m 'subtree split' \
942 "refs/heads/$arg_split_branch" "$latest_new" ||
exit $?
943 say
>&2 "$action branch '$arg_split_branch'"
949 # Usage: cmd_merge REV
952 die
"You must provide exactly one revision. Got: '$*'"
953 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
954 die
"'$1' does not refer to a commit"
957 if test -n "$arg_addmerge_squash"
959 first_split
="$(find_latest_squash "$dir")" ||
exit $?
960 if test -z "$first_split"
962 die
"Can't squash-merge: '$dir' was never added."
967 if test "$sub" = "$rev"
969 say
>&2 "Subtree is already at commit $rev."
972 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
973 debug
"New squash commit: $new"
977 if test -n "$arg_addmerge_message"
979 git merge
-Xsubtree="$arg_prefix" \
980 --message="$arg_addmerge_message" "$rev"
982 git merge
-Xsubtree="$arg_prefix" $rev
986 # Usage: cmd_pull REPOSITORY REMOTEREF
990 die
"You must provide <repository> <ref>"
993 ensure_valid_ref_format
"$2"
994 git fetch
"$@" ||
exit $?
998 # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF
1002 die
"You must provide <repository> <refspec>"
1008 remoteref
=${refspec#*:}
1009 if test "$remoteref" = "$refspec"
1011 localrevname_presplit
=HEAD
1013 localrevname_presplit
=${refspec%%:*}
1015 ensure_valid_ref_format
"$remoteref"
1016 localrev_presplit
=$
(git rev-parse
-q --verify "$localrevname_presplit^{commit}") ||
1017 die
"'$localrevname_presplit' does not refer to a commit"
1019 echo "git push using: " "$repository" "$refspec"
1020 localrev
=$
(cmd_split
"$localrev_presplit") || die
1021 git push
"$repository" "$localrev":"refs/heads/$remoteref"
1023 die
"'$dir' must already exist. Try 'git subtree add'."