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 prefix= the name of the subdir to split out
21 annotate= add a prefix to commit message of new commits
22 b,branch= create a new branch from the split subtree
23 ignore-joins ignore prior --rejoin commits
24 onto= try connecting new tree to an existing one
25 rejoin merge the new branch back into HEAD
26 options for 'add', 'merge', and 'pull'
27 squash merge subtree changes as a single commit
29 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
30 PATH
=$
(git
--exec-path):$PATH
46 if [ -n "$debug" ]; then
53 if [ -z "$quiet" ]; then
63 die
"assertion failed: " "$@"
70 while [ $# -gt 0 ]; do
76 --annotate) annotate
="$1"; shift ;;
77 --no-annotate) annotate
= ;;
78 -b) branch
="$1"; shift ;;
79 --prefix) prefix
="$1"; shift ;;
80 --no-prefix) prefix
= ;;
81 --onto) onto
="$1"; shift ;;
84 --no-rejoin) rejoin
= ;;
85 --ignore-joins) ignore_joins
=1 ;;
86 --no-ignore-joins) ignore_joins
= ;;
88 --no-squash) squash
= ;;
90 *) die
"Unexpected option: $opt" ;;
97 add|merge|pull
) default
= ;;
98 split) default
="--default HEAD" ;;
99 *) die
"Unknown command '$command'" ;;
102 if [ -z "$prefix" ]; then
103 die
"You must provide the --prefix option."
105 dir
="$(dirname "$prefix/.
")"
107 if [ "$command" != "pull" ]; then
108 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
109 dirs="$(git rev-parse --no-revs --no-flags "$@
")" ||
exit $?
110 if [ -n "$dirs" ]; then
111 die
"Error: Use --prefix instead of bare filenames."
115 debug
"command: {$command}"
116 debug
"quiet: {$quiet}"
117 debug
"revs: {$revs}"
124 cachedir
="$GIT_DIR/subtree-cache/$$"
125 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
126 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
127 debug
"Using cachedir: $cachedir" >&2
133 if [ -r "$cachedir/$oldrev" ]; then
134 read newrev
<"$cachedir/$oldrev"
144 if [ "$oldrev" != "latest_old" \
145 -a "$oldrev" != "latest_new" \
146 -a -e "$cachedir/$oldrev" ]; then
147 die
"cache for $oldrev already exists!"
149 echo "$newrev" >"$cachedir/$oldrev"
154 if git rev-parse
"$1" >/dev
/null
2>&1; then
161 # if a commit doesn't have a parent, this might not work. But we only want
162 # to remove the parent from the rev-list, and since it doesn't exist, it won't
163 # be there anyway, so do nothing in that case.
164 try_remove_previous
()
166 if rev_exists
"$1^"; then
173 debug
"Looking for latest squash ($dir)..."
178 git log
--grep="^git-subtree-dir: $dir/*\$" \
179 --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
180 while read a b junk
; do
182 debug
"{{$sq/$main/$sub}}"
185 git-subtree-mainline
:) main
="$b" ;;
186 git-subtree-split
:) sub
="$b" ;;
188 if [ -n "$sub" ]; then
189 if [ -n "$main" ]; then
191 # Pretend its sub was a squash.
194 debug
"Squash found: $sq $sub"
206 find_existing_splits
()
208 debug
"Looking for prior splits..."
213 git log
--grep="^git-subtree-dir: $dir/*\$" \
214 --pretty=format
:'START %H%n%s%n%n%b%nEND%n' $revs |
215 while read a b junk
; do
218 git-subtree-mainline
:) main
="$b" ;;
219 git-subtree-split
:) sub
="$b" ;;
221 debug
" Main is: '$main'"
222 if [ -z "$main" -a -n "$sub" ]; then
223 # squash commits refer to a subtree
224 debug
" Squash: $sq from $sub"
225 cache_set
"$sq" "$sub"
227 if [ -n "$main" -a -n "$sub" ]; then
228 debug
" Prior: $main -> $sub"
230 try_remove_previous
"$main"
231 try_remove_previous
"$sub"
242 # We're going to set some environment vars here, so
243 # do it in a subshell to get rid of them safely later
244 debug copy_commit
"{$1}" "{$2}" "{$3}"
245 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
248 read GIT_AUTHOR_EMAIL
250 read GIT_COMMITTER_NAME
251 read GIT_COMMITTER_EMAIL
252 read GIT_COMMITTER_DATE
253 export GIT_AUTHOR_NAME \
257 GIT_COMMITTER_EMAIL \
259 (echo -n "$annotate"; cat ) |
260 git commit-tree
"$2" $3 # reads the rest of stdin
261 ) || die
"Can't copy commit $1"
270 Add '$dir/' from commit '$latest_new'
272 git-subtree-dir: $dir
273 git-subtree-mainline: $latest_old
274 git-subtree-split: $latest_new
284 Split '$dir/' into commit '$latest_new'
286 git-subtree-dir: $dir
287 git-subtree-mainline: $latest_old
288 git-subtree-split: $latest_new
297 newsub_short
=$
(git rev-parse
--short "$newsub")
299 if [ -n "$oldsub" ]; then
300 oldsub_short
=$
(git rev-parse
--short "$oldsub")
301 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
303 git log
--pretty=tformat
:'%h %s' "$oldsub..$newsub"
304 git log
--pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
306 echo "Squashed '$dir/' content from commit $newsub_short"
310 echo "git-subtree-dir: $dir"
311 echo "git-subtree-split: $newsub"
317 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
324 git ls-tree
"$commit" -- "$dir" |
325 while read mode
type tree name
; do
326 assert
[ "$name" = "$dir" ]
327 assert
[ "$type" = "tree" ]
337 if [ $# -ne 1 ]; then
338 return 0 # weird parents, consider it changed
340 ptree
=$
(toptree_for_commit
$1)
341 if [ "$ptree" != "$tree" ]; then
344 return 1 # not changed
354 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
355 if [ -n "$old" ]; then
356 squash_msg
"$dir" "$oldsub" "$newsub" |
357 git commit-tree
"$tree" -p "$old" ||
exit $?
359 squash_msg
"$dir" "" "$newsub" |
360 git commit-tree
"$tree" ||
exit $?
369 assert
[ -n "$tree" ]
375 for parent
in $newparents; do
376 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
377 [ -z "$ptree" ] && continue
378 if [ "$ptree" = "$tree" ]; then
379 # an identical parent could be used in place of this rev.
382 nonidentical
="$parent"
385 # sometimes both old parents map to the same newparent;
386 # eliminate duplicates
388 for gp
in $gotparents; do
389 if [ "$gp" = "$parent" ]; then
394 if [ -n "$is_new" ]; then
395 gotparents
="$gotparents $parent"
400 if [ -n "$identical" ]; then
403 copy_commit
$rev $tree "$p" ||
exit $?
409 if ! git diff-index HEAD
--exit-code --quiet; then
410 die
"Working tree has modifications. Cannot add."
412 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
413 die
"Index has modifications. Cannot add."
419 if [ -e "$dir" ]; then
420 die
"'$dir' already exists. Cannot add."
425 if [ $# -ne 1 ]; then
426 die
"You must provide exactly one revision. Got: '$revs'"
430 debug
"Adding $dir as '$rev'..."
431 git read-tree
--prefix="$dir" $rev ||
exit $?
432 git checkout
-- "$dir" ||
exit $?
433 tree
=$
(git write-tree
) ||
exit $?
435 headrev
=$
(git rev-parse HEAD
) ||
exit $?
436 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
442 if [ -n "$squash" ]; then
443 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
444 commit
=$
(echo "Merge commit '$rev' as '$dir'" |
445 git commit-tree
$tree $headp -p "$rev") ||
exit $?
447 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
448 git commit-tree
$tree $headp -p "$rev") ||
exit $?
450 git
reset "$commit" ||
exit $?
452 say
"Added dir '$dir'"
457 if [ -n "$branch" ] && rev_exists
"refs/heads/$branch"; then
458 die
"Branch '$branch' already exists."
461 debug
"Splitting $dir..."
462 cache_setup ||
exit $?
464 if [ -n "$onto" ]; then
465 debug
"Reading history for --onto=$onto..."
468 # the 'onto' history is already just the subdir, so
469 # any parent we find there can be used verbatim
475 if [ -n "$ignore_joins" ]; then
478 unrevs
="$(find_existing_splits "$dir" "$revs")"
481 # We can't restrict rev-list to only $dir here, because some of our
482 # parents have the $dir contents the root, and those won't match.
483 # (and rev-list --follow doesn't seem to solve this)
484 grl
='git rev-list --reverse --parents $revs $unrevs'
485 revmax
=$
(eval "$grl" |
wc -l)
489 while read rev parents
; do
490 revcount
=$
(($revcount + 1))
491 say
-n "$revcount/$revmax ($createcount)
"
492 debug
"Processing commit: $rev"
493 exists
=$
(cache_get
$rev)
494 if [ -n "$exists" ]; then
495 debug
" prior: $exists"
498 createcount
=$
(($createcount + 1))
499 debug
" parents: $parents"
500 newparents
=$
(cache_get
$parents)
501 debug
" newparents: $newparents"
503 tree
=$
(subtree_for_commit
$rev "$dir")
504 debug
" tree is: $tree"
506 # ugly. is there no better way to tell if this is a subtree
507 # vs. a mainline commit? Does it matter?
508 [ -z $tree ] && continue
510 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
511 debug
" newrev is: $newrev"
512 cache_set
$rev $newrev
513 cache_set latest_new
$newrev
514 cache_set latest_old
$rev
516 latest_new
=$
(cache_get latest_new
)
517 if [ -z "$latest_new" ]; then
518 die
"No new revisions were found"
521 if [ -n "$rejoin" ]; then
522 debug
"Merging split branch into HEAD..."
523 latest_old
=$
(cache_get latest_old
)
525 -m "$(rejoin_msg $dir $latest_old $latest_new)" \
526 $latest_new >&2 ||
exit $?
528 if [ -n "$branch" ]; then
529 git update-ref
-m 'subtree split' "refs/heads/$branch" \
530 $latest_new "" ||
exit $?
531 say
"Created branch '$branch'"
542 if [ $# -ne 1 ]; then
543 die
"You must provide exactly one revision. Got: '$revs'"
547 if [ -n "$squash" ]; then
548 first_split
="$(find_latest_squash "$dir")"
549 if [ -z "$first_split" ]; then
550 die
"Can't squash-merge: '$dir' was never added."
555 if [ "$sub" = "$rev" ]; then
556 say
"Subtree is already at commit $rev."
559 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
560 debug
"New squash commit: $new"
564 git merge
-s subtree
$rev
570 git fetch
"$@" ||
exit $?