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."
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
217 START
) main
="$b"; sq="$b" ;;
218 git-subtree-mainline
:) main
="$b" ;;
219 git-subtree-split
:) sub
="$b" ;;
221 if [ -z "$main" -a -n "$sub" ]; then
222 # squash commits refer to a subtree
223 cache_set
"$sq" "$sub"
225 if [ -n "$main" -a -n "$sub" ]; then
226 debug
" Prior: $main -> $sub"
228 try_remove_previous
"$main"
229 try_remove_previous
"$sub"
240 # We're going to set some environment vars here, so
241 # do it in a subshell to get rid of them safely later
242 debug copy_commit
"{$1}" "{$2}" "{$3}"
243 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
246 read GIT_AUTHOR_EMAIL
248 read GIT_COMMITTER_NAME
249 read GIT_COMMITTER_EMAIL
250 read GIT_COMMITTER_DATE
251 export GIT_AUTHOR_NAME \
255 GIT_COMMITTER_EMAIL \
257 (echo -n "$annotate"; cat ) |
258 git commit-tree
"$2" $3 # reads the rest of stdin
259 ) || die
"Can't copy commit $1"
268 Add '$dir/' from commit '$latest_new'
270 git-subtree-dir: $dir
271 git-subtree-mainline: $latest_old
272 git-subtree-split: $latest_new
282 Split '$dir/' into commit '$latest_new'
284 git-subtree-dir: $dir
285 git-subtree-mainline: $latest_old
286 git-subtree-split: $latest_new
295 newsub_short
=$
(git rev-parse
--short "$newsub")
297 if [ -n "$oldsub" ]; then
298 oldsub_short
=$
(git rev-parse
--short "$oldsub")
299 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
301 git log
--pretty=tformat
:'%h %s' "$oldsub..$newsub"
302 git log
--pretty=tformat
:'REVERT: %h %s' "$newsub..$oldsub"
304 echo "Squashed '$dir/' content from commit $newsub_short"
308 echo "git-subtree-dir: $dir"
309 echo "git-subtree-split: $newsub"
315 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
322 git ls-tree
"$commit" -- "$dir" |
323 while read mode
type tree name
; do
324 assert
[ "$name" = "$dir" ]
334 if [ $# -ne 1 ]; then
335 return 0 # weird parents, consider it changed
337 ptree
=$
(toptree_for_commit
$1)
338 if [ "$ptree" != "$tree" ]; then
341 return 1 # not changed
351 tree
=$
(toptree_for_commit
$newsub) ||
exit $?
352 if [ -n "$old" ]; then
353 squash_msg
"$dir" "$oldsub" "$newsub" |
354 git commit-tree
"$tree" -p "$old" ||
exit $?
356 squash_msg
"$dir" "" "$newsub" |
357 git commit-tree
"$tree" ||
exit $?
366 assert
[ -n "$tree" ]
372 for parent
in $newparents; do
373 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
374 [ -z "$ptree" ] && continue
375 if [ "$ptree" = "$tree" ]; then
376 # an identical parent could be used in place of this rev.
379 nonidentical
="$parent"
382 # sometimes both old parents map to the same newparent;
383 # eliminate duplicates
385 for gp
in $gotparents; do
386 if [ "$gp" = "$parent" ]; then
391 if [ -n "$is_new" ]; then
392 gotparents
="$gotparents $parent"
397 if [ -n "$identical" ]; then
400 copy_commit
$rev $tree "$p" ||
exit $?
406 if ! git diff-index HEAD
--exit-code --quiet; then
407 die
"Working tree has modifications. Cannot add."
409 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
410 die
"Index has modifications. Cannot add."
416 if [ -e "$dir" ]; then
417 die
"'$dir' already exists. Cannot add."
422 if [ $# -ne 1 ]; then
423 die
"You must provide exactly one revision. Got: '$revs'"
427 debug
"Adding $dir as '$rev'..."
428 git read-tree
--prefix="$dir" $rev ||
exit $?
429 git checkout
"$dir" ||
exit $?
430 tree
=$
(git write-tree
) ||
exit $?
432 headrev
=$
(git rev-parse HEAD
) ||
exit $?
433 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
439 if [ -n "$squash" ]; then
440 rev=$
(new_squash_commit
"" "" "$rev") ||
exit $?
441 commit
=$
(echo "Merge commit '$rev' as '$dir'" |
442 git commit-tree
$tree $headp -p "$rev") ||
exit $?
444 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
445 git commit-tree
$tree $headp -p "$rev") ||
exit $?
447 git
reset "$commit" ||
exit $?
449 say
"Added dir '$dir'"
454 if [ -n "$branch" ] && rev_exists
"refs/heads/$branch"; then
455 die
"Branch '$branch' already exists."
458 debug
"Splitting $dir..."
459 cache_setup ||
exit $?
461 if [ -n "$onto" ]; then
462 debug
"Reading history for --onto=$onto..."
465 # the 'onto' history is already just the subdir, so
466 # any parent we find there can be used verbatim
472 if [ -n "$ignore_joins" ]; then
475 unrevs
="$(find_existing_splits "$dir" "$revs")"
478 # We can't restrict rev-list to only $dir here, because some of our
479 # parents have the $dir contents the root, and those won't match.
480 # (and rev-list --follow doesn't seem to solve this)
481 grl
='git rev-list --reverse --parents $revs $unrevs'
482 revmax
=$
(eval "$grl" |
wc -l)
486 while read rev parents
; do
487 revcount
=$
(($revcount + 1))
488 say
-n "$revcount/$revmax ($createcount)
"
489 debug
"Processing commit: $rev"
490 exists
=$
(cache_get
$rev)
491 if [ -n "$exists" ]; then
492 debug
" prior: $exists"
495 createcount
=$
(($createcount + 1))
496 debug
" parents: $parents"
497 newparents
=$
(cache_get
$parents)
498 debug
" newparents: $newparents"
500 tree
=$
(subtree_for_commit
$rev "$dir")
501 debug
" tree is: $tree"
503 # ugly. is there no better way to tell if this is a subtree
504 # vs. a mainline commit? Does it matter?
505 [ -z $tree ] && continue
507 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
508 debug
" newrev is: $newrev"
509 cache_set
$rev $newrev
510 cache_set latest_new
$newrev
511 cache_set latest_old
$rev
513 latest_new
=$
(cache_get latest_new
)
514 if [ -z "$latest_new" ]; then
515 die
"No new revisions were found"
518 if [ -n "$rejoin" ]; then
519 debug
"Merging split branch into HEAD..."
520 latest_old
=$
(cache_get latest_old
)
522 -m "$(rejoin_msg $dir $latest_old $latest_new)" \
523 $latest_new >&2 ||
exit $?
525 if [ -n "$branch" ]; then
526 git update-ref
-m 'subtree split' "refs/heads/$branch" \
527 $latest_new "" ||
exit $?
528 say
"Created branch '$branch'"
539 if [ $# -ne 1 ]; then
540 die
"You must provide exactly one revision. Got: '$revs'"
544 if [ -n "$squash" ]; then
545 first_split
="$(find_latest_squash "$dir")"
546 if [ -z "$first_split" ]; then
547 die
"Can't squash-merge: '$dir' was never added."
552 if [ "$sub" = "$rev" ]; then
553 say
"Subtree is already at commit $rev."
556 new
=$
(new_squash_commit
"$old" "$sub" "$rev") ||
exit $?
557 debug
"New squash commit: $new"
561 git merge
-s subtree
$rev
568 git pull
-s subtree
"$@"