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: " "$@"
80 ensure_single_rev
() {
83 die
"You must provide exactly one revision. Got: '$@'"
150 die
"Unexpected option: $opt"
163 default
="--default HEAD"
166 die
"Unknown command '$command'"
172 die
"You must provide the --prefix option."
178 die
"prefix '$prefix' already exists."
182 die
"'$prefix' does not exist; use 'git subtree add'"
186 dir
="$(dirname "$prefix/.
")"
188 if test "$command" != "pull" &&
189 test "$command" != "add" &&
190 test "$command" != "push"
192 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
193 dirs=$
(git rev-parse
--no-revs --no-flags "$@") ||
exit $?
194 ensure_single_rev
$revs
197 die
"Error: Use --prefix instead of bare filenames."
201 debug
"command: {$command}"
202 debug
"quiet: {$quiet}"
203 debug
"revs: {$revs}"
209 cachedir
="$GIT_DIR/subtree-cache/$$"
210 rm -rf "$cachedir" ||
211 die
"Can't delete old cachedir: $cachedir"
212 mkdir
-p "$cachedir" ||
213 die
"Can't create new cachedir: $cachedir"
214 mkdir
-p "$cachedir/notree" ||
215 die
"Can't create new cachedir: $cachedir/notree"
216 debug
"Using cachedir: $cachedir" >&2
222 if test -r "$cachedir/$oldrev"
224 read newrev
<"$cachedir/$oldrev"
233 if ! test -r "$cachedir/$oldrev"
241 missed
=$
(cache_miss
"$1")
242 local indent
=$
(($2 + 1))
245 if ! test -r "$cachedir/notree/$miss"
247 debug
" incorrect order: $miss"
248 process_split_commit
"$miss" "" "$indent"
254 echo "1" > "$cachedir/notree/$1"
260 if test "$oldrev" != "latest_old" &&
261 test "$oldrev" != "latest_new" &&
262 test -e "$cachedir/$oldrev"
264 die
"cache for $oldrev already exists!"
266 echo "$newrev" >"$cachedir/$oldrev"
270 if git rev-parse
"$1" >/dev
/null
2>&1
278 rev_is_descendant_of_branch
() {
281 branch_hash
=$
(git rev-parse
"$branch")
282 match
=$
(git rev-list
-1 "$branch_hash" "^$newrev")
292 # if a commit doesn't have a parent, this might not work. But we only want
293 # to remove the parent from the rev-list, and since it doesn't exist, it won't
294 # be there anyway, so do nothing in that case.
295 try_remove_previous
() {
302 find_latest_squash
() {
303 debug
"Looking for latest squash ($dir)..."
308 git log
--grep="^git-subtree-dir: $dir/*\$" \
309 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
313 debug
"{{$sq/$main/$sub}}"
318 git-subtree-mainline
:)
322 sub
="$(git rev-parse "$b^
0")" ||
323 die
"could not rev-parse split hash $b from commit $sq"
331 # Pretend its sub was a squash.
334 debug
"Squash found: $sq $sub"
346 find_existing_splits
() {
347 debug
"Looking for prior splits..."
352 local grep_format
="^git-subtree-dir: $dir/*\$"
353 if test -n "$ignore_joins"
355 grep_format
="^Add '$dir/' from commit '"
357 git log
--grep="$grep_format" \
358 --no-show-signature --pretty=format
:'START %H%n%s%n%n%b%nEND%n' $revs |
365 git-subtree-mainline
:)
369 sub
="$(git rev-parse "$b^
0")" ||
370 die
"could not rev-parse split hash $b from commit $sq"
373 debug
" Main is: '$main'"
374 if test -z "$main" -a -n "$sub"
376 # squash commits refer to a subtree
377 debug
" Squash: $sq from $sub"
378 cache_set
"$sq" "$sub"
380 if test -n "$main" -a -n "$sub"
382 debug
" Prior: $main -> $sub"
385 try_remove_previous
"$main"
386 try_remove_previous
"$sub"
396 # We're going to set some environment vars here, so
397 # do it in a subshell to get rid of them safely later
398 debug copy_commit
"{$1}" "{$2}" "{$3}"
399 git log
-1 --no-show-signature --pretty=format
:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
402 read GIT_AUTHOR_EMAIL
404 read GIT_COMMITTER_NAME
405 read GIT_COMMITTER_EMAIL
406 read GIT_COMMITTER_DATE
407 export GIT_AUTHOR_NAME \
411 GIT_COMMITTER_EMAIL \
414 printf "%s" "$annotate"
417 git commit-tree
"$2" $3 # reads the rest of stdin
418 ) || die
"Can't copy commit $1"
425 if test -n "$message"
427 commit_message
="$message"
429 commit_message
="Add '$dir/' from commit '$latest_new'"
434 git-subtree-dir: $dir
435 git-subtree-mainline: $latest_old
436 git-subtree-split: $latest_new
440 add_squashed_msg
() {
441 if test -n "$message"
445 echo "Merge commit '$1' as '$2'"
453 if test -n "$message"
455 commit_message
="$message"
457 commit_message
="Split '$dir/' into commit '$latest_new'"
462 git-subtree-dir: $dir
463 git-subtree-mainline: $latest_old
464 git-subtree-split: $latest_new
472 newsub_short
=$
(git rev-parse
--short "$newsub")
476 oldsub_short
=$
(git rev-parse
--short "$oldsub")
477 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
479 git log
--no-show-signature --pretty=tformat
:'%h %s' "$oldsub..$newsub"
480 git log
--no-show-signature --pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
482 echo "Squashed '$dir/' content from commit $newsub_short"
486 echo "git-subtree-dir: $dir"
487 echo "git-subtree-split: $newsub"
490 toptree_for_commit
() {
492 git rev-parse
--verify "$commit^{tree}" ||
exit $?
495 subtree_for_commit
() {
498 git ls-tree
"$commit" -- "$dir" |
499 while read mode
type tree name
501 assert
test "$name" = "$dir"
502 assert
test "$type" = "tree" -o "$type" = "commit"
503 test "$type" = "commit" && continue # ignore submodules
514 return 0 # weird parents, consider it changed
516 ptree
=$
(toptree_for_commit
$1)
517 if test "$ptree" != "$tree"
521 return 1 # not changed
526 new_squash_commit
() {
530 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
533 squash_msg
"$dir" "$oldsub" "$newsub" |
534 git commit-tree
"$tree" -p "$old" ||
exit $?
536 squash_msg
"$dir" "" "$newsub" |
537 git commit-tree
"$tree" ||
exit $?
545 assert
test -n "$tree"
552 for parent
in $newparents
554 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
555 test -z "$ptree" && continue
556 if test "$ptree" = "$tree"
558 # an identical parent could be used in place of this rev.
559 if test -n "$identical"
561 # if a previous identical parent was found, check whether
562 # one is already an ancestor of the other
563 mergebase
=$
(git merge-base
$identical $parent)
564 if test "$identical" = "$mergebase"
566 # current identical commit is an ancestor of parent
568 elif test "$parent" != "$mergebase"
570 # no common history; commit must be copied
574 # first identical parent detected
578 nonidentical
="$parent"
581 # sometimes both old parents map to the same newparent;
582 # eliminate duplicates
584 for gp
in $gotparents
586 if test "$gp" = "$parent"
594 gotparents
="$gotparents $parent"
599 if test -n "$identical" && test -n "$nonidentical"
601 extras
=$
(git rev-list
--count $identical..
$nonidentical)
602 if test "$extras" -ne 0
604 # we need to preserve history along the other branch
608 if test -n "$identical" && test -z "$copycommit"
612 copy_commit
"$rev" "$tree" "$p" ||
exit $?
617 if ! git diff-index HEAD
--exit-code --quiet 2>&1
619 die
"Working tree has modifications. Cannot add."
621 if ! git diff-index
--cached HEAD
--exit-code --quiet 2>&1
623 die
"Index has modifications. Cannot add."
627 ensure_valid_ref_format
() {
628 git check-ref-format
"refs/heads/$1" ||
629 die
"'$1' does not look like a ref"
632 process_split_commit
() {
637 if test $indent -eq 0
639 revcount
=$
(($revcount + 1))
641 # processing commit without normal parent information;
643 parents
=$
(git rev-parse
"$rev^@")
644 extracount
=$
(($extracount + 1))
647 progress
"$revcount/$revmax ($createcount) [$extracount]"
649 debug
"Processing commit: $rev"
650 exists
=$
(cache_get
"$rev")
653 debug
" prior: $exists"
656 createcount
=$
(($createcount + 1))
657 debug
" parents: $parents"
658 check_parents
"$parents" "$indent"
659 newparents
=$
(cache_get
$parents)
660 debug
" newparents: $newparents"
662 tree
=$
(subtree_for_commit
"$rev" "$dir")
663 debug
" tree is: $tree"
665 # ugly. is there no better way to tell if this is a subtree
666 # vs. a mainline commit? Does it matter?
670 if test -n "$newparents"
672 cache_set
"$rev" "$rev"
677 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
678 debug
" newrev is: $newrev"
679 cache_set
"$rev" "$newrev"
680 cache_set latest_new
"$newrev"
681 cache_set latest_old
"$rev"
687 die
"'$dir' already exists. Cannot add."
694 git rev-parse
-q --verify "$1^{commit}" >/dev
/null ||
695 die
"'$1' does not refer to a commit"
701 # Technically we could accept a refspec here but we're
702 # just going to turn around and add FETCH_HEAD under the
703 # specified directory. Allowing a refspec might be
704 # misleading because we won't do anything with any other
705 # branches fetched via the refspec.
706 ensure_valid_ref_format
"$2"
708 cmd_add_repository
"$@"
710 say
"error: parameters were '$@'"
711 die
"Provide either a commit or a repository and commit."
715 cmd_add_repository
() {
716 echo "git fetch" "$@"
719 git fetch
"$@" ||
exit $?
726 rev=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
727 ensure_single_rev
$rev
729 debug
"Adding $dir as '$rev'..."
730 git read-tree
--prefix="$dir" $rev ||
exit $?
731 git checkout
-- "$dir" ||
exit $?
732 tree
=$
(git write-tree
) ||
exit $?
734 headrev
=$
(git rev-parse HEAD
) ||
exit $?
735 if test -n "$headrev" && test "$headrev" != "$rev"
744 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
745 commit
=$
(add_squashed_msg
"$rev" "$dir" |
746 git commit-tree
"$tree" $headp -p "$rev") ||
exit $?
748 revp
=$
(peel_committish
"$rev") &&
749 commit
=$
(add_msg
"$dir" $headrev "$rev" |
750 git commit-tree
"$tree" $headp -p "$revp") ||
exit $?
752 git
reset "$commit" ||
exit $?
754 say
"Added dir '$dir'"
758 debug
"Splitting $dir..."
759 cache_setup ||
exit $?
763 debug
"Reading history for --onto=$onto..."
767 # the 'onto' history is already just the subdir, so
768 # any parent we find there can be used verbatim
770 cache_set
"$rev" "$rev"
774 unrevs
="$(find_existing_splits "$dir" "$revs")"
776 # We can't restrict rev-list to only $dir here, because some of our
777 # parents have the $dir contents the root, and those won't match.
778 # (and rev-list --follow doesn't seem to solve this)
779 grl
='git rev-list --topo-order --reverse --parents $revs $unrevs'
780 revmax
=$
(eval "$grl" |
wc -l)
785 while read rev parents
787 process_split_commit
"$rev" "$parents" 0
790 latest_new
=$
(cache_get latest_new
)
791 if test -z "$latest_new"
793 die
"No new revisions were found"
798 debug
"Merging split branch into HEAD..."
799 latest_old
=$
(cache_get latest_old
)
801 --allow-unrelated-histories \
802 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
803 "$latest_new" >&2 ||
exit $?
807 if rev_exists
"refs/heads/$branch"
809 if ! rev_is_descendant_of_branch
"$latest_new" "$branch"
811 die
"Branch '$branch' is not an ancestor of commit '$latest_new'."
817 git update-ref
-m 'subtree split' \
818 "refs/heads/$branch" "$latest_new" ||
exit $?
819 say
"$action branch '$branch'"
826 rev=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
827 ensure_single_rev
$rev
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'."