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