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