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