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