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 merge --prefix=<prefix> <commit>
13 git subtree pull --prefix=<prefix> <repository> <refspec...>
14 git subtree split --prefix=<prefix> <commit...>
19 P,prefix= the name of the subdir to split out
20 m,message= use the given message as the commit message for the merge commit
22 annotate= add a prefix to commit message of new commits
23 b,branch= create a new branch from the split subtree
24 ignore-joins ignore prior --rejoin commits
25 onto= try connecting new tree to an existing one
26 rejoin merge the new branch back into HEAD
27 options for 'add', 'merge', and 'pull'
28 squash merge subtree changes as a single commit
30 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
31 PATH
=$
(git
--exec-path):$PATH
48 if [ -n "$debug" ]; then
55 if [ -z "$quiet" ]; then
65 die
"assertion failed: " "$@"
72 while [ $# -gt 0 ]; do
78 --annotate) annotate
="$1"; shift ;;
79 --no-annotate) annotate
= ;;
80 -b) branch
="$1"; shift ;;
81 -P) prefix
="$1"; shift ;;
82 -m) message
="$1"; shift ;;
83 --no-prefix) prefix
= ;;
84 --onto) onto
="$1"; shift ;;
87 --no-rejoin) rejoin
= ;;
88 --ignore-joins) ignore_joins
=1 ;;
89 --no-ignore-joins) ignore_joins
= ;;
91 --no-squash) squash
= ;;
93 *) die
"Unexpected option: $opt" ;;
100 add|merge|pull
) default
= ;;
101 split) default
="--default HEAD" ;;
102 *) die
"Unknown command '$command'" ;;
105 if [ -z "$prefix" ]; then
106 die
"You must provide the --prefix option."
108 dir
="$(dirname "$prefix/.
")"
110 if [ "$command" != "pull" ]; then
111 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
112 dirs="$(git rev-parse --no-revs --no-flags "$@
")" ||
exit $?
113 if [ -n "$dirs" ]; then
114 die
"Error: Use --prefix instead of bare filenames."
118 debug
"command: {$command}"
119 debug
"quiet: {$quiet}"
120 debug
"revs: {$revs}"
127 cachedir
="$GIT_DIR/subtree-cache/$$"
128 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
129 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
130 debug
"Using cachedir: $cachedir" >&2
136 if [ -r "$cachedir/$oldrev" ]; then
137 read newrev
<"$cachedir/$oldrev"
147 if [ "$oldrev" != "latest_old" \
148 -a "$oldrev" != "latest_new" \
149 -a -e "$cachedir/$oldrev" ]; then
150 die
"cache for $oldrev already exists!"
152 echo "$newrev" >"$cachedir/$oldrev"
157 if git rev-parse
"$1" >/dev
/null
2>&1; then
164 rev_is_descendant_of_branch
()
168 branch_hash
=$
(git rev-parse
$branch)
169 match
=$
(git rev-list
-1 $branch_hash ^
$newrev)
171 if [ -z "$match" ]; then
178 # if a commit doesn't have a parent, this might not work. But we only want
179 # to remove the parent from the rev-list, and since it doesn't exist, it won't
180 # be there anyway, so do nothing in that case.
181 try_remove_previous
()
183 if rev_exists
"$1^"; then
190 debug
"Looking for latest squash ($dir)..."
195 git log
--grep="^git-subtree-dir: $dir/*\$" \
196 --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
197 while read a b junk
; do
199 debug
"{{$sq/$main/$sub}}"
202 git-subtree-mainline
:) main
="$b" ;;
203 git-subtree-split
:) sub
="$b" ;;
205 if [ -n "$sub" ]; then
206 if [ -n "$main" ]; then
208 # Pretend its sub was a squash.
211 debug
"Squash found: $sq $sub"
223 find_existing_splits
()
225 debug
"Looking for prior splits..."
230 git log
--grep="^git-subtree-dir: $dir/*\$" \
231 --pretty=format
:'START %H%n%s%n%n%b%nEND%n' $revs |
232 while read a b junk
; do
235 git-subtree-mainline
:) main
="$b" ;;
236 git-subtree-split
:) sub
="$b" ;;
238 debug
" Main is: '$main'"
239 if [ -z "$main" -a -n "$sub" ]; then
240 # squash commits refer to a subtree
241 debug
" Squash: $sq from $sub"
242 cache_set
"$sq" "$sub"
244 if [ -n "$main" -a -n "$sub" ]; then
245 debug
" Prior: $main -> $sub"
247 try_remove_previous
"$main"
248 try_remove_previous
"$sub"
259 # We're going to set some environment vars here, so
260 # do it in a subshell to get rid of them safely later
261 debug copy_commit
"{$1}" "{$2}" "{$3}"
262 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
265 read GIT_AUTHOR_EMAIL
267 read GIT_COMMITTER_NAME
268 read GIT_COMMITTER_EMAIL
269 read GIT_COMMITTER_DATE
270 export GIT_AUTHOR_NAME \
274 GIT_COMMITTER_EMAIL \
276 (echo -n "$annotate"; cat ) |
277 git commit-tree
"$2" $3 # reads the rest of stdin
278 ) || die
"Can't copy commit $1"
286 if [ -n "$message" ]; then
287 commit_message
="$message"
289 commit_message
="Add '$dir/' from commit '$latest_new'"
294 git-subtree-dir: $dir
295 git-subtree-mainline: $latest_old
296 git-subtree-split: $latest_new
302 if [ -n "$message" ]; then
305 echo "Merge commit '$1' as '$2'"
314 if [ -n "$message" ]; then
315 commit_message
="$message"
317 commit_message
="Split '$dir/' into commit '$latest_new'"
322 git-subtree-dir: $dir
323 git-subtree-mainline: $latest_old
324 git-subtree-split: $latest_new
333 newsub_short
=$
(git rev-parse
--short "$newsub")
335 if [ -n "$oldsub" ]; then
336 oldsub_short
=$
(git rev-parse
--short "$oldsub")
337 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
339 git log
--pretty=tformat
:'%h %s' "$oldsub..$newsub"
340 git log
--pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
342 echo "Squashed '$dir/' content from commit $newsub_short"
346 echo "git-subtree-dir: $dir"
347 echo "git-subtree-split: $newsub"
353 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
360 git ls-tree
"$commit" -- "$dir" |
361 while read mode
type tree name
; do
362 assert
[ "$name" = "$dir" ]
363 assert
[ "$type" = "tree" ]
373 if [ $# -ne 1 ]; then
374 return 0 # weird parents, consider it changed
376 ptree
=$
(toptree_for_commit
$1)
377 if [ "$ptree" != "$tree" ]; then
380 return 1 # not changed
390 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
391 if [ -n "$old" ]; then
392 squash_msg
"$dir" "$oldsub" "$newsub" |
393 git commit-tree
"$tree" -p "$old" ||
exit $?
395 squash_msg
"$dir" "" "$newsub" |
396 git commit-tree
"$tree" ||
exit $?
405 assert
[ -n "$tree" ]
411 for parent
in $newparents; do
412 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
413 [ -z "$ptree" ] && continue
414 if [ "$ptree" = "$tree" ]; then
415 # an identical parent could be used in place of this rev.
418 nonidentical
="$parent"
421 # sometimes both old parents map to the same newparent;
422 # eliminate duplicates
424 for gp
in $gotparents; do
425 if [ "$gp" = "$parent" ]; then
430 if [ -n "$is_new" ]; then
431 gotparents
="$gotparents $parent"
436 if [ -n "$identical" ]; then
439 copy_commit
$rev $tree "$p" ||
exit $?
445 if ! git diff-index HEAD
--exit-code --quiet; then
446 die
"Working tree has modifications. Cannot add."
448 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
449 die
"Index has modifications. Cannot add."
455 if [ -e "$dir" ]; then
456 die
"'$dir' already exists. Cannot add."
461 if [ $# -ne 1 ]; then
462 die
"You must provide exactly one revision. Got: '$revs'"
466 debug
"Adding $dir as '$rev'..."
467 git read-tree
--prefix="$dir" $rev ||
exit $?
468 git checkout
-- "$dir" ||
exit $?
469 tree
=$
(git write-tree
) ||
exit $?
471 headrev
=$
(git rev-parse HEAD
) ||
exit $?
472 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
478 if [ -n "$squash" ]; then
479 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
480 commit
=$
(add_squashed_msg
"$rev" "$dir" |
481 git commit-tree
$tree $headp -p "$rev") ||
exit $?
483 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
484 git commit-tree
$tree $headp -p "$rev") ||
exit $?
486 git
reset "$commit" ||
exit $?
488 say
"Added dir '$dir'"
493 debug
"Splitting $dir..."
494 cache_setup ||
exit $?
496 if [ -n "$onto" ]; then
497 debug
"Reading history for --onto=$onto..."
500 # the 'onto' history is already just the subdir, so
501 # any parent we find there can be used verbatim
507 if [ -n "$ignore_joins" ]; then
510 unrevs
="$(find_existing_splits "$dir" "$revs")"
513 # We can't restrict rev-list to only $dir here, because some of our
514 # parents have the $dir contents the root, and those won't match.
515 # (and rev-list --follow doesn't seem to solve this)
516 grl
='git rev-list --reverse --parents $revs $unrevs'
517 revmax
=$
(eval "$grl" |
wc -l)
521 while read rev parents
; do
522 revcount
=$
(($revcount + 1))
523 say
-n "$revcount/$revmax ($createcount)
"
524 debug
"Processing commit: $rev"
525 exists
=$
(cache_get
$rev)
526 if [ -n "$exists" ]; then
527 debug
" prior: $exists"
530 createcount
=$
(($createcount + 1))
531 debug
" parents: $parents"
532 newparents
=$
(cache_get
$parents)
533 debug
" newparents: $newparents"
535 tree
=$
(subtree_for_commit
$rev "$dir")
536 debug
" tree is: $tree"
538 # ugly. is there no better way to tell if this is a subtree
539 # vs. a mainline commit? Does it matter?
540 if [ -z $tree ]; then
545 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
546 debug
" newrev is: $newrev"
547 cache_set
$rev $newrev
548 cache_set latest_new
$newrev
549 cache_set latest_old
$rev
551 latest_new
=$
(cache_get latest_new
)
552 if [ -z "$latest_new" ]; then
553 die
"No new revisions were found"
556 if [ -n "$rejoin" ]; then
557 debug
"Merging split branch into HEAD..."
558 latest_old
=$
(cache_get latest_old
)
560 -m "$(rejoin_msg $dir $latest_old $latest_new)" \
561 $latest_new >&2 ||
exit $?
563 if [ -n "$branch" ]; then
564 if rev_exists
"refs/heads/$branch"; then
565 if ! rev_is_descendant_of_branch
$latest_new $branch; then
566 die
"Branch '$branch' is not an ancestor of commit '$latest_new'."
572 git update-ref
-m 'subtree split' "refs/heads/$branch" $latest_new ||
exit $?
573 say
"$action branch '$branch'"
584 if [ $# -ne 1 ]; then
585 die
"You must provide exactly one revision. Got: '$revs'"
589 if [ -n "$squash" ]; then
590 first_split
="$(find_latest_squash "$dir")"
591 if [ -z "$first_split" ]; then
592 die
"Can't squash-merge: '$dir' was never added."
597 if [ "$sub" = "$rev" ]; then
598 say
"Subtree is already at commit $rev."
601 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
602 debug
"New squash commit: $new"
606 git merge
-s subtree
--message="$message" $rev
612 git fetch
"$@" ||
exit $?