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...>
18 prefix= the name of the subdir to split out
20 annotate= add a prefix to commit message of new commits
21 onto= try connecting new tree to an existing one
22 rejoin merge the new branch back into HEAD
23 ignore-joins ignore prior --rejoin commits
25 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
38 if [ -z "$quiet" ]; then
48 die
"assertion failed: " "$@"
55 while [ $# -gt 0 ]; do
60 --annotate) annotate
="$1"; shift ;;
61 --no-annotate) annotate
= ;;
62 --prefix) prefix
="$1"; shift ;;
63 --no-prefix) prefix
= ;;
64 --onto) onto
="$1"; shift ;;
67 --no-rejoin) rejoin
= ;;
68 --ignore-joins) ignore_joins
=1 ;;
69 --no-ignore-joins) ignore_joins
= ;;
77 add|merge|pull
) default
= ;;
78 split) default
="--default HEAD" ;;
79 *) die
"Unknown command '$command'" ;;
82 if [ -z "$prefix" ]; then
83 die
"You must provide the --prefix option."
87 if [ "$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."
95 debug
"command: {$command}"
96 debug
"quiet: {$quiet}"
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
113 if [ -r "$cachedir/$oldrev" ]; then
114 read newrev
<"$cachedir/$oldrev"
124 if [ "$oldrev" != "latest_old" \
125 -a "$oldrev" != "latest_new" \
126 -a -e "$cachedir/$oldrev" ]; then
127 die
"cache for $oldrev already exists!"
129 echo "$newrev" >"$cachedir/$oldrev"
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.
135 try_remove_previous
()
137 if git rev-parse
"$1^" >/dev
/null
2>&1; then
142 find_existing_splits
()
144 debug
"Looking for prior splits..."
147 git log
--grep="^git-subtree-dir: $dir\$" \
148 --pretty=format
:'%s%n%n%b%nEND' $revs |
149 while read a b junk
; do
151 git-subtree-mainline
:) main
="$b" ;;
152 git-subtree-split
:) sub
="$b" ;;
154 if [ -n "$main" -a -n "$sub" ]; then
155 debug
" Prior: $main -> $sub"
157 try_remove_previous
"$main"
158 try_remove_previous
"$sub"
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" |
174 read GIT_AUTHOR_EMAIL
176 read GIT_COMMITTER_NAME
177 read GIT_COMMITTER_EMAIL
178 read GIT_COMMITTER_DATE
179 export GIT_AUTHOR_NAME \
183 GIT_COMMITTER_EMAIL \
185 (echo -n "$annotate"; cat ) |
186 git commit-tree
"$2" $3 # reads the rest of stdin
187 ) || die
"Can't copy commit $1"
196 Add '$dir/' from commit '$latest_new'
198 git-subtree-dir: $dir
199 git-subtree-mainline: $latest_old
200 git-subtree-split: $latest_new
210 Split '$dir/' into commit '$latest_new'
212 git-subtree-dir: $dir
213 git-subtree-mainline: $latest_old
214 git-subtree-split: $latest_new
221 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
228 git ls-tree
"$commit" -- "$dir" |
229 while read mode
type tree name
; do
230 assert
[ "$name" = "$dir" ]
240 if [ $# -ne 1 ]; then
241 return 0 # weird parents, consider it changed
243 ptree
=$
(toptree_for_commit
$1)
244 if [ "$ptree" != "$tree" ]; then
247 return 1 # not changed
257 assert
[ -n "$tree" ]
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.
267 if [ -n "$ptree" ]; then
268 parentmatch
="$parentmatch$parent"
273 if [ -n "$identical" -a "$parentmatch" = "$identical" ]; then
276 copy_commit
$rev $tree "$p" ||
exit $?
282 if ! git diff-index HEAD
--exit-code --quiet; then
283 die
"Working tree has modifications. Cannot add."
285 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
286 die
"Index has modifications. Cannot add."
292 if [ -e "$dir" ]; then
293 die
"'$dir' already exists. Cannot add."
298 if [ $# -ne 1 ]; then
299 die
"You must provide exactly one revision. Got: '$revs'"
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 $?
308 headrev
=$
(git rev-parse HEAD
) ||
exit $?
309 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
314 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
315 git commit-tree
$tree $headp -p "$rev") ||
exit $?
316 git
reset "$commit" ||
exit $?
321 debug
"Splitting $dir..."
322 cache_setup ||
exit $?
324 if [ -n "$onto" ]; then
325 debug
"Reading history for --onto=$onto..."
328 # the 'onto' history is already just the subdir, so
329 # any parent we find there can be used verbatim
335 if [ -n "$ignore_joins" ]; then
338 unrevs
="$(find_existing_splits "$dir" "$revs")"
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
347 debug
"Processing commit: $rev"
348 exists
=$
(cache_get
$rev)
349 if [ -n "$exists" ]; then
350 debug
" prior: $exists"
353 debug
" parents: $parents"
354 newparents
=$
(cache_get
$parents)
355 debug
" newparents: $newparents"
357 tree
=$
(subtree_for_commit
$rev "$dir")
358 debug
" tree is: $tree"
359 [ -z $tree ] && continue
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
367 latest_new
=$
(cache_get latest_new
)
368 if [ -z "$latest_new" ]; then
369 die
"No new revisions were found"
372 if [ -n "$rejoin" ]; then
373 debug
"Merging split branch into HEAD..."
374 latest_old
=$
(cache_get latest_old
)
376 -m "$(merge_msg $dir $latest_old $latest_new)" \
388 if [ $# -ne 1 ]; then
389 die
"You must provide exactly one revision. Got: '$revs'"
393 git merge
-s subtree
$rev
400 git pull
-s subtree
"$@"