allow using --branch with existing branches if it makes sense
Jakub Suder committed
Jan 9, 2010 at 19:56 UTC
0a562948ae1fe7130f4e9a29ce4107311ab93b91
1 file changed
+26
-8
git-subtree.sh
+26
-8
@@ -161,6 +161,20 @@ rev_exists()
161
fi
162
}
163
164
+rev_is_descendant_of_branch()
165
+{
166
+ newrev="$1"
167
+ branch="$2"
168
+ branch_hash=$(git rev-parse $branch)
169
+ match=$(git rev-list $newrev | grep $branch_hash)
170
+
171
+ if [ -n "$match" ]; then
172
+ return 0
173
+ else
174
+ return 1
175
+ fi
176
+}
177
+
178
# if a commit doesn't have a parent, this might not work. But we only want
179
# to remove the parent from the rev-list, and since it doesn't exist, it won't
180
# be there anyway, so do nothing in that case.
@@ -476,10 +490,6 @@ cmd_add()
490
491
cmd_split()
492
{
479
- if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then
480
- die "Branch '$branch' already exists."
481
- fi
482
-
493
debug "Splitting $dir..."
494
cache_setup || exit $?
495
@@ -510,7 +520,8 @@ cmd_split()
520
eval "$grl" |
521
while read rev parents; do
522
revcount=$(($revcount + 1))
513
- say -n "$revcount/$revmax ($createcount)
"
523
+ say -n "$revcount/$revmax ($createcount)
524
+"
525
debug "Processing commit: $rev"
526
exists=$(cache_get $rev)
527
if [ -n "$exists" ]; then
@@ -548,9 +559,16 @@ cmd_split()
559
$latest_new >&2 || exit $?
560
fi
561
if [ -n "$branch" ]; then
551
- git update-ref -m 'subtree split' "refs/heads/$branch" \
552
- $latest_new "" || exit $?
553
- say "Created branch '$branch'"
562
+ if rev_exists "refs/heads/$branch"; then
563
+ if ! rev_is_descendant_of_branch $latest_new $branch; then
564
+ die "Branch '$branch' is not an ancestor of commit '$latest_new'."
565
+ fi
566
+ action='Updated'
567
+ else
568
+ action='Created'
569
+ fi
570
+ git update-ref -m 'subtree split' "refs/heads/$branch" $latest_new || exit $?
571
+ say "$action branch '$branch'"
572
fi
573
echo $latest_new
574
exit 0