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 --no-show-signature --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 local grep_format="^git-subtree-dir: $dir/*\$"
344 if test -n "$ignore_joins"
346 grep_format="^Add '$dir/' from commit '"
348 git log --grep="$grep_format" \
349 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
356 git-subtree-mainline:)
360 sub="$(git rev-parse "$b^0")" ||
361 die "could not rev-parse split hash $b from commit $sq"
364 debug " Main is: '$main'"
365 if test -z "$main" -a -n "$sub"
367 # squash commits refer to a subtree
368 debug " Squash: $sq from $sub"
369 cache_set "$sq" "$sub"
371 if test -n "$main" -a -n "$sub"
373 debug " Prior: $main -> $sub"
376 try_remove_previous "$main"
377 try_remove_previous "$sub"
387 # We're going to set some environment vars here, so
388 # do it in a subshell to get rid of them safely later
389 debug copy_commit "{$1}" "{$2}" "{$3}"
390 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
393 read GIT_AUTHOR_EMAIL
395 read GIT_COMMITTER_NAME
396 read GIT_COMMITTER_EMAIL
397 read GIT_COMMITTER_DATE
398 export GIT_AUTHOR_NAME \
402 GIT_COMMITTER_EMAIL \
405 printf "%s" "$annotate"
408 git commit-tree "$2" $3 # reads the rest of stdin
409 ) || die "Can't copy commit $1"
416 if test -n "$message"
418 commit_message="$message"
420 commit_message="Add '$dir/' from commit '$latest_new'"
425 git-subtree-dir: $dir
426 git-subtree-mainline: $latest_old
427 git-subtree-split: $latest_new
431 add_squashed_msg () {
432 if test -n "$message"
436 echo "Merge commit '$1' as '$2'"
444 if test -n "$message"
446 commit_message="$message"
448 commit_message="Split '$dir/' into commit '$latest_new'"
453 git-subtree-dir: $dir
454 git-subtree-mainline: $latest_old
455 git-subtree-split: $latest_new
463 newsub_short=$(git rev-parse --short "$newsub")
467 oldsub_short=$(git rev-parse --short "$oldsub")
468 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
470 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
471 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
473 echo "Squashed '$dir/' content from commit $newsub_short"
477 echo "git-subtree-dir: $dir"
478 echo "git-subtree-split: $newsub"
481 toptree_for_commit () {
483 git rev-parse --verify "$commit^{tree}" || exit $?
486 subtree_for_commit () {
489 git ls-tree "$commit" -- "$dir" |
490 while read mode type tree name
492 assert test "$name" = "$dir"
493 assert test "$type" = "tree" -o "$type" = "commit"
494 test "$type" = "commit" && continue # ignore submodules
505 return 0 # weird parents, consider it changed
507 ptree=$(toptree_for_commit $1)
508 if test "$ptree" != "$tree"
512 return 1 # not changed
517 new_squash_commit () {
521 tree=$(toptree_for_commit $newsub) || exit $?
524 squash_msg "$dir" "$oldsub" "$newsub" |
525 git commit-tree "$tree" -p "$old" || exit $?
527 squash_msg "$dir" "" "$newsub" |
528 git commit-tree "$tree" || exit $?
536 assert test -n "$tree"
542 for parent in $newparents
544 ptree=$(toptree_for_commit $parent) || exit $?
545 test -z "$ptree" && continue
546 if test "$ptree" = "$tree"
548 # an identical parent could be used in place of this rev.
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"
573 if test -n "$identical" && test -n "$nonidentical"
575 extras=$(git rev-list --count $identical..$nonidentical)
576 if test "$extras" -ne 0
578 # we need to preserve history along the other branch
582 if test -n "$identical" && test -z "$copycommit"
586 copy_commit "$rev" "$tree" "$p" || exit $?
591 if ! git diff-index HEAD --exit-code --quiet 2>&1
593 die "Working tree has modifications. Cannot add."
595 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
597 die "Index has modifications. Cannot add."
601 ensure_valid_ref_format () {
602 git check-ref-format "refs/heads/$1" ||
603 die "'$1' does not look like a ref"
606 process_split_commit () {
609 revcount=$(($revcount + 1))
610 progress "$revcount/$revmax ($createcount)"
611 debug "Processing commit: $rev"
612 exists=$(cache_get "$rev")
615 debug " prior: $exists"
618 createcount=$(($createcount + 1))
619 debug " parents: $parents"
620 newparents=$(cache_get $parents)
621 debug " newparents: $newparents"
623 tree=$(subtree_for_commit "$rev" "$dir")
624 debug " tree is: $tree"
626 check_parents $parents
628 # ugly. is there no better way to tell if this is a subtree
629 # vs. a mainline commit? Does it matter?
633 if test -n "$newparents"
635 cache_set "$rev" "$rev"
640 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
641 debug " newrev is: $newrev"
642 cache_set "$rev" "$newrev"
643 cache_set latest_new "$newrev"
644 cache_set latest_old "$rev"
650 die "'$dir' already exists. Cannot add."
657 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
658 die "'$1' does not refer to a commit"
664 # Technically we could accept a refspec here but we're
665 # just going to turn around and add FETCH_HEAD under the
666 # specified directory. Allowing a refspec might be
667 # misleading because we won't do anything with any other
668 # branches fetched via the refspec.
669 ensure_valid_ref_format "$2"
671 cmd_add_repository "$@"
673 say "error: parameters were '$@'"
674 die "Provide either a commit or a repository and commit."
678 cmd_add_repository () {
679 echo "git fetch" "$@"
682 git fetch "$@" || exit $?
689 revs=$(git rev-parse $default --revs-only "$@") || exit $?
693 debug "Adding $dir as '$rev'..."
694 git read-tree --prefix="$dir" $rev || exit $?
695 git checkout -- "$dir" || exit $?
696 tree=$(git write-tree) || exit $?
698 headrev=$(git rev-parse HEAD) || exit $?
699 if test -n "$headrev" && test "$headrev" != "$rev"
708 rev=$(new_squash_commit "" "" "$rev") || exit $?
709 commit=$(add_squashed_msg "$rev" "$dir" |
710 git commit-tree "$tree" $headp -p "$rev") || exit $?
712 revp=$(peel_committish "$rev") &&
713 commit=$(add_msg "$dir" $headrev "$rev" |
714 git commit-tree "$tree" $headp -p "$revp") || exit $?
716 git reset "$commit" || exit $?
718 say "Added dir '$dir'"
722 debug "Splitting $dir..."
723 cache_setup || exit $?
727 debug "Reading history for --onto=$onto..."
731 # the 'onto' history is already just the subdir, so
732 # any parent we find there can be used verbatim
734 cache_set "$rev" "$rev"
738 unrevs="$(find_existing_splits "$dir" "$revs")"
740 # We can't restrict rev-list to only $dir here, because some of our
741 # parents have the $dir contents the root, and those won't match.
742 # (and rev-list --follow doesn't seem to solve this)
743 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
744 revmax=$(eval "$grl" | wc -l)
748 while read rev parents
750 process_split_commit "$rev" "$parents"
753 latest_new=$(cache_get latest_new)
754 if test -z "$latest_new"
756 die "No new revisions were found"
761 debug "Merging split branch into HEAD..."
762 latest_old=$(cache_get latest_old)
764 --allow-unrelated-histories \
765 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
766 "$latest_new" >&2 || exit $?
770 if rev_exists "refs/heads/$branch"
772 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
774 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
780 git update-ref -m 'subtree split' \
781 "refs/heads/$branch" "$latest_new" || exit $?
782 say "$action branch '$branch'"
789 revs=$(git rev-parse $default --revs-only "$@") || exit $?
795 die "You must provide exactly one revision. Got: '$revs'"
801 first_split="$(find_latest_squash "$dir")"
802 if test -z "$first_split"
804 die "Can't squash-merge: '$dir' was never added."
809 if test "$sub" = "$rev"
811 say "Subtree is already at commit $rev."
814 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
815 debug "New squash commit: $new"
819 version=$(git version)
820 if test "$version" \< "git version 1.7"
822 if test -n "$message"
824 git merge -s subtree --message="$message" "$rev"
826 git merge -s subtree "$rev"
829 if test -n "$message"
831 git merge -Xsubtree="$prefix" \
832 --message="$message" "$rev"
834 git merge -Xsubtree="$prefix" $rev
842 die "You must provide <repository> <ref>"
845 ensure_valid_ref_format "$2"
846 git fetch "$@" || exit $?
855 die "You must provide <repository> <ref>"
857 ensure_valid_ref_format "$2"
862 echo "git push using: " "$repository" "$refspec"
863 localrev=$(git subtree split --prefix="$prefix") || die
864 git push "$repository" "$localrev":"refs/heads/$refspec"
866 die "'$dir' must already exist. Try 'git subtree add'."