Merge branch 'dl/subtree-limit-to-one-rev'
[git/git.git] / contrib / subtree / git-subtree.sh
CommitLineData
6912ea95 1#!/bin/sh
0ca71b37
AP
2#
3# git-subtree.sh: split/join git repositories in subdirectories of this one
4#
b77172f8 5# Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
0ca71b37 6#
6ae6a233
DA
7if test $# -eq 0
8then
9 set -- -h
96db2c04 10fi
0ca71b37 11OPTS_SPEC="\
f4f29557 12git subtree add --prefix=<prefix> <commit>
1c3e0f00 13git subtree add --prefix=<prefix> <repository> <ref>
13648af5 14git subtree merge --prefix=<prefix> <commit>
1c3e0f00
AB
15git subtree pull --prefix=<prefix> <repository> <ref>
16git subtree push --prefix=<prefix> <repository> <ref>
77128ed9 17git subtree split --prefix=<prefix> <commit>
0ca71b37 18--
96db2c04
AP
19h,help show the help
20q quiet
942dce55 21d show debug messages
6e25f79f 22P,prefix= the name of the subdir to split out
2da0969a 23m,message= use the given message as the commit message for the merge commit
13648af5 24 options for 'split'
d0eb1b14 25annotate= add a prefix to commit message of new commits
43a39512
AP
26b,branch= create a new branch from the split subtree
27ignore-joins ignore prior --rejoin commits
96db2c04
AP
28onto= try connecting new tree to an existing one
29rejoin merge the new branch back into HEAD
6ccc71a9 30 options for 'add', 'merge', and 'pull'
8e79043c 31squash merge subtree changes as a single commit
0ca71b37 32"
11f1511e 33eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
9c632ea2 34
df2302d7 35PATH=$PATH:$(git --exec-path)
33aaa697 36. git-sh-setup
9c632ea2 37
0ca71b37
AP
38require_work_tree
39
40quiet=
43a39512 41branch=
942dce55 42debug=
0ca71b37 43command=
b77172f8
AP
44onto=
45rejoin=
96db2c04 46ignore_joins=
d0eb1b14 47annotate=
8e79043c 48squash=
2da0969a 49message=
cba5e284 50prefix=
0ca71b37 51
d7fd792e 52debug () {
6ae6a233
DA
53 if test -n "$debug"
54 then
2ded109b 55 printf "%s\n" "$*" >&2
942dce55
AP
56 fi
57}
58
d7fd792e 59say () {
6ae6a233
DA
60 if test -z "$quiet"
61 then
2ded109b
DL
62 printf "%s\n" "$*" >&2
63 fi
64}
65
d7fd792e 66progress () {
6ae6a233
DA
67 if test -z "$quiet"
68 then
2ded109b 69 printf "%s\r" "$*" >&2
0ca71b37
AP
70 fi
71}
72
d7fd792e 73assert () {
6ae6a233
DA
74 if ! "$@"
75 then
2573354e
AP
76 die "assertion failed: " "$@"
77 fi
78}
79
77128ed9
DL
80ensure_single_rev () {
81 if test $# -ne 1
82 then
83 die "You must provide exactly one revision. Got: '$@'"
84 fi
85}
2573354e 86
6ae6a233
DA
87while test $# -gt 0
88do
0ca71b37
AP
89 opt="$1"
90 shift
6ae6a233 91
0ca71b37 92 case "$opt" in
6ae6a233
DA
93 -q)
94 quiet=1
95 ;;
96 -d)
97 debug=1
98 ;;
99 --annotate)
100 annotate="$1"
101 shift
102 ;;
103 --no-annotate)
104 annotate=
105 ;;
106 -b)
107 branch="$1"
108 shift
109 ;;
110 -P)
111 prefix="${1%/}"
112 shift
113 ;;
114 -m)
115 message="$1"
116 shift
117 ;;
118 --no-prefix)
119 prefix=
120 ;;
121 --onto)
122 onto="$1"
123 shift
124 ;;
125 --no-onto)
126 onto=
127 ;;
128 --rejoin)
129 rejoin=1
130 ;;
131 --no-rejoin)
132 rejoin=
133 ;;
134 --ignore-joins)
135 ignore_joins=1
136 ;;
137 --no-ignore-joins)
138 ignore_joins=
139 ;;
140 --squash)
141 squash=1
142 ;;
143 --no-squash)
144 squash=
145 ;;
146 --)
147 break
148 ;;
149 *)
150 die "Unexpected option: $opt"
151 ;;
0ca71b37
AP
152 esac
153done
154
155command="$1"
156shift
6ae6a233 157
0ca71b37 158case "$command" in
6ae6a233
DA
159add|merge|pull)
160 default=
161 ;;
162split|push)
163 default="--default HEAD"
164 ;;
165*)
166 die "Unknown command '$command'"
167 ;;
0ca71b37
AP
168esac
169
6ae6a233
DA
170if test -z "$prefix"
171then
9a8821ff
AP
172 die "You must provide the --prefix option."
173fi
ec54f0d9 174
77ba3058 175case "$command" in
6ae6a233
DA
176add)
177 test -e "$prefix" &&
178 die "prefix '$prefix' already exists."
179 ;;
180*)
181 test -e "$prefix" ||
182 die "'$prefix' does not exist; use 'git subtree add'"
183 ;;
77ba3058 184esac
ec54f0d9 185
6f2012cd 186dir="$(dirname "$prefix/.")"
9a8821ff 187
6ae6a233
DA
188if test "$command" != "pull" &&
189 test "$command" != "add" &&
190 test "$command" != "push"
191then
13648af5 192 revs=$(git rev-parse $default --revs-only "$@") || exit $?
6ae6a233 193 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
77128ed9 194 ensure_single_rev $revs
6ae6a233
DA
195 if test -n "$dirs"
196 then
13648af5
AP
197 die "Error: Use --prefix instead of bare filenames."
198 fi
0ca71b37 199fi
0ca71b37
AP
200
201debug "command: {$command}"
202debug "quiet: {$quiet}"
203debug "revs: {$revs}"
204debug "dir: {$dir}"
13648af5 205debug "opts: {$*}"
eb7b590c 206debug
0ca71b37 207
d7fd792e 208cache_setup () {
2573354e 209 cachedir="$GIT_DIR/subtree-cache/$$"
6ae6a233
DA
210 rm -rf "$cachedir" ||
211 die "Can't delete old cachedir: $cachedir"
212 mkdir -p "$cachedir" ||
213 die "Can't create new cachedir: $cachedir"
214 mkdir -p "$cachedir/notree" ||
215 die "Can't create new cachedir: $cachedir/notree"
0ca71b37 216 debug "Using cachedir: $cachedir" >&2
0ca71b37
AP
217}
218
d7fd792e 219cache_get () {
6ae6a233
DA
220 for oldrev in "$@"
221 do
222 if test -r "$cachedir/$oldrev"
223 then
0ca71b37
AP
224 read newrev <"$cachedir/$oldrev"
225 echo $newrev
226 fi
227 done
228}
229
d7fd792e 230cache_miss () {
6ae6a233
DA
231 for oldrev in "$@"
232 do
233 if ! test -r "$cachedir/$oldrev"
234 then
915b9894
JG
235 echo $oldrev
236 fi
237 done
238}
239
d7fd792e 240check_parents () {
315a84f9
SR
241 missed=$(cache_miss "$1")
242 local indent=$(($2 + 1))
6ae6a233
DA
243 for miss in $missed
244 do
245 if ! test -r "$cachedir/notree/$miss"
246 then
915b9894 247 debug " incorrect order: $miss"
315a84f9 248 process_split_commit "$miss" "" "$indent"
915b9894
JG
249 fi
250 done
251}
252
d7fd792e 253set_notree () {
915b9894
JG
254 echo "1" > "$cachedir/notree/$1"
255}
256
d7fd792e 257cache_set () {
0ca71b37
AP
258 oldrev="$1"
259 newrev="$2"
6ae6a233
DA
260 if test "$oldrev" != "latest_old" &&
261 test "$oldrev" != "latest_new" &&
262 test -e "$cachedir/$oldrev"
263 then
0ca71b37
AP
264 die "cache for $oldrev already exists!"
265 fi
266 echo "$newrev" >"$cachedir/$oldrev"
267}
268
d7fd792e 269rev_exists () {
6ae6a233
DA
270 if git rev-parse "$1" >/dev/null 2>&1
271 then
43a39512
AP
272 return 0
273 else
274 return 1
275 fi
276}
277
d7fd792e 278rev_is_descendant_of_branch () {
0a562948
JS
279 newrev="$1"
280 branch="$2"
6ae6a233
DA
281 branch_hash=$(git rev-parse "$branch")
282 match=$(git rev-list -1 "$branch_hash" "^$newrev")
0a562948 283
6ae6a233
DA
284 if test -z "$match"
285 then
0a562948
JS
286 return 0
287 else
288 return 1
289 fi
290}
291
b9de5353
AP
292# if a commit doesn't have a parent, this might not work. But we only want
293# to remove the parent from the rev-list, and since it doesn't exist, it won't
294# be there anyway, so do nothing in that case.
d7fd792e 295try_remove_previous () {
6ae6a233
DA
296 if rev_exists "$1^"
297 then
b9de5353
AP
298 echo "^$1^"
299 fi
300}
301
d7fd792e 302find_latest_squash () {
d713e2d8 303 debug "Looking for latest squash ($dir)..."
1cc2cfff 304 dir="$1"
d713e2d8
AP
305 sq=
306 main=
307 sub=
6f2012cd 308 git log --grep="^git-subtree-dir: $dir/*\$" \
8841b522 309 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
6ae6a233
DA
310 while read a b junk
311 do
d713e2d8
AP
312 debug "$a $b $junk"
313 debug "{{$sq/$main/$sub}}"
1cc2cfff 314 case "$a" in
6ae6a233
DA
315 START)
316 sq="$b"
317 ;;
318 git-subtree-mainline:)
319 main="$b"
320 ;;
321 git-subtree-split:)
322 sub="$(git rev-parse "$b^0")" ||
323 die "could not rev-parse split hash $b from commit $sq"
324 ;;
325 END)
326 if test -n "$sub"
327 then
328 if test -n "$main"
329 then
330 # a rejoin commit?
331 # Pretend its sub was a squash.
332 sq="$sub"
1cc2cfff 333 fi
6ae6a233
DA
334 debug "Squash found: $sq $sub"
335 echo "$sq" "$sub"
336 break
337 fi
338 sq=
339 main=
340 sub=
341 ;;
1cc2cfff
AP
342 esac
343 done
344}
345
d7fd792e 346find_existing_splits () {
8b4a77f2
AP
347 debug "Looking for prior splits..."
348 dir="$1"
349 revs="$2"
d713e2d8
AP
350 main=
351 sub=
dd21d43b
SR
352 local grep_format="^git-subtree-dir: $dir/*\$"
353 if test -n "$ignore_joins"
354 then
355 grep_format="^Add '$dir/' from commit '"
356 fi
357 git log --grep="$grep_format" \
8841b522 358 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
6ae6a233
DA
359 while read a b junk
360 do
8b4a77f2 361 case "$a" in
6ae6a233
DA
362 START)
363 sq="$b"
364 ;;
365 git-subtree-mainline:)
366 main="$b"
367 ;;
368 git-subtree-split:)
369 sub="$(git rev-parse "$b^0")" ||
370 die "could not rev-parse split hash $b from commit $sq"
371 ;;
372 END)
373 debug " Main is: '$main'"
374 if test -z "$main" -a -n "$sub"
375 then
376 # squash commits refer to a subtree
377 debug " Squash: $sq from $sub"
378 cache_set "$sq" "$sub"
379 fi
380 if test -n "$main" -a -n "$sub"
381 then
382 debug " Prior: $main -> $sub"
383 cache_set $main $sub
384 cache_set $sub $sub
385 try_remove_previous "$main"
386 try_remove_previous "$sub"
387 fi
388 main=
389 sub=
390 ;;
8b4a77f2
AP
391 esac
392 done
393}
394
d7fd792e 395copy_commit () {
f96bc790 396 # We're going to set some environment vars here, so
fd9500ee 397 # do it in a subshell to get rid of them safely later
a64f3a72 398 debug copy_commit "{$1}" "{$2}" "{$3}"
8841b522 399 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
fd9500ee
AP
400 (
401 read GIT_AUTHOR_NAME
402 read GIT_AUTHOR_EMAIL
403 read GIT_AUTHOR_DATE
404 read GIT_COMMITTER_NAME
405 read GIT_COMMITTER_EMAIL
406 read GIT_COMMITTER_DATE
b77172f8
AP
407 export GIT_AUTHOR_NAME \
408 GIT_AUTHOR_EMAIL \
409 GIT_AUTHOR_DATE \
410 GIT_COMMITTER_NAME \
411 GIT_COMMITTER_EMAIL \
412 GIT_COMMITTER_DATE
6ae6a233
DA
413 (
414 printf "%s" "$annotate"
415 cat
416 ) |
fd9500ee
AP
417 git commit-tree "$2" $3 # reads the rest of stdin
418 ) || die "Can't copy commit $1"
419}
420
d7fd792e 421add_msg () {
eb7b590c
AP
422 dir="$1"
423 latest_old="$2"
424 latest_new="$3"
6ae6a233
DA
425 if test -n "$message"
426 then
2da0969a
JS
427 commit_message="$message"
428 else
429 commit_message="Add '$dir/' from commit '$latest_new'"
430 fi
eb7b590c 431 cat <<-EOF
2da0969a 432 $commit_message
6ae6a233 433
eb7b590c
AP
434 git-subtree-dir: $dir
435 git-subtree-mainline: $latest_old
436 git-subtree-split: $latest_new
437 EOF
438}
439
d7fd792e 440add_squashed_msg () {
6ae6a233
DA
441 if test -n "$message"
442 then
2da0969a
JS
443 echo "$message"
444 else
445 echo "Merge commit '$1' as '$2'"
446 fi
447}
448
d7fd792e 449rejoin_msg () {
b77172f8
AP
450 dir="$1"
451 latest_old="$2"
452 latest_new="$3"
6ae6a233
DA
453 if test -n "$message"
454 then
2da0969a
JS
455 commit_message="$message"
456 else
457 commit_message="Split '$dir/' into commit '$latest_new'"
458 fi
b77172f8 459 cat <<-EOF
12629161 460 $commit_message
6ae6a233 461
b77172f8 462 git-subtree-dir: $dir
8b4a77f2
AP
463 git-subtree-mainline: $latest_old
464 git-subtree-split: $latest_new
b77172f8
AP
465 EOF
466}
467
d7fd792e 468squash_msg () {
1cc2cfff
AP
469 dir="$1"
470 oldsub="$2"
471 newsub="$3"
1cc2cfff 472 newsub_short=$(git rev-parse --short "$newsub")
6ae6a233
DA
473
474 if test -n "$oldsub"
475 then
d713e2d8
AP
476 oldsub_short=$(git rev-parse --short "$oldsub")
477 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
478 echo
8841b522
SG
479 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
480 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
d713e2d8
AP
481 else
482 echo "Squashed '$dir/' content from commit $newsub_short"
483 fi
6ae6a233 484
d713e2d8
AP
485 echo
486 echo "git-subtree-dir: $dir"
487 echo "git-subtree-split: $newsub"
1cc2cfff
AP
488}
489
d7fd792e 490toptree_for_commit () {
210d0839 491 commit="$1"
8841b522 492 git rev-parse --verify "$commit^{tree}" || exit $?
210d0839
AP
493}
494
d7fd792e 495subtree_for_commit () {
210d0839
AP
496 commit="$1"
497 dir="$2"
498 git ls-tree "$commit" -- "$dir" |
6ae6a233
DA
499 while read mode type tree name
500 do
501 assert test "$name" = "$dir"
502 assert test "$type" = "tree" -o "$type" = "commit"
503 test "$type" = "commit" && continue # ignore submodules
768d6d10
AP
504 echo $tree
505 break
506 done
507}
508
d7fd792e 509tree_changed () {
768d6d10
AP
510 tree=$1
511 shift
6ae6a233
DA
512 if test $# -ne 1
513 then
768d6d10
AP
514 return 0 # weird parents, consider it changed
515 else
210d0839 516 ptree=$(toptree_for_commit $1)
6ae6a233
DA
517 if test "$ptree" != "$tree"
518 then
768d6d10
AP
519 return 0 # changed
520 else
521 return 1 # not changed
522 fi
523 fi
524}
525
d7fd792e 526new_squash_commit () {
1cc2cfff
AP
527 old="$1"
528 oldsub="$2"
529 newsub="$3"
530 tree=$(toptree_for_commit $newsub) || exit $?
6ae6a233
DA
531 if test -n "$old"
532 then
533 squash_msg "$dir" "$oldsub" "$newsub" |
534 git commit-tree "$tree" -p "$old" || exit $?
d713e2d8
AP
535 else
536 squash_msg "$dir" "" "$newsub" |
6ae6a233 537 git commit-tree "$tree" || exit $?
d713e2d8 538 fi
1cc2cfff
AP
539}
540
d7fd792e 541copy_or_skip () {
d6912658
AP
542 rev="$1"
543 tree="$2"
544 newparents="$3"
6ae6a233 545 assert test -n "$tree"
d6912658 546
96db2c04 547 identical=
49cf8228 548 nonidentical=
96db2c04 549 p=
a64f3a72 550 gotparents=
68f8ff81 551 copycommit=
6ae6a233
DA
552 for parent in $newparents
553 do
d6912658 554 ptree=$(toptree_for_commit $parent) || exit $?
6ae6a233
DA
555 test -z "$ptree" && continue
556 if test "$ptree" = "$tree"
557 then
96db2c04 558 # an identical parent could be used in place of this rev.
68f8ff81
SR
559 if test -n "$identical"
560 then
561 # if a previous identical parent was found, check whether
562 # one is already an ancestor of the other
563 mergebase=$(git merge-base $identical $parent)
564 if test "$identical" = "$mergebase"
565 then
566 # current identical commit is an ancestor of parent
567 identical="$parent"
568 elif test "$parent" != "$mergebase"
569 then
570 # no common history; commit must be copied
571 copycommit=1
572 fi
573 else
574 # first identical parent detected
575 identical="$parent"
576 fi
49cf8228
AP
577 else
578 nonidentical="$parent"
96db2c04 579 fi
6ae6a233 580
a64f3a72
AP
581 # sometimes both old parents map to the same newparent;
582 # eliminate duplicates
583 is_new=1
6ae6a233
DA
584 for gp in $gotparents
585 do
586 if test "$gp" = "$parent"
587 then
a64f3a72
AP
588 is_new=
589 break
590 fi
591 done
6ae6a233
DA
592 if test -n "$is_new"
593 then
a64f3a72 594 gotparents="$gotparents $parent"
d6912658
AP
595 p="$p -p $parent"
596 fi
597 done
933cfeb9 598
6ae6a233
DA
599 if test -n "$identical" && test -n "$nonidentical"
600 then
933cfeb9 601 extras=$(git rev-list --count $identical..$nonidentical)
6ae6a233
DA
602 if test "$extras" -ne 0
603 then
933cfeb9
DW
604 # we need to preserve history along the other branch
605 copycommit=1
606 fi
607 fi
6ae6a233
DA
608 if test -n "$identical" && test -z "$copycommit"
609 then
96db2c04
AP
610 echo $identical
611 else
6ae6a233 612 copy_commit "$rev" "$tree" "$p" || exit $?
96db2c04 613 fi
d6912658
AP
614}
615
d7fd792e 616ensure_clean () {
6ae6a233
DA
617 if ! git diff-index HEAD --exit-code --quiet 2>&1
618 then
eb7b590c
AP
619 die "Working tree has modifications. Cannot add."
620 fi
6ae6a233
DA
621 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
622 then
eb7b590c
AP
623 die "Index has modifications. Cannot add."
624 fi
13648af5
AP
625}
626
d7fd792e 627ensure_valid_ref_format () {
1c3e0f00 628 git check-ref-format "refs/heads/$1" ||
6ae6a233 629 die "'$1' does not look like a ref"
1c3e0f00
AB
630}
631
565e4b79
SR
632process_split_commit () {
633 local rev="$1"
634 local parents="$2"
315a84f9
SR
635 local indent=$3
636
637 if test $indent -eq 0
638 then
639 revcount=$(($revcount + 1))
640 else
641 # processing commit without normal parent information;
642 # fetch from repo
19ad68d9 643 parents=$(git rev-parse "$rev^@")
315a84f9
SR
644 extracount=$(($extracount + 1))
645 fi
646
647 progress "$revcount/$revmax ($createcount) [$extracount]"
648
565e4b79
SR
649 debug "Processing commit: $rev"
650 exists=$(cache_get "$rev")
651 if test -n "$exists"
652 then
653 debug " prior: $exists"
654 return
655 fi
656 createcount=$(($createcount + 1))
657 debug " parents: $parents"
315a84f9 658 check_parents "$parents" "$indent"
565e4b79
SR
659 newparents=$(cache_get $parents)
660 debug " newparents: $newparents"
661
662 tree=$(subtree_for_commit "$rev" "$dir")
663 debug " tree is: $tree"
664
565e4b79
SR
665 # ugly. is there no better way to tell if this is a subtree
666 # vs. a mainline commit? Does it matter?
667 if test -z "$tree"
668 then
669 set_notree "$rev"
670 if test -n "$newparents"
671 then
672 cache_set "$rev" "$rev"
673 fi
674 return
675 fi
676
677 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
678 debug " newrev is: $newrev"
679 cache_set "$rev" "$newrev"
680 cache_set latest_new "$newrev"
681 cache_set latest_old "$rev"
682}
683
d7fd792e 684cmd_add () {
6ae6a233
DA
685 if test -e "$dir"
686 then
13648af5
AP
687 die "'$dir' already exists. Cannot add."
688 fi
c00d1d11 689
13648af5 690 ensure_clean
6ae6a233
DA
691
692 if test $# -eq 1
693 then
694 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
695 die "'$1' does not refer to a commit"
696
697 cmd_add_commit "$@"
698
699 elif test $# -eq 2
700 then
701 # Technically we could accept a refspec here but we're
702 # just going to turn around and add FETCH_HEAD under the
703 # specified directory. Allowing a refspec might be
704 # misleading because we won't do anything with any other
705 # branches fetched via the refspec.
706 ensure_valid_ref_format "$2"
707
708 cmd_add_repository "$@"
c00d1d11 709 else
6ae6a233
DA
710 say "error: parameters were '$@'"
711 die "Provide either a commit or a repository and commit."
eb7b590c 712 fi
c00d1d11
WW
713}
714
d7fd792e 715cmd_add_repository () {
c00d1d11
WW
716 echo "git fetch" "$@"
717 repository=$1
718 refspec=$2
719 git fetch "$@" || exit $?
720 revs=FETCH_HEAD
721 set -- $revs
722 cmd_add_commit "$@"
723}
724
d7fd792e 725cmd_add_commit () {
77128ed9
DL
726 rev=$(git rev-parse $default --revs-only "$@") || exit $?
727 ensure_single_rev $rev
6ae6a233 728
eb7b590c
AP
729 debug "Adding $dir as '$rev'..."
730 git read-tree --prefix="$dir" $rev || exit $?
227f7811 731 git checkout -- "$dir" || exit $?
eb7b590c 732 tree=$(git write-tree) || exit $?
6ae6a233 733
eb7b590c 734 headrev=$(git rev-parse HEAD) || exit $?
6ae6a233
DA
735 if test -n "$headrev" && test "$headrev" != "$rev"
736 then
eb7b590c
AP
737 headp="-p $headrev"
738 else
739 headp=
740 fi
6ae6a233
DA
741
742 if test -n "$squash"
743 then
d713e2d8 744 rev=$(new_squash_commit "" "" "$rev") || exit $?
2da0969a 745 commit=$(add_squashed_msg "$rev" "$dir" |
6ae6a233 746 git commit-tree "$tree" $headp -p "$rev") || exit $?
d713e2d8 747 else
6f1871fe 748 revp=$(peel_committish "$rev") &&
6ae6a233
DA
749 commit=$(add_msg "$dir" $headrev "$rev" |
750 git commit-tree "$tree" $headp -p "$revp") || exit $?
d713e2d8 751 fi
eb7b590c 752 git reset "$commit" || exit $?
6ae6a233 753
d713e2d8 754 say "Added dir '$dir'"
eb7b590c
AP
755}
756
d7fd792e 757cmd_split () {
0ca71b37
AP
758 debug "Splitting $dir..."
759 cache_setup || exit $?
6ae6a233
DA
760
761 if test -n "$onto"
762 then
847e8681 763 debug "Reading history for --onto=$onto..."
33ff583a 764 git rev-list $onto |
6ae6a233
DA
765 while read rev
766 do
33ff583a
AP
767 # the 'onto' history is already just the subdir, so
768 # any parent we find there can be used verbatim
2c71b7c4 769 debug " cache: $rev"
6ae6a233 770 cache_set "$rev" "$rev"
33ff583a
AP
771 done
772 fi
6ae6a233 773
dd21d43b 774 unrevs="$(find_existing_splits "$dir" "$revs")"
6ae6a233 775
1f73862f
AP
776 # We can't restrict rev-list to only $dir here, because some of our
777 # parents have the $dir contents the root, and those won't match.
778 # (and rev-list --follow doesn't seem to solve this)
6f4f84fa 779 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
942dce55
AP
780 revmax=$(eval "$grl" | wc -l)
781 revcount=0
782 createcount=0
315a84f9 783 extracount=0
942dce55 784 eval "$grl" |
6ae6a233
DA
785 while read rev parents
786 do
315a84f9 787 process_split_commit "$rev" "$parents" 0
2573354e 788 done || exit $?
6ae6a233 789
b77172f8 790 latest_new=$(cache_get latest_new)
6ae6a233
DA
791 if test -z "$latest_new"
792 then
e25a6bf8
AP
793 die "No new revisions were found"
794 fi
6ae6a233
DA
795
796 if test -n "$rejoin"
797 then
b77172f8
AP
798 debug "Merging split branch into HEAD..."
799 latest_old=$(cache_get latest_old)
800 git merge -s ours \
0f12c7d4 801 --allow-unrelated-histories \
6ae6a233
DA
802 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
803 "$latest_new" >&2 || exit $?
804 fi
805 if test -n "$branch"
806 then
807 if rev_exists "refs/heads/$branch"
808 then
809 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
810 then
0a562948
JS
811 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
812 fi
813 action='Updated'
814 else
815 action='Created'
816 fi
6ae6a233
DA
817 git update-ref -m 'subtree split' \
818 "refs/heads/$branch" "$latest_new" || exit $?
0a562948 819 say "$action branch '$branch'"
43a39512 820 fi
6ae6a233 821 echo "$latest_new"
0ca71b37
AP
822 exit 0
823}
824
d7fd792e 825cmd_merge () {
77128ed9
DL
826 rev=$(git rev-parse $default --revs-only "$@") || exit $?
827 ensure_single_rev $rev
13648af5 828 ensure_clean
6ae6a233 829
6ae6a233
DA
830 if test -n "$squash"
831 then
1cc2cfff 832 first_split="$(find_latest_squash "$dir")"
6ae6a233
DA
833 if test -z "$first_split"
834 then
1cc2cfff
AP
835 die "Can't squash-merge: '$dir' was never added."
836 fi
837 set $first_split
838 old=$1
839 sub=$2
6ae6a233
DA
840 if test "$sub" = "$rev"
841 then
eb4fb910
AP
842 say "Subtree is already at commit $rev."
843 exit 0
844 fi
1cc2cfff
AP
845 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
846 debug "New squash commit: $new"
847 rev="$new"
848 fi
448e71e2
PW
849
850 version=$(git version)
6ae6a233
DA
851 if test "$version" \< "git version 1.7"
852 then
853 if test -n "$message"
854 then
855 git merge -s subtree --message="$message" "$rev"
448e71e2 856 else
6ae6a233 857 git merge -s subtree "$rev"
448e71e2 858 fi
349a70d5 859 else
6ae6a233
DA
860 if test -n "$message"
861 then
862 git merge -Xsubtree="$prefix" \
863 --message="$message" "$rev"
448e71e2
PW
864 else
865 git merge -Xsubtree="$prefix" $rev
866 fi
349a70d5 867 fi
13648af5
AP
868}
869
d7fd792e 870cmd_pull () {
6ae6a233
DA
871 if test $# -ne 2
872 then
873 die "You must provide <repository> <ref>"
1c3e0f00 874 fi
13648af5 875 ensure_clean
1c3e0f00 876 ensure_valid_ref_format "$2"
e31d1e2f
AP
877 git fetch "$@" || exit $?
878 revs=FETCH_HEAD
c00d1d11
WW
879 set -- $revs
880 cmd_merge "$@"
881}
882
d7fd792e 883cmd_push () {
6ae6a233
DA
884 if test $# -ne 2
885 then
886 die "You must provide <repository> <ref>"
c00d1d11 887 fi
1c3e0f00 888 ensure_valid_ref_format "$2"
6ae6a233
DA
889 if test -e "$dir"
890 then
891 repository=$1
892 refspec=$2
893 echo "git push using: " "$repository" "$refspec"
894 localrev=$(git subtree split --prefix="$prefix") || die
895 git push "$repository" "$localrev":"refs/heads/$refspec"
c00d1d11 896 else
6ae6a233 897 die "'$dir' must already exist. Try 'git subtree add'."
c00d1d11 898 fi
0ca71b37
AP
899}
900
13648af5 901"cmd_$command" "$@"