1 #!/bin/bash
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 if [ $# -eq 0 ]; then
8 set -- -h
9 fi
10 OPTS_SPEC="\
11 git subtree add --prefix=<prefix> <commit>
12 git subtree merge --prefix=<prefix> <commit>
13 git subtree pull --prefix=<prefix> <repository> <refspec...>
14 git subtree split --prefix=<prefix> <commit...>
15 --
16 h,help show the help
17 q quiet
18 d show debug messages
19 prefix= the name of the subdir to split out
20 options for 'split'
21 annotate= add a prefix to commit message of new commits
22 b,branch= create a new branch from the split subtree
23 ignore-joins ignore prior --rejoin commits
24 onto= try connecting new tree to an existing one
25 rejoin merge the new branch back into HEAD
26 options for 'add', 'merge', and 'pull'
27 squash merge subtree changes as a single commit
28 "
29 eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)
30 PATH=$(git --exec-path):$PATH
31 . git-sh-setup
32 require_work_tree
34 quiet=
35 branch=
36 debug=
37 command=
38 onto=
39 rejoin=
40 ignore_joins=
41 annotate=
42 squash=
44 debug()
45 {
46 if [ -n "$debug" ]; then
47 echo "$@" >&2
48 fi
49 }
51 say()
52 {
53 if [ -z "$quiet" ]; then
54 echo "$@" >&2
55 fi
56 }
58 assert()
59 {
60 if "$@"; then
61 :
62 else
63 die "assertion failed: " "$@"
64 fi
65 }
68 #echo "Options: $*"
70 while [ $# -gt 0 ]; do
71 opt="$1"
72 shift
73 case "$opt" in
74 -q) quiet=1 ;;
75 -d) debug=1 ;;
76 --annotate) annotate="$1"; shift ;;
77 --no-annotate) annotate= ;;
78 -b) branch="$1"; shift ;;
79 --prefix) prefix="$1"; shift ;;
80 --no-prefix) prefix= ;;
81 --onto) onto="$1"; shift ;;
82 --no-onto) onto= ;;
83 --rejoin) rejoin=1 ;;
84 --no-rejoin) rejoin= ;;
85 --ignore-joins) ignore_joins=1 ;;
86 --no-ignore-joins) ignore_joins= ;;
87 --squash) squash=1 ;;
88 --no-squash) squash= ;;
89 --) break ;;
90 *) die "Unexpected option: $opt" ;;
91 esac
92 done
94 command="$1"
95 shift
96 case "$command" in
97 add|merge|pull) default= ;;
98 split) default="--default HEAD" ;;
99 *) die "Unknown command '$command'" ;;
100 esac
102 if [ -z "$prefix" ]; then
103 die "You must provide the --prefix option."
104 fi
105 dir="$(dirname "$prefix/.")"
107 if [ "$command" != "pull" ]; then
108 revs=$(git rev-parse $default --revs-only "$@") || exit $?
109 dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $?
110 if [ -n "$dirs" ]; then
111 die "Error: Use --prefix instead of bare filenames."
112 fi
113 fi
115 debug "command: {$command}"
116 debug "quiet: {$quiet}"
117 debug "revs: {$revs}"
118 debug "dir: {$dir}"
119 debug "opts: {$*}"
120 debug
122 cache_setup()
123 {
124 cachedir="$GIT_DIR/subtree-cache/$$"
125 rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir"
126 mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir"
127 debug "Using cachedir: $cachedir" >&2
128 }
130 cache_get()
131 {
132 for oldrev in $*; do
133 if [ -r "$cachedir/$oldrev" ]; then
134 read newrev <"$cachedir/$oldrev"
135 echo $newrev
136 fi
137 done
138 }
140 cache_set()
141 {
142 oldrev="$1"
143 newrev="$2"
144 if [ "$oldrev" != "latest_old" \
145 -a "$oldrev" != "latest_new" \
146 -a -e "$cachedir/$oldrev" ]; then
147 die "cache for $oldrev already exists!"
148 fi
149 echo "$newrev" >"$cachedir/$oldrev"
150 }
152 rev_exists()
153 {
154 if git rev-parse "$1" >/dev/null 2>&1; then
155 return 0
156 else
157 return 1
158 fi
159 }
161 # if a commit doesn't have a parent, this might not work. But we only want
162 # to remove the parent from the rev-list, and since it doesn't exist, it won't
163 # be there anyway, so do nothing in that case.
164 try_remove_previous()
165 {
166 if rev_exists "$1^"; then
167 echo "^$1^"
168 fi
169 }
171 find_latest_squash()
172 {
173 debug "Looking for latest squash ($dir)..."
174 dir="$1"
175 sq=
176 main=
177 sub=
178 git log --grep="^git-subtree-dir: $dir/*\$" \
179 --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
180 while read a b junk; do
181 debug "$a $b $junk"
182 debug "{{$sq/$main/$sub}}"
183 case "$a" in
184 START) sq="$b" ;;
185 git-subtree-mainline:) main="$b" ;;
186 git-subtree-split:) sub="$b" ;;
187 END)
188 if [ -n "$sub" ]; then
189 if [ -n "$main" ]; then
190 # a rejoin commit?
191 # Pretend its sub was a squash.
192 sq="$sub"
193 fi
194 debug "Squash found: $sq $sub"
195 echo "$sq" "$sub"
196 break
197 fi
198 sq=
199 main=
200 sub=
201 ;;
202 esac
203 done
204 }
206 find_existing_splits()
207 {
208 debug "Looking for prior splits..."
209 dir="$1"
210 revs="$2"
211 main=
212 sub=
213 git log --grep="^git-subtree-dir: $dir/*\$" \
214 --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
215 while read a b junk; do
216 case "$a" in
217 START) main="$b"; sq="$b" ;;
218 git-subtree-mainline:) main="$b" ;;
219 git-subtree-split:) sub="$b" ;;
220 END)
221 if [ -z "$main" -a -n "$sub" ]; then
222 # squash commits refer to a subtree
223 cache_set "$sq" "$sub"
224 fi
225 if [ -n "$main" -a -n "$sub" ]; then
226 debug " Prior: $main -> $sub"
227 cache_set $main $sub
228 try_remove_previous "$main"
229 try_remove_previous "$sub"
230 fi
231 main=
232 sub=
233 ;;
234 esac
235 done
236 }
238 copy_commit()
239 {
240 # We're going to set some environment vars here, so
241 # do it in a subshell to get rid of them safely later
242 debug copy_commit "{$1}" "{$2}" "{$3}"
243 git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
244 (
245 read GIT_AUTHOR_NAME
246 read GIT_AUTHOR_EMAIL
247 read GIT_AUTHOR_DATE
248 read GIT_COMMITTER_NAME
249 read GIT_COMMITTER_EMAIL
250 read GIT_COMMITTER_DATE
251 export GIT_AUTHOR_NAME \
252 GIT_AUTHOR_EMAIL \
253 GIT_AUTHOR_DATE \
254 GIT_COMMITTER_NAME \
255 GIT_COMMITTER_EMAIL \
256 GIT_COMMITTER_DATE
257 (echo -n "$annotate"; cat ) |
258 git commit-tree "$2" $3 # reads the rest of stdin
259 ) || die "Can't copy commit $1"
260 }
262 add_msg()
263 {
264 dir="$1"
265 latest_old="$2"
266 latest_new="$3"
267 cat <<-EOF
268 Add '$dir/' from commit '$latest_new'
270 git-subtree-dir: $dir
271 git-subtree-mainline: $latest_old
272 git-subtree-split: $latest_new
273 EOF
274 }
276 rejoin_msg()
277 {
278 dir="$1"
279 latest_old="$2"
280 latest_new="$3"
281 cat <<-EOF
282 Split '$dir/' into commit '$latest_new'
284 git-subtree-dir: $dir
285 git-subtree-mainline: $latest_old
286 git-subtree-split: $latest_new
287 EOF
288 }
290 squash_msg()
291 {
292 dir="$1"
293 oldsub="$2"
294 newsub="$3"
295 newsub_short=$(git rev-parse --short "$newsub")
297 if [ -n "$oldsub" ]; then
298 oldsub_short=$(git rev-parse --short "$oldsub")
299 echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
300 echo
301 git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
302 git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
303 else
304 echo "Squashed '$dir/' content from commit $newsub_short"
305 fi
307 echo
308 echo "git-subtree-dir: $dir"
309 echo "git-subtree-split: $newsub"
310 }
312 toptree_for_commit()
313 {
314 commit="$1"
315 git log -1 --pretty=format:'%T' "$commit" -- || exit $?
316 }
318 subtree_for_commit()
319 {
320 commit="$1"
321 dir="$2"
322 git ls-tree "$commit" -- "$dir" |
323 while read mode type tree name; do
324 assert [ "$name" = "$dir" ]
325 assert [ "$type" = "tree" ]
326 echo $tree
327 break
328 done
329 }
331 tree_changed()
332 {
333 tree=$1
334 shift
335 if [ $# -ne 1 ]; then
336 return 0 # weird parents, consider it changed
337 else
338 ptree=$(toptree_for_commit $1)
339 if [ "$ptree" != "$tree" ]; then
340 return 0 # changed
341 else
342 return 1 # not changed
343 fi
344 fi
345 }
347 new_squash_commit()
348 {
349 old="$1"
350 oldsub="$2"
351 newsub="$3"
352 tree=$(toptree_for_commit $newsub) || exit $?
353 if [ -n "$old" ]; then
354 squash_msg "$dir" "$oldsub" "$newsub" |
355 git commit-tree "$tree" -p "$old" || exit $?
356 else
357 squash_msg "$dir" "" "$newsub" |
358 git commit-tree "$tree" || exit $?
359 fi
360 }
362 copy_or_skip()
363 {
364 rev="$1"
365 tree="$2"
366 newparents="$3"
367 assert [ -n "$tree" ]
369 identical=
370 nonidentical=
371 p=
372 gotparents=
373 for parent in $newparents; do
374 ptree=$(toptree_for_commit $parent) || exit $?
375 [ -z "$ptree" ] && continue
376 if [ "$ptree" = "$tree" ]; then
377 # an identical parent could be used in place of this rev.
378 identical="$parent"
379 else
380 nonidentical="$parent"
381 fi
383 # sometimes both old parents map to the same newparent;
384 # eliminate duplicates
385 is_new=1
386 for gp in $gotparents; do
387 if [ "$gp" = "$parent" ]; then
388 is_new=
389 break
390 fi
391 done
392 if [ -n "$is_new" ]; then
393 gotparents="$gotparents $parent"
394 p="$p -p $parent"
395 fi
396 done
398 if [ -n "$identical" ]; then
399 echo $identical
400 else
401 copy_commit $rev $tree "$p" || exit $?
402 fi
403 }
405 ensure_clean()
406 {
407 if ! git diff-index HEAD --exit-code --quiet; then
408 die "Working tree has modifications. Cannot add."
409 fi
410 if ! git diff-index --cached HEAD --exit-code --quiet; then
411 die "Index has modifications. Cannot add."
412 fi
413 }
415 cmd_add()
416 {
417 if [ -e "$dir" ]; then
418 die "'$dir' already exists. Cannot add."
419 fi
420 ensure_clean
422 set -- $revs
423 if [ $# -ne 1 ]; then
424 die "You must provide exactly one revision. Got: '$revs'"
425 fi
426 rev="$1"
428 debug "Adding $dir as '$rev'..."
429 git read-tree --prefix="$dir" $rev || exit $?
430 git checkout -- "$dir" || exit $?
431 tree=$(git write-tree) || exit $?
433 headrev=$(git rev-parse HEAD) || exit $?
434 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
435 headp="-p $headrev"
436 else
437 headp=
438 fi
440 if [ -n "$squash" ]; then
441 rev=$(new_squash_commit "" "" "$rev") || exit $?
442 commit=$(echo "Merge commit '$rev' as '$dir'" |
443 git commit-tree $tree $headp -p "$rev") || exit $?
444 else
445 commit=$(add_msg "$dir" "$headrev" "$rev" |
446 git commit-tree $tree $headp -p "$rev") || exit $?
447 fi
448 git reset "$commit" || exit $?
450 say "Added dir '$dir'"
451 }
453 cmd_split()
454 {
455 if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then
456 die "Branch '$branch' already exists."
457 fi
459 debug "Splitting $dir..."
460 cache_setup || exit $?
462 if [ -n "$onto" ]; then
463 debug "Reading history for --onto=$onto..."
464 git rev-list $onto |
465 while read rev; do
466 # the 'onto' history is already just the subdir, so
467 # any parent we find there can be used verbatim
468 debug " cache: $rev"
469 cache_set $rev $rev
470 done
471 fi
473 if [ -n "$ignore_joins" ]; then
474 unrevs=
475 else
476 unrevs="$(find_existing_splits "$dir" "$revs")"
477 fi
479 # We can't restrict rev-list to only $dir here, because some of our
480 # parents have the $dir contents the root, and those won't match.
481 # (and rev-list --follow doesn't seem to solve this)
482 grl='git rev-list --reverse --parents $revs $unrevs'
483 revmax=$(eval "$grl" | wc -l)
484 revcount=0
485 createcount=0
486 eval "$grl" |
487 while read rev parents; do
488 revcount=$(($revcount + 1))
489 say -n "$revcount/$revmax ($createcount)\r"
490 debug "Processing commit: $rev"
491 exists=$(cache_get $rev)
492 if [ -n "$exists" ]; then
493 debug " prior: $exists"
494 continue
495 fi
496 createcount=$(($createcount + 1))
497 debug " parents: $parents"
498 newparents=$(cache_get $parents)
499 debug " newparents: $newparents"
501 tree=$(subtree_for_commit $rev "$dir")
502 debug " tree is: $tree"
504 # ugly. is there no better way to tell if this is a subtree
505 # vs. a mainline commit? Does it matter?
506 [ -z $tree ] && continue
508 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
509 debug " newrev is: $newrev"
510 cache_set $rev $newrev
511 cache_set latest_new $newrev
512 cache_set latest_old $rev
513 done || exit $?
514 latest_new=$(cache_get latest_new)
515 if [ -z "$latest_new" ]; then
516 die "No new revisions were found"
517 fi
519 if [ -n "$rejoin" ]; then
520 debug "Merging split branch into HEAD..."
521 latest_old=$(cache_get latest_old)
522 git merge -s ours \
523 -m "$(rejoin_msg $dir $latest_old $latest_new)" \
524 $latest_new >&2 || exit $?
525 fi
526 if [ -n "$branch" ]; then
527 git update-ref -m 'subtree split' "refs/heads/$branch" \
528 $latest_new "" || exit $?
529 say "Created branch '$branch'"
530 fi
531 echo $latest_new
532 exit 0
533 }
535 cmd_merge()
536 {
537 ensure_clean
539 set -- $revs
540 if [ $# -ne 1 ]; then
541 die "You must provide exactly one revision. Got: '$revs'"
542 fi
543 rev="$1"
545 if [ -n "$squash" ]; then
546 first_split="$(find_latest_squash "$dir")"
547 if [ -z "$first_split" ]; then
548 die "Can't squash-merge: '$dir' was never added."
549 fi
550 set $first_split
551 old=$1
552 sub=$2
553 if [ "$sub" = "$rev" ]; then
554 say "Subtree is already at commit $rev."
555 exit 0
556 fi
557 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
558 debug "New squash commit: $new"
559 rev="$new"
560 fi
562 git merge -s subtree $rev
563 }
565 cmd_pull()
566 {
567 ensure_clean
568 set -x
569 git pull -s subtree "$@"
570 }
572 "cmd_$command" "$@"