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