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