chiark / gitweb /
Clean-up if the branch creation fails (bug #10015)
authorCatalin Marinas <catalin.marinas@gmail.com>
Tue, 4 Dec 2007 21:46:14 +0000 (21:46 +0000)
committerCatalin Marinas <catalin.marinas@gmail.com>
Tue, 4 Dec 2007 21:46:14 +0000 (21:46 +0000)
If the tree switching fails during the branch creation, the new head
will be reverted and the new branch deleted.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
stgit/git.py
t/t1000-branch-create.sh

index 6744be0e09750578b8e25a9628c27c37a26b1ce7..4cc35c9cd13e52c0a03d7da173e675911edd702e 100644 (file)
@@ -366,13 +366,20 @@ def create_branch(new_branch, tree_id = None):
     if branch_exists(new_branch):
         raise GitException, 'Branch "%s" already exists' % new_branch
 
+    current_head_file = get_head_file()
     current_head = get_head()
     set_head_file(new_branch)
     __set_head(current_head)
 
     # a checkout isn't needed if new branch points to the current head
     if tree_id:
-        switch(tree_id)
+        try:
+            switch(tree_id)
+        except GitException:
+            # Tree switching failed. Revert the head file
+            set_head_file(current_head_file)
+            delete_branch(new_branch)
+            raise
 
     if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
         os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
index 848686c40bfe66768a80974d9cf51b861ce93644..bc796b4095c50e26d99f9a8c90387243f5231a2b 100755 (executable)
@@ -49,7 +49,30 @@ test_expect_success \
 test_expect_success \
     'Create an invalid refs/heads/ entry' '
     find .git -name foo | xargs rm -rf &&
-    touch .git/refs/heads/foo
+    touch .git/refs/heads/foo &&
+    ! stg branch -c foo
+'
+
+test_expect_success \
+    'Setup two commits including removal of generated files' '
+    git init &&
+    touch a.c a.o &&
+    git add a.c a.o &&
+    git commit -m 1 &&
+    git rm a.c a.o &&
+    git commit -m 2 &&
+    touch a.o
+'
+
+test_expect_failure \
+    'Create branch down the stack, behind the conflict caused by the generated file' '
+    stg branch --create bar master^
+'
+
+test_expect_success \
+    'Check the branch was not created' '
+    test ! -r .git/refs/heads/bar &&
+    test ! -r a.c
 '
 
 test_done