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