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 "$@")
237 if ! test -r "$cachedir/notree/$miss"
239 debug " incorrect order: $miss"
245 echo "1" > "$cachedir/notree/$1"
251 if test "$oldrev" != "latest_old" &&
252 test "$oldrev" != "latest_new" &&
253 test -e "$cachedir/$oldrev"
255 die "cache for $oldrev already exists!"
257 echo "$newrev" >"$cachedir/$oldrev"
261 if git rev-parse "$1" >/dev/null 2>&1
269 rev_is_descendant_of_branch () {
272 branch_hash=$(git rev-parse "$branch")
273 match=$(git rev-list -1 "$branch_hash" "^$newrev")
283 # if a commit doesn't have a parent, this might not work. But we only want
284 # to remove the parent from the rev-list, and since it doesn't exist, it won't
285 # be there anyway, so do nothing in that case.
286 try_remove_previous () {
293 find_latest_squash () {
294 debug "Looking for latest squash ($dir)..."
299 git log --grep="^git-subtree-dir: $dir/*\$" \
300 --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
304 debug "{{$sq/$main/$sub}}"
309 git-subtree-mainline:)
313 sub="$(git rev-parse "$b^0")" ||
314 die "could not rev-parse split hash $b from commit $sq"
322 # Pretend its sub was a squash.
325 debug "Squash found: $sq $sub"
337 find_existing_splits () {
338 debug "Looking for prior splits..."
343 git log --grep="^git-subtree-dir: $dir/*\$" \
344 --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
351 git-subtree-mainline:)
355 sub="$(git rev-parse "$b^0")" ||
356 die "could not rev-parse split hash $b from commit $sq"
359 debug " Main is: '$main'"
360 if test -z "$main" -a -n "$sub"
362 # squash commits refer to a subtree
363 debug " Squash: $sq from $sub"
364 cache_set "$sq" "$sub"
366 if test -n "$main" -a -n "$sub"
368 debug " Prior: $main -> $sub"
371 try_remove_previous "$main"
372 try_remove_previous "$sub"
382 # We're going to set some environment vars here, so
383 # do it in a subshell to get rid of them safely later
384 debug copy_commit "{$1}" "{$2}" "{$3}"
385 git log -1 --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
388 read GIT_AUTHOR_EMAIL
390 read GIT_COMMITTER_NAME
391 read GIT_COMMITTER_EMAIL
392 read GIT_COMMITTER_DATE
393 export GIT_AUTHOR_NAME \
397 GIT_COMMITTER_EMAIL \
400 printf "%s" "$annotate"
403 git commit-tree "$2" $3 # reads the rest of stdin
404 ) || die "Can't copy commit $1"
411 if test -n "$message"
413 commit_message="$message"
415 commit_message="Add '$dir/' from commit '$latest_new'"
420 git-subtree-dir: $dir
421 git-subtree-mainline: $latest_old
422 git-subtree-split: $latest_new
426 add_squashed_msg () {
427 if test -n "$message"
431 echo "Merge commit '$1' as '$2'"
439 if test -n "$message"
441 commit_message="$message"
443 commit_message="Split '$dir/' into commit '$latest_new'"
448 git-subtree-dir: $dir
449 git-subtree-mainline: $latest_old
450 git-subtree-split: $latest_new
458 newsub_short=$(git rev-parse --short "$newsub")
462 oldsub_short=$(git rev-parse --short "$oldsub")
463 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
465 git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
466 git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
468 echo "Squashed '$dir/' content from commit $newsub_short"
472 echo "git-subtree-dir: $dir"
473 echo "git-subtree-split: $newsub"
476 toptree_for_commit () {
478 git log -1 --pretty=format:'%T' "$commit" -- || exit $?
481 subtree_for_commit () {
484 git ls-tree "$commit" -- "$dir" |
485 while read mode type tree name
487 assert test "$name" = "$dir"
488 assert test "$type" = "tree" -o "$type" = "commit"
489 test "$type" = "commit" && continue # ignore submodules
500 return 0 # weird parents, consider it changed
502 ptree=$(toptree_for_commit $1)
503 if test "$ptree" != "$tree"
507 return 1 # not changed
512 new_squash_commit () {
516 tree=$(toptree_for_commit $newsub) || exit $?
519 squash_msg "$dir" "$oldsub" "$newsub" |
520 git commit-tree "$tree" -p "$old" || exit $?
522 squash_msg "$dir" "" "$newsub" |
523 git commit-tree "$tree" || exit $?
531 assert test -n "$tree"
537 for parent in $newparents
539 ptree=$(toptree_for_commit $parent) || exit $?
540 test -z "$ptree" && continue
541 if test "$ptree" = "$tree"
543 # an identical parent could be used in place of this rev.
546 nonidentical="$parent"
549 # sometimes both old parents map to the same newparent;
550 # eliminate duplicates
552 for gp in $gotparents
554 if test "$gp" = "$parent"
562 gotparents="$gotparents $parent"
568 if test -n "$identical" && test -n "$nonidentical"
570 extras=$(git rev-list --count $identical..$nonidentical)
571 if test "$extras" -ne 0
573 # we need to preserve history along the other branch
577 if test -n "$identical" && test -z "$copycommit"
581 copy_commit "$rev" "$tree" "$p" || exit $?
586 if ! git diff-index HEAD --exit-code --quiet 2>&1
588 die "Working tree has modifications. Cannot add."
590 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
592 die "Index has modifications. Cannot add."
596 ensure_valid_ref_format () {
597 git check-ref-format "refs/heads/$1" ||
598 die "'$1' does not look like a ref"
604 die "'$dir' already exists. Cannot add."
611 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
612 die "'$1' does not refer to a commit"
618 # Technically we could accept a refspec here but we're
619 # just going to turn around and add FETCH_HEAD under the
620 # specified directory. Allowing a refspec might be
621 # misleading because we won't do anything with any other
622 # branches fetched via the refspec.
623 ensure_valid_ref_format "$2"
625 cmd_add_repository "$@"
627 say "error: parameters were '$@'"
628 die "Provide either a commit or a repository and commit."
632 cmd_add_repository () {
633 echo "git fetch" "$@"
636 git fetch "$@" || exit $?
643 revs=$(git rev-parse $default --revs-only "$@") || exit $?
647 debug "Adding $dir as '$rev'..."
648 git read-tree --prefix="$dir" $rev || exit $?
649 git checkout -- "$dir" || exit $?
650 tree=$(git write-tree) || exit $?
652 headrev=$(git rev-parse HEAD) || exit $?
653 if test -n "$headrev" && test "$headrev" != "$rev"
662 rev=$(new_squash_commit "" "" "$rev") || exit $?
663 commit=$(add_squashed_msg "$rev" "$dir" |
664 git commit-tree "$tree" $headp -p "$rev") || exit $?
666 revp=$(peel_committish "$rev") &&
667 commit=$(add_msg "$dir" $headrev "$rev" |
668 git commit-tree "$tree" $headp -p "$revp") || exit $?
670 git reset "$commit" || exit $?
672 say "Added dir '$dir'"
676 debug "Splitting $dir..."
677 cache_setup || exit $?
681 debug "Reading history for --onto=$onto..."
685 # the 'onto' history is already just the subdir, so
686 # any parent we find there can be used verbatim
688 cache_set "$rev" "$rev"
692 if test -n "$ignore_joins"
696 unrevs="$(find_existing_splits "$dir" "$revs")"
699 # We can't restrict rev-list to only $dir here, because some of our
700 # parents have the $dir contents the root, and those won't match.
701 # (and rev-list --follow doesn't seem to solve this)
702 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
703 revmax=$(eval "$grl" | wc -l)
707 while read rev parents
709 revcount=$(($revcount + 1))
710 progress "$revcount/$revmax ($createcount)"
711 debug "Processing commit: $rev"
712 exists=$(cache_get "$rev")
715 debug " prior: $exists"
718 createcount=$(($createcount + 1))
719 debug " parents: $parents"
720 newparents=$(cache_get $parents)
721 debug " newparents: $newparents"
723 tree=$(subtree_for_commit "$rev" "$dir")
724 debug " tree is: $tree"
726 check_parents $parents
728 # ugly. is there no better way to tell if this is a subtree
729 # vs. a mainline commit? Does it matter?
733 if test -n "$newparents"
735 cache_set "$rev" "$rev"
740 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
741 debug " newrev is: $newrev"
742 cache_set "$rev" "$newrev"
743 cache_set latest_new "$newrev"
744 cache_set latest_old "$rev"
747 latest_new=$(cache_get latest_new)
748 if test -z "$latest_new"
750 die "No new revisions were found"
755 debug "Merging split branch into HEAD..."
756 latest_old=$(cache_get latest_old)
758 --allow-unrelated-histories \
759 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
760 "$latest_new" >&2 || exit $?
764 if rev_exists "refs/heads/$branch"
766 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
768 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
774 git update-ref -m 'subtree split' \
775 "refs/heads/$branch" "$latest_new" || exit $?
776 say "$action branch '$branch'"
783 revs=$(git rev-parse $default --revs-only "$@") || exit $?
789 die "You must provide exactly one revision. Got: '$revs'"
795 first_split="$(find_latest_squash "$dir")"
796 if test -z "$first_split"
798 die "Can't squash-merge: '$dir' was never added."
803 if test "$sub" = "$rev"
805 say "Subtree is already at commit $rev."
808 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
809 debug "New squash commit: $new"
813 version=$(git version)
814 if test "$version" \< "git version 1.7"
816 if test -n "$message"
818 git merge -s subtree --message="$message" "$rev"
820 git merge -s subtree "$rev"
823 if test -n "$message"
825 git merge -Xsubtree="$prefix" \
826 --message="$message" "$rev"
828 git merge -Xsubtree="$prefix" $rev
836 die "You must provide <repository> <ref>"
839 ensure_valid_ref_format "$2"
840 git fetch "$@" || exit $?
849 die "You must provide <repository> <ref>"
851 ensure_valid_ref_format "$2"
856 echo "git push using: " "$repository" "$refspec"
857 localrev=$(git subtree split --prefix="$prefix") || die
858 git push "$repository" "$localrev":"refs/heads/$refspec"
860 die "'$dir' must already exist. Try 'git subtree add'."