3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
9 git subtree add --prefix=<prefix> <commit>
10 git subtree add --prefix=<prefix> <repository> <ref>
11 git subtree merge --prefix=<prefix> <commit>
12 git subtree pull --prefix=<prefix> <repository> <ref>
13 git subtree push --prefix=<prefix> <repository> <ref>
14 git subtree split --prefix=<prefix> <commit>
19 P,prefix= the name of the subdir to split out
20 m,message= use the given message as the commit message for the merge commit
22 annotate= add a prefix to commit message of new commits
23 b,branch= create a new branch from the split subtree
24 ignore-joins ignore prior --rejoin commits
25 onto= try connecting new tree to an existing one
26 rejoin merge the new branch back into HEAD
27 options for 'add', 'merge', and 'pull'
28 squash merge subtree changes as a single commit
31 PATH=$PATH:$(git --exec-path)
39 arg_split_ignore_joins=
45 if test -n "$arg_debug"
47 printf "%s\n" "$*" >&2
52 if test -z "$GIT_QUIET"
54 printf "%s\r" "$*" >&2
61 die "assertion failed: $*"
65 ensure_single_rev () {
68 die "You must provide exactly one revision. Got: '$*'"
77 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
94 arg_split_annotate="$1"
101 arg_split_branch="$1"
109 arg_addmerge_message="$1"
129 arg_split_ignore_joins=1
132 arg_split_ignore_joins=
135 arg_addmerge_squash=1
144 die "Unexpected option: $opt"
152 case "$arg_command" in
157 default="--default HEAD"
160 die "Unknown command '$arg_command'"
164 if test -z "$arg_prefix"
166 die "You must provide the --prefix option."
169 case "$arg_command" in
171 test -e "$arg_prefix" &&
172 die "prefix '$arg_prefix' already exists."
175 test -e "$arg_prefix" ||
176 die "'$arg_prefix' does not exist; use 'git subtree add'"
180 dir="$(dirname "$arg_prefix/.")"
182 if test "$arg_command" != "pull" &&
183 test "$arg_command" != "add" &&
184 test "$arg_command" != "push"
186 revs=$(git rev-parse $default --revs-only "$@") || exit $?
187 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
188 ensure_single_rev $revs
191 die "Error: Use --prefix instead of bare filenames."
195 debug "command: {$arg_command}"
196 debug "quiet: {$GIT_QUIET}"
197 debug "revs: {$revs}"
202 "cmd_$arg_command" "$@"
206 cachedir="$GIT_DIR/subtree-cache/$$"
207 rm -rf "$cachedir" ||
208 die "Can't delete old cachedir: $cachedir"
209 mkdir -p "$cachedir" ||
210 die "Can't create new cachedir: $cachedir"
211 mkdir -p "$cachedir/notree" ||
212 die "Can't create new cachedir: $cachedir/notree"
213 debug "Using cachedir: $cachedir" >&2
219 if test -r "$cachedir/$oldrev"
221 read newrev <"$cachedir/$oldrev"
230 if ! test -r "$cachedir/$oldrev"
238 missed=$(cache_miss "$1") || exit $?
239 local indent=$(($2 + 1))
242 if ! test -r "$cachedir/notree/$miss"
244 debug " incorrect order: $miss"
245 process_split_commit "$miss" "" "$indent"
251 echo "1" > "$cachedir/notree/$1"
257 if test "$oldrev" != "latest_old" &&
258 test "$oldrev" != "latest_new" &&
259 test -e "$cachedir/$oldrev"
261 die "cache for $oldrev already exists!"
263 echo "$newrev" >"$cachedir/$oldrev"
267 if git rev-parse "$1" >/dev/null 2>&1
275 # if a commit doesn't have a parent, this might not work. But we only want
276 # to remove the parent from the rev-list, and since it doesn't exist, it won't
277 # be there anyway, so do nothing in that case.
278 try_remove_previous () {
285 find_latest_squash () {
286 debug "Looking for latest squash ($dir)..."
291 git log --grep="^git-subtree-dir: $dir/*\$" \
292 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
296 debug "{{$sq/$main/$sub}}"
301 git-subtree-mainline:)
305 sub="$(git rev-parse "$b^0")" ||
306 die "could not rev-parse split hash $b from commit $sq"
314 # Pretend its sub was a squash.
317 debug "Squash found: $sq $sub"
329 find_existing_splits () {
330 debug "Looking for prior splits..."
335 local grep_format="^git-subtree-dir: $dir/*\$"
336 if test -n "$arg_split_ignore_joins"
338 grep_format="^Add '$dir/' from commit '"
340 git log --grep="$grep_format" \
341 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
348 git-subtree-mainline:)
352 sub="$(git rev-parse "$b^0")" ||
353 die "could not rev-parse split hash $b from commit $sq"
356 debug " Main is: '$main'"
357 if test -z "$main" -a -n "$sub"
359 # squash commits refer to a subtree
360 debug " Squash: $sq from $sub"
361 cache_set "$sq" "$sub"
363 if test -n "$main" -a -n "$sub"
365 debug " Prior: $main -> $sub"
368 try_remove_previous "$main"
369 try_remove_previous "$sub"
379 # We're going to set some environment vars here, so
380 # do it in a subshell to get rid of them safely later
381 debug copy_commit "{$1}" "{$2}" "{$3}"
382 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
385 read GIT_AUTHOR_EMAIL
387 read GIT_COMMITTER_NAME
388 read GIT_COMMITTER_EMAIL
389 read GIT_COMMITTER_DATE
390 export GIT_AUTHOR_NAME \
394 GIT_COMMITTER_EMAIL \
397 printf "%s" "$arg_split_annotate"
400 git commit-tree "$2" $3 # reads the rest of stdin
401 ) || die "Can't copy commit $1"
408 if test -n "$arg_addmerge_message"
410 commit_message="$arg_addmerge_message"
412 commit_message="Add '$dir/' from commit '$latest_new'"
417 git-subtree-dir: $dir
418 git-subtree-mainline: $latest_old
419 git-subtree-split: $latest_new
423 add_squashed_msg () {
424 if test -n "$arg_addmerge_message"
426 echo "$arg_addmerge_message"
428 echo "Merge commit '$1' as '$2'"
436 if test -n "$arg_addmerge_message"
438 commit_message="$arg_addmerge_message"
440 commit_message="Split '$dir/' into commit '$latest_new'"
445 git-subtree-dir: $dir
446 git-subtree-mainline: $latest_old
447 git-subtree-split: $latest_new
455 newsub_short=$(git rev-parse --short "$newsub")
459 oldsub_short=$(git rev-parse --short "$oldsub")
460 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
462 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
463 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
465 echo "Squashed '$dir/' content from commit $newsub_short"
469 echo "git-subtree-dir: $dir"
470 echo "git-subtree-split: $newsub"
473 toptree_for_commit () {
475 git rev-parse --verify "$commit^{tree}" || exit $?
478 subtree_for_commit () {
481 git ls-tree "$commit" -- "$dir" |
482 while read mode type tree name
484 assert test "$name" = "$dir"
485 assert test "$type" = "tree" -o "$type" = "commit"
486 test "$type" = "commit" && continue # ignore submodules
497 return 0 # weird parents, consider it changed
499 ptree=$(toptree_for_commit $1) || exit $?
500 if test "$ptree" != "$tree"
504 return 1 # not changed
509 new_squash_commit () {
513 tree=$(toptree_for_commit $newsub) || exit $?
516 squash_msg "$dir" "$oldsub" "$newsub" |
517 git commit-tree "$tree" -p "$old" || exit $?
519 squash_msg "$dir" "" "$newsub" |
520 git commit-tree "$tree" || exit $?
528 assert test -n "$tree"
535 for parent in $newparents
537 ptree=$(toptree_for_commit $parent) || exit $?
538 test -z "$ptree" && continue
539 if test "$ptree" = "$tree"
541 # an identical parent could be used in place of this rev.
542 if test -n "$identical"
544 # if a previous identical parent was found, check whether
545 # one is already an ancestor of the other
546 mergebase=$(git merge-base $identical $parent)
547 if test "$identical" = "$mergebase"
549 # current identical commit is an ancestor of parent
551 elif test "$parent" != "$mergebase"
553 # no common history; commit must be copied
557 # first identical parent detected
561 nonidentical="$parent"
564 # sometimes both old parents map to the same newparent;
565 # eliminate duplicates
567 for gp in $gotparents
569 if test "$gp" = "$parent"
577 gotparents="$gotparents $parent"
582 if test -n "$identical" && test -n "$nonidentical"
584 extras=$(git rev-list --count $identical..$nonidentical)
585 if test "$extras" -ne 0
587 # we need to preserve history along the other branch
591 if test -n "$identical" && test -z "$copycommit"
595 copy_commit "$rev" "$tree" "$p" || exit $?
600 if ! git diff-index HEAD --exit-code --quiet 2>&1
602 die "Working tree has modifications. Cannot add."
604 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
606 die "Index has modifications. Cannot add."
610 ensure_valid_ref_format () {
611 git check-ref-format "refs/heads/$1" ||
612 die "'$1' does not look like a ref"
615 process_split_commit () {
620 if test $indent -eq 0
622 revcount=$(($revcount + 1))
624 # processing commit without normal parent information;
626 parents=$(git rev-parse "$rev^@")
627 extracount=$(($extracount + 1))
630 progress "$revcount/$revmax ($createcount) [$extracount]"
632 debug "Processing commit: $rev"
633 exists=$(cache_get "$rev") || exit $?
636 debug " prior: $exists"
639 createcount=$(($createcount + 1))
640 debug " parents: $parents"
641 check_parents "$parents" "$indent"
642 newparents=$(cache_get $parents) || exit $?
643 debug " newparents: $newparents"
645 tree=$(subtree_for_commit "$rev" "$dir") || exit $?
646 debug " tree is: $tree"
648 # ugly. is there no better way to tell if this is a subtree
649 # vs. a mainline commit? Does it matter?
653 if test -n "$newparents"
655 cache_set "$rev" "$rev"
660 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
661 debug " newrev is: $newrev"
662 cache_set "$rev" "$newrev"
663 cache_set latest_new "$newrev"
664 cache_set latest_old "$rev"
670 die "'$dir' already exists. Cannot add."
677 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
678 die "'$1' does not refer to a commit"
684 # Technically we could accept a refspec here but we're
685 # just going to turn around and add FETCH_HEAD under the
686 # specified directory. Allowing a refspec might be
687 # misleading because we won't do anything with any other
688 # branches fetched via the refspec.
689 ensure_valid_ref_format "$2"
691 cmd_add_repository "$@"
693 say >&2 "error: parameters were '$*'"
694 die "Provide either a commit or a repository and commit."
698 cmd_add_repository () {
699 echo "git fetch" "$@"
702 git fetch "$@" || exit $?
709 rev=$(git rev-parse $default --revs-only "$@") || exit $?
710 ensure_single_rev $rev
712 debug "Adding $dir as '$rev'..."
713 git read-tree --prefix="$dir" $rev || exit $?
714 git checkout -- "$dir" || exit $?
715 tree=$(git write-tree) || exit $?
717 headrev=$(git rev-parse HEAD) || exit $?
718 if test -n "$headrev" && test "$headrev" != "$rev"
725 if test -n "$arg_addmerge_squash"
727 rev=$(new_squash_commit "" "" "$rev") || exit $?
728 commit=$(add_squashed_msg "$rev" "$dir" |
729 git commit-tree "$tree" $headp -p "$rev") || exit $?
731 revp=$(peel_committish "$rev") || exit $?
732 commit=$(add_msg "$dir" $headrev "$rev" |
733 git commit-tree "$tree" $headp -p "$revp") || exit $?
735 git reset "$commit" || exit $?
737 say >&2 "Added dir '$dir'"
741 debug "Splitting $dir..."
742 cache_setup || exit $?
744 if test -n "$arg_split_onto"
746 debug "Reading history for --onto=$arg_split_onto..."
747 git rev-list $arg_split_onto |
750 # the 'onto' history is already just the subdir, so
751 # any parent we find there can be used verbatim
753 cache_set "$rev" "$rev"
757 unrevs="$(find_existing_splits "$dir" "$revs")" || exit $?
759 # We can't restrict rev-list to only $dir here, because some of our
760 # parents have the $dir contents the root, and those won't match.
761 # (and rev-list --follow doesn't seem to solve this)
762 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
763 revmax=$(eval "$grl" | wc -l)
768 while read rev parents
770 process_split_commit "$rev" "$parents" 0
773 latest_new=$(cache_get latest_new) || exit $?
774 if test -z "$latest_new"
776 die "No new revisions were found"
779 if test -n "$arg_split_rejoin"
781 debug "Merging split branch into HEAD..."
782 latest_old=$(cache_get latest_old) || exit $?
784 --allow-unrelated-histories \
785 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
786 "$latest_new" >&2 || exit $?
788 if test -n "$arg_split_branch"
790 if rev_exists "refs/heads/$arg_split_branch"
792 if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new"
794 die "Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
800 git update-ref -m 'subtree split' \
801 "refs/heads/$arg_split_branch" "$latest_new" || exit $?
802 say >&2 "$action branch '$arg_split_branch'"
809 rev=$(git rev-parse $default --revs-only "$@") || exit $?
810 ensure_single_rev $rev
813 if test -n "$arg_addmerge_squash"
815 first_split="$(find_latest_squash "$dir")" || exit $?
816 if test -z "$first_split"
818 die "Can't squash-merge: '$dir' was never added."
823 if test "$sub" = "$rev"
825 say >&2 "Subtree is already at commit $rev."
828 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
829 debug "New squash commit: $new"
833 if test -n "$arg_addmerge_message"
835 git merge -Xsubtree="$arg_prefix" \
836 --message="$arg_addmerge_message" "$rev"
838 git merge -Xsubtree="$arg_prefix" $rev
845 die "You must provide <repository> <ref>"
848 ensure_valid_ref_format "$2"
849 git fetch "$@" || exit $?
858 die "You must provide <repository> <ref>"
860 ensure_valid_ref_format "$2"
865 echo "git push using: " "$repository" "$refspec"
866 localrev=$(git subtree split --prefix="$arg_prefix") || die
867 git push "$repository" "$localrev":"refs/heads/$refspec"
869 die "'$dir' must already exist. Try 'git subtree add'."