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 pull --prefix=<prefix> <repository> <ref>
29 git subtree push --prefix=<prefix> <repository> <ref>
30 git subtree split --prefix=<prefix> <commit>
35 P,prefix= the name of the subdir to split out
36 m,message= use the given message as the commit message for the merge commit
38 annotate= add a prefix to commit message of new commits
39 b,branch= create a new branch from the split subtree
40 ignore-joins ignore prior --rejoin commits
41 onto= try connecting new tree to an existing one
42 rejoin merge the new branch back into HEAD
43 options for 'add', 'merge', and 'pull'
44 squash merge subtree changes as a single commit
53 arg_split_ignore_joins=
59 if test -n "$arg_debug"
61 printf "%s\n" "$*" >&2
66 if test -z "$GIT_QUIET"
68 printf "%s\r" "$*" >&2
75 die "assertion failed: $*"
79 ensure_single_rev () {
82 die "You must provide exactly one revision. Got: '$*'"
91 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
108 arg_split_annotate="$1"
115 arg_split_branch="$1"
123 arg_addmerge_message="$1"
143 arg_split_ignore_joins=1
146 arg_split_ignore_joins=
149 arg_addmerge_squash=1
158 die "Unexpected option: $opt"
166 case "$arg_command" in
171 default="--default HEAD"
174 die "Unknown command '$arg_command'"
178 if test -z "$arg_prefix"
180 die "You must provide the --prefix option."
183 case "$arg_command" in
185 test -e "$arg_prefix" &&
186 die "prefix '$arg_prefix' already exists."
189 test -e "$arg_prefix" ||
190 die "'$arg_prefix' does not exist; use 'git subtree add'"
194 dir="$(dirname "$arg_prefix/.")"
196 if test "$arg_command" != "pull" &&
197 test "$arg_command" != "add" &&
198 test "$arg_command" != "push"
200 revs=$(git rev-parse $default --revs-only "$@") || exit $?
201 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
202 ensure_single_rev $revs
205 die "Error: Use --prefix instead of bare filenames."
209 debug "command: {$arg_command}"
210 debug "quiet: {$GIT_QUIET}"
211 debug "revs: {$revs}"
216 "cmd_$arg_command" "$@"
220 cachedir="$GIT_DIR/subtree-cache/$$"
221 rm -rf "$cachedir" ||
222 die "Can't delete old cachedir: $cachedir"
223 mkdir -p "$cachedir" ||
224 die "Can't create new cachedir: $cachedir"
225 mkdir -p "$cachedir/notree" ||
226 die "Can't create new cachedir: $cachedir/notree"
227 debug "Using cachedir: $cachedir" >&2
233 if test -r "$cachedir/$oldrev"
235 read newrev <"$cachedir/$oldrev"
244 if ! test -r "$cachedir/$oldrev"
252 missed=$(cache_miss "$1") || exit $?
253 local indent=$(($2 + 1))
256 if ! test -r "$cachedir/notree/$miss"
258 debug " incorrect order: $miss"
259 process_split_commit "$miss" "" "$indent"
265 echo "1" > "$cachedir/notree/$1"
271 if test "$oldrev" != "latest_old" &&
272 test "$oldrev" != "latest_new" &&
273 test -e "$cachedir/$oldrev"
275 die "cache for $oldrev already exists!"
277 echo "$newrev" >"$cachedir/$oldrev"
281 if git rev-parse "$1" >/dev/null 2>&1
289 # if a commit doesn't have a parent, this might not work. But we only want
290 # to remove the parent from the rev-list, and since it doesn't exist, it won't
291 # be there anyway, so do nothing in that case.
292 try_remove_previous () {
299 find_latest_squash () {
300 debug "Looking for latest squash ($dir)..."
305 git log --grep="^git-subtree-dir: $dir/*\$" \
306 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
310 debug "{{$sq/$main/$sub}}"
315 git-subtree-mainline:)
319 sub="$(git rev-parse "$b^{commit}")" ||
320 die "could not rev-parse split hash $b from commit $sq"
328 # Pretend its sub was a squash.
331 debug "Squash found: $sq $sub"
343 find_existing_splits () {
344 debug "Looking for prior splits..."
349 local grep_format="^git-subtree-dir: $dir/*\$"
350 if test -n "$arg_split_ignore_joins"
352 grep_format="^Add '$dir/' from commit '"
354 git log --grep="$grep_format" \
355 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
362 git-subtree-mainline:)
366 sub="$(git rev-parse "$b^{commit}")" ||
367 die "could not rev-parse split hash $b from commit $sq"
370 debug " Main is: '$main'"
371 if test -z "$main" -a -n "$sub"
373 # squash commits refer to a subtree
374 debug " Squash: $sq from $sub"
375 cache_set "$sq" "$sub"
377 if test -n "$main" -a -n "$sub"
379 debug " Prior: $main -> $sub"
382 try_remove_previous "$main"
383 try_remove_previous "$sub"
393 # We're going to set some environment vars here, so
394 # do it in a subshell to get rid of them safely later
395 debug copy_commit "{$1}" "{$2}" "{$3}"
396 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
399 read GIT_AUTHOR_EMAIL
401 read GIT_COMMITTER_NAME
402 read GIT_COMMITTER_EMAIL
403 read GIT_COMMITTER_DATE
404 export GIT_AUTHOR_NAME \
408 GIT_COMMITTER_EMAIL \
411 printf "%s" "$arg_split_annotate"
414 git commit-tree "$2" $3 # reads the rest of stdin
415 ) || die "Can't copy commit $1"
422 if test -n "$arg_addmerge_message"
424 commit_message="$arg_addmerge_message"
426 commit_message="Add '$dir/' from commit '$latest_new'"
431 git-subtree-dir: $dir
432 git-subtree-mainline: $latest_old
433 git-subtree-split: $latest_new
437 add_squashed_msg () {
438 if test -n "$arg_addmerge_message"
440 echo "$arg_addmerge_message"
442 echo "Merge commit '$1' as '$2'"
450 if test -n "$arg_addmerge_message"
452 commit_message="$arg_addmerge_message"
454 commit_message="Split '$dir/' into commit '$latest_new'"
459 git-subtree-dir: $dir
460 git-subtree-mainline: $latest_old
461 git-subtree-split: $latest_new
469 newsub_short=$(git rev-parse --short "$newsub")
473 oldsub_short=$(git rev-parse --short "$oldsub")
474 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
476 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
477 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
479 echo "Squashed '$dir/' content from commit $newsub_short"
483 echo "git-subtree-dir: $dir"
484 echo "git-subtree-split: $newsub"
487 toptree_for_commit () {
489 git rev-parse --verify "$commit^{tree}" || exit $?
492 subtree_for_commit () {
495 git ls-tree "$commit" -- "$dir" |
496 while read mode type tree name
498 assert test "$name" = "$dir"
499 assert test "$type" = "tree" -o "$type" = "commit"
500 test "$type" = "commit" && continue # ignore submodules
511 return 0 # weird parents, consider it changed
513 ptree=$(toptree_for_commit $1) || exit $?
514 if test "$ptree" != "$tree"
518 return 1 # not changed
523 new_squash_commit () {
527 tree=$(toptree_for_commit $newsub) || exit $?
530 squash_msg "$dir" "$oldsub" "$newsub" |
531 git commit-tree "$tree" -p "$old" || exit $?
533 squash_msg "$dir" "" "$newsub" |
534 git commit-tree "$tree" || exit $?
542 assert test -n "$tree"
549 for parent in $newparents
551 ptree=$(toptree_for_commit $parent) || exit $?
552 test -z "$ptree" && continue
553 if test "$ptree" = "$tree"
555 # an identical parent could be used in place of this rev.
556 if test -n "$identical"
558 # if a previous identical parent was found, check whether
559 # one is already an ancestor of the other
560 mergebase=$(git merge-base $identical $parent)
561 if test "$identical" = "$mergebase"
563 # current identical commit is an ancestor of parent
565 elif test "$parent" != "$mergebase"
567 # no common history; commit must be copied
571 # first identical parent detected
575 nonidentical="$parent"
578 # sometimes both old parents map to the same newparent;
579 # eliminate duplicates
581 for gp in $gotparents
583 if test "$gp" = "$parent"
591 gotparents="$gotparents $parent"
596 if test -n "$identical" && test -n "$nonidentical"
598 extras=$(git rev-list --count $identical..$nonidentical)
599 if test "$extras" -ne 0
601 # we need to preserve history along the other branch
605 if test -n "$identical" && test -z "$copycommit"
609 copy_commit "$rev" "$tree" "$p" || exit $?
614 if ! git diff-index HEAD --exit-code --quiet 2>&1
616 die "Working tree has modifications. Cannot add."
618 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
620 die "Index has modifications. Cannot add."
624 ensure_valid_ref_format () {
625 git check-ref-format "refs/heads/$1" ||
626 die "'$1' does not look like a ref"
629 process_split_commit () {
634 if test $indent -eq 0
636 revcount=$(($revcount + 1))
638 # processing commit without normal parent information;
640 parents=$(git rev-parse "$rev^@")
641 extracount=$(($extracount + 1))
644 progress "$revcount/$revmax ($createcount) [$extracount]"
646 debug "Processing commit: $rev"
647 exists=$(cache_get "$rev") || exit $?
650 debug " prior: $exists"
653 createcount=$(($createcount + 1))
654 debug " parents: $parents"
655 check_parents "$parents" "$indent"
656 newparents=$(cache_get $parents) || exit $?
657 debug " newparents: $newparents"
659 tree=$(subtree_for_commit "$rev" "$dir") || exit $?
660 debug " tree is: $tree"
662 # ugly. is there no better way to tell if this is a subtree
663 # vs. a mainline commit? Does it matter?
667 if test -n "$newparents"
669 cache_set "$rev" "$rev"
674 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
675 debug " newrev is: $newrev"
676 cache_set "$rev" "$newrev"
677 cache_set latest_new "$newrev"
678 cache_set latest_old "$rev"
684 die "'$dir' already exists. Cannot add."
691 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
692 die "'$1' does not refer to a commit"
698 # Technically we could accept a refspec here but we're
699 # just going to turn around and add FETCH_HEAD under the
700 # specified directory. Allowing a refspec might be
701 # misleading because we won't do anything with any other
702 # branches fetched via the refspec.
703 ensure_valid_ref_format "$2"
705 cmd_add_repository "$@"
707 say >&2 "error: parameters were '$*'"
708 die "Provide either a commit or a repository and commit."
712 cmd_add_repository () {
713 echo "git fetch" "$@"
716 git fetch "$@" || exit $?
723 rev=$(git rev-parse $default --revs-only "$@") || exit $?
724 ensure_single_rev $rev
726 debug "Adding $dir as '$rev'..."
727 git read-tree --prefix="$dir" $rev || exit $?
728 git checkout -- "$dir" || exit $?
729 tree=$(git write-tree) || exit $?
731 headrev=$(git rev-parse HEAD) || exit $?
732 if test -n "$headrev" && test "$headrev" != "$rev"
739 if test -n "$arg_addmerge_squash"
741 rev=$(new_squash_commit "" "" "$rev") || exit $?
742 commit=$(add_squashed_msg "$rev" "$dir" |
743 git commit-tree "$tree" $headp -p "$rev") || exit $?
745 revp=$(peel_committish "$rev") || exit $?
746 commit=$(add_msg "$dir" $headrev "$rev" |
747 git commit-tree "$tree" $headp -p "$revp") || exit $?
749 git reset "$commit" || exit $?
751 say >&2 "Added dir '$dir'"
755 debug "Splitting $dir..."
756 cache_setup || exit $?
758 if test -n "$arg_split_onto"
760 debug "Reading history for --onto=$arg_split_onto..."
761 git rev-list $arg_split_onto |
764 # the 'onto' history is already just the subdir, so
765 # any parent we find there can be used verbatim
767 cache_set "$rev" "$rev"
771 unrevs="$(find_existing_splits "$dir" "$revs")" || exit $?
773 # We can't restrict rev-list to only $dir here, because some of our
774 # parents have the $dir contents the root, and those won't match.
775 # (and rev-list --follow doesn't seem to solve this)
776 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
777 revmax=$(eval "$grl" | wc -l)
782 while read rev parents
784 process_split_commit "$rev" "$parents" 0
787 latest_new=$(cache_get latest_new) || exit $?
788 if test -z "$latest_new"
790 die "No new revisions were found"
793 if test -n "$arg_split_rejoin"
795 debug "Merging split branch into HEAD..."
796 latest_old=$(cache_get latest_old) || exit $?
798 --allow-unrelated-histories \
799 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
800 "$latest_new" >&2 || exit $?
802 if test -n "$arg_split_branch"
804 if rev_exists "refs/heads/$arg_split_branch"
806 if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new"
808 die "Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
814 git update-ref -m 'subtree split' \
815 "refs/heads/$arg_split_branch" "$latest_new" || exit $?
816 say >&2 "$action branch '$arg_split_branch'"
823 rev=$(git rev-parse $default --revs-only "$@") || exit $?
824 ensure_single_rev $rev
827 if test -n "$arg_addmerge_squash"
829 first_split="$(find_latest_squash "$dir")" || exit $?
830 if test -z "$first_split"
832 die "Can't squash-merge: '$dir' was never added."
837 if test "$sub" = "$rev"
839 say >&2 "Subtree is already at commit $rev."
842 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
843 debug "New squash commit: $new"
847 if test -n "$arg_addmerge_message"
849 git merge -Xsubtree="$arg_prefix" \
850 --message="$arg_addmerge_message" "$rev"
852 git merge -Xsubtree="$arg_prefix" $rev
859 die "You must provide <repository> <ref>"
862 ensure_valid_ref_format "$2"
863 git fetch "$@" || exit $?
872 die "You must provide <repository> <ref>"
874 ensure_valid_ref_format "$2"
879 echo "git push using: " "$repository" "$refspec"
880 localrev=$(git subtree split --prefix="$arg_prefix") || die
881 git push "$repository" "$localrev":"refs/heads/$refspec"
883 die "'$dir' must already exist. Try 'git subtree add'."