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