3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
11 git subtree add --prefix=<prefix> <commit>
12 git subtree add --prefix=<prefix> <repository> <ref>
13 git subtree merge --prefix=<prefix> <commit>
14 git subtree pull --prefix=<prefix> <repository> <ref>
15 git subtree push --prefix=<prefix> <repository> <ref>
16 git subtree split --prefix=<prefix> <commit...>
21 P,prefix= the name of the subdir to split out
22 m,message= use the given message as the commit message for the merge commit
24 annotate= add a prefix to commit message of new commits
25 b,branch= create a new branch from the split subtree
26 ignore-joins ignore prior --rejoin commits
27 onto= try connecting new tree to an existing one
28 rejoin merge the new branch back into HEAD
29 options for 'add', 'merge', and 'pull'
30 squash merge subtree changes as a single commit
32 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
34 PATH=$PATH:$(git --exec-path)
53 if [ -n "$debug" ]; then
54 printf "%s\n" "$*" >&2
60 if [ -z "$quiet" ]; then
61 printf "%s\n" "$*" >&2
67 if [ -z "$quiet" ]; then
68 printf "%s\r" "$*" >&2
77 die "assertion failed: " "$@"
84 while [ $# -gt 0 ]; do
90 --annotate) annotate="$1"; shift ;;
91 --no-annotate) annotate= ;;
92 -b) branch="$1"; shift ;;
93 -P) prefix="${1%/}"; shift ;;
94 -m) message="$1"; shift ;;
95 --no-prefix) prefix= ;;
96 --onto) onto="$1"; shift ;;
99 --no-rejoin) rejoin= ;;
100 --ignore-joins) ignore_joins=1 ;;
101 --no-ignore-joins) ignore_joins= ;;
102 --squash) squash=1 ;;
103 --no-squash) squash= ;;
105 *) die "Unexpected option: $opt" ;;
112 add|merge|pull) default= ;;
113 split|push) default="--default HEAD" ;;
114 *) die "Unknown command '$command'" ;;
117 if [ -z "$prefix" ]; then
118 die "You must provide the --prefix option."
122 add) [ -e "$prefix" ] &&
123 die "prefix '$prefix' already exists." ;;
124 *) [ -e "$prefix" ] ||
125 die "'$prefix' does not exist; use 'git subtree add'" ;;
128 dir="$(dirname "$prefix/.")"
130 if [ "$command" != "pull" -a "$command" != "add" -a "$command" != "push" ]; then
131 revs=$(git rev-parse $default --revs-only "$@") || exit $?
132 dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $?
133 if [ -n "$dirs" ]; then
134 die "Error: Use --prefix instead of bare filenames."
138 debug "command: {$command}"
139 debug "quiet: {$quiet}"
140 debug "revs: {$revs}"
147 cachedir="$GIT_DIR/subtree-cache/$$"
148 rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir"
149 mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir"
150 mkdir -p "$cachedir/notree" || die "Can't create new cachedir: $cachedir/notree"
151 debug "Using cachedir: $cachedir" >&2
157 if [ -r "$cachedir/$oldrev" ]; then
158 read newrev <"$cachedir/$oldrev"
167 if [ ! -r "$cachedir/$oldrev" ]; then
175 missed=$(cache_miss $*)
176 for miss in $missed; do
177 if [ ! -r "$cachedir/notree/$miss" ]; then
178 debug " incorrect order: $miss"
185 echo "1" > "$cachedir/notree/$1"
192 if [ "$oldrev" != "latest_old" \
193 -a "$oldrev" != "latest_new" \
194 -a -e "$cachedir/$oldrev" ]; then
195 die "cache for $oldrev already exists!"
197 echo "$newrev" >"$cachedir/$oldrev"
202 if git rev-parse "$1" >/dev/null 2>&1; then
209 rev_is_descendant_of_branch()
213 branch_hash=$(git rev-parse $branch)
214 match=$(git rev-list -1 $branch_hash ^$newrev)
216 if [ -z "$match" ]; then
223 # if a commit doesn't have a parent, this might not work. But we only want
224 # to remove the parent from the rev-list, and since it doesn't exist, it won't
225 # be there anyway, so do nothing in that case.
226 try_remove_previous()
228 if rev_exists "$1^"; then
235 debug "Looking for latest squash ($dir)..."
240 git log --grep="^git-subtree-dir: $dir/*\$" \
241 --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
242 while read a b junk; do
244 debug "{{$sq/$main/$sub}}"
247 git-subtree-mainline:) main="$b" ;;
249 sub="$(git rev-parse "$b^0")" ||
250 die "could not rev-parse split hash $b from commit $sq"
253 if [ -n "$sub" ]; then
254 if [ -n "$main" ]; then
256 # Pretend its sub was a squash.
259 debug "Squash found: $sq $sub"
271 find_existing_splits()
273 debug "Looking for prior splits..."
278 git log --grep="^git-subtree-dir: $dir/*\$" \
279 --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
280 while read a b junk; do
283 git-subtree-mainline:) main="$b" ;;
285 sub="$(git rev-parse "$b^0")" ||
286 die "could not rev-parse split hash $b from commit $sq"
289 debug " Main is: '$main'"
290 if [ -z "$main" -a -n "$sub" ]; then
291 # squash commits refer to a subtree
292 debug " Squash: $sq from $sub"
293 cache_set "$sq" "$sub"
295 if [ -n "$main" -a -n "$sub" ]; then
296 debug " Prior: $main -> $sub"
299 try_remove_previous "$main"
300 try_remove_previous "$sub"
311 # We're going to set some environment vars here, so
312 # do it in a subshell to get rid of them safely later
313 debug copy_commit "{$1}" "{$2}" "{$3}"
314 git log -1 --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
317 read GIT_AUTHOR_EMAIL
319 read GIT_COMMITTER_NAME
320 read GIT_COMMITTER_EMAIL
321 read GIT_COMMITTER_DATE
322 export GIT_AUTHOR_NAME \
326 GIT_COMMITTER_EMAIL \
328 (printf "%s" "$annotate"; cat ) |
329 git commit-tree "$2" $3 # reads the rest of stdin
330 ) || die "Can't copy commit $1"
338 if [ -n "$message" ]; then
339 commit_message="$message"
341 commit_message="Add '$dir/' from commit '$latest_new'"
346 git-subtree-dir: $dir
347 git-subtree-mainline: $latest_old
348 git-subtree-split: $latest_new
354 if [ -n "$message" ]; then
357 echo "Merge commit '$1' as '$2'"
366 if [ -n "$message" ]; then
367 commit_message="$message"
369 commit_message="Split '$dir/' into commit '$latest_new'"
374 git-subtree-dir: $dir
375 git-subtree-mainline: $latest_old
376 git-subtree-split: $latest_new
385 newsub_short=$(git rev-parse --short "$newsub")
387 if [ -n "$oldsub" ]; then
388 oldsub_short=$(git rev-parse --short "$oldsub")
389 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
391 git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
392 git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
394 echo "Squashed '$dir/' content from commit $newsub_short"
398 echo "git-subtree-dir: $dir"
399 echo "git-subtree-split: $newsub"
405 git log -1 --pretty=format:'%T' "$commit" -- || exit $?
412 git ls-tree "$commit" -- "$dir" |
413 while read mode type tree name; do
414 assert [ "$name" = "$dir" ]
415 assert [ "$type" = "tree" -o "$type" = "commit" ]
416 [ "$type" = "commit" ] && continue # ignore submodules
426 if [ $# -ne 1 ]; then
427 return 0 # weird parents, consider it changed
429 ptree=$(toptree_for_commit $1)
430 if [ "$ptree" != "$tree" ]; then
433 return 1 # not changed
443 tree=$(toptree_for_commit $newsub) || exit $?
444 if [ -n "$old" ]; then
445 squash_msg "$dir" "$oldsub" "$newsub" |
446 git commit-tree "$tree" -p "$old" || exit $?
448 squash_msg "$dir" "" "$newsub" |
449 git commit-tree "$tree" || exit $?
458 assert [ -n "$tree" ]
464 for parent in $newparents; do
465 ptree=$(toptree_for_commit $parent) || exit $?
466 [ -z "$ptree" ] && continue
467 if [ "$ptree" = "$tree" ]; then
468 # an identical parent could be used in place of this rev.
471 nonidentical="$parent"
474 # sometimes both old parents map to the same newparent;
475 # eliminate duplicates
477 for gp in $gotparents; do
478 if [ "$gp" = "$parent" ]; then
483 if [ -n "$is_new" ]; then
484 gotparents="$gotparents $parent"
489 if [ -n "$identical" ]; then
492 copy_commit $rev $tree "$p" || exit $?
498 if ! git diff-index HEAD --exit-code --quiet 2>&1; then
499 die "Working tree has modifications. Cannot add."
501 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1; then
502 die "Index has modifications. Cannot add."
506 ensure_valid_ref_format()
508 git check-ref-format "refs/heads/$1" ||
509 die "'$1' does not look like a ref"
514 if [ -e "$dir" ]; then
515 die "'$dir' already exists. Cannot add."
520 if [ $# -eq 1 ]; then
521 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
522 die "'$1' does not refer to a commit"
524 "cmd_add_commit" "$@"
525 elif [ $# -eq 2 ]; then
526 # Technically we could accept a refspec here but we're
527 # just going to turn around and add FETCH_HEAD under the
528 # specified directory. Allowing a refspec might be
529 # misleading because we won't do anything with any other
530 # branches fetched via the refspec.
531 ensure_valid_ref_format "$2"
533 "cmd_add_repository" "$@"
535 say "error: parameters were '$@'"
536 die "Provide either a commit or a repository and commit."
542 echo "git fetch" "$@"
545 git fetch "$@" || exit $?
553 revs=$(git rev-parse $default --revs-only "$@") || exit $?
557 debug "Adding $dir as '$rev'..."
558 git read-tree --prefix="$dir" $rev || exit $?
559 git checkout -- "$dir" || exit $?
560 tree=$(git write-tree) || exit $?
562 headrev=$(git rev-parse HEAD) || exit $?
563 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
569 if [ -n "$squash" ]; then
570 rev=$(new_squash_commit "" "" "$rev") || exit $?
571 commit=$(add_squashed_msg "$rev" "$dir" |
572 git commit-tree $tree $headp -p "$rev") || exit $?
574 revp=$(peel_committish "$rev") &&
575 commit=$(add_msg "$dir" "$headrev" "$rev" |
576 git commit-tree $tree $headp -p "$revp") || exit $?
578 git reset "$commit" || exit $?
580 say "Added dir '$dir'"
585 debug "Splitting $dir..."
586 cache_setup || exit $?
588 if [ -n "$onto" ]; then
589 debug "Reading history for --onto=$onto..."
592 # the 'onto' history is already just the subdir, so
593 # any parent we find there can be used verbatim
599 if [ -n "$ignore_joins" ]; then
602 unrevs="$(find_existing_splits "$dir" "$revs")"
605 # We can't restrict rev-list to only $dir here, because some of our
606 # parents have the $dir contents the root, and those won't match.
607 # (and rev-list --follow doesn't seem to solve this)
608 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
609 revmax=$(eval "$grl" | wc -l)
613 while read rev parents; do
614 revcount=$(($revcount + 1))
615 progress "$revcount/$revmax ($createcount)"
616 debug "Processing commit: $rev"
617 exists=$(cache_get $rev)
618 if [ -n "$exists" ]; then
619 debug " prior: $exists"
622 createcount=$(($createcount + 1))
623 debug " parents: $parents"
624 newparents=$(cache_get $parents)
625 debug " newparents: $newparents"
627 tree=$(subtree_for_commit $rev "$dir")
628 debug " tree is: $tree"
630 check_parents $parents
632 # ugly. is there no better way to tell if this is a subtree
633 # vs. a mainline commit? Does it matter?
634 if [ -z $tree ]; then
636 if [ -n "$newparents" ]; then
642 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
643 debug " newrev is: $newrev"
644 cache_set $rev $newrev
645 cache_set latest_new $newrev
646 cache_set latest_old $rev
648 latest_new=$(cache_get latest_new)
649 if [ -z "$latest_new" ]; then
650 die "No new revisions were found"
653 if [ -n "$rejoin" ]; then
654 debug "Merging split branch into HEAD..."
655 latest_old=$(cache_get latest_old)
657 -m "$(rejoin_msg "$dir" $latest_old $latest_new)" \
658 $latest_new >&2 || exit $?
660 if [ -n "$branch" ]; then
661 if rev_exists "refs/heads/$branch"; then
662 if ! rev_is_descendant_of_branch $latest_new $branch; then
663 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
669 git update-ref -m 'subtree split' "refs/heads/$branch" $latest_new || exit $?
670 say "$action branch '$branch'"
678 revs=$(git rev-parse $default --revs-only "$@") || exit $?
682 if [ $# -ne 1 ]; then
683 die "You must provide exactly one revision. Got: '$revs'"
687 if [ -n "$squash" ]; then
688 first_split="$(find_latest_squash "$dir")"
689 if [ -z "$first_split" ]; then
690 die "Can't squash-merge: '$dir' was never added."
695 if [ "$sub" = "$rev" ]; then
696 say "Subtree is already at commit $rev."
699 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
700 debug "New squash commit: $new"
704 version=$(git version)
705 if [ "$version" \< "git version 1.7" ]; then
706 if [ -n "$message" ]; then
707 git merge -s subtree --message="$message" $rev
709 git merge -s subtree $rev
712 if [ -n "$message" ]; then
713 git merge -Xsubtree="$prefix" --message="$message" $rev
715 git merge -Xsubtree="$prefix" $rev
722 if [ $# -ne 2 ]; then
723 die "You must provide <repository> <ref>"
726 ensure_valid_ref_format "$2"
727 git fetch "$@" || exit $?
735 if [ $# -ne 2 ]; then
736 die "You must provide <repository> <ref>"
738 ensure_valid_ref_format "$2"
739 if [ -e "$dir" ]; then
742 echo "git push using: " $repository $refspec
743 localrev=$(git subtree split --prefix="$prefix") || die
744 git push "$repository" $localrev:refs/heads/$refspec
746 die "'$dir' must already exist. Try 'git subtree add'."