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#
7if test $# -eq 0
8then
9 set -- -h
10fi
11OPTS_SPEC="\
12git subtree add --prefix=<prefix> <commit>
13git subtree add --prefix=<prefix> <repository> <ref>
14git subtree merge --prefix=<prefix> <commit>
15git subtree pull --prefix=<prefix> <repository> <ref>
16git subtree push --prefix=<prefix> <repository> <ref>
17git subtree split --prefix=<prefix> <commit...>
18--
19h,help show the help
20q quiet
21d show debug messages
22P,prefix= the name of the subdir to split out
23m,message= use the given message as the commit message for the merge commit
24 options for 'split'
25annotate= add a prefix to commit message of new commits
26b,branch= create a new branch from the split subtree
27ignore-joins ignore prior --rejoin commits
28onto= try connecting new tree to an existing one
29rejoin merge the new branch back into HEAD
30 options for 'add', 'merge', and 'pull'
31squash merge subtree changes as a single commit
32"
33eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
34
35PATH=$PATH:$(git --exec-path)
36. git-sh-setup
37
38require_work_tree
39
40quiet=
41branch=
42debug=
43command=
44onto=
45rejoin=
46ignore_joins=
47annotate=
48squash=
49message=
50prefix=
51
52debug () {
53 if test -n "$debug"
54 then
55 printf "%s\n" "$*" >&2
56 fi
57}
58
59say () {
60 if test -z "$quiet"
61 then
62 printf "%s\n" "$*" >&2
63 fi
64}
65
66progress () {
67 if test -z "$quiet"
68 then
69 printf "%s\r" "$*" >&2
70 fi
71}
72
73assert () {
74 if ! "$@"
75 then
76 die "assertion failed: " "$@"
77 fi
78}
79
80
81while test $# -gt 0
82do
83 opt="$1"
84 shift
85
86 case "$opt" in
87 -q)
88 quiet=1
89 ;;
90 -d)
91 debug=1
92 ;;
93 --annotate)
94 annotate="$1"
95 shift
96 ;;
97 --no-annotate)
98 annotate=
99 ;;
100 -b)
101 branch="$1"
102 shift
103 ;;
104 -P)
105 prefix="${1%/}"
106 shift
107 ;;
108 -m)
109 message="$1"
110 shift
111 ;;
112 --no-prefix)
113 prefix=
114 ;;
115 --onto)
116 onto="$1"
117 shift
118 ;;
119 --no-onto)
120 onto=
121 ;;
122 --rejoin)
123 rejoin=1
124 ;;
125 --no-rejoin)
126 rejoin=
127 ;;
128 --ignore-joins)
129 ignore_joins=1
130 ;;
131 --no-ignore-joins)
132 ignore_joins=
133 ;;
134 --squash)
135 squash=1
136 ;;
137 --no-squash)
138 squash=
139 ;;
140 --)
141 break
142 ;;
143 *)
144 die "Unexpected option: $opt"
145 ;;
146 esac
147done
148
149command="$1"
150shift
151
152case "$command" in
153add|merge|pull)
154 default=
155 ;;
156split|push)
157 default="--default HEAD"
158 ;;
159*)
160 die "Unknown command '$command'"
161 ;;
162esac
163
164if test -z "$prefix"
165then
166 die "You must provide the --prefix option."
167fi
168
169case "$command" in
170add)
171 test -e "$prefix" &&
172 die "prefix '$prefix' already exists."
173 ;;
174*)
175 test -e "$prefix" ||
176 die "'$prefix' does not exist; use 'git subtree add'"
177 ;;
178esac
179
180dir="$(dirname "$prefix/.")"
181
182if test "$command" != "pull" &&
183 test "$command" != "add" &&
184 test "$command" != "push"
185then
186 revs=$(git rev-parse $default --revs-only "$@") || exit $?
187 dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
188 if test -n "$dirs"
189 then
190 die "Error: Use --prefix instead of bare filenames."
191 fi
192fi
193
194debug "command: {$command}"
195debug "quiet: {$quiet}"
196debug "revs: {$revs}"
197debug "dir: {$dir}"
198debug "opts: {$*}"
199debug
200
201cache_setup () {
202 cachedir="$GIT_DIR/subtree-cache/$$"
203 rm -rf "$cachedir" ||
204 die "Can't delete old cachedir: $cachedir"
205 mkdir -p "$cachedir" ||
206 die "Can't create new cachedir: $cachedir"
207 mkdir -p "$cachedir/notree" ||
208 die "Can't create new cachedir: $cachedir/notree"
209 debug "Using cachedir: $cachedir" >&2
210}
211
212cache_get () {
213 for oldrev in "$@"
214 do
215 if test -r "$cachedir/$oldrev"
216 then
217 read newrev <"$cachedir/$oldrev"
218 echo $newrev
219 fi
220 done
221}
222
223cache_miss () {
224 for oldrev in "$@"
225 do
226 if ! test -r "$cachedir/$oldrev"
227 then
228 echo $oldrev
229 fi
230 done
231}
232
233check_parents () {
234 missed=$(cache_miss "$@")
235 for miss in $missed
236 do
237 if ! test -r "$cachedir/notree/$miss"
238 then
239 debug " incorrect order: $miss"
240 fi
241 done
242}
243
244set_notree () {
245 echo "1" > "$cachedir/notree/$1"
246}
247
248cache_set () {
249 oldrev="$1"
250 newrev="$2"
251 if test "$oldrev" != "latest_old" &&
252 test "$oldrev" != "latest_new" &&
253 test -e "$cachedir/$oldrev"
254 then
255 die "cache for $oldrev already exists!"
256 fi
257 echo "$newrev" >"$cachedir/$oldrev"
258}
259
260rev_exists () {
261 if git rev-parse "$1" >/dev/null 2>&1
262 then
263 return 0
264 else
265 return 1
266 fi
267}
268
269rev_is_descendant_of_branch () {
270 newrev="$1"
271 branch="$2"
272 branch_hash=$(git rev-parse "$branch")
273 match=$(git rev-list -1 "$branch_hash" "^$newrev")
274
275 if test -z "$match"
276 then
277 return 0
278 else
279 return 1
280 fi
281}
282
283# if a commit doesn't have a parent, this might not work. But we only want
284# to remove the parent from the rev-list, and since it doesn't exist, it won't
285# be there anyway, so do nothing in that case.
286try_remove_previous () {
287 if rev_exists "$1^"
288 then
289 echo "^$1^"
290 fi
291}
292
293find_latest_squash () {
294 debug "Looking for latest squash ($dir)..."
295 dir="$1"
296 sq=
297 main=
298 sub=
299 git log --grep="^git-subtree-dir: $dir/*\$" \
300 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
301 while read a b junk
302 do
303 debug "$a $b $junk"
304 debug "{{$sq/$main/$sub}}"
305 case "$a" in
306 START)
307 sq="$b"
308 ;;
309 git-subtree-mainline:)
310 main="$b"
311 ;;
312 git-subtree-split:)
313 sub="$(git rev-parse "$b^0")" ||
314 die "could not rev-parse split hash $b from commit $sq"
315 ;;
316 END)
317 if test -n "$sub"
318 then
319 if test -n "$main"
320 then
321 # a rejoin commit?
322 # Pretend its sub was a squash.
323 sq="$sub"
324 fi
325 debug "Squash found: $sq $sub"
326 echo "$sq" "$sub"
327 break
328 fi
329 sq=
330 main=
331 sub=
332 ;;
333 esac
334 done
335}
336
337find_existing_splits () {
338 debug "Looking for prior splits..."
339 dir="$1"
340 revs="$2"
341 main=
342 sub=
343 local grep_format="^git-subtree-dir: $dir/*\$"
344 if test -n "$ignore_joins"
345 then
346 grep_format="^Add '$dir/' from commit '"
347 fi
348 git log --grep="$grep_format" \
349 --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
350 while read a b junk
351 do
352 case "$a" in
353 START)
354 sq="$b"
355 ;;
356 git-subtree-mainline:)
357 main="$b"
358 ;;
359 git-subtree-split:)
360 sub="$(git rev-parse "$b^0")" ||
361 die "could not rev-parse split hash $b from commit $sq"
362 ;;
363 END)
364 debug " Main is: '$main'"
365 if test -z "$main" -a -n "$sub"
366 then
367 # squash commits refer to a subtree
368 debug " Squash: $sq from $sub"
369 cache_set "$sq" "$sub"
370 fi
371 if test -n "$main" -a -n "$sub"
372 then
373 debug " Prior: $main -> $sub"
374 cache_set $main $sub
375 cache_set $sub $sub
376 try_remove_previous "$main"
377 try_remove_previous "$sub"
378 fi
379 main=
380 sub=
381 ;;
382 esac
383 done
384}
385
386copy_commit () {
387 # We're going to set some environment vars here, so
388 # do it in a subshell to get rid of them safely later
389 debug copy_commit "{$1}" "{$2}" "{$3}"
390 git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
391 (
392 read GIT_AUTHOR_NAME
393 read GIT_AUTHOR_EMAIL
394 read GIT_AUTHOR_DATE
395 read GIT_COMMITTER_NAME
396 read GIT_COMMITTER_EMAIL
397 read GIT_COMMITTER_DATE
398 export GIT_AUTHOR_NAME \
399 GIT_AUTHOR_EMAIL \
400 GIT_AUTHOR_DATE \
401 GIT_COMMITTER_NAME \
402 GIT_COMMITTER_EMAIL \
403 GIT_COMMITTER_DATE
404 (
405 printf "%s" "$annotate"
406 cat
407 ) |
408 git commit-tree "$2" $3 # reads the rest of stdin
409 ) || die "Can't copy commit $1"
410}
411
412add_msg () {
413 dir="$1"
414 latest_old="$2"
415 latest_new="$3"
416 if test -n "$message"
417 then
418 commit_message="$message"
419 else
420 commit_message="Add '$dir/' from commit '$latest_new'"
421 fi
422 cat <<-EOF
423 $commit_message
424
425 git-subtree-dir: $dir
426 git-subtree-mainline: $latest_old
427 git-subtree-split: $latest_new
428 EOF
429}
430
431add_squashed_msg () {
432 if test -n "$message"
433 then
434 echo "$message"
435 else
436 echo "Merge commit '$1' as '$2'"
437 fi
438}
439
440rejoin_msg () {
441 dir="$1"
442 latest_old="$2"
443 latest_new="$3"
444 if test -n "$message"
445 then
446 commit_message="$message"
447 else
448 commit_message="Split '$dir/' into commit '$latest_new'"
449 fi
450 cat <<-EOF
451 $commit_message
452
453 git-subtree-dir: $dir
454 git-subtree-mainline: $latest_old
455 git-subtree-split: $latest_new
456 EOF
457}
458
459squash_msg () {
460 dir="$1"
461 oldsub="$2"
462 newsub="$3"
463 newsub_short=$(git rev-parse --short "$newsub")
464
465 if test -n "$oldsub"
466 then
467 oldsub_short=$(git rev-parse --short "$oldsub")
468 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
469 echo
470 git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
471 git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
472 else
473 echo "Squashed '$dir/' content from commit $newsub_short"
474 fi
475
476 echo
477 echo "git-subtree-dir: $dir"
478 echo "git-subtree-split: $newsub"
479}
480
481toptree_for_commit () {
482 commit="$1"
483 git rev-parse --verify "$commit^{tree}" || exit $?
484}
485
486subtree_for_commit () {
487 commit="$1"
488 dir="$2"
489 git ls-tree "$commit" -- "$dir" |
490 while read mode type tree name
491 do
492 assert test "$name" = "$dir"
493 assert test "$type" = "tree" -o "$type" = "commit"
494 test "$type" = "commit" && continue # ignore submodules
495 echo $tree
496 break
497 done
498}
499
500tree_changed () {
501 tree=$1
502 shift
503 if test $# -ne 1
504 then
505 return 0 # weird parents, consider it changed
506 else
507 ptree=$(toptree_for_commit $1)
508 if test "$ptree" != "$tree"
509 then
510 return 0 # changed
511 else
512 return 1 # not changed
513 fi
514 fi
515}
516
517new_squash_commit () {
518 old="$1"
519 oldsub="$2"
520 newsub="$3"
521 tree=$(toptree_for_commit $newsub) || exit $?
522 if test -n "$old"
523 then
524 squash_msg "$dir" "$oldsub" "$newsub" |
525 git commit-tree "$tree" -p "$old" || exit $?
526 else
527 squash_msg "$dir" "" "$newsub" |
528 git commit-tree "$tree" || exit $?
529 fi
530}
531
532copy_or_skip () {
533 rev="$1"
534 tree="$2"
535 newparents="$3"
536 assert test -n "$tree"
537
538 identical=
539 nonidentical=
540 p=
541 gotparents=
542 for parent in $newparents
543 do
544 ptree=$(toptree_for_commit $parent) || exit $?
545 test -z "$ptree" && continue
546 if test "$ptree" = "$tree"
547 then
548 # an identical parent could be used in place of this rev.
549 identical="$parent"
550 else
551 nonidentical="$parent"
552 fi
553
554 # sometimes both old parents map to the same newparent;
555 # eliminate duplicates
556 is_new=1
557 for gp in $gotparents
558 do
559 if test "$gp" = "$parent"
560 then
561 is_new=
562 break
563 fi
564 done
565 if test -n "$is_new"
566 then
567 gotparents="$gotparents $parent"
568 p="$p -p $parent"
569 fi
570 done
571
572 copycommit=
573 if test -n "$identical" && test -n "$nonidentical"
574 then
575 extras=$(git rev-list --count $identical..$nonidentical)
576 if test "$extras" -ne 0
577 then
578 # we need to preserve history along the other branch
579 copycommit=1
580 fi
581 fi
582 if test -n "$identical" && test -z "$copycommit"
583 then
584 echo $identical
585 else
586 copy_commit "$rev" "$tree" "$p" || exit $?
587 fi
588}
589
590ensure_clean () {
591 if ! git diff-index HEAD --exit-code --quiet 2>&1
592 then
593 die "Working tree has modifications. Cannot add."
594 fi
595 if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
596 then
597 die "Index has modifications. Cannot add."
598 fi
599}
600
601ensure_valid_ref_format () {
602 git check-ref-format "refs/heads/$1" ||
603 die "'$1' does not look like a ref"
604}
605
606process_split_commit () {
607 local rev="$1"
608 local parents="$2"
609 revcount=$(($revcount + 1))
610 progress "$revcount/$revmax ($createcount)"
611 debug "Processing commit: $rev"
612 exists=$(cache_get "$rev")
613 if test -n "$exists"
614 then
615 debug " prior: $exists"
616 return
617 fi
618 createcount=$(($createcount + 1))
619 debug " parents: $parents"
620 newparents=$(cache_get $parents)
621 debug " newparents: $newparents"
622
623 tree=$(subtree_for_commit "$rev" "$dir")
624 debug " tree is: $tree"
625
626 check_parents $parents
627
628 # ugly. is there no better way to tell if this is a subtree
629 # vs. a mainline commit? Does it matter?
630 if test -z "$tree"
631 then
632 set_notree "$rev"
633 if test -n "$newparents"
634 then
635 cache_set "$rev" "$rev"
636 fi
637 return
638 fi
639
640 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
641 debug " newrev is: $newrev"
642 cache_set "$rev" "$newrev"
643 cache_set latest_new "$newrev"
644 cache_set latest_old "$rev"
645}
646
647cmd_add () {
648 if test -e "$dir"
649 then
650 die "'$dir' already exists. Cannot add."
651 fi
652
653 ensure_clean
654
655 if test $# -eq 1
656 then
657 git rev-parse -q --verify "$1^{commit}" >/dev/null ||
658 die "'$1' does not refer to a commit"
659
660 cmd_add_commit "$@"
661
662 elif test $# -eq 2
663 then
664 # Technically we could accept a refspec here but we're
665 # just going to turn around and add FETCH_HEAD under the
666 # specified directory. Allowing a refspec might be
667 # misleading because we won't do anything with any other
668 # branches fetched via the refspec.
669 ensure_valid_ref_format "$2"
670
671 cmd_add_repository "$@"
672 else
673 say "error: parameters were '$@'"
674 die "Provide either a commit or a repository and commit."
675 fi
676}
677
678cmd_add_repository () {
679 echo "git fetch" "$@"
680 repository=$1
681 refspec=$2
682 git fetch "$@" || exit $?
683 revs=FETCH_HEAD
684 set -- $revs
685 cmd_add_commit "$@"
686}
687
688cmd_add_commit () {
689 revs=$(git rev-parse $default --revs-only "$@") || exit $?
690 set -- $revs
691 rev="$1"
692
693 debug "Adding $dir as '$rev'..."
694 git read-tree --prefix="$dir" $rev || exit $?
695 git checkout -- "$dir" || exit $?
696 tree=$(git write-tree) || exit $?
697
698 headrev=$(git rev-parse HEAD) || exit $?
699 if test -n "$headrev" && test "$headrev" != "$rev"
700 then
701 headp="-p $headrev"
702 else
703 headp=
704 fi
705
706 if test -n "$squash"
707 then
708 rev=$(new_squash_commit "" "" "$rev") || exit $?
709 commit=$(add_squashed_msg "$rev" "$dir" |
710 git commit-tree "$tree" $headp -p "$rev") || exit $?
711 else
712 revp=$(peel_committish "$rev") &&
713 commit=$(add_msg "$dir" $headrev "$rev" |
714 git commit-tree "$tree" $headp -p "$revp") || exit $?
715 fi
716 git reset "$commit" || exit $?
717
718 say "Added dir '$dir'"
719}
720
721cmd_split () {
722 debug "Splitting $dir..."
723 cache_setup || exit $?
724
725 if test -n "$onto"
726 then
727 debug "Reading history for --onto=$onto..."
728 git rev-list $onto |
729 while read rev
730 do
731 # the 'onto' history is already just the subdir, so
732 # any parent we find there can be used verbatim
733 debug " cache: $rev"
734 cache_set "$rev" "$rev"
735 done
736 fi
737
738 unrevs="$(find_existing_splits "$dir" "$revs")"
739
740 # We can't restrict rev-list to only $dir here, because some of our
741 # parents have the $dir contents the root, and those won't match.
742 # (and rev-list --follow doesn't seem to solve this)
743 grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
744 revmax=$(eval "$grl" | wc -l)
745 revcount=0
746 createcount=0
747 eval "$grl" |
748 while read rev parents
749 do
750 process_split_commit "$rev" "$parents"
751 done || exit $?
752
753 latest_new=$(cache_get latest_new)
754 if test -z "$latest_new"
755 then
756 die "No new revisions were found"
757 fi
758
759 if test -n "$rejoin"
760 then
761 debug "Merging split branch into HEAD..."
762 latest_old=$(cache_get latest_old)
763 git merge -s ours \
764 --allow-unrelated-histories \
765 -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
766 "$latest_new" >&2 || exit $?
767 fi
768 if test -n "$branch"
769 then
770 if rev_exists "refs/heads/$branch"
771 then
772 if ! rev_is_descendant_of_branch "$latest_new" "$branch"
773 then
774 die "Branch '$branch' is not an ancestor of commit '$latest_new'."
775 fi
776 action='Updated'
777 else
778 action='Created'
779 fi
780 git update-ref -m 'subtree split' \
781 "refs/heads/$branch" "$latest_new" || exit $?
782 say "$action branch '$branch'"
783 fi
784 echo "$latest_new"
785 exit 0
786}
787
788cmd_merge () {
789 revs=$(git rev-parse $default --revs-only "$@") || exit $?
790 ensure_clean
791
792 set -- $revs
793 if test $# -ne 1
794 then
795 die "You must provide exactly one revision. Got: '$revs'"
796 fi
797 rev="$1"
798
799 if test -n "$squash"
800 then
801 first_split="$(find_latest_squash "$dir")"
802 if test -z "$first_split"
803 then
804 die "Can't squash-merge: '$dir' was never added."
805 fi
806 set $first_split
807 old=$1
808 sub=$2
809 if test "$sub" = "$rev"
810 then
811 say "Subtree is already at commit $rev."
812 exit 0
813 fi
814 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
815 debug "New squash commit: $new"
816 rev="$new"
817 fi
818
819 version=$(git version)
820 if test "$version" \< "git version 1.7"
821 then
822 if test -n "$message"
823 then
824 git merge -s subtree --message="$message" "$rev"
825 else
826 git merge -s subtree "$rev"
827 fi
828 else
829 if test -n "$message"
830 then
831 git merge -Xsubtree="$prefix" \
832 --message="$message" "$rev"
833 else
834 git merge -Xsubtree="$prefix" $rev
835 fi
836 fi
837}
838
839cmd_pull () {
840 if test $# -ne 2
841 then
842 die "You must provide <repository> <ref>"
843 fi
844 ensure_clean
845 ensure_valid_ref_format "$2"
846 git fetch "$@" || exit $?
847 revs=FETCH_HEAD
848 set -- $revs
849 cmd_merge "$@"
850}
851
852cmd_push () {
853 if test $# -ne 2
854 then
855 die "You must provide <repository> <ref>"
856 fi
857 ensure_valid_ref_format "$2"
858 if test -e "$dir"
859 then
860 repository=$1
861 refspec=$2
862 echo "git push using: " "$repository" "$refspec"
863 localrev=$(git subtree split --prefix="$prefix") || die
864 git push "$repository" "$localrev":"refs/heads/$refspec"
865 else
866 die "'$dir' must already exist. Try 'git subtree add'."
867 fi
868}
869
870"cmd_$command" "$@"