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 onto= try connecting new tree to an existing one
21 rejoin merge the new branch back into HEAD
22 ignore-joins ignore prior --rejoin commits
24 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
36 if [ -z "$quiet" ]; then
46 die
"assertion failed: " "$@"
53 while [ $# -gt 0 ]; do
58 --prefix) prefix
="$1"; shift ;;
59 --no-prefix) prefix
= ;;
60 --onto) onto
="$1"; shift ;;
63 --no-rejoin) rejoin
= ;;
64 --ignore-joins) ignore_joins
=1 ;;
65 --no-ignore-joins) ignore_joins
= ;;
73 add|merge|pull
) default
= ;;
74 split) default
="--default HEAD" ;;
75 *) die
"Unknown command '$command'" ;;
78 if [ -z "$prefix" ]; then
79 die
"You must provide the --prefix option."
83 if [ "$command" != "pull" ]; then
84 revs
=$
(git rev-parse
$default --revs-only "$@") ||
exit $?
85 dirs="$(git rev-parse --no-revs --no-flags "$@
")" ||
exit $?
86 if [ -n "$dirs" ]; then
87 die
"Error: Use --prefix instead of bare filenames."
91 debug
"command: {$command}"
92 debug
"quiet: {$quiet}"
100 cachedir
="$GIT_DIR/subtree-cache/$$"
101 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
102 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
103 debug
"Using cachedir: $cachedir" >&2
109 if [ -r "$cachedir/$oldrev" ]; then
110 read newrev
<"$cachedir/$oldrev"
120 if [ "$oldrev" != "latest_old" \
121 -a "$oldrev" != "latest_new" \
122 -a -e "$cachedir/$oldrev" ]; then
123 die
"cache for $oldrev already exists!"
125 echo "$newrev" >"$cachedir/$oldrev"
128 # if a commit doesn't have a parent, this might not work. But we only want
129 # to remove the parent from the rev-list, and since it doesn't exist, it won't
130 # be there anyway, so do nothing in that case.
131 try_remove_previous
()
133 if git rev-parse
"$1^" >/dev
/null
2>&1; then
138 find_existing_splits
()
140 debug
"Looking for prior splits..."
143 git log
--grep="^git-subtree-dir: $dir\$" \
144 --pretty=format
:'%s%n%n%b%nEND' "$revs" |
145 while read a b junk
; do
147 git-subtree-mainline
:) main
="$b" ;;
148 git-subtree-split
:) sub
="$b" ;;
150 if [ -n "$main" -a -n "$sub" ]; then
151 debug
" Prior: $main -> $sub"
153 try_remove_previous
"$main"
154 try_remove_previous
"$sub"
165 # We're doing to set some environment vars here, so
166 # do it in a subshell to get rid of them safely later
167 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
170 read GIT_AUTHOR_EMAIL
172 read GIT_COMMITTER_NAME
173 read GIT_COMMITTER_EMAIL
174 read GIT_COMMITTER_DATE
175 export GIT_AUTHOR_NAME \
179 GIT_COMMITTER_EMAIL \
181 (echo -n '*'; cat ) |
# FIXME
182 git commit-tree
"$2" $3 # reads the rest of stdin
183 ) || die
"Can't copy commit $1"
192 Add '$dir/' from commit '$latest_new'
194 git-subtree-dir: $dir
195 git-subtree-mainline: $latest_old
196 git-subtree-split: $latest_new
206 Split '$dir/' into commit '$latest_new'
208 git-subtree-dir: $dir
209 git-subtree-mainline: $latest_old
210 git-subtree-split: $latest_new
217 git log
-1 --pretty=format
:'%T' "$commit" -- ||
exit $?
224 git ls-tree
"$commit" -- "$dir" |
225 while read mode
type tree name
; do
226 assert
[ "$name" = "$dir" ]
236 if [ $# -ne 1 ]; then
237 return 0 # weird parents, consider it changed
239 ptree
=$
(toptree_for_commit
$1)
240 if [ "$ptree" != "$tree" ]; then
243 return 1 # not changed
253 assert
[ -n "$tree" ]
257 for parent
in $newparents; do
258 ptree
=$
(toptree_for_commit
$parent) ||
exit $?
259 if [ "$ptree" = "$tree" ]; then
260 # an identical parent could be used in place of this rev.
263 if [ -n "$ptree" ]; then
264 parentmatch
="$parentmatch$parent"
269 if [ -n "$identical" -a "$parentmatch" = "$identical" ]; then
272 copy_commit
$rev $tree "$p" ||
exit $?
278 if ! git diff-index HEAD
--exit-code --quiet; then
279 die
"Working tree has modifications. Cannot add."
281 if ! git diff-index
--cached HEAD
--exit-code --quiet; then
282 die
"Index has modifications. Cannot add."
288 if [ -e "$dir" ]; then
289 die
"'$dir' already exists. Cannot add."
294 if [ $# -ne 1 ]; then
295 die
"You must provide exactly one revision. Got: '$revs'"
299 debug
"Adding $dir as '$rev'..."
300 git read-tree
--prefix="$dir" $rev ||
exit $?
301 git checkout
"$dir" ||
exit $?
302 tree
=$
(git write-tree
) ||
exit $?
304 headrev
=$
(git rev-parse HEAD
) ||
exit $?
305 if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
310 commit
=$
(add_msg
"$dir" "$headrev" "$rev" |
311 git commit-tree
$tree $headp -p "$rev") ||
exit $?
312 git
reset "$commit" ||
exit $?
317 debug
"Splitting $dir..."
318 cache_setup ||
exit $?
320 if [ -n "$onto" ]; then
321 debug
"Reading history for --onto=$onto..."
324 # the 'onto' history is already just the subdir, so
325 # any parent we find there can be used verbatim
331 if [ -n "$ignore_joins" ]; then
334 unrevs
="$(find_existing_splits "$dir" "$revs")"
337 # We can't restrict rev-list to only "$dir" here, because that leaves out
338 # critical information about commit parents.
339 debug
"git rev-list --reverse --parents $revs $unrevs"
340 git rev-list
--reverse --parents $revs $unrevs |
341 while read rev parents
; do
343 debug
"Processing commit: $rev"
344 exists
=$
(cache_get
$rev)
345 if [ -n "$exists" ]; then
346 debug
" prior: $exists"
349 debug
" parents: $parents"
350 newparents
=$
(cache_get
$parents)
351 debug
" newparents: $newparents"
353 tree
=$
(subtree_for_commit
$rev "$dir")
354 debug
" tree is: $tree"
355 [ -z $tree ] && continue
357 newrev
=$
(copy_or_skip
"$rev" "$tree" "$newparents") ||
exit $?
358 debug
" newrev is: $newrev"
359 cache_set
$rev $newrev
360 cache_set latest_new
$newrev
361 cache_set latest_old
$rev
363 latest_new
=$
(cache_get latest_new
)
364 if [ -z "$latest_new" ]; then
365 die
"No new revisions were found"
368 if [ -n "$rejoin" ]; then
369 debug
"Merging split branch into HEAD..."
370 latest_old
=$
(cache_get latest_old
)
372 -m "$(merge_msg $dir $latest_old $latest_new)" \
384 if [ $# -ne 1 ]; then
385 die
"You must provide exactly one revision. Got: '$revs'"
389 git merge
-s subtree
$rev
396 git pull
-s subtree
"$@"