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 "${PATH#"${GIT_EXEC_PATH}:"}" = "$PATH" || ! test -f "$GIT_EXEC_PATH/git-sh-setup"
10 echo >&2 'It looks like either your git installation or your'
11 echo >&2 'git-subtree installation is broken.'
14 echo >&2 " - If \`git --exec-path\` does not print the correct path to"
15 echo >&2 " your git install directory, then set the GIT_EXEC_PATH"
16 echo >&2 " environment variable to the correct directory."
17 echo >&2 " - Make sure that your \`${0##*/}\` file is either in your"
18 echo >&2 " PATH or in your git exec path (\`$(git --exec-path)\`)."
19 echo >&2 " - You should run git-subtree as \`git ${0##*/git-}\`,"
20 echo >&2 " not as \`${0##*/}\`." >&2
25 git subtree add --prefix=<prefix> <commit>
26 git subtree add --prefix=<prefix> <repository> <ref>
27 git subtree merge --prefix=<prefix> <commit>
28 git subtree split --prefix=<prefix> [<commit>]
29 git subtree pull --prefix=<prefix> <repository> <ref>
30 git subtree push --prefix=<prefix> <repository> <ref>
35 P,prefix= the name of the subdir to split out
37 annotate= add a prefix to commit message of new commits
38 b,branch= create a new branch from the split subtree
39 ignore-joins ignore prior --rejoin commits
40 onto= try connecting new tree to an existing one
41 rejoin merge the new branch back into HEAD
42 options for 'add' and 'merge' (also: 'pull' and 'split --rejoin')
43 squash merge subtree changes as a single commit
44 m,message= use the given message as the commit message for the merge commit
53 arg_split_ignore_joins=
60 # Usage: debug [MSG...]
62 if test -n "$arg_debug"
64 printf "%$(($indent * 2))s%s\n" '' "$*" >&2
68 # Usage: progress [MSG...]
70 if test -z "$GIT_QUIET"
72 if test -z "$arg_debug"
76 # Print one progress line that we keep updating (use
77 # "\r" to return to the beginning of the line, rather
78 # than "\n" to start a new line). This only really
79 # works when stderr is a terminal.
80 printf "%s\r" "$*" >&2
82 # Debug mode is on. The `debug` function is regularly
85 # Don't do the one-line-with-"\r" thing, because on a
86 # terminal the debug output would overwrite and hide the
87 # progress output. Add a "progress:" prefix to make the
88 # progress output and the debug output easy to
89 # distinguish. This ensures maximum readability whether
90 # stderr is a terminal or a file.
91 printf "progress: %s\n" "$*" >&2
96 # Usage: assert CMD...
100 die "assertion failed: $*"
109 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
126 arg_split_annotate="$1"
133 arg_split_branch="$1"
141 arg_addmerge_message="$1"
161 arg_split_ignore_joins=1
164 arg_split_ignore_joins=
167 arg_addmerge_squash=1
176 die "Unexpected option: $opt"
184 case "$arg_command" in
185 add|merge|pull|split|push)
189 die "Unknown command '$arg_command'"
193 if test -z "$arg_prefix"
195 die "You must provide the --prefix option."
198 case "$arg_command" in
200 test -e "$arg_prefix" &&
201 die "prefix '$arg_prefix' already exists."
204 test -e "$arg_prefix" ||
205 die "'$arg_prefix' does not exist; use 'git subtree add'"
209 dir="$(dirname "$arg_prefix/.")"
211 debug "command: {$arg_command}"
212 debug "quiet: {$GIT_QUIET}"
217 "cmd_$arg_command" "$@"
223 cachedir="$GIT_DIR/subtree-cache/$$"
224 rm -rf "$cachedir" ||
225 die "Can't delete old cachedir: $cachedir"
226 mkdir -p "$cachedir" ||
227 die "Can't create new cachedir: $cachedir"
228 mkdir -p "$cachedir/notree" ||
229 die "Can't create new cachedir: $cachedir/notree"
230 debug "Using cachedir: $cachedir" >&2
233 # Usage: cache_get [REVS...]
237 if test -r "$cachedir/$oldrev"
239 read newrev <"$cachedir/$oldrev"
245 # Usage: cache_miss [REVS...]
249 if ! test -r "$cachedir/$oldrev"
256 # Usage: check_parents PARENTS_EXPR
259 missed=$(cache_miss "$1") || exit $?
260 local indent=$(($indent + 1))
263 if ! test -r "$cachedir/notree/$miss"
265 debug "incorrect order: $miss"
266 process_split_commit "$miss" ""
271 # Usage: set_notree REV
274 echo "1" > "$cachedir/notree/$1"
277 # Usage: cache_set OLDREV NEWREV
282 if test "$oldrev" != "latest_old" &&
283 test "$oldrev" != "latest_new" &&
284 test -e "$cachedir/$oldrev"
286 die "cache for $oldrev already exists!"
288 echo "$newrev" >"$cachedir/$oldrev"
291 # Usage: rev_exists REV
294 if git rev-parse "$1" >/dev/null 2>&1
302 # Usage: try_remove_previous REV
304 # If a commit doesn't have a parent, this might not work. But we only want
305 # to remove the parent from the rev-list, and since it doesn't exist, it won't
306 # be there anyway, so do nothing in that case.
307 try_remove_previous () {
315 # Usage: find_latest_squash DIR
316 find_latest_squash () {
318 debug "Looking for latest squash ($dir)..."
319 local indent=$(($indent + 1))
325 git log --grep="^git-subtree-dir: $dir/*\$" \
326 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
330 debug "{{$sq/$main/$sub}}"
335 git-subtree-mainline:)
339 sub="$(git rev-parse "$b^{commit}")" ||
340 die "could not rev-parse split hash $b from commit $sq"
348 # Pretend its sub was a squash.
349 sq=$(git rev-parse --verify "$sq^2") ||
352 debug "Squash found: $sq $sub"
364 # Usage: find_existing_splits DIR REV
365 find_existing_splits () {
367 debug "Looking for prior splits..."
368 local indent=$(($indent + 1))
374 local grep_format="^git-subtree-dir: $dir/*\$"
375 if test -n "$arg_split_ignore_joins"
377 grep_format="^Add '$dir/' from commit '"
379 git log --grep="$grep_format" \
380 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$rev" |
387 git-subtree-mainline:)
391 sub="$(git rev-parse "$b^{commit}")" ||
392 die "could not rev-parse split hash $b from commit $sq"
395 debug "Main is: '$main'"
396 if test -z "$main" -a -n "$sub"
398 # squash commits refer to a subtree
399 debug " Squash: $sq from $sub"
400 cache_set "$sq" "$sub"
402 if test -n "$main" -a -n "$sub"
404 debug " Prior: $main -> $sub"
407 try_remove_previous "$main"
408 try_remove_previous "$sub"
417 # Usage: copy_commit REV TREE FLAGS_STR
420 # We're going to set some environment vars here, so
421 # do it in a subshell to get rid of them safely later
422 debug copy_commit "{$1}" "{$2}" "{$3}"
423 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
426 read GIT_AUTHOR_EMAIL
428 read GIT_COMMITTER_NAME
429 read GIT_COMMITTER_EMAIL
430 read GIT_COMMITTER_DATE
431 export GIT_AUTHOR_NAME \
435 GIT_COMMITTER_EMAIL \
438 printf "%s" "$arg_split_annotate"
441 git commit-tree "$2" $3 # reads the rest of stdin
442 ) || die "Can't copy commit $1"
445 # Usage: add_msg DIR LATEST_OLD LATEST_NEW
451 if test -n "$arg_addmerge_message"
453 commit_message="$arg_addmerge_message"
455 commit_message="Add '$dir/' from commit '$latest_new'"
457 if test -n "$arg_split_rejoin"
459 # If this is from a --rejoin, then rejoin_msg has
460 # already inserted the `git-subtree-xxx:` tags
461 echo "$commit_message"
467 git-subtree-dir: $dir
468 git-subtree-mainline: $latest_old
469 git-subtree-split: $latest_new
473 # Usage: add_squashed_msg REV DIR
474 add_squashed_msg () {
476 if test -n "$arg_addmerge_message"
478 echo "$arg_addmerge_message"
480 echo "Merge commit '$1' as '$2'"
484 # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
490 if test -n "$arg_addmerge_message"
492 commit_message="$arg_addmerge_message"
494 commit_message="Split '$dir/' into commit '$latest_new'"
499 git-subtree-dir: $dir
500 git-subtree-mainline: $latest_old
501 git-subtree-split: $latest_new
505 # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
511 newsub_short=$(git rev-parse --short "$newsub")
515 oldsub_short=$(git rev-parse --short "$oldsub")
516 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
518 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
519 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
521 echo "Squashed '$dir/' content from commit $newsub_short"
525 echo "git-subtree-dir: $dir"
526 echo "git-subtree-split: $newsub"
529 # Usage: toptree_for_commit COMMIT
530 toptree_for_commit () {
533 git rev-parse --verify "$commit^{tree}" || exit $?
536 # Usage: subtree_for_commit COMMIT DIR
537 subtree_for_commit () {
541 git ls-tree "$commit" -- "$dir" |
542 while read mode type tree name
544 assert test "$name" = "$dir"
545 assert test "$type" = "tree" -o "$type" = "commit"
546 test "$type" = "commit" && continue # ignore submodules
552 # Usage: tree_changed TREE [PARENTS...]
559 return 0 # weird parents, consider it changed
561 ptree=$(toptree_for_commit $1) || exit $?
562 if test "$ptree" != "$tree"
566 return 1 # not changed
571 # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
572 new_squash_commit () {
577 tree=$(toptree_for_commit $newsub) || exit $?
580 squash_msg "$dir" "$oldsub" "$newsub" |
581 git commit-tree "$tree" -p "$old" || exit $?
583 squash_msg "$dir" "" "$newsub" |
584 git commit-tree "$tree" || exit $?
588 # Usage: copy_or_skip REV TREE NEWPARENTS
594 assert test -n "$tree"
601 for parent in $newparents
603 ptree=$(toptree_for_commit $parent) || exit $?
604 test -z "$ptree" && continue
605 if test "$ptree" = "$tree"
607 # an identical parent could be used in place of this rev.
608 if test -n "$identical"
610 # if a previous identical parent was found, check whether
611 # one is already an ancestor of the other
612 mergebase=$(git merge-base $identical $parent)
613 if test "$identical" = "$mergebase"
615 # current identical commit is an ancestor of parent
617 elif test "$parent" != "$mergebase"
619 # no common history; commit must be copied
623 # first identical parent detected
627 nonidentical="$parent"
630 # sometimes both old parents map to the same newparent;
631 # eliminate duplicates
633 for gp in $gotparents
635 if test "$gp" = "$parent"
643 gotparents="$gotparents $parent"
648 if test -n "$identical" && test -n "$nonidentical"
650 extras=$(git rev-list --count $identical..$nonidentical)
651 if test "$extras" -ne 0
653 # we need to preserve history along the other branch
657 if test -n "$identical" && test -z "$copycommit"
661 copy_commit "$rev" "$tree" "$p" || exit $?
665 # Usage: ensure_clean
668 if ! git diff-index HEAD --exit-code --quiet 2>&1
670 die "Working tree has modifications. Cannot add."
672 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
674 die "Index has modifications. Cannot add."
678 # Usage: ensure_valid_ref_format REF
679 ensure_valid_ref_format () {
681 git check-ref-format "refs/heads/$1" ||
682 die "'$1' does not look like a ref"
685 # Usage: process_split_commit REV PARENTS
686 process_split_commit () {
691 if test $indent -eq 0
693 revcount=$(($revcount + 1))
695 # processing commit without normal parent information;
697 parents=$(git rev-parse "$rev^@")
698 extracount=$(($extracount + 1))
701 progress "$revcount/$revmax ($createcount) [$extracount]"
703 debug "Processing commit: $rev"
704 local indent=$(($indent + 1))
705 exists=$(cache_get "$rev") || exit $?
708 debug "prior: $exists"
711 createcount=$(($createcount + 1))
712 debug "parents: $parents"
713 check_parents "$parents"
714 newparents=$(cache_get $parents) || exit $?
715 debug "newparents: $newparents"
717 tree=$(subtree_for_commit "$rev" "$dir") || exit $?
718 debug "tree is: $tree"
720 # ugly. is there no better way to tell if this is a subtree
721 # vs. a mainline commit? Does it matter?
725 if test -n "$newparents"
727 cache_set "$rev" "$rev"
732 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
733 debug "newrev is: $newrev"
734 cache_set "$rev" "$newrev"
735 cache_set latest_new "$newrev"
736 cache_set latest_old "$rev"
740 # Or: cmd_add REPOSITORY REF
747 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
748 die "'$1' does not refer to a commit"
754 # Technically we could accept a refspec here but we're
755 # just going to turn around and add FETCH_HEAD under the
756 # specified directory. Allowing a refspec might be
757 # misleading because we won't do anything with any other
758 # branches fetched via the refspec.
759 ensure_valid_ref_format "$2"
761 cmd_add_repository "$@"
763 say >&2 "error: parameters were '$*'"
764 die "Provide either a commit or a repository and commit."
768 # Usage: cmd_add_repository REPOSITORY REFSPEC
769 cmd_add_repository () {
771 echo "git fetch" "$@"
774 git fetch "$@" || exit $?
775 cmd_add_commit FETCH_HEAD
778 # Usage: cmd_add_commit REV
780 # The rev has already been validated by cmd_add(), we just
781 # need to normalize it.
783 rev=$(git rev-parse --verify "$1^{commit}") || exit $?
785 debug "Adding $dir as '$rev'..."
786 if test -z "$arg_split_rejoin"
788 # Only bother doing this if this is a genuine 'add',
789 # not a synthetic 'add' from '--rejoin'.
790 git read-tree --prefix="$dir" $rev || exit $?
792 git checkout -- "$dir" || exit $?
793 tree=$(git write-tree) || exit $?
795 headrev=$(git rev-parse HEAD) || exit $?
796 if test -n "$headrev" && test "$headrev" != "$rev"
803 if test -n "$arg_addmerge_squash"
805 rev=$(new_squash_commit "" "" "$rev") || exit $?
806 commit=$(add_squashed_msg "$rev" "$dir" |
807 git commit-tree "$tree" $headp -p "$rev") || exit $?
809 revp=$(peel_committish "$rev") || exit $?
810 commit=$(add_msg "$dir" $headrev "$rev" |
811 git commit-tree "$tree" $headp -p "$revp") || exit $?
813 git reset "$commit" || exit $?
815 say >&2 "Added dir '$dir'"
818 # Usage: cmd_split [REV]
822 rev=$(git rev-parse HEAD)
825 rev=$(git rev-parse -q --verify "$1^{commit}") ||
826 die "'$1' does not refer to a commit"
828 die "You must provide exactly one revision. Got: '$*'"
831 if test -n "$arg_split_rejoin"
836 debug "Splitting $dir..."
837 cache_setup || exit $?
839 if test -n "$arg_split_onto"
841 debug "Reading history for --onto=$arg_split_onto..."
842 git rev-list $arg_split_onto |
845 # the 'onto' history is already just the subdir, so
846 # any parent we find there can be used verbatim
848 cache_set "$rev" "$rev"
852 unrevs="$(find_existing_splits "$dir" "$rev")" || exit $?
854 # We can't restrict rev-list to only $dir here, because some of our
855 # parents have the $dir contents the root, and those won't match.
856 # (and rev-list --follow doesn't seem to solve this)
857 grl='git rev-list --topo-order --reverse --parents $rev $unrevs'
858 revmax=$(eval "$grl" | wc -l)
863 while read rev parents
865 process_split_commit "$rev" "$parents"
868 latest_new=$(cache_get latest_new) || exit $?
869 if test -z "$latest_new"
871 die "No new revisions were found"
874 if test -n "$arg_split_rejoin"
876 debug "Merging split branch into HEAD..."
877 latest_old=$(cache_get latest_old) || exit $?
878 arg_addmerge_message="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" || exit $?
879 if test -z "$(find_latest_squash "$dir")"
881 cmd_add "$latest_new" >&2 || exit $?
883 cmd_merge "$latest_new" >&2 || exit $?
886 if test -n "$arg_split_branch"
888 if rev_exists "refs/heads/$arg_split_branch"
890 if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new"
892 die "Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
898 git update-ref -m 'subtree split' \
899 "refs/heads/$arg_split_branch" "$latest_new" || exit $?
900 say >&2 "$action branch '$arg_split_branch'"
906 # Usage: cmd_merge REV
909 die "You must provide exactly one revision. Got: '$*'"
910 rev=$(git rev-parse -q --verify "$1^{commit}") ||
911 die "'$1' does not refer to a commit"
914 if test -n "$arg_addmerge_squash"
916 first_split="$(find_latest_squash "$dir")" || exit $?
917 if test -z "$first_split"
919 die "Can't squash-merge: '$dir' was never added."
924 if test "$sub" = "$rev"
926 say >&2 "Subtree is already at commit $rev."
929 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
930 debug "New squash commit: $new"
934 if test -n "$arg_addmerge_message"
936 git merge -Xsubtree="$arg_prefix" \
937 --message="$arg_addmerge_message" "$rev"
939 git merge -Xsubtree="$arg_prefix" $rev
943 # Usage: cmd_pull REPOSITORY REMOTEREF
947 die "You must provide <repository> <ref>"
950 ensure_valid_ref_format "$2"
951 git fetch "$@" || exit $?
955 # Usage: cmd_push REPOSITORY REMOTEREF
959 die "You must provide <repository> <ref>"
961 ensure_valid_ref_format "$2"
966 echo "git push using: " "$repository" "$refspec"
967 localrev=$(git subtree split --prefix="$arg_prefix") || die
968 git push "$repository" "$localrev":"refs/heads/$refspec"
970 die "'$dir' must already exist. Try 'git subtree add'."