3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
12 git subtree add --prefix=<prefix> <commit>
13 git subtree add --prefix=<prefix> <repository> <ref>
14 git subtree merge --prefix=<prefix> <commit>
15 git subtree pull --prefix=<prefix> <repository> <ref>
16 git subtree push --prefix=<prefix> <repository> <ref>
17 git subtree split --prefix=<prefix> <commit...>
22 P,prefix= the name of the subdir to split out
23 m,message= use the given message as the commit message for the merge commit
25 annotate= add a prefix to commit message of new commits
26 b,branch= create a new branch from the split subtree
27 ignore-joins ignore prior --rejoin commits
28 onto= try connecting new tree to an existing one
29 rejoin merge the new branch back into HEAD
30 options for 'add', 'merge', and 'pull'
31 squash merge subtree changes as a single commit
33 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
35 PATH=$PATH:$(git --exec-path)
55 printf "%s\n" "$*" >&2
62 printf "%s\n" "$*" >&2
69 printf "%s\r" "$*" >&2
76 die "assertion failed: " "$@"
144 die "Unexpected option: $opt"
157 default="--default HEAD"
160 die "Unknown command '$command'"
166 die "You must provide the --prefix option."
172 die "prefix '$prefix' already exists."
176 die "'$prefix' does not exist; use 'git subtree add'"
180 dir="$(dirname "$prefix/.")"
182 if test "$command" != "pull" &&
183 test "$command" != "add" &&
184 test "$command" != "push"
186 revs=$(git rev-parse $default --revs-only "$@") || exit $?
187 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
190 die "Error: Use --prefix instead of bare filenames."
194 debug "command: {$command}"
195 debug "quiet: {$quiet}"
196 debug "revs: {$revs}"
202 cachedir="$GIT_DIR/subtree-cache/$$"
203 rm -rf "$cachedir" ||
204 die "Can't delete old cachedir: $cachedir"
205 mkdir -p "$cachedir" ||
206 die "Can't create new cachedir: $cachedir"
207 mkdir -p "$cachedir/notree" ||
208 die "Can't create new cachedir: $cachedir/notree"
209 debug "Using cachedir: $cachedir" >&2
215 if test -r "$cachedir/$oldrev"
217 read newrev <"$cachedir/$oldrev"
226 if ! test -r "$cachedir/$oldrev"
234 missed=$(cache_miss "$1")
235 local indent=$(($2 + 1))
238 if ! test -r "$cachedir/notree/$miss"
240 debug " incorrect order: $miss"
241 process_split_commit "$miss" "" "$indent"
247 echo "1" > "$cachedir/notree/$1"
253 if test "$oldrev" != "latest_old" &&
254 test "$oldrev" != "latest_new" &&
255 test -e "$cachedir/$oldrev"
257 die "cache for $oldrev already exists!"
259 echo "$newrev" >"$cachedir/$oldrev"
263 if git rev-parse "$1" >/dev/null 2>&1
271 rev_is_descendant_of_branch () {
274 branch_hash=$(git rev-parse "$branch")
275 match=$(git rev-list -1 "$branch_hash" "^$newrev")
285 # if a commit doesn't have a parent, this might not work. But we only want
286 # to remove the parent from the rev-list, and since it doesn't exist, it won't
287 # be there anyway, so do nothing in that case.
288 try_remove_previous () {
295 find_latest_squash () {
296 debug "Looking for latest squash ($dir)..."
301 git log --grep="^git-subtree-dir: $dir/*\$" \
302 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
306 debug "{{$sq/$main/$sub}}"
311 git-subtree-mainline:)
315 sub="$(git rev-parse "$b^0")" ||
316 die "could not rev-parse split hash $b from commit $sq"
324 # Pretend its sub was a squash.
327 debug "Squash found: $sq $sub"
339 find_existing_splits () {
340 debug "Looking for prior splits..."
345 local grep_format="^git-subtree-dir: $dir/*\$"
346 if test -n "$ignore_joins"
348 grep_format="^Add '$dir/' from commit '"
350 git log --grep="$grep_format" \
351 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
358 git-subtree-mainline:)
362 sub="$(git rev-parse "$b^0")" ||
363 die "could not rev-parse split hash $b from commit $sq"
366 debug " Main is: '$main'"
367 if test -z "$main" -a -n "$sub"
369 # squash commits refer to a subtree
370 debug " Squash: $sq from $sub"
371 cache_set "$sq" "$sub"
373 if test -n "$main" -a -n "$sub"
375 debug " Prior: $main -> $sub"
378 try_remove_previous "$main"
379 try_remove_previous "$sub"
389 # We're going to set some environment vars here, so
390 # do it in a subshell to get rid of them safely later
391 debug copy_commit "{$1}" "{$2}" "{$3}"
392 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
395 read GIT_AUTHOR_EMAIL
397 read GIT_COMMITTER_NAME
398 read GIT_COMMITTER_EMAIL
399 read GIT_COMMITTER_DATE
400 export GIT_AUTHOR_NAME \
404 GIT_COMMITTER_EMAIL \
407 printf "%s" "$annotate"
410 git commit-tree "$2" $3 # reads the rest of stdin
411 ) || die "Can't copy commit $1"
418 if test -n "$message"
420 commit_message="$message"
422 commit_message="Add '$dir/' from commit '$latest_new'"
427 git-subtree-dir: $dir
428 git-subtree-mainline: $latest_old
429 git-subtree-split: $latest_new
433 add_squashed_msg () {
434 if test -n "$message"
438 echo "Merge commit '$1' as '$2'"
446 if test -n "$message"
448 commit_message="$message"
450 commit_message="Split '$dir/' into commit '$latest_new'"
455 git-subtree-dir: $dir
456 git-subtree-mainline: $latest_old
457 git-subtree-split: $latest_new
465 newsub_short=$(git rev-parse --short "$newsub")
469 oldsub_short=$(git rev-parse --short "$oldsub")
470 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
472 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
473 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
475 echo "Squashed '$dir/' content from commit $newsub_short"
479 echo "git-subtree-dir: $dir"
480 echo "git-subtree-split: $newsub"
483 toptree_for_commit () {
485 git rev-parse --verify "$commit^{tree}" || exit $?
488 subtree_for_commit () {
491 git ls-tree "$commit" -- "$dir" |
492 while read mode type tree name
494 assert test "$name" = "$dir"
495 assert test "$type" = "tree" -o "$type" = "commit"
496 test "$type" = "commit" && continue # ignore submodules
507 return 0 # weird parents, consider it changed
509 ptree=$(toptree_for_commit $1)
510 if test "$ptree" != "$tree"
514 return 1 # not changed
519 new_squash_commit () {
523 tree=$(toptree_for_commit $newsub) || exit $?
526 squash_msg "$dir" "$oldsub" "$newsub" |
527 git commit-tree "$tree" -p "$old" || exit $?
529 squash_msg "$dir" "" "$newsub" |
530 git commit-tree "$tree" || exit $?
538 assert test -n "$tree"
545 for parent in $newparents
547 ptree=$(toptree_for_commit $parent) || exit $?
548 test -z "$ptree" && continue
549 if test "$ptree" = "$tree"
551 # an identical parent could be used in place of this rev.
552 if test -n "$identical"
554 # if a previous identical parent was found, check whether
555 # one is already an ancestor of the other
556 mergebase=$(git merge-base $identical $parent)
557 if test "$identical" = "$mergebase"
559 # current identical commit is an ancestor of parent
561 elif test "$parent" != "$mergebase"
563 # no common history; commit must be copied
567 # first identical parent detected
571 nonidentical="$parent"
574 # sometimes both old parents map to the same newparent;
575 # eliminate duplicates
577 for gp in $gotparents
579 if test "$gp" = "$parent"
587 gotparents="$gotparents $parent"
592 if test -n "$identical" && test -n "$nonidentical"
594 extras=$(git rev-list --count $identical..$nonidentical)
595 if test "$extras" -ne 0
597 # we need to preserve history along the other branch
601 if test -n "$identical" && test -z "$copycommit"
605 copy_commit "$rev" "$tree" "$p" || exit $?
610 if ! git diff-index HEAD --exit-code --quiet 2>&1
612 die "Working tree has modifications. Cannot add."
614 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
616 die "Index has modifications. Cannot add."
620 ensure_valid_ref_format () {
621 git check-ref-format "refs/heads/$1" ||
622 die "'$1' does not look like a ref"
625 process_split_commit () {
630 if test $indent -eq 0
632 revcount=$(($revcount + 1))
634 # processing commit without normal parent information;
636 parents=$(git rev-parse "$rev^@")
637 extracount=$(($extracount + 1))
640 progress "$revcount/$revmax ($createcount) [$extracount]"
642 debug "Processing commit: $rev"
643 exists=$(cache_get "$rev")
646 debug " prior: $exists"
649 createcount=$(($createcount + 1))
650 debug " parents: $parents"
651 check_parents "$parents" "$indent"
652 newparents=$(cache_get $parents)
653 debug " newparents: $newparents"
655 tree=$(subtree_for_commit "$rev" "$dir")
656 debug " tree is: $tree"
658 # ugly. is there no better way to tell if this is a subtree
659 # vs. a mainline commit? Does it matter?
663 if test -n "$newparents"
665 cache_set "$rev" "$rev"
670 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
671 debug " newrev is: $newrev"
672 cache_set "$rev" "$newrev"
673 cache_set latest_new "$newrev"
674 cache_set latest_old "$rev"
680 die "'$dir' already exists. Cannot add."
687 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
688 die "'$1' does not refer to a commit"
694 # Technically we could accept a refspec here but we're
695 # just going to turn around and add FETCH_HEAD under the
696 # specified directory. Allowing a refspec might be
697 # misleading because we won't do anything with any other
698 # branches fetched via the refspec.
699 ensure_valid_ref_format "$2"
701 cmd_add_repository "$@"
703 say "error: parameters were '$@'"
704 die "Provide either a commit or a repository and commit."
708 cmd_add_repository () {
709 echo "git fetch" "$@"
712 git fetch "$@" || exit $?
719 revs=$(git rev-parse $default --revs-only "$@") || exit $?
723 debug "Adding $dir as '$rev'..."
724 git read-tree --prefix="$dir" $rev || exit $?
725 git checkout -- "$dir" || exit $?
726 tree=$(git write-tree) || exit $?
728 headrev=$(git rev-parse HEAD) || exit $?
729 if test -n "$headrev" && test "$headrev" != "$rev"
738 rev=$(new_squash_commit "" "" "$rev") || exit $?
739 commit=$(add_squashed_msg "$rev" "$dir" |
740 git commit-tree "$tree" $headp -p "$rev") || exit $?
742 revp=$(peel_committish "$rev") &&
743 commit=$(add_msg "$dir" $headrev "$rev" |
744 git commit-tree "$tree" $headp -p "$revp") || exit $?
746 git reset "$commit" || exit $?
748 say "Added dir '$dir'"
752 debug "Splitting $dir..."
753 cache_setup || exit $?
757 debug "Reading history for --onto=$onto..."
761 # the 'onto' history is already just the subdir, so
762 # any parent we find there can be used verbatim
764 cache_set "$rev" "$rev"
768 unrevs="$(find_existing_splits "$dir" "$revs")"
770 # We can't restrict rev-list to only $dir here, because some of our
771 # parents have the $dir contents the root, and those won't match.
772 # (and rev-list --follow doesn't seem to solve this)
773 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
774 revmax=$(eval "$grl" | wc -l)
779 while read rev parents
781 process_split_commit "$rev" "$parents" 0
784 latest_new=$(cache_get latest_new)
785 if test -z "$latest_new"
787 die "No new revisions were found"
792 debug "Merging split branch into HEAD..."
793 latest_old=$(cache_get latest_old)
795 --allow-unrelated-histories \
796 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
797 "$latest_new" >&2 || exit $?
801 if rev_exists "refs/heads/$branch"
803 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
805 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
811 git update-ref -m 'subtree split' \
812 "refs/heads/$branch" "$latest_new" || exit $?
813 say "$action branch '$branch'"
820 revs=$(git rev-parse $default --revs-only "$@") || exit $?
826 die "You must provide exactly one revision. Got: '$revs'"
832 first_split="$(find_latest_squash "$dir")"
833 if test -z "$first_split"
835 die "Can't squash-merge: '$dir' was never added."
840 if test "$sub" = "$rev"
842 say "Subtree is already at commit $rev."
845 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
846 debug "New squash commit: $new"
850 version=$(git version)
851 if test "$version" \< "git version 1.7"
853 if test -n "$message"
855 git merge -s subtree --message="$message" "$rev"
857 git merge -s subtree "$rev"
860 if test -n "$message"
862 git merge -Xsubtree="$prefix" \
863 --message="$message" "$rev"
865 git merge -Xsubtree="$prefix" $rev
873 die "You must provide <repository> <ref>"
876 ensure_valid_ref_format "$2"
877 git fetch "$@" || exit $?
886 die "You must provide <repository> <ref>"
888 ensure_valid_ref_format "$2"
893 echo "git push using: " "$repository" "$refspec"
894 localrev=$(git subtree split --prefix="$prefix") || die
895 git push "$repository" "$localrev":"refs/heads/$refspec"
897 die "'$dir' must already exist. Try 'git subtree add'."