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 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
27 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
42 if [ -n "$debug" ]; then
49 if [ -z "$quiet" ]; then
59 die
"assertion failed: " "$@"
66 while [ $# -gt 0 ]; do
72 --annotate) annotate
="$1"; shift ;;
73 --no-annotate) annotate
= ;;
74 -b) branch
="$1"; shift ;;
75 --prefix) prefix
="$1"; shift ;;
76 --no-prefix) prefix
= ;;
77 --onto) onto
="$1"; shift ;;
80 --no-rejoin) rejoin
= ;;
81 --ignore-joins) ignore_joins
=1 ;;
82 --no-ignore-joins) ignore_joins
= ;;
84 *) die
"Unexpected option: $opt" ;;
91 add|merge|pull
) default
= ;;
92 split) default
="--default HEAD" ;;
93 *) die
"Unknown command '$command'" ;;
96 if [ -z "$prefix" ]; then
97 die
"You must provide the --prefix option."
101 if [ "$command" != "pull" ]; then
102 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
103 dirs="$(git rev-parse --no-revs --no-flags "$@
")" ||
exit $?
104 if [ -n "$dirs" ]; then
105 die
"Error: Use --prefix instead of bare filenames."
109 debug
"command: {$command}"
110 debug
"quiet: {$quiet}"
111 debug
"revs: {$revs}"
118 cachedir
="$GIT_DIR/subtree-cache/$$"
119 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
120 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
121 debug
"Using cachedir: $cachedir" >&2
127 if [ -r "$cachedir/$oldrev" ]; then
128 read newrev
<"$cachedir/$oldrev"
138 if [ "$oldrev" != "latest_old" \
139 -a "$oldrev" != "latest_new" \
140 -a -e "$cachedir/$oldrev" ]; then
141 die
"cache for $oldrev already exists!"
143 echo "$newrev" >"$cachedir/$oldrev"
148 if git rev-parse
"$1" >/dev
/null
2>&1; then
155 # if a commit doesn't have a parent, this might not work. But we only want
156 # to remove the parent from the rev-list, and since it doesn't exist, it won't
157 # be there anyway, so do nothing in that case.
158 try_remove_previous
()
160 if rev_exists
"$1^"; then
165 find_existing_splits
()
167 debug
"Looking for prior splits..."
170 git log
--grep="^git-subtree-dir: $dir\$" \
171 --pretty=format
:'%s%n%n%b%nEND' $revs |
172 while read a b junk
; do
174 git-subtree-mainline
:) main
="$b" ;;
175 git-subtree-split
:) sub
="$b" ;;
177 if [ -n "$main" -a -n "$sub" ]; then
178 debug
" Prior: $main -> $sub"
180 try_remove_previous
"$main"
181 try_remove_previous
"$sub"
192 # We're going to set some environment vars here, so
193 # do it in a subshell to get rid of them safely later
194 debug copy_commit
"{$1}" "{$2}" "{$3}"
195 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
198 read GIT_AUTHOR_EMAIL
200 read GIT_COMMITTER_NAME
201 read GIT_COMMITTER_EMAIL
202 read GIT_COMMITTER_DATE
203 export GIT_AUTHOR_NAME \
207 GIT_COMMITTER_EMAIL \
209 (echo -n "$annotate"; cat ) |
210 git commit-tree
"$2" $3 # reads the rest of stdin
211 ) || die
"Can't copy commit $1"
220 Add '$dir/' from commit '$latest_new'
222 git-subtree-dir: $dir
223 git-subtree-mainline: $latest_old
224 git-subtree-split: $latest_new
234 Split '$dir/' into commit '$latest_new'
236 git-subtree-dir: $dir
237 git-subtree-mainline: $latest_old
238 git-subtree-split: $latest_new
245 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
252 git ls-tree
"$commit" -- "$dir" |
253 while read mode
type tree name
; do
254 assert
[ "$name" = "$dir" ]
264 if [ $# -ne 1 ]; then
265 return 0 # weird parents, consider it changed
267 ptree
=$
(toptree_for_commit
$1)
268 if [ "$ptree" != "$tree" ]; then
271 return 1 # not changed
281 assert
[ -n "$tree" ]
287 for parent
in $newparents; do
288 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
289 [ -z "$ptree" ] && continue
290 if [ "$ptree" = "$tree" ]; then
291 # an identical parent could be used in place of this rev.
294 nonidentical
="$parent"
297 # sometimes both old parents map to the same newparent;
298 # eliminate duplicates
300 for gp
in $gotparents; do
301 if [ "$gp" = "$parent" ]; then
306 if [ -n "$is_new" ]; then
307 gotparents
="$gotparents $parent"
312 if [ -n "$identical" ]; then
315 copy_commit
$rev $tree "$p" ||
exit $?
321 if ! git diff-index HEAD
--exit-code --quiet; then
322 die
"Working tree has modifications. Cannot add."
324 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
325 die
"Index has modifications. Cannot add."
331 if [ -e "$dir" ]; then
332 die
"'$dir' already exists. Cannot add."
337 if [ $# -ne 1 ]; then
338 die
"You must provide exactly one revision. Got: '$revs'"
342 debug
"Adding $dir as '$rev'..."
343 git read-tree
--prefix="$dir" $rev ||
exit $?
344 git checkout
"$dir" ||
exit $?
345 tree
=$
(git write-tree
) ||
exit $?
347 headrev
=$
(git rev-parse HEAD
) ||
exit $?
348 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
353 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
354 git commit-tree
$tree $headp -p "$rev") ||
exit $?
355 git
reset "$commit" ||
exit $?
360 if [ -n "$branch" ] && rev_exists
"refs/heads/$branch"; then
361 die
"Branch '$branch' already exists."
364 debug
"Splitting $dir..."
365 cache_setup ||
exit $?
367 if [ -n "$onto" ]; then
368 debug
"Reading history for --onto=$onto..."
371 # the 'onto' history is already just the subdir, so
372 # any parent we find there can be used verbatim
378 if [ -n "$ignore_joins" ]; then
381 unrevs
="$(find_existing_splits "$dir" "$revs")"
384 # We can't restrict rev-list to only $dir here, because some of our
385 # parents have the $dir contents the root, and those won't match.
386 # (and rev-list --follow doesn't seem to solve this)
387 grl
='git rev-list --reverse --parents $revs $unrevs'
388 revmax
=$
(eval "$grl" |
wc -l)
392 while read rev parents
; do
393 revcount
=$
(($revcount + 1))
394 say
-n "$revcount/$revmax ($createcount)
"
395 debug
"Processing commit: $rev"
396 exists
=$
(cache_get
$rev)
397 if [ -n "$exists" ]; then
398 debug
" prior: $exists"
401 createcount
=$
(($createcount + 1))
402 debug
" parents: $parents"
403 newparents
=$
(cache_get
$parents)
404 debug
" newparents: $newparents"
406 tree
=$
(subtree_for_commit
$rev "$dir")
407 debug
" tree is: $tree"
408 [ -z $tree ] && continue
410 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
411 debug
" newrev is: $newrev"
412 cache_set
$rev $newrev
413 cache_set latest_new
$newrev
414 cache_set latest_old
$rev
416 latest_new
=$
(cache_get latest_new
)
417 if [ -z "$latest_new" ]; then
418 die
"No new revisions were found"
421 if [ -n "$rejoin" ]; then
422 debug
"Merging split branch into HEAD..."
423 latest_old
=$
(cache_get latest_old
)
425 -m "$(merge_msg $dir $latest_old $latest_new)" \
426 $latest_new >&2 ||
exit $?
428 if [ -n "$branch" ]; then
429 git update-ref
-m 'subtree split' "refs/heads/$branch" \
430 $latest_new "" ||
exit $?
431 say
"Created branch '$branch'"
442 if [ $# -ne 1 ]; then
443 die
"You must provide exactly one revision. Got: '$revs'"
447 git merge
-s subtree
$rev
454 git pull
-s subtree
"$@"