3 # git-subtree.sh: split/join git repositories in subdirectories of this one
5 # Copyright (c) 2009 Avery Pennarun <apenwarr@gmail.com>
8 git subtree split <revisions> -- <subdir>
11 git subtree does foo and bar!
17 eval $
(echo "$OPTS_SPEC" | git rev-parse
--parseopt -- "$@" ||
echo exit $?
)
26 if [ -z "$quiet" ]; then
36 die
"assertion failed: " "$@"
43 while [ $# -gt 0 ]; do
56 *) die
"Unknown command '$command'" ;;
59 revs
=$
(git rev-parse
--default HEAD
--revs-only "$@") ||
exit $?
60 dirs="$(git rev-parse --sq --no-revs --no-flags "$@
")" ||
exit $?
62 #echo "dirs is {$dirs}"
63 eval $
(echo set -- $dirs)
64 if [ "$#" -ne 1 ]; then
65 die
"Must provide exactly one subtree dir (got $#)"
69 debug
"command: {$command}"
70 debug
"quiet: {$quiet}"
76 cachedir
="$GIT_DIR/subtree-cache/$$"
77 rm -rf "$cachedir" || die
"Can't delete old cachedir: $cachedir"
78 mkdir
-p "$cachedir" || die
"Can't create new cachedir: $cachedir"
79 debug
"Using cachedir: $cachedir" >&2
85 if [ -r "$cachedir/$oldrev" ]; then
86 read newrev
<"$cachedir/$oldrev"
96 if [ "$oldrev" != "latest" -a -e "$cachedir/$oldrev" ]; then
97 die
"cache for $oldrev already exists!"
99 echo "$newrev" >"$cachedir/$oldrev"
104 # We're doing to set some environment vars here, so
105 # do it in a subshell to get rid of them safely later
106 git log
-1 --pretty=format
:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
109 read GIT_AUTHOR_EMAIL
111 read GIT_COMMITTER_NAME
112 read GIT_COMMITTER_EMAIL
113 read GIT_COMMITTER_DATE
114 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
115 export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
116 git commit-tree
"$2" $3 # reads the rest of stdin
117 ) || die
"Can't copy commit $1"
122 debug
"Splitting $dir..."
123 cache_setup ||
exit $?
125 git rev-list
--reverse --parents $revs -- "$dir" |
126 while read rev parents
; do
127 newparents
=$
(cache_get
$parents)
129 debug
"Processing commit: $rev / $newparents"
131 git ls-tree
$rev -- "$dir" |
132 while read mode
type tree name
; do
133 assert
[ "$name" = "$dir" ]
134 debug
" tree is: $tree"
136 for parent
in $newparents; do
140 newrev
=$
(copy_commit
$rev $tree "$p") ||
exit $?
141 debug
" newrev is: $newrev"
142 cache_set
$rev $newrev
143 cache_set latest
$newrev
146 latest
=$
(cache_get latest
)
147 if [ -z "$latest" ]; then
148 die
"No new revisions were found"
156 die
"merge command not implemented yet"