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 $?
)
45 if [ -n "$debug" ]; then
52 if [ -z "$quiet" ]; then
62 die
"assertion failed: " "$@"
69 while [ $# -gt 0 ]; do
75 --annotate) annotate
="$1"; shift ;;
76 --no-annotate) annotate
= ;;
77 -b) branch
="$1"; shift ;;
78 --prefix) prefix
="$1"; shift ;;
79 --no-prefix) prefix
= ;;
80 --onto) onto
="$1"; shift ;;
83 --no-rejoin) rejoin
= ;;
84 --ignore-joins) ignore_joins
=1 ;;
85 --no-ignore-joins) ignore_joins
= ;;
87 --no-squash) squash
= ;;
89 *) die
"Unexpected option: $opt" ;;
96 add|merge|pull
) default
= ;;
97 split) default
="--default HEAD" ;;
98 *) die
"Unknown command '$command'" ;;
101 if [ -z "$prefix" ]; then
102 die
"You must provide the --prefix option."
106 if [ "$command" != "pull" ]; then
107 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
108 dirs="$(git rev-parse --no-revs --no-flags "$@
")" ||
exit $?
109 if [ -n "$dirs" ]; then
110 die
"Error: Use --prefix instead of bare filenames."
114 debug
"command: {$command}"
115 debug
"quiet: {$quiet}"
116 debug
"revs: {$revs}"
123 cachedir
="$GIT_DIR/subtree-cache/$$"
124 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
125 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
126 debug
"Using cachedir: $cachedir" >&2
132 if [ -r "$cachedir/$oldrev" ]; then
133 read newrev
<"$cachedir/$oldrev"
143 if [ "$oldrev" != "latest_old" \
144 -a "$oldrev" != "latest_new" \
145 -a -e "$cachedir/$oldrev" ]; then
146 die
"cache for $oldrev already exists!"
148 echo "$newrev" >"$cachedir/$oldrev"
153 if git rev-parse
"$1" >/dev
/null
2>&1; then
160 # if a commit doesn't have a parent, this might not work. But we only want
161 # to remove the parent from the rev-list, and since it doesn't exist, it won't
162 # be there anyway, so do nothing in that case.
163 try_remove_previous
()
165 if rev_exists
"$1^"; then
172 debug
"Looking for latest squash ($dir)..."
177 git log
--grep="^git-subtree-dir: $dir\$" \
178 --pretty=format
:'START %H%n%s%n%n%b%nEND%n' HEAD |
179 while read a b junk
; do
181 debug
"{{$sq/$main/$sub}}"
184 git-subtree-mainline
:) main
="$b" ;;
185 git-subtree-split
:) sub
="$b" ;;
187 if [ -n "$sub" ]; then
188 if [ -n "$main" ]; then
190 # Pretend its sub was a squash.
193 debug
"Squash found: $sq $sub"
205 find_existing_splits
()
207 debug
"Looking for prior splits..."
212 git log
--grep="^git-subtree-dir: $dir\$" \
213 --pretty=format
:'START %H%n%s%n%n%b%nEND%n' $revs |
214 while read a b junk
; do
216 START
) main
="$b"; sq="$b" ;;
217 git-subtree-mainline
:) main
="$b" ;;
218 git-subtree-split
:) sub
="$b" ;;
220 if [ -z "$main" -a -n "$sub" ]; then
221 # squash commits refer to a subtree
222 cache_set
"$sq" "$sub"
224 if [ -n "$main" -a -n "$sub" ]; then
225 debug
" Prior: $main -> $sub"
227 try_remove_previous
"$main"
228 try_remove_previous
"$sub"
239 # We're going to set some environment vars here, so
240 # do it in a subshell to get rid of them safely later
241 debug copy_commit
"{$1}" "{$2}" "{$3}"
242 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
245 read GIT_AUTHOR_EMAIL
247 read GIT_COMMITTER_NAME
248 read GIT_COMMITTER_EMAIL
249 read GIT_COMMITTER_DATE
250 export GIT_AUTHOR_NAME \
254 GIT_COMMITTER_EMAIL \
256 (echo -n "$annotate"; cat ) |
257 git commit-tree
"$2" $3 # reads the rest of stdin
258 ) || die
"Can't copy commit $1"
267 Add '$dir/' from commit '$latest_new'
269 git-subtree-dir: $dir
270 git-subtree-mainline: $latest_old
271 git-subtree-split: $latest_new
281 Split '$dir/' into commit '$latest_new'
283 git-subtree-dir: $dir
284 git-subtree-mainline: $latest_old
285 git-subtree-split: $latest_new
294 newsub_short
=$
(git rev-parse
--short "$newsub")
296 if [ -n "$oldsub" ]; then
297 oldsub_short
=$
(git rev-parse
--short "$oldsub")
298 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
300 git log
--pretty=tformat
:'%h %s' "$oldsub..$newsub"
301 git log
--pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
303 echo "Squashed '$dir/' content from commit $newsub_short"
307 echo "git-subtree-dir: $dir"
308 echo "git-subtree-split: $newsub"
314 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
321 git ls-tree
"$commit" -- "$dir" |
322 while read mode
type tree name
; do
323 assert
[ "$name" = "$dir" ]
333 if [ $# -ne 1 ]; then
334 return 0 # weird parents, consider it changed
336 ptree
=$
(toptree_for_commit
$1)
337 if [ "$ptree" != "$tree" ]; then
340 return 1 # not changed
350 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
351 if [ -n "$old" ]; then
352 squash_msg
"$dir" "$oldsub" "$newsub" |
353 git commit-tree
"$tree" -p "$old" ||
exit $?
355 squash_msg
"$dir" "" "$newsub" |
356 git commit-tree
"$tree" ||
exit $?
365 assert
[ -n "$tree" ]
371 for parent
in $newparents; do
372 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
373 [ -z "$ptree" ] && continue
374 if [ "$ptree" = "$tree" ]; then
375 # an identical parent could be used in place of this rev.
378 nonidentical
="$parent"
381 # sometimes both old parents map to the same newparent;
382 # eliminate duplicates
384 for gp
in $gotparents; do
385 if [ "$gp" = "$parent" ]; then
390 if [ -n "$is_new" ]; then
391 gotparents
="$gotparents $parent"
396 if [ -n "$identical" ]; then
399 copy_commit
$rev $tree "$p" ||
exit $?
405 if ! git diff-index HEAD
--exit-code --quiet; then
406 die
"Working tree has modifications. Cannot add."
408 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
409 die
"Index has modifications. Cannot add."
415 if [ -e "$dir" ]; then
416 die
"'$dir' already exists. Cannot add."
421 if [ $# -ne 1 ]; then
422 die
"You must provide exactly one revision. Got: '$revs'"
426 debug
"Adding $dir as '$rev'..."
427 git read-tree
--prefix="$dir" $rev ||
exit $?
428 git checkout
"$dir" ||
exit $?
429 tree
=$
(git write-tree
) ||
exit $?
431 headrev
=$
(git rev-parse HEAD
) ||
exit $?
432 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
438 if [ -n "$squash" ]; then
439 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
440 commit
=$
(echo "Merge commit '$rev' as '$dir'" |
441 git commit-tree
$tree $headp -p "$rev") ||
exit $?
443 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
444 git commit-tree
$tree $headp -p "$rev") ||
exit $?
446 git
reset "$commit" ||
exit $?
448 say
"Added dir '$dir'"
453 if [ -n "$branch" ] && rev_exists
"refs/heads/$branch"; then
454 die
"Branch '$branch' already exists."
457 debug
"Splitting $dir..."
458 cache_setup ||
exit $?
460 if [ -n "$onto" ]; then
461 debug
"Reading history for --onto=$onto..."
464 # the 'onto' history is already just the subdir, so
465 # any parent we find there can be used verbatim
471 if [ -n "$ignore_joins" ]; then
474 unrevs
="$(find_existing_splits "$dir" "$revs")"
477 # We can't restrict rev-list to only $dir here, because some of our
478 # parents have the $dir contents the root, and those won't match.
479 # (and rev-list --follow doesn't seem to solve this)
480 grl
='git rev-list --reverse --parents $revs $unrevs'
481 revmax
=$
(eval "$grl" |
wc -l)
485 while read rev parents
; do
486 revcount
=$
(($revcount + 1))
487 say
-n "$revcount/$revmax ($createcount)
"
488 debug
"Processing commit: $rev"
489 exists
=$
(cache_get
$rev)
490 if [ -n "$exists" ]; then
491 debug
" prior: $exists"
494 createcount
=$
(($createcount + 1))
495 debug
" parents: $parents"
496 newparents
=$
(cache_get
$parents)
497 debug
" newparents: $newparents"
499 tree
=$
(subtree_for_commit
$rev "$dir")
500 debug
" tree is: $tree"
502 # ugly. is there no better way to tell if this is a subtree
503 # vs. a mainline commit? Does it matter?
504 [ -z $tree ] && continue
506 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
507 debug
" newrev is: $newrev"
508 cache_set
$rev $newrev
509 cache_set latest_new
$newrev
510 cache_set latest_old
$rev
512 latest_new
=$
(cache_get latest_new
)
513 if [ -z "$latest_new" ]; then
514 die
"No new revisions were found"
517 if [ -n "$rejoin" ]; then
518 debug
"Merging split branch into HEAD..."
519 latest_old
=$
(cache_get latest_old
)
521 -m "$(rejoin_msg $dir $latest_old $latest_new)" \
522 $latest_new >&2 ||
exit $?
524 if [ -n "$branch" ]; then
525 git update-ref
-m 'subtree split' "refs/heads/$branch" \
526 $latest_new "" ||
exit $?
527 say
"Created branch '$branch'"
538 if [ $# -ne 1 ]; then
539 die
"You must provide exactly one revision. Got: '$revs'"
543 if [ -n "$squash" ]; then
544 first_split
="$(find_latest_squash "$dir")"
545 if [ -z "$first_split" ]; then
546 die
"Can't squash-merge: '$dir' was never added."
551 if [ "$sub" = "$rev" ]; then
552 say
"Subtree is already at commit $rev."
555 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
556 debug
"New squash commit: $new"
560 git merge
-s subtree
$rev
567 git pull
-s subtree
"$@"