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
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
"assertion failed: $*"
110 set_args
="$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@
" || echo exit $?)"
115 # First figure out the command and whether we use --rejoin, so
116 # that we can provide more helpful validation when we do the
117 # "real" flag parsing.
126 --annotate|
-b|
-P|
-m|
--onto)
141 case "$arg_command" in
147 allow_addmerge
=$arg_split_rejoin
150 die
"Unknown command '$arg_command'"
153 # Reset the arguments array for "real" flag parsing.
156 # Begin "real" flag parsing.
162 arg_split_ignore_joins
=
165 arg_addmerge_message
=
179 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
180 arg_split_annotate
="$1"
184 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
188 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
189 arg_split_branch
="$1"
197 test -n "$allow_addmerge" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
198 arg_addmerge_message
="$1"
205 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
210 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
214 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
217 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
220 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
221 arg_split_ignore_joins
=1
224 test -n "$allow_split" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
225 arg_split_ignore_joins
=
228 test -n "$allow_addmerge" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
229 arg_addmerge_squash
=1
232 test -n "$allow_addmerge" || die
"The '$opt' flag does not make sense with 'git subtree $arg_command'."
239 die
"Unexpected option: $opt"
245 if test -z "$arg_prefix"
247 die
"You must provide the --prefix option."
250 case "$arg_command" in
252 test -e "$arg_prefix" &&
253 die
"prefix '$arg_prefix' already exists."
256 test -e "$arg_prefix" ||
257 die
"'$arg_prefix' does not exist; use 'git subtree add'"
261 dir
="$(dirname "$arg_prefix/.
")"
263 debug
"command: {$arg_command}"
264 debug
"quiet: {$arg_quiet}"
269 "cmd_$arg_command" "$@"
275 cachedir
="$GIT_DIR/subtree-cache/$$"
276 rm -rf "$cachedir" ||
277 die
"Can't delete old cachedir: $cachedir"
278 mkdir
-p "$cachedir" ||
279 die
"Can't create new cachedir: $cachedir"
280 mkdir
-p "$cachedir/notree" ||
281 die
"Can't create new cachedir: $cachedir/notree"
282 debug
"Using cachedir: $cachedir" >&2
285 # Usage: cache_get [REVS...]
289 if test -r "$cachedir/$oldrev"
291 read newrev
<"$cachedir/$oldrev"
297 # Usage: cache_miss [REVS...]
301 if ! test -r "$cachedir/$oldrev"
308 # Usage: check_parents [REVS...]
310 missed
=$
(cache_miss
"$@") ||
exit $?
311 local indent
=$
(($indent + 1))
314 if ! test -r "$cachedir/notree/$miss"
316 debug
"incorrect order: $miss"
317 process_split_commit
"$miss" ""
322 # Usage: set_notree REV
325 echo "1" > "$cachedir/notree/$1"
328 # Usage: cache_set OLDREV NEWREV
333 if test "$oldrev" != "latest_old" &&
334 test "$oldrev" != "latest_new" &&
335 test -e "$cachedir/$oldrev"
337 die
"cache for $oldrev already exists!"
339 echo "$newrev" >"$cachedir/$oldrev"
342 # Usage: rev_exists REV
345 if git rev-parse
"$1" >/dev
/null
2>&1
353 # Usage: try_remove_previous REV
355 # If a commit doesn't have a parent, this might not work. But we only want
356 # to remove the parent from the rev-list, and since it doesn't exist, it won't
357 # be there anyway, so do nothing in that case.
358 try_remove_previous
() {
366 # Usage: find_latest_squash DIR
367 find_latest_squash
() {
369 debug
"Looking for latest squash ($dir)..."
370 local indent
=$
(($indent + 1))
376 git log
--grep="^git-subtree-dir: $dir/*\$" \
377 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
381 debug
"{{$sq/$main/$sub}}"
386 git-subtree-mainline
:)
390 sub
="$(git rev-parse "$b^
{commit
}")" ||
391 die
"could not rev-parse split hash $b from commit $sq"
399 # Pretend its sub was a squash.
400 sq=$
(git rev-parse
--verify "$sq^2") ||
403 debug
"Squash found: $sq $sub"
415 # Usage: find_existing_splits DIR REV
416 find_existing_splits
() {
418 debug
"Looking for prior splits..."
419 local indent
=$
(($indent + 1))
425 local grep_format
="^git-subtree-dir: $dir/*\$"
426 if test -n "$arg_split_ignore_joins"
428 grep_format
="^Add '$dir/' from commit '"
430 git log
--grep="$grep_format" \
431 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' "$rev" |
438 git-subtree-mainline
:)
442 sub
="$(git rev-parse "$b^
{commit
}")" ||
443 die
"could not rev-parse split hash $b from commit $sq"
446 debug
"Main is: '$main'"
447 if test -z "$main" -a -n "$sub"
449 # squash commits refer to a subtree
450 debug
" Squash: $sq from $sub"
451 cache_set
"$sq" "$sub"
453 if test -n "$main" -a -n "$sub"
455 debug
" Prior: $main -> $sub"
458 try_remove_previous
"$main"
459 try_remove_previous
"$sub"
468 # Usage: copy_commit REV TREE FLAGS_STR
471 # We're going to set some environment vars here, so
472 # do it in a subshell to get rid of them safely later
473 debug copy_commit
"{$1}" "{$2}" "{$3}"
474 git log
-1 --no-show-signature --pretty=format
:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
477 read GIT_AUTHOR_EMAIL
479 read GIT_COMMITTER_NAME
480 read GIT_COMMITTER_EMAIL
481 read GIT_COMMITTER_DATE
482 export GIT_AUTHOR_NAME \
486 GIT_COMMITTER_EMAIL \
489 printf "%s" "$arg_split_annotate"
492 git commit-tree
"$2" $3 # reads the rest of stdin
493 ) || die
"Can't copy commit $1"
496 # Usage: add_msg DIR LATEST_OLD LATEST_NEW
502 if test -n "$arg_addmerge_message"
504 commit_message
="$arg_addmerge_message"
506 commit_message
="Add '$dir/' from commit '$latest_new'"
508 if test -n "$arg_split_rejoin"
510 # If this is from a --rejoin, then rejoin_msg has
511 # already inserted the `git-subtree-xxx:` tags
512 echo "$commit_message"
518 git-subtree-dir: $dir
519 git-subtree-mainline: $latest_old
520 git-subtree-split: $latest_new
524 # Usage: add_squashed_msg REV DIR
525 add_squashed_msg
() {
527 if test -n "$arg_addmerge_message"
529 echo "$arg_addmerge_message"
531 echo "Merge commit '$1' as '$2'"
535 # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
541 if test -n "$arg_addmerge_message"
543 commit_message
="$arg_addmerge_message"
545 commit_message
="Split '$dir/' into commit '$latest_new'"
550 git-subtree-dir: $dir
551 git-subtree-mainline: $latest_old
552 git-subtree-split: $latest_new
556 # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
562 newsub_short
=$
(git rev-parse
--short "$newsub")
566 oldsub_short
=$
(git rev-parse
--short "$oldsub")
567 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
569 git log
--no-show-signature --pretty=tformat
:'%h %s' "$oldsub..$newsub"
570 git log
--no-show-signature --pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
572 echo "Squashed '$dir/' content from commit $newsub_short"
576 echo "git-subtree-dir: $dir"
577 echo "git-subtree-split: $newsub"
580 # Usage: toptree_for_commit COMMIT
581 toptree_for_commit
() {
584 git rev-parse
--verify "$commit^{tree}" ||
exit $?
587 # Usage: subtree_for_commit COMMIT DIR
588 subtree_for_commit
() {
592 git ls-tree
"$commit" -- "$dir" |
593 while read mode
type tree name
595 assert
test "$name" = "$dir"
596 assert
test "$type" = "tree" -o "$type" = "commit"
597 test "$type" = "commit" && continue # ignore submodules
603 # Usage: tree_changed TREE [PARENTS...]
610 return 0 # weird parents, consider it changed
612 ptree
=$
(toptree_for_commit
$1) ||
exit $?
613 if test "$ptree" != "$tree"
617 return 1 # not changed
622 # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
623 new_squash_commit
() {
628 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
631 squash_msg
"$dir" "$oldsub" "$newsub" |
632 git commit-tree
"$tree" -p "$old" ||
exit $?
634 squash_msg
"$dir" "" "$newsub" |
635 git commit-tree
"$tree" ||
exit $?
639 # Usage: copy_or_skip REV TREE NEWPARENTS
645 assert
test -n "$tree"
652 for parent
in $newparents
654 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
655 test -z "$ptree" && continue
656 if test "$ptree" = "$tree"
658 # an identical parent could be used in place of this rev.
659 if test -n "$identical"
661 # if a previous identical parent was found, check whether
662 # one is already an ancestor of the other
663 mergebase
=$
(git merge-base
$identical $parent)
664 if test "$identical" = "$mergebase"
666 # current identical commit is an ancestor of parent
668 elif test "$parent" != "$mergebase"
670 # no common history; commit must be copied
674 # first identical parent detected
678 nonidentical
="$parent"
681 # sometimes both old parents map to the same newparent;
682 # eliminate duplicates
684 for gp
in $gotparents
686 if test "$gp" = "$parent"
694 gotparents
="$gotparents $parent"
699 if test -n "$identical" && test -n "$nonidentical"
701 extras
=$
(git rev-list
--count $identical..
$nonidentical)
702 if test "$extras" -ne 0
704 # we need to preserve history along the other branch
708 if test -n "$identical" && test -z "$copycommit"
712 copy_commit
"$rev" "$tree" "$p" ||
exit $?
716 # Usage: ensure_clean
719 if ! git diff-index HEAD
--exit-code --quiet 2>&1
721 die
"Working tree has modifications. Cannot add."
723 if ! git diff-index
--cached HEAD
--exit-code --quiet 2>&1
725 die
"Index has modifications. Cannot add."
729 # Usage: ensure_valid_ref_format REF
730 ensure_valid_ref_format
() {
732 git check-ref-format
"refs/heads/$1" ||
733 die
"'$1' does not look like a ref"
736 # Usage: process_split_commit REV PARENTS
737 process_split_commit
() {
742 if test $indent -eq 0
744 revcount
=$
(($revcount + 1))
746 # processing commit without normal parent information;
748 parents
=$
(git rev-parse
"$rev^@")
749 extracount
=$
(($extracount + 1))
752 progress
"$revcount/$revmax ($createcount) [$extracount]"
754 debug
"Processing commit: $rev"
755 local indent
=$
(($indent + 1))
756 exists
=$
(cache_get
"$rev") ||
exit $?
759 debug
"prior: $exists"
762 createcount
=$
(($createcount + 1))
763 debug
"parents: $parents"
764 check_parents
$parents
765 newparents
=$
(cache_get
$parents) ||
exit $?
766 debug
"newparents: $newparents"
768 tree
=$
(subtree_for_commit
"$rev" "$dir") ||
exit $?
769 debug
"tree is: $tree"
771 # ugly. is there no better way to tell if this is a subtree
772 # vs. a mainline commit? Does it matter?
776 if test -n "$newparents"
778 cache_set
"$rev" "$rev"
783 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
784 debug
"newrev is: $newrev"
785 cache_set
"$rev" "$newrev"
786 cache_set latest_new
"$newrev"
787 cache_set latest_old
"$rev"
791 # Or: cmd_add REPOSITORY REF
798 git rev-parse
-q --verify "$1^{commit}" >/dev
/null ||
799 die
"'$1' does not refer to a commit"
805 # Technically we could accept a refspec here but we're
806 # just going to turn around and add FETCH_HEAD under the
807 # specified directory. Allowing a refspec might be
808 # misleading because we won't do anything with any other
809 # branches fetched via the refspec.
810 ensure_valid_ref_format
"$2"
812 cmd_add_repository
"$@"
814 say
>&2 "error: parameters were '$*'"
815 die
"Provide either a commit or a repository and commit."
819 # Usage: cmd_add_repository REPOSITORY REFSPEC
820 cmd_add_repository
() {
822 echo "git fetch" "$@"
825 git fetch
"$@" ||
exit $?
826 cmd_add_commit FETCH_HEAD
829 # Usage: cmd_add_commit REV
831 # The rev has already been validated by cmd_add(), we just
832 # need to normalize it.
834 rev=$
(git rev-parse
--verify "$1^{commit}") ||
exit $?
836 debug
"Adding $dir as '$rev'..."
837 if test -z "$arg_split_rejoin"
839 # Only bother doing this if this is a genuine 'add',
840 # not a synthetic 'add' from '--rejoin'.
841 git read-tree
--prefix="$dir" $rev ||
exit $?
843 git checkout
-- "$dir" ||
exit $?
844 tree
=$
(git write-tree
) ||
exit $?
846 headrev
=$
(git rev-parse HEAD
) ||
exit $?
847 if test -n "$headrev" && test "$headrev" != "$rev"
854 if test -n "$arg_addmerge_squash"
856 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
857 commit
=$
(add_squashed_msg
"$rev" "$dir" |
858 git commit-tree
"$tree" $headp -p "$rev") ||
exit $?
860 revp
=$
(peel_committish
"$rev") ||
exit $?
861 commit
=$
(add_msg
"$dir" $headrev "$rev" |
862 git commit-tree
"$tree" $headp -p "$revp") ||
exit $?
864 git
reset "$commit" ||
exit $?
866 say
>&2 "Added dir '$dir'"
869 # Usage: cmd_split [REV]
873 rev=$
(git rev-parse HEAD
)
876 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
877 die
"'$1' does not refer to a commit"
879 die
"You must provide exactly one revision. Got: '$*'"
882 if test -n "$arg_split_rejoin"
887 debug
"Splitting $dir..."
888 cache_setup ||
exit $?
890 if test -n "$arg_split_onto"
892 debug
"Reading history for --onto=$arg_split_onto..."
893 git rev-list
$arg_split_onto |
896 # the 'onto' history is already just the subdir, so
897 # any parent we find there can be used verbatim
899 cache_set
"$rev" "$rev"
903 unrevs
="$(find_existing_splits "$dir" "$rev")" ||
exit $?
905 # We can't restrict rev-list to only $dir here, because some of our
906 # parents have the $dir contents the root, and those won't match.
907 # (and rev-list --follow doesn't seem to solve this)
908 grl
='git rev-list --topo-order --reverse --parents $rev $unrevs'
909 revmax
=$
(eval "$grl" |
wc -l)
914 while read rev parents
916 process_split_commit
"$rev" "$parents"
919 latest_new
=$
(cache_get latest_new
) ||
exit $?
920 if test -z "$latest_new"
922 die
"No new revisions were found"
925 if test -n "$arg_split_rejoin"
927 debug
"Merging split branch into HEAD..."
928 latest_old
=$
(cache_get latest_old
) ||
exit $?
929 arg_addmerge_message
="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" ||
exit $?
930 if test -z "$(find_latest_squash "$dir")"
932 cmd_add
"$latest_new" >&2 ||
exit $?
934 cmd_merge
"$latest_new" >&2 ||
exit $?
937 if test -n "$arg_split_branch"
939 if rev_exists
"refs/heads/$arg_split_branch"
941 if ! git merge-base
--is-ancestor "$arg_split_branch" "$latest_new"
943 die
"Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
949 git update-ref
-m 'subtree split' \
950 "refs/heads/$arg_split_branch" "$latest_new" ||
exit $?
951 say
>&2 "$action branch '$arg_split_branch'"
957 # Usage: cmd_merge REV
960 die
"You must provide exactly one revision. Got: '$*'"
961 rev=$
(git rev-parse
-q --verify "$1^{commit}") ||
962 die
"'$1' does not refer to a commit"
965 if test -n "$arg_addmerge_squash"
967 first_split
="$(find_latest_squash "$dir")" ||
exit $?
968 if test -z "$first_split"
970 die
"Can't squash-merge: '$dir' was never added."
975 if test "$sub" = "$rev"
977 say
>&2 "Subtree is already at commit $rev."
980 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
981 debug
"New squash commit: $new"
985 if test -n "$arg_addmerge_message"
987 git merge
--no-ff -Xsubtree="$arg_prefix" \
988 --message="$arg_addmerge_message" "$rev"
990 git merge
--no-ff -Xsubtree="$arg_prefix" $rev
994 # Usage: cmd_pull REPOSITORY REMOTEREF
998 die
"You must provide <repository> <ref>"
1001 ensure_valid_ref_format
"$2"
1002 git fetch
"$@" ||
exit $?
1003 cmd_merge FETCH_HEAD
1006 # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF
1010 die
"You must provide <repository> <refspec>"
1016 remoteref
=${refspec#*:}
1017 if test "$remoteref" = "$refspec"
1019 localrevname_presplit
=HEAD
1021 localrevname_presplit
=${refspec%%:*}
1023 ensure_valid_ref_format
"$remoteref"
1024 localrev_presplit
=$
(git rev-parse
-q --verify "$localrevname_presplit^{commit}") ||
1025 die
"'$localrevname_presplit' does not refer to a commit"
1027 echo "git push using: " "$repository" "$refspec"
1028 localrev
=$
(cmd_split
"$localrev_presplit") || die
1029 git push
"$repository" "$localrev":"refs/heads/$remoteref"
1031 die
"'$dir' must already exist. Try 'git subtree add'."