summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Suder <jakub.suder@gmail.com>2010-01-09 19:56:05 +0100
committerJakub Suder <jakub.suder@gmail.com>2010-01-10 13:09:26 +0100
commit0a562948ae1fe7130f4e9a29ce4107311ab93b91 (patch)
treeb53b44ff1a168c06d023593ba82ffca075fa8bea
parent2da0969a794eac50401fd25ea072224d4378a5fe (diff)
downloadgit-0a562948ae1fe7130f4e9a29ce4107311ab93b91.tar.gz
allow using --branch with existing branches if it makes sense
-rwxr-xr-xgit-subtree.sh34
1 files changed, 26 insertions, 8 deletions
diff --git a/git-subtree.sh b/git-subtree.sh
index 96118735b2..09992e39d5 100755
--- a/git-subtree.sh
+++ b/git-subtree.sh
@@ -161,6 +161,20 @@ rev_exists()
fi
}
+rev_is_descendant_of_branch()
+{
+ newrev="$1"
+ branch="$2"
+ branch_hash=$(git rev-parse $branch)
+ match=$(git rev-list $newrev | grep $branch_hash)
+
+ if [ -n "$match" ]; then
+ return 0
+ else
+ return 1
+ fi
+}
+
# if a commit doesn't have a parent, this might not work. But we only want
# to remove the parent from the rev-list, and since it doesn't exist, it won't
# be there anyway, so do nothing in that case.
@@ -476,10 +490,6 @@ cmd_add()
cmd_split()
{
- if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then
- die "Branch '$branch' already exists."
- fi
-
debug "Splitting $dir..."
cache_setup || exit $?
@@ -510,7 +520,8 @@ cmd_split()
eval "$grl" |
while read rev parents; do
revcount=$(($revcount + 1))
- say -n "$revcount/$revmax ($createcount) "
+ say -n "$revcount/$revmax ($createcount)
+"
debug "Processing commit: $rev"
exists=$(cache_get $rev)
if [ -n "$exists" ]; then
@@ -548,9 +559,16 @@ cmd_split()
$latest_new >&2 || exit $?
fi
if [ -n "$branch" ]; then
- git update-ref -m 'subtree split' "refs/heads/$branch" \
- $latest_new "" || exit $?
- say "Created branch '$branch'"
+ if rev_exists "refs/heads/$branch"; then
+ if ! rev_is_descendant_of_branch $latest_new $branch; then
+ die "Branch '$branch' is not an ancestor of commit '$latest_new'."
+ fi
+ action='Updated'
+ else
+ action='Created'
+ fi
+ git update-ref -m 'subtree split' "refs/heads/$branch" $latest_new || exit $?
+ say "$action branch '$branch'"
fi
echo $latest_new
exit 0