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