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