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: $*"
84 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
101 arg_split_annotate="$1"
108 arg_split_branch="$1"
116 arg_addmerge_message="$1"
136 arg_split_ignore_joins=1
139 arg_split_ignore_joins=
142 arg_addmerge_squash=1
151 die "Unexpected option: $opt"
159 case "$arg_command" in
160 add|merge|pull|split|push)
164 die "Unknown command '$arg_command'"
168 if test -z "$arg_prefix"
170 die "You must provide the --prefix option."
173 case "$arg_command" in
175 test -e "$arg_prefix" &&
176 die "prefix '$arg_prefix' already exists."
179 test -e "$arg_prefix" ||
180 die "'$arg_prefix' does not exist; use 'git subtree add'"
184 dir="$(dirname "$arg_prefix/.")"
186 debug "command: {$arg_command}"
187 debug "quiet: {$GIT_QUIET}"
192 "cmd_$arg_command" "$@"
196 cachedir="$GIT_DIR/subtree-cache/$$"
197 rm -rf "$cachedir" ||
198 die "Can't delete old cachedir: $cachedir"
199 mkdir -p "$cachedir" ||
200 die "Can't create new cachedir: $cachedir"
201 mkdir -p "$cachedir/notree" ||
202 die "Can't create new cachedir: $cachedir/notree"
203 debug "Using cachedir: $cachedir" >&2
209 if test -r "$cachedir/$oldrev"
211 read newrev <"$cachedir/$oldrev"
220 if ! test -r "$cachedir/$oldrev"
228 missed=$(cache_miss "$1") || exit $?
229 local indent=$(($2 + 1))
232 if ! test -r "$cachedir/notree/$miss"
234 debug " incorrect order: $miss"
235 process_split_commit "$miss" "" "$indent"
241 echo "1" > "$cachedir/notree/$1"
247 if test "$oldrev" != "latest_old" &&
248 test "$oldrev" != "latest_new" &&
249 test -e "$cachedir/$oldrev"
251 die "cache for $oldrev already exists!"
253 echo "$newrev" >"$cachedir/$oldrev"
257 if git rev-parse "$1" >/dev/null 2>&1
265 # if a commit doesn't have a parent, this might not work. But we only want
266 # to remove the parent from the rev-list, and since it doesn't exist, it won't
267 # be there anyway, so do nothing in that case.
268 try_remove_previous () {
275 find_latest_squash () {
276 debug "Looking for latest squash ($dir)..."
281 git log --grep="^git-subtree-dir: $dir/*\$" \
282 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
286 debug "{{$sq/$main/$sub}}"
291 git-subtree-mainline:)
295 sub="$(git rev-parse "$b^{commit}")" ||
296 die "could not rev-parse split hash $b from commit $sq"
304 # Pretend its sub was a squash.
307 debug "Squash found: $sq $sub"
319 find_existing_splits () {
320 debug "Looking for prior splits..."
325 local grep_format="^git-subtree-dir: $dir/*\$"
326 if test -n "$arg_split_ignore_joins"
328 grep_format="^Add '$dir/' from commit '"
330 git log --grep="$grep_format" \
331 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
338 git-subtree-mainline:)
342 sub="$(git rev-parse "$b^{commit}")" ||
343 die "could not rev-parse split hash $b from commit $sq"
346 debug " Main is: '$main'"
347 if test -z "$main" -a -n "$sub"
349 # squash commits refer to a subtree
350 debug " Squash: $sq from $sub"
351 cache_set "$sq" "$sub"
353 if test -n "$main" -a -n "$sub"
355 debug " Prior: $main -> $sub"
358 try_remove_previous "$main"
359 try_remove_previous "$sub"
369 # We're going to set some environment vars here, so
370 # do it in a subshell to get rid of them safely later
371 debug copy_commit "{$1}" "{$2}" "{$3}"
372 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
375 read GIT_AUTHOR_EMAIL
377 read GIT_COMMITTER_NAME
378 read GIT_COMMITTER_EMAIL
379 read GIT_COMMITTER_DATE
380 export GIT_AUTHOR_NAME \
384 GIT_COMMITTER_EMAIL \
387 printf "%s" "$arg_split_annotate"
390 git commit-tree "$2" $3 # reads the rest of stdin
391 ) || die "Can't copy commit $1"
398 if test -n "$arg_addmerge_message"
400 commit_message="$arg_addmerge_message"
402 commit_message="Add '$dir/' from commit '$latest_new'"
407 git-subtree-dir: $dir
408 git-subtree-mainline: $latest_old
409 git-subtree-split: $latest_new
413 add_squashed_msg () {
414 if test -n "$arg_addmerge_message"
416 echo "$arg_addmerge_message"
418 echo "Merge commit '$1' as '$2'"
426 if test -n "$arg_addmerge_message"
428 commit_message="$arg_addmerge_message"
430 commit_message="Split '$dir/' into commit '$latest_new'"
435 git-subtree-dir: $dir
436 git-subtree-mainline: $latest_old
437 git-subtree-split: $latest_new
445 newsub_short=$(git rev-parse --short "$newsub")
449 oldsub_short=$(git rev-parse --short "$oldsub")
450 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
452 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
453 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
455 echo "Squashed '$dir/' content from commit $newsub_short"
459 echo "git-subtree-dir: $dir"
460 echo "git-subtree-split: $newsub"
463 toptree_for_commit () {
465 git rev-parse --verify "$commit^{tree}" || exit $?
468 subtree_for_commit () {
471 git ls-tree "$commit" -- "$dir" |
472 while read mode type tree name
474 assert test "$name" = "$dir"
475 assert test "$type" = "tree" -o "$type" = "commit"
476 test "$type" = "commit" && continue # ignore submodules
487 return 0 # weird parents, consider it changed
489 ptree=$(toptree_for_commit $1) || exit $?
490 if test "$ptree" != "$tree"
494 return 1 # not changed
499 new_squash_commit () {
503 tree=$(toptree_for_commit $newsub) || exit $?
506 squash_msg "$dir" "$oldsub" "$newsub" |
507 git commit-tree "$tree" -p "$old" || exit $?
509 squash_msg "$dir" "" "$newsub" |
510 git commit-tree "$tree" || exit $?
518 assert test -n "$tree"
525 for parent in $newparents
527 ptree=$(toptree_for_commit $parent) || exit $?
528 test -z "$ptree" && continue
529 if test "$ptree" = "$tree"
531 # an identical parent could be used in place of this rev.
532 if test -n "$identical"
534 # if a previous identical parent was found, check whether
535 # one is already an ancestor of the other
536 mergebase=$(git merge-base $identical $parent)
537 if test "$identical" = "$mergebase"
539 # current identical commit is an ancestor of parent
541 elif test "$parent" != "$mergebase"
543 # no common history; commit must be copied
547 # first identical parent detected
551 nonidentical="$parent"
554 # sometimes both old parents map to the same newparent;
555 # eliminate duplicates
557 for gp in $gotparents
559 if test "$gp" = "$parent"
567 gotparents="$gotparents $parent"
572 if test -n "$identical" && test -n "$nonidentical"
574 extras=$(git rev-list --count $identical..$nonidentical)
575 if test "$extras" -ne 0
577 # we need to preserve history along the other branch
581 if test -n "$identical" && test -z "$copycommit"
585 copy_commit "$rev" "$tree" "$p" || exit $?
590 if ! git diff-index HEAD --exit-code --quiet 2>&1
592 die "Working tree has modifications. Cannot add."
594 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
596 die "Index has modifications. Cannot add."
600 ensure_valid_ref_format () {
601 git check-ref-format "refs/heads/$1" ||
602 die "'$1' does not look like a ref"
605 process_split_commit () {
610 if test $indent -eq 0
612 revcount=$(($revcount + 1))
614 # processing commit without normal parent information;
616 parents=$(git rev-parse "$rev^@")
617 extracount=$(($extracount + 1))
620 progress "$revcount/$revmax ($createcount) [$extracount]"
622 debug "Processing commit: $rev"
623 exists=$(cache_get "$rev") || exit $?
626 debug " prior: $exists"
629 createcount=$(($createcount + 1))
630 debug " parents: $parents"
631 check_parents "$parents" "$indent"
632 newparents=$(cache_get $parents) || exit $?
633 debug " newparents: $newparents"
635 tree=$(subtree_for_commit "$rev" "$dir") || exit $?
636 debug " tree is: $tree"
638 # ugly. is there no better way to tell if this is a subtree
639 # vs. a mainline commit? Does it matter?
643 if test -n "$newparents"
645 cache_set "$rev" "$rev"
650 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
651 debug " newrev is: $newrev"
652 cache_set "$rev" "$newrev"
653 cache_set latest_new "$newrev"
654 cache_set latest_old "$rev"
663 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
664 die "'$1' does not refer to a commit"
670 # Technically we could accept a refspec here but we're
671 # just going to turn around and add FETCH_HEAD under the
672 # specified directory. Allowing a refspec might be
673 # misleading because we won't do anything with any other
674 # branches fetched via the refspec.
675 ensure_valid_ref_format "$2"
677 cmd_add_repository "$@"
679 say >&2 "error: parameters were '$*'"
680 die "Provide either a commit or a repository and commit."
684 cmd_add_repository () {
685 echo "git fetch" "$@"
688 git fetch "$@" || exit $?
689 cmd_add_commit FETCH_HEAD
693 # The rev has already been validated by cmd_add(), we just
694 # need to normalize it.
695 rev=$(git rev-parse --verify "$1^{commit}") || exit $?
697 debug "Adding $dir as '$rev'..."
698 git read-tree --prefix="$dir" $rev || exit $?
699 git checkout -- "$dir" || exit $?
700 tree=$(git write-tree) || exit $?
702 headrev=$(git rev-parse HEAD) || exit $?
703 if test -n "$headrev" && test "$headrev" != "$rev"
710 if test -n "$arg_addmerge_squash"
712 rev=$(new_squash_commit "" "" "$rev") || exit $?
713 commit=$(add_squashed_msg "$rev" "$dir" |
714 git commit-tree "$tree" $headp -p "$rev") || exit $?
716 revp=$(peel_committish "$rev") || exit $?
717 commit=$(add_msg "$dir" $headrev "$rev" |
718 git commit-tree "$tree" $headp -p "$revp") || exit $?
720 git reset "$commit" || exit $?
722 say >&2 "Added dir '$dir'"
728 rev=$(git rev-parse HEAD)
731 rev=$(git rev-parse -q --verify "$1^{commit}") ||
732 die "'$1' does not refer to a commit"
734 die "You must provide exactly one revision. Got: '$*'"
737 debug "Splitting $dir..."
738 cache_setup || exit $?
740 if test -n "$arg_split_onto"
742 debug "Reading history for --onto=$arg_split_onto..."
743 git rev-list $arg_split_onto |
746 # the 'onto' history is already just the subdir, so
747 # any parent we find there can be used verbatim
749 cache_set "$rev" "$rev"
753 unrevs="$(find_existing_splits "$dir" "$rev")" || exit $?
755 # We can't restrict rev-list to only $dir here, because some of our
756 # parents have the $dir contents the root, and those won't match.
757 # (and rev-list --follow doesn't seem to solve this)
758 grl='git rev-list --topo-order --reverse --parents $rev $unrevs'
759 revmax=$(eval "$grl" | wc -l)
764 while read rev parents
766 process_split_commit "$rev" "$parents" 0
769 latest_new=$(cache_get latest_new) || exit $?
770 if test -z "$latest_new"
772 die "No new revisions were found"
775 if test -n "$arg_split_rejoin"
777 debug "Merging split branch into HEAD..."
778 latest_old=$(cache_get latest_old) || exit $?
780 --allow-unrelated-histories \
781 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
782 "$latest_new" >&2 || exit $?
784 if test -n "$arg_split_branch"
786 if rev_exists "refs/heads/$arg_split_branch"
788 if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new"
790 die "Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
796 git update-ref -m 'subtree split' \
797 "refs/heads/$arg_split_branch" "$latest_new" || exit $?
798 say >&2 "$action branch '$arg_split_branch'"
806 die "You must provide exactly one revision. Got: '$*'"
807 rev=$(git rev-parse -q --verify "$1^{commit}") ||
808 die "'$1' does not refer to a commit"
811 if test -n "$arg_addmerge_squash"
813 first_split="$(find_latest_squash "$dir")" || exit $?
814 if test -z "$first_split"
816 die "Can't squash-merge: '$dir' was never added."
821 if test "$sub" = "$rev"
823 say >&2 "Subtree is already at commit $rev."
826 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
827 debug "New squash commit: $new"
831 if test -n "$arg_addmerge_message"
833 git merge -Xsubtree="$arg_prefix" \
834 --message="$arg_addmerge_message" "$rev"
836 git merge -Xsubtree="$arg_prefix" $rev
843 die "You must provide <repository> <ref>"
846 ensure_valid_ref_format "$2"
847 git fetch "$@" || exit $?
854 die "You must provide <repository> <ref>"
856 ensure_valid_ref_format "$2"
861 echo "git push using: " "$repository" "$refspec"
862 localrev=$(git subtree split --prefix="$arg_prefix") || die
863 git push "$repository" "$localrev":"refs/heads/$refspec"
865 die "'$dir' must already exist. Try 'git subtree add'."