]> git.ipfire.org Git - thirdparty/git.git/blob - contrib/subtree/git-subtree.sh
Merge branch 'mk/doc-gitfile-more' into maint-2.43
[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 $# = 2 -o $# = 3
377 b="$1"
378 sq="$2"
379 repository=""
380 if test "$#" = 3
381 then
382 repository="$3"
383 fi
384 fail_msg="fatal: could not rev-parse split hash $b from commit $sq"
385 if ! sub="$(git rev-parse --verify --quiet "$b^{commit}")"
386 then
387 # if 'repository' was given, try to fetch the 'git-subtree-split' hash
388 # before 'rev-parse'-ing it again, as it might be a tag that we do not have locally
389 if test -n "${repository}"
390 then
391 git fetch "$repository" "$b"
392 sub="$(git rev-parse --verify --quiet "$b^{commit}")" ||
393 die "$fail_msg"
394 else
395 hint1=$(printf "hint: hash might be a tag, try fetching it from the subtree repository:")
396 hint2=$(printf "hint: git fetch <subtree-repository> $b")
397 fail_msg=$(printf "$fail_msg\n$hint1\n$hint2")
398 die "$fail_msg"
399 fi
400 fi
401 }
402
403 # Usage: find_latest_squash DIR [REPOSITORY]
404 find_latest_squash () {
405 assert test $# = 1 -o $# = 2
406 dir="$1"
407 repository=""
408 if test "$#" = 2
409 then
410 repository="$2"
411 fi
412 debug "Looking for latest squash (dir=$dir, repository=$repository)..."
413 local indent=$(($indent + 1))
414
415 sq=
416 main=
417 sub=
418 git log --grep="^git-subtree-dir: $dir/*\$" \
419 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
420 while read a b junk
421 do
422 debug "$a $b $junk"
423 debug "{{$sq/$main/$sub}}"
424 case "$a" in
425 START)
426 sq="$b"
427 ;;
428 git-subtree-mainline:)
429 main="$b"
430 ;;
431 git-subtree-split:)
432 process_subtree_split_trailer "$b" "$sq" "$repository"
433 ;;
434 END)
435 if test -n "$sub"
436 then
437 if test -n "$main"
438 then
439 # a rejoin commit?
440 # Pretend its sub was a squash.
441 sq=$(git rev-parse --verify "$sq^2") ||
442 die
443 fi
444 debug "Squash found: $sq $sub"
445 echo "$sq" "$sub"
446 break
447 fi
448 sq=
449 main=
450 sub=
451 ;;
452 esac
453 done || exit $?
454 }
455
456 # Usage: find_existing_splits DIR REV [REPOSITORY]
457 find_existing_splits () {
458 assert test $# = 2 -o $# = 3
459 debug "Looking for prior splits..."
460 local indent=$(($indent + 1))
461
462 dir="$1"
463 rev="$2"
464 repository=""
465 if test "$#" = 3
466 then
467 repository="$3"
468 fi
469 main=
470 sub=
471 local grep_format="^git-subtree-dir: $dir/*\$"
472 if test -n "$arg_split_ignore_joins"
473 then
474 grep_format="^Add '$dir/' from commit '"
475 fi
476 git log --grep="$grep_format" \
477 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$rev" |
478 while read a b junk
479 do
480 case "$a" in
481 START)
482 sq="$b"
483 ;;
484 git-subtree-mainline:)
485 main="$b"
486 ;;
487 git-subtree-split:)
488 process_subtree_split_trailer "$b" "$sq" "$repository"
489 ;;
490 END)
491 debug "Main is: '$main'"
492 if test -z "$main" -a -n "$sub"
493 then
494 # squash commits refer to a subtree
495 debug " Squash: $sq from $sub"
496 cache_set "$sq" "$sub"
497 fi
498 if test -n "$main" -a -n "$sub"
499 then
500 debug " Prior: $main -> $sub"
501 cache_set $main $sub
502 cache_set $sub $sub
503 try_remove_previous "$main"
504 try_remove_previous "$sub"
505 fi
506 main=
507 sub=
508 ;;
509 esac
510 done || exit $?
511 }
512
513 # Usage: copy_commit REV TREE FLAGS_STR
514 copy_commit () {
515 assert test $# = 3
516 # We're going to set some environment vars here, so
517 # do it in a subshell to get rid of them safely later
518 debug copy_commit "{$1}" "{$2}" "{$3}"
519 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
520 (
521 read GIT_AUTHOR_NAME
522 read GIT_AUTHOR_EMAIL
523 read GIT_AUTHOR_DATE
524 read GIT_COMMITTER_NAME
525 read GIT_COMMITTER_EMAIL
526 read GIT_COMMITTER_DATE
527 export GIT_AUTHOR_NAME \
528 GIT_AUTHOR_EMAIL \
529 GIT_AUTHOR_DATE \
530 GIT_COMMITTER_NAME \
531 GIT_COMMITTER_EMAIL \
532 GIT_COMMITTER_DATE
533 (
534 printf "%s" "$arg_split_annotate"
535 cat
536 ) |
537 git commit-tree "$2" $3 # reads the rest of stdin
538 ) || die "fatal: can't copy commit $1"
539 }
540
541 # Usage: add_msg DIR LATEST_OLD LATEST_NEW
542 add_msg () {
543 assert test $# = 3
544 dir="$1"
545 latest_old="$2"
546 latest_new="$3"
547 if test -n "$arg_addmerge_message"
548 then
549 commit_message="$arg_addmerge_message"
550 else
551 commit_message="Add '$dir/' from commit '$latest_new'"
552 fi
553 if test -n "$arg_split_rejoin"
554 then
555 # If this is from a --rejoin, then rejoin_msg has
556 # already inserted the `git-subtree-xxx:` tags
557 echo "$commit_message"
558 return
559 fi
560 cat <<-EOF
561 $commit_message
562
563 git-subtree-dir: $dir
564 git-subtree-mainline: $latest_old
565 git-subtree-split: $latest_new
566 EOF
567 }
568
569 # Usage: add_squashed_msg REV DIR
570 add_squashed_msg () {
571 assert test $# = 2
572 if test -n "$arg_addmerge_message"
573 then
574 echo "$arg_addmerge_message"
575 else
576 echo "Merge commit '$1' as '$2'"
577 fi
578 }
579
580 # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
581 rejoin_msg () {
582 assert test $# = 3
583 dir="$1"
584 latest_old="$2"
585 latest_new="$3"
586 if test -n "$arg_addmerge_message"
587 then
588 commit_message="$arg_addmerge_message"
589 else
590 commit_message="Split '$dir/' into commit '$latest_new'"
591 fi
592 cat <<-EOF
593 $commit_message
594
595 git-subtree-dir: $dir
596 git-subtree-mainline: $latest_old
597 git-subtree-split: $latest_new
598 EOF
599 }
600
601 # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
602 squash_msg () {
603 assert test $# = 3
604 dir="$1"
605 oldsub="$2"
606 newsub="$3"
607 newsub_short=$(git rev-parse --short "$newsub")
608
609 if test -n "$oldsub"
610 then
611 oldsub_short=$(git rev-parse --short "$oldsub")
612 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
613 echo
614 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
615 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
616 else
617 echo "Squashed '$dir/' content from commit $newsub_short"
618 fi
619
620 echo
621 echo "git-subtree-dir: $dir"
622 echo "git-subtree-split: $newsub"
623 }
624
625 # Usage: toptree_for_commit COMMIT
626 toptree_for_commit () {
627 assert test $# = 1
628 commit="$1"
629 git rev-parse --verify "$commit^{tree}" || exit $?
630 }
631
632 # Usage: subtree_for_commit COMMIT DIR
633 subtree_for_commit () {
634 assert test $# = 2
635 commit="$1"
636 dir="$2"
637 git ls-tree "$commit" -- "$dir" |
638 while read mode type tree name
639 do
640 assert test "$name" = "$dir"
641 assert test "$type" = "tree" -o "$type" = "commit"
642 test "$type" = "commit" && continue # ignore submodules
643 echo $tree
644 break
645 done || exit $?
646 }
647
648 # Usage: tree_changed TREE [PARENTS...]
649 tree_changed () {
650 assert test $# -gt 0
651 tree=$1
652 shift
653 if test $# -ne 1
654 then
655 return 0 # weird parents, consider it changed
656 else
657 ptree=$(toptree_for_commit $1) || exit $?
658 if test "$ptree" != "$tree"
659 then
660 return 0 # changed
661 else
662 return 1 # not changed
663 fi
664 fi
665 }
666
667 # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
668 new_squash_commit () {
669 assert test $# = 3
670 old="$1"
671 oldsub="$2"
672 newsub="$3"
673 tree=$(toptree_for_commit $newsub) || exit $?
674 if test -n "$old"
675 then
676 squash_msg "$dir" "$oldsub" "$newsub" |
677 git commit-tree "$tree" -p "$old" || exit $?
678 else
679 squash_msg "$dir" "" "$newsub" |
680 git commit-tree "$tree" || exit $?
681 fi
682 }
683
684 # Usage: copy_or_skip REV TREE NEWPARENTS
685 copy_or_skip () {
686 assert test $# = 3
687 rev="$1"
688 tree="$2"
689 newparents="$3"
690 assert test -n "$tree"
691
692 identical=
693 nonidentical=
694 p=
695 gotparents=
696 copycommit=
697 for parent in $newparents
698 do
699 ptree=$(toptree_for_commit $parent) || exit $?
700 test -z "$ptree" && continue
701 if test "$ptree" = "$tree"
702 then
703 # an identical parent could be used in place of this rev.
704 if test -n "$identical"
705 then
706 # if a previous identical parent was found, check whether
707 # one is already an ancestor of the other
708 mergebase=$(git merge-base $identical $parent)
709 if test "$identical" = "$mergebase"
710 then
711 # current identical commit is an ancestor of parent
712 identical="$parent"
713 elif test "$parent" != "$mergebase"
714 then
715 # no common history; commit must be copied
716 copycommit=1
717 fi
718 else
719 # first identical parent detected
720 identical="$parent"
721 fi
722 else
723 nonidentical="$parent"
724 fi
725
726 # sometimes both old parents map to the same newparent;
727 # eliminate duplicates
728 is_new=1
729 for gp in $gotparents
730 do
731 if test "$gp" = "$parent"
732 then
733 is_new=
734 break
735 fi
736 done
737 if test -n "$is_new"
738 then
739 gotparents="$gotparents $parent"
740 p="$p -p $parent"
741 fi
742 done
743
744 if test -n "$identical" && test -n "$nonidentical"
745 then
746 extras=$(git rev-list --count $identical..$nonidentical)
747 if test "$extras" -ne 0
748 then
749 # we need to preserve history along the other branch
750 copycommit=1
751 fi
752 fi
753 if test -n "$identical" && test -z "$copycommit"
754 then
755 echo $identical
756 else
757 copy_commit "$rev" "$tree" "$p" || exit $?
758 fi
759 }
760
761 # Usage: ensure_clean
762 ensure_clean () {
763 assert test $# = 0
764 if ! git diff-index HEAD --exit-code --quiet 2>&1
765 then
766 die "fatal: working tree has modifications. Cannot add."
767 fi
768 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
769 then
770 die "fatal: index has modifications. Cannot add."
771 fi
772 }
773
774 # Usage: ensure_valid_ref_format REF
775 ensure_valid_ref_format () {
776 assert test $# = 1
777 git check-ref-format "refs/heads/$1" ||
778 die "fatal: '$1' does not look like a ref"
779 }
780
781 # Usage: process_split_commit REV PARENTS
782 process_split_commit () {
783 assert test $# = 2
784 local rev="$1"
785 local parents="$2"
786
787 if test $indent -eq 0
788 then
789 revcount=$(($revcount + 1))
790 else
791 # processing commit without normal parent information;
792 # fetch from repo
793 parents=$(git rev-parse "$rev^@")
794 extracount=$(($extracount + 1))
795 fi
796
797 progress "$revcount/$revmax ($createcount) [$extracount]"
798
799 debug "Processing commit: $rev"
800 local indent=$(($indent + 1))
801 exists=$(cache_get "$rev") || exit $?
802 if test -n "$exists"
803 then
804 debug "prior: $exists"
805 return
806 fi
807 createcount=$(($createcount + 1))
808 debug "parents: $parents"
809 check_parents $parents
810 newparents=$(cache_get $parents) || exit $?
811 debug "newparents: $newparents"
812
813 tree=$(subtree_for_commit "$rev" "$dir") || exit $?
814 debug "tree is: $tree"
815
816 # ugly. is there no better way to tell if this is a subtree
817 # vs. a mainline commit? Does it matter?
818 if test -z "$tree"
819 then
820 set_notree "$rev"
821 if test -n "$newparents"
822 then
823 cache_set "$rev" "$rev"
824 fi
825 return
826 fi
827
828 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
829 debug "newrev is: $newrev"
830 cache_set "$rev" "$newrev"
831 cache_set latest_new "$newrev"
832 cache_set latest_old "$rev"
833 }
834
835 # Usage: cmd_add REV
836 # Or: cmd_add REPOSITORY REF
837 cmd_add () {
838
839 ensure_clean
840
841 if test $# -eq 1
842 then
843 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
844 die "fatal: '$1' does not refer to a commit"
845
846 cmd_add_commit "$@"
847
848 elif test $# -eq 2
849 then
850 # Technically we could accept a refspec here but we're
851 # just going to turn around and add FETCH_HEAD under the
852 # specified directory. Allowing a refspec might be
853 # misleading because we won't do anything with any other
854 # branches fetched via the refspec.
855 ensure_valid_ref_format "$2"
856
857 cmd_add_repository "$@"
858 else
859 say >&2 "fatal: parameters were '$*'"
860 die "Provide either a commit or a repository and commit."
861 fi
862 }
863
864 # Usage: cmd_add_repository REPOSITORY REFSPEC
865 cmd_add_repository () {
866 assert test $# = 2
867 echo "git fetch" "$@"
868 repository=$1
869 refspec=$2
870 git fetch "$@" || exit $?
871 cmd_add_commit FETCH_HEAD
872 }
873
874 # Usage: cmd_add_commit REV
875 cmd_add_commit () {
876 # The rev has already been validated by cmd_add(), we just
877 # need to normalize it.
878 assert test $# = 1
879 rev=$(git rev-parse --verify "$1^{commit}") || exit $?
880
881 debug "Adding $dir as '$rev'..."
882 if test -z "$arg_split_rejoin"
883 then
884 # Only bother doing this if this is a genuine 'add',
885 # not a synthetic 'add' from '--rejoin'.
886 git read-tree --prefix="$dir" $rev || exit $?
887 fi
888 git checkout -- "$dir" || exit $?
889 tree=$(git write-tree) || exit $?
890
891 headrev=$(git rev-parse --verify HEAD) || exit $?
892 if test -n "$headrev" && test "$headrev" != "$rev"
893 then
894 headp="-p $headrev"
895 else
896 headp=
897 fi
898
899 if test -n "$arg_addmerge_squash"
900 then
901 rev=$(new_squash_commit "" "" "$rev") || exit $?
902 commit=$(add_squashed_msg "$rev" "$dir" |
903 git commit-tree "$tree" $headp -p "$rev") || exit $?
904 else
905 revp=$(peel_committish "$rev") || exit $?
906 commit=$(add_msg "$dir" $headrev "$rev" |
907 git commit-tree "$tree" $headp -p "$revp") || exit $?
908 fi
909 git reset "$commit" || exit $?
910
911 say >&2 "Added dir '$dir'"
912 }
913
914 # Usage: cmd_split [REV] [REPOSITORY]
915 cmd_split () {
916 if test $# -eq 0
917 then
918 rev=$(git rev-parse HEAD)
919 elif test $# -eq 1 -o $# -eq 2
920 then
921 rev=$(git rev-parse -q --verify "$1^{commit}") ||
922 die "fatal: '$1' does not refer to a commit"
923 else
924 die "fatal: you must provide exactly one revision, and optionnally a repository. Got: '$*'"
925 fi
926 repository=""
927 if test "$#" = 2
928 then
929 repository="$2"
930 fi
931
932 if test -n "$arg_split_rejoin"
933 then
934 ensure_clean
935 fi
936
937 debug "Splitting $dir..."
938 cache_setup || exit $?
939
940 if test -n "$arg_split_onto"
941 then
942 debug "Reading history for --onto=$arg_split_onto..."
943 git rev-list $arg_split_onto |
944 while read rev
945 do
946 # the 'onto' history is already just the subdir, so
947 # any parent we find there can be used verbatim
948 debug "cache: $rev"
949 cache_set "$rev" "$rev"
950 done || exit $?
951 fi
952
953 unrevs="$(find_existing_splits "$dir" "$rev" "$repository")" || exit $?
954
955 # We can't restrict rev-list to only $dir here, because some of our
956 # parents have the $dir contents the root, and those won't match.
957 # (and rev-list --follow doesn't seem to solve this)
958 grl='git rev-list --topo-order --reverse --parents $rev $unrevs'
959 revmax=$(eval "$grl" | wc -l)
960 revcount=0
961 createcount=0
962 extracount=0
963 eval "$grl" |
964 while read rev parents
965 do
966 process_split_commit "$rev" "$parents"
967 done || exit $?
968
969 latest_new=$(cache_get latest_new) || exit $?
970 if test -z "$latest_new"
971 then
972 die "fatal: no new revisions were found"
973 fi
974
975 if test -n "$arg_split_rejoin"
976 then
977 debug "Merging split branch into HEAD..."
978 latest_old=$(cache_get latest_old) || exit $?
979 arg_addmerge_message="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" || exit $?
980 if test -z "$(find_latest_squash "$dir")"
981 then
982 cmd_add "$latest_new" >&2 || exit $?
983 else
984 cmd_merge "$latest_new" >&2 || exit $?
985 fi
986 fi
987 if test -n "$arg_split_branch"
988 then
989 if rev_exists "refs/heads/$arg_split_branch"
990 then
991 if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new"
992 then
993 die "fatal: branch '$arg_split_branch' is not an ancestor of commit '$latest_new'."
994 fi
995 action='Updated'
996 else
997 action='Created'
998 fi
999 git update-ref -m 'subtree split' \
1000 "refs/heads/$arg_split_branch" "$latest_new" || exit $?
1001 say >&2 "$action branch '$arg_split_branch'"
1002 fi
1003 echo "$latest_new"
1004 exit 0
1005 }
1006
1007 # Usage: cmd_merge REV [REPOSITORY]
1008 cmd_merge () {
1009 test $# -eq 1 -o $# -eq 2 ||
1010 die "fatal: you must provide exactly one revision, and optionally a repository. Got: '$*'"
1011 rev=$(git rev-parse -q --verify "$1^{commit}") ||
1012 die "fatal: '$1' does not refer to a commit"
1013 repository=""
1014 if test "$#" = 2
1015 then
1016 repository="$2"
1017 fi
1018 ensure_clean
1019
1020 if test -n "$arg_addmerge_squash"
1021 then
1022 first_split="$(find_latest_squash "$dir" "$repository")" || exit $?
1023 if test -z "$first_split"
1024 then
1025 die "fatal: can't squash-merge: '$dir' was never added."
1026 fi
1027 set $first_split
1028 old=$1
1029 sub=$2
1030 if test "$sub" = "$rev"
1031 then
1032 say >&2 "Subtree is already at commit $rev."
1033 exit 0
1034 fi
1035 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
1036 debug "New squash commit: $new"
1037 rev="$new"
1038 fi
1039
1040 if test -n "$arg_addmerge_message"
1041 then
1042 git merge --no-ff -Xsubtree="$arg_prefix" \
1043 --message="$arg_addmerge_message" "$rev"
1044 else
1045 git merge --no-ff -Xsubtree="$arg_prefix" $rev
1046 fi
1047 }
1048
1049 # Usage: cmd_pull REPOSITORY REMOTEREF
1050 cmd_pull () {
1051 if test $# -ne 2
1052 then
1053 die "fatal: you must provide <repository> <ref>"
1054 fi
1055 repository="$1"
1056 ref="$2"
1057 ensure_clean
1058 ensure_valid_ref_format "$ref"
1059 git fetch "$repository" "$ref" || exit $?
1060 cmd_merge FETCH_HEAD "$repository"
1061 }
1062
1063 # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF
1064 cmd_push () {
1065 if test $# -ne 2
1066 then
1067 die "fatal: you must provide <repository> <refspec>"
1068 fi
1069 if test -e "$dir"
1070 then
1071 repository=$1
1072 refspec=${2#+}
1073 remoteref=${refspec#*:}
1074 if test "$remoteref" = "$refspec"
1075 then
1076 localrevname_presplit=HEAD
1077 else
1078 localrevname_presplit=${refspec%%:*}
1079 fi
1080 ensure_valid_ref_format "$remoteref"
1081 localrev_presplit=$(git rev-parse -q --verify "$localrevname_presplit^{commit}") ||
1082 die "fatal: '$localrevname_presplit' does not refer to a commit"
1083
1084 echo "git push using: " "$repository" "$refspec"
1085 localrev=$(cmd_split "$localrev_presplit" "$repository") || die
1086 git push "$repository" "$localrev":"refs/heads/$remoteref"
1087 else
1088 die "fatal: '$dir' must already exist. Try 'git subtree add'."
1089 fi
1090 }
1091
1092 main "$@"