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)
48 printf "%s\n" "$*" >&2
55 printf "%s\n" "$*" >&2
62 printf "%s\r" "$*" >&2
69 die "assertion failed: " "$@"
73 ensure_single_rev () {
76 die "You must provide exactly one revision. Got: '$@'"
85 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
152 die "Unexpected option: $opt"
165 default="--default HEAD"
168 die "Unknown command '$command'"
174 die "You must provide the --prefix option."
180 die "prefix '$prefix' already exists."
184 die "'$prefix' does not exist; use 'git subtree add'"
188 dir="$(dirname "$prefix/.")"
190 if test "$command" != "pull" &&
191 test "$command" != "add" &&
192 test "$command" != "push"
194 revs=$(git rev-parse $default --revs-only "$@") || exit $?
195 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
196 ensure_single_rev $revs
199 die "Error: Use --prefix instead of bare filenames."
203 debug "command: {$command}"
204 debug "quiet: {$quiet}"
205 debug "revs: {$revs}"
214 cachedir="$GIT_DIR/subtree-cache/$$"
215 rm -rf "$cachedir" ||
216 die "Can't delete old cachedir: $cachedir"
217 mkdir -p "$cachedir" ||
218 die "Can't create new cachedir: $cachedir"
219 mkdir -p "$cachedir/notree" ||
220 die "Can't create new cachedir: $cachedir/notree"
221 debug "Using cachedir: $cachedir" >&2
227 if test -r "$cachedir/$oldrev"
229 read newrev <"$cachedir/$oldrev"
238 if ! test -r "$cachedir/$oldrev"
246 missed=$(cache_miss "$1")
247 local indent=$(($2 + 1))
250 if ! test -r "$cachedir/notree/$miss"
252 debug " incorrect order: $miss"
253 process_split_commit "$miss" "" "$indent"
259 echo "1" > "$cachedir/notree/$1"
265 if test "$oldrev" != "latest_old" &&
266 test "$oldrev" != "latest_new" &&
267 test -e "$cachedir/$oldrev"
269 die "cache for $oldrev already exists!"
271 echo "$newrev" >"$cachedir/$oldrev"
275 if git rev-parse "$1" >/dev/null 2>&1
283 rev_is_descendant_of_branch () {
286 branch_hash=$(git rev-parse "$branch")
287 match=$(git rev-list -1 "$branch_hash" "^$newrev")
297 # if a commit doesn't have a parent, this might not work. But we only want
298 # to remove the parent from the rev-list, and since it doesn't exist, it won't
299 # be there anyway, so do nothing in that case.
300 try_remove_previous () {
307 find_latest_squash () {
308 debug "Looking for latest squash ($dir)..."
313 git log --grep="^git-subtree-dir: $dir/*\$" \
314 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
318 debug "{{$sq/$main/$sub}}"
323 git-subtree-mainline:)
327 sub="$(git rev-parse "$b^0")" ||
328 die "could not rev-parse split hash $b from commit $sq"
336 # Pretend its sub was a squash.
339 debug "Squash found: $sq $sub"
351 find_existing_splits () {
352 debug "Looking for prior splits..."
357 local grep_format="^git-subtree-dir: $dir/*\$"
358 if test -n "$ignore_joins"
360 grep_format="^Add '$dir/' from commit '"
362 git log --grep="$grep_format" \
363 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
370 git-subtree-mainline:)
374 sub="$(git rev-parse "$b^0")" ||
375 die "could not rev-parse split hash $b from commit $sq"
378 debug " Main is: '$main'"
379 if test -z "$main" -a -n "$sub"
381 # squash commits refer to a subtree
382 debug " Squash: $sq from $sub"
383 cache_set "$sq" "$sub"
385 if test -n "$main" -a -n "$sub"
387 debug " Prior: $main -> $sub"
390 try_remove_previous "$main"
391 try_remove_previous "$sub"
401 # We're going to set some environment vars here, so
402 # do it in a subshell to get rid of them safely later
403 debug copy_commit "{$1}" "{$2}" "{$3}"
404 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
407 read GIT_AUTHOR_EMAIL
409 read GIT_COMMITTER_NAME
410 read GIT_COMMITTER_EMAIL
411 read GIT_COMMITTER_DATE
412 export GIT_AUTHOR_NAME \
416 GIT_COMMITTER_EMAIL \
419 printf "%s" "$annotate"
422 git commit-tree "$2" $3 # reads the rest of stdin
423 ) || die "Can't copy commit $1"
430 if test -n "$message"
432 commit_message="$message"
434 commit_message="Add '$dir/' from commit '$latest_new'"
439 git-subtree-dir: $dir
440 git-subtree-mainline: $latest_old
441 git-subtree-split: $latest_new
445 add_squashed_msg () {
446 if test -n "$message"
450 echo "Merge commit '$1' as '$2'"
458 if test -n "$message"
460 commit_message="$message"
462 commit_message="Split '$dir/' into commit '$latest_new'"
467 git-subtree-dir: $dir
468 git-subtree-mainline: $latest_old
469 git-subtree-split: $latest_new
477 newsub_short=$(git rev-parse --short "$newsub")
481 oldsub_short=$(git rev-parse --short "$oldsub")
482 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
484 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
485 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
487 echo "Squashed '$dir/' content from commit $newsub_short"
491 echo "git-subtree-dir: $dir"
492 echo "git-subtree-split: $newsub"
495 toptree_for_commit () {
497 git rev-parse --verify "$commit^{tree}" || exit $?
500 subtree_for_commit () {
503 git ls-tree "$commit" -- "$dir" |
504 while read mode type tree name
506 assert test "$name" = "$dir"
507 assert test "$type" = "tree" -o "$type" = "commit"
508 test "$type" = "commit" && continue # ignore submodules
519 return 0 # weird parents, consider it changed
521 ptree=$(toptree_for_commit $1)
522 if test "$ptree" != "$tree"
526 return 1 # not changed
531 new_squash_commit () {
535 tree=$(toptree_for_commit $newsub) || exit $?
538 squash_msg "$dir" "$oldsub" "$newsub" |
539 git commit-tree "$tree" -p "$old" || exit $?
541 squash_msg "$dir" "" "$newsub" |
542 git commit-tree "$tree" || exit $?
550 assert test -n "$tree"
557 for parent in $newparents
559 ptree=$(toptree_for_commit $parent) || exit $?
560 test -z "$ptree" && continue
561 if test "$ptree" = "$tree"
563 # an identical parent could be used in place of this rev.
564 if test -n "$identical"
566 # if a previous identical parent was found, check whether
567 # one is already an ancestor of the other
568 mergebase=$(git merge-base $identical $parent)
569 if test "$identical" = "$mergebase"
571 # current identical commit is an ancestor of parent
573 elif test "$parent" != "$mergebase"
575 # no common history; commit must be copied
579 # first identical parent detected
583 nonidentical="$parent"
586 # sometimes both old parents map to the same newparent;
587 # eliminate duplicates
589 for gp in $gotparents
591 if test "$gp" = "$parent"
599 gotparents="$gotparents $parent"
604 if test -n "$identical" && test -n "$nonidentical"
606 extras=$(git rev-list --count $identical..$nonidentical)
607 if test "$extras" -ne 0
609 # we need to preserve history along the other branch
613 if test -n "$identical" && test -z "$copycommit"
617 copy_commit "$rev" "$tree" "$p" || exit $?
622 if ! git diff-index HEAD --exit-code --quiet 2>&1
624 die "Working tree has modifications. Cannot add."
626 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
628 die "Index has modifications. Cannot add."
632 ensure_valid_ref_format () {
633 git check-ref-format "refs/heads/$1" ||
634 die "'$1' does not look like a ref"
637 process_split_commit () {
642 if test $indent -eq 0
644 revcount=$(($revcount + 1))
646 # processing commit without normal parent information;
648 parents=$(git rev-parse "$rev^@")
649 extracount=$(($extracount + 1))
652 progress "$revcount/$revmax ($createcount) [$extracount]"
654 debug "Processing commit: $rev"
655 exists=$(cache_get "$rev")
658 debug " prior: $exists"
661 createcount=$(($createcount + 1))
662 debug " parents: $parents"
663 check_parents "$parents" "$indent"
664 newparents=$(cache_get $parents)
665 debug " newparents: $newparents"
667 tree=$(subtree_for_commit "$rev" "$dir")
668 debug " tree is: $tree"
670 # ugly. is there no better way to tell if this is a subtree
671 # vs. a mainline commit? Does it matter?
675 if test -n "$newparents"
677 cache_set "$rev" "$rev"
682 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
683 debug " newrev is: $newrev"
684 cache_set "$rev" "$newrev"
685 cache_set latest_new "$newrev"
686 cache_set latest_old "$rev"
692 die "'$dir' already exists. Cannot add."
699 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
700 die "'$1' does not refer to a commit"
706 # Technically we could accept a refspec here but we're
707 # just going to turn around and add FETCH_HEAD under the
708 # specified directory. Allowing a refspec might be
709 # misleading because we won't do anything with any other
710 # branches fetched via the refspec.
711 ensure_valid_ref_format "$2"
713 cmd_add_repository "$@"
715 say "error: parameters were '$@'"
716 die "Provide either a commit or a repository and commit."
720 cmd_add_repository () {
721 echo "git fetch" "$@"
724 git fetch "$@" || exit $?
731 rev=$(git rev-parse $default --revs-only "$@") || exit $?
732 ensure_single_rev $rev
734 debug "Adding $dir as '$rev'..."
735 git read-tree --prefix="$dir" $rev || exit $?
736 git checkout -- "$dir" || exit $?
737 tree=$(git write-tree) || exit $?
739 headrev=$(git rev-parse HEAD) || exit $?
740 if test -n "$headrev" && test "$headrev" != "$rev"
749 rev=$(new_squash_commit "" "" "$rev") || exit $?
750 commit=$(add_squashed_msg "$rev" "$dir" |
751 git commit-tree "$tree" $headp -p "$rev") || exit $?
753 revp=$(peel_committish "$rev") &&
754 commit=$(add_msg "$dir" $headrev "$rev" |
755 git commit-tree "$tree" $headp -p "$revp") || exit $?
757 git reset "$commit" || exit $?
759 say "Added dir '$dir'"
763 debug "Splitting $dir..."
764 cache_setup || exit $?
768 debug "Reading history for --onto=$onto..."
772 # the 'onto' history is already just the subdir, so
773 # any parent we find there can be used verbatim
775 cache_set "$rev" "$rev"
779 unrevs="$(find_existing_splits "$dir" "$revs")"
781 # We can't restrict rev-list to only $dir here, because some of our
782 # parents have the $dir contents the root, and those won't match.
783 # (and rev-list --follow doesn't seem to solve this)
784 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
785 revmax=$(eval "$grl" | wc -l)
790 while read rev parents
792 process_split_commit "$rev" "$parents" 0
795 latest_new=$(cache_get latest_new)
796 if test -z "$latest_new"
798 die "No new revisions were found"
803 debug "Merging split branch into HEAD..."
804 latest_old=$(cache_get latest_old)
806 --allow-unrelated-histories \
807 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
808 "$latest_new" >&2 || exit $?
812 if rev_exists "refs/heads/$branch"
814 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
816 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
822 git update-ref -m 'subtree split' \
823 "refs/heads/$branch" "$latest_new" || exit $?
824 say "$action branch '$branch'"
831 rev=$(git rev-parse $default --revs-only "$@") || exit $?
832 ensure_single_rev $rev
837 first_split="$(find_latest_squash "$dir")"
838 if test -z "$first_split"
840 die "Can't squash-merge: '$dir' was never added."
845 if test "$sub" = "$rev"
847 say "Subtree is already at commit $rev."
850 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
851 debug "New squash commit: $new"
855 version=$(git version)
856 if test "$version" \< "git version 1.7"
858 if test -n "$message"
860 git merge -s subtree --message="$message" "$rev"
862 git merge -s subtree "$rev"
865 if test -n "$message"
867 git merge -Xsubtree="$prefix" \
868 --message="$message" "$rev"
870 git merge -Xsubtree="$prefix" $rev
878 die "You must provide <repository> <ref>"
881 ensure_valid_ref_format "$2"
882 git fetch "$@" || exit $?
891 die "You must provide <repository> <ref>"
893 ensure_valid_ref_format "$2"
898 echo "git push using: " "$repository" "$refspec"
899 localrev=$(git subtree split --prefix="$prefix") || die
900 git push "$repository" "$localrev":"refs/heads/$refspec"
902 die "'$dir' must already exist. Try 'git subtree add'."