3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
11 git subtree add --prefix=<prefix> <commit>
12 git subtree split [options...] --prefix=<prefix> <commit...>
13 git subtree merge --prefix=<prefix> <commit>
14 git subtree pull --prefix=<prefix> <repository> <refspec...>
19 prefix= the name of the subdir to split out
21 annotate= add a prefix to commit message of new commits
22 onto= try connecting new tree to an existing one
23 rejoin merge the new branch back into HEAD
24 ignore-joins ignore prior --rejoin commits
26 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
40 if [ -n "$debug" ]; then
47 if [ -z "$quiet" ]; then
57 die
"assertion failed: " "$@"
64 while [ $# -gt 0 ]; do
70 --annotate) annotate
="$1"; shift ;;
71 --no-annotate) annotate
= ;;
72 --prefix) prefix
="$1"; shift ;;
73 --no-prefix) prefix
= ;;
74 --onto) onto
="$1"; shift ;;
77 --no-rejoin) rejoin
= ;;
78 --ignore-joins) ignore_joins
=1 ;;
79 --no-ignore-joins) ignore_joins
= ;;
87 add|merge|pull
) default
= ;;
88 split) default
="--default HEAD" ;;
89 *) die
"Unknown command '$command'" ;;
92 if [ -z "$prefix" ]; then
93 die
"You must provide the --prefix option."
97 if [ "$command" != "pull" ]; then
98 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
99 dirs="$(git rev-parse --no-revs --no-flags "$@
")" ||
exit $?
100 if [ -n "$dirs" ]; then
101 die
"Error: Use --prefix instead of bare filenames."
105 debug
"command: {$command}"
106 debug
"quiet: {$quiet}"
107 debug
"revs: {$revs}"
114 cachedir
="$GIT_DIR/subtree-cache/$$"
115 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
116 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
117 debug
"Using cachedir: $cachedir" >&2
123 if [ -r "$cachedir/$oldrev" ]; then
124 read newrev
<"$cachedir/$oldrev"
134 if [ "$oldrev" != "latest_old" \
135 -a "$oldrev" != "latest_new" \
136 -a -e "$cachedir/$oldrev" ]; then
137 die
"cache for $oldrev already exists!"
139 echo "$newrev" >"$cachedir/$oldrev"
142 # if a commit doesn't have a parent, this might not work. But we only want
143 # to remove the parent from the rev-list, and since it doesn't exist, it won't
144 # be there anyway, so do nothing in that case.
145 try_remove_previous
()
147 if git rev-parse
"$1^" >/dev
/null
2>&1; then
152 find_existing_splits
()
154 debug
"Looking for prior splits..."
157 git log
--grep="^git-subtree-dir: $dir\$" \
158 --pretty=format
:'%s%n%n%b%nEND' $revs |
159 while read a b junk
; do
161 git-subtree-mainline
:) main
="$b" ;;
162 git-subtree-split
:) sub
="$b" ;;
164 if [ -n "$main" -a -n "$sub" ]; then
165 debug
" Prior: $main -> $sub"
167 try_remove_previous
"$main"
168 try_remove_previous
"$sub"
179 # We're doing to set some environment vars here, so
180 # do it in a subshell to get rid of them safely later
181 debug copy_commit
"{$1}" "{$2}" "{$3}"
182 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
185 read GIT_AUTHOR_EMAIL
187 read GIT_COMMITTER_NAME
188 read GIT_COMMITTER_EMAIL
189 read GIT_COMMITTER_DATE
190 export GIT_AUTHOR_NAME \
194 GIT_COMMITTER_EMAIL \
196 (echo -n "$annotate"; cat ) |
197 git commit-tree
"$2" $3 # reads the rest of stdin
198 ) || die
"Can't copy commit $1"
207 Add '$dir/' from commit '$latest_new'
209 git-subtree-dir: $dir
210 git-subtree-mainline: $latest_old
211 git-subtree-split: $latest_new
221 Split '$dir/' into commit '$latest_new'
223 git-subtree-dir: $dir
224 git-subtree-mainline: $latest_old
225 git-subtree-split: $latest_new
232 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
239 git ls-tree
"$commit" -- "$dir" |
240 while read mode
type tree name
; do
241 assert
[ "$name" = "$dir" ]
251 if [ $# -ne 1 ]; then
252 return 0 # weird parents, consider it changed
254 ptree
=$
(toptree_for_commit
$1)
255 if [ "$ptree" != "$tree" ]; then
258 return 1 # not changed
268 assert
[ -n "$tree" ]
274 for parent
in $newparents; do
275 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
276 [ -z "$ptree" ] && continue
277 if [ "$ptree" = "$tree" ]; then
278 # an identical parent could be used in place of this rev.
281 nonidentical
="$parent"
284 # sometimes both old parents map to the same newparent;
285 # eliminate duplicates
287 for gp
in $gotparents; do
288 if [ "$gp" = "$parent" ]; then
293 if [ -n "$is_new" ]; then
294 gotparents
="$gotparents $parent"
299 if [ -n "$identical" ]; then
302 copy_commit
$rev $tree "$p" ||
exit $?
308 if ! git diff-index HEAD
--exit-code --quiet; then
309 die
"Working tree has modifications. Cannot add."
311 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
312 die
"Index has modifications. Cannot add."
318 if [ -e "$dir" ]; then
319 die
"'$dir' already exists. Cannot add."
324 if [ $# -ne 1 ]; then
325 die
"You must provide exactly one revision. Got: '$revs'"
329 debug
"Adding $dir as '$rev'..."
330 git read-tree
--prefix="$dir" $rev ||
exit $?
331 git checkout
"$dir" ||
exit $?
332 tree
=$
(git write-tree
) ||
exit $?
334 headrev
=$
(git rev-parse HEAD
) ||
exit $?
335 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
340 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
341 git commit-tree
$tree $headp -p "$rev") ||
exit $?
342 git
reset "$commit" ||
exit $?
347 debug
"Splitting $dir..."
348 cache_setup ||
exit $?
350 if [ -n "$onto" ]; then
351 debug
"Reading history for --onto=$onto..."
354 # the 'onto' history is already just the subdir, so
355 # any parent we find there can be used verbatim
361 if [ -n "$ignore_joins" ]; then
364 unrevs
="$(find_existing_splits "$dir" "$revs")"
367 # We can't restrict rev-list to only $dir here, because some of our
368 # parents have the $dir contents the root, and those won't match.
369 # (and rev-list --follow doesn't seem to solve this)
370 grl
='git rev-list --reverse --parents $revs $unrevs'
371 revmax
=$
(eval "$grl" |
wc -l)
375 while read rev parents
; do
376 revcount
=$
(($revcount + 1))
377 say
-n "$revcount/$revmax ($createcount)
"
378 debug
"Processing commit: $rev"
379 exists
=$
(cache_get
$rev)
380 if [ -n "$exists" ]; then
381 debug
" prior: $exists"
384 createcount
=$
(($createcount + 1))
385 debug
" parents: $parents"
386 newparents
=$
(cache_get
$parents)
387 debug
" newparents: $newparents"
389 tree
=$
(subtree_for_commit
$rev "$dir")
390 debug
" tree is: $tree"
391 [ -z $tree ] && continue
393 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
394 debug
" newrev is: $newrev"
395 cache_set
$rev $newrev
396 cache_set latest_new
$newrev
397 cache_set latest_old
$rev
399 latest_new
=$
(cache_get latest_new
)
400 if [ -z "$latest_new" ]; then
401 die
"No new revisions were found"
404 if [ -n "$rejoin" ]; then
405 debug
"Merging split branch into HEAD..."
406 latest_old
=$
(cache_get latest_old
)
408 -m "$(merge_msg $dir $latest_old $latest_new)" \
420 if [ $# -ne 1 ]; then
421 die
"You must provide exactly one revision. Got: '$revs'"
425 git merge
-s subtree
$rev
432 git pull
-s subtree
"$@"