Add a new 'git subtree add' command for adding subtrees from a given rev.
Avery Pennarun committed
Apr 24, 2009 at 23:28 UTC
eb7b590c8c63994051545bf1fe1f650f5dc4aedb
1 file changed
+53
-3
git-subtree.sh
+53
-3
@@ -8,7 +8,8 @@ if [ $# -eq 0 ]; then
8
set -- -h
9
fi
10
OPTS_SPEC="\
11
-git subtree split [options...] <--prefix=prefix> <commit...> -- <path>
11
+git subtree add <--prefix=prefix <commit>
12
+git subtree split [options...] <--prefix=prefix> <commit...>
13
git subtree merge
14
--
15
h,help show the help
@@ -67,11 +68,12 @@ done
68
command="$1"
69
shift
70
case "$command" in
70
- split|merge) ;;
71
+ add|merge) default= ;;
72
+ split) default="--default HEAD" ;;
73
*) die "Unknown command '$command'" ;;
74
esac
75
74
-revs=$(git rev-parse --default HEAD --revs-only "$@") || exit $?
76
+revs=$(git rev-parse $default --revs-only "$@") || exit $?
77
78
if [ -z "$prefix" ]; then
79
die "You must provide the --prefix option."
@@ -87,6 +89,7 @@ debug "command: {$command}"
89
debug "quiet: {$quiet}"
90
debug "revs: {$revs}"
91
debug "dir: {$dir}"
92
+debug
93
94
cache_setup()
95
{
@@ -165,6 +168,20 @@ copy_commit()
168
) || die "Can't copy commit $1"
169
}
170
171
+add_msg()
172
+{
173
+ dir="$1"
174
+ latest_old="$2"
175
+ latest_new="$3"
176
+ cat <<-EOF
177
+ Add '$dir/' from commit '$latest_new'
178
+
179
+ git-subtree-dir: $dir
180
+ git-subtree-mainline: $latest_old
181
+ git-subtree-split: $latest_new
182
+ EOF
183
+}
184
+
185
merge_msg()
186
{
187
dir="$1"
@@ -241,6 +258,39 @@ copy_or_skip()
258
fi
259
}
260
261
+cmd_add()
262
+{
263
+ if [ -e "$dir" ]; then
264
+ die "'$dir' already exists. Cannot add."
265
+ fi
266
+ if ! git diff-index HEAD --exit-code --quiet; then
267
+ die "Working tree has modifications. Cannot add."
268
+ fi
269
+ if ! git diff-index --cached HEAD --exit-code --quiet; then
270
+ die "Index has modifications. Cannot add."
271
+ fi
272
+ set -- $revs
273
+ if [ $# -ne 1 ]; then
274
+ die "You must provide exactly one revision. Got: '$revs'"
275
+ fi
276
+ rev="$1"
277
+
278
+ debug "Adding $dir as '$rev'..."
279
+ git read-tree --prefix="$dir" $rev || exit $?
280
+ git checkout "$dir" || exit $?
281
+ tree=$(git write-tree) || exit $?
282
+
283
+ headrev=$(git rev-parse HEAD) || exit $?
284
+ if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
285
+ headp="-p $headrev"
286
+ else
287
+ headp=
288
+ fi
289
+ commit=$(add_msg "$dir" "$headrev" "$rev" |
290
+ git commit-tree $tree $headp -p "$rev") || exit $?
291
+ git reset "$commit" || exit $?
292
+}
293
+
294
cmd_split()
295
{
296
debug "Splitting $dir..."