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