if len(args) == 2:
tree_id = git_id(args[1])
- git.create_branch(args[0], tree_id)
+ if git.branch_exists(args[0]):
+ raise CmdException, 'Branch "%s" already exists' % args[0]
+
stack.Series(args[0]).init()
+ git.create_branch(args[0], tree_id)
print 'Branch "%s" created.' % args[0]
return
def branch_exists(branch):
"""Existence check for the named branch
"""
- for line in _output_lines(['git-rev-parse', '--symbolic', '--all']):
+ for line in _output_lines('git-rev-parse --symbolic --all 2>&1'):
if line.strip() == branch:
return True
+ if re.compile('[ |/]'+branch+' ').search(line):
+ raise GitException, 'Bogus branch: %s' % line
return False
def create_branch(new_branch, tree_id = None):
"""
bases_dir = os.path.join(self.__base_dir, 'refs', 'bases')
- if self.is_initialised():
+ if os.path.exists(self.__patch_dir):
raise StackException, self.__patch_dir + ' already exists'
+ if os.path.exists(self.__refs_dir):
+ raise StackException, self.__refs_dir + ' already exists'
+ if os.path.exists(self.__base_file):
+ raise StackException, self.__base_file + ' already exists'
+
os.makedirs(self.__patch_dir)
if not os.path.isdir(bases_dir):
--- /dev/null
+#!/bin/sh
+#
+# Copyright (c) 2006 Yann Dirson
+#
+
+test_description='Branch operations.
+
+Exercises the "stg branch" commands.
+'
+
+. ./test-lib.sh
+
+stg init
+
+test_expect_failure \
+ 'Try to create an stgit branch with a spurious refs/patches/ entry' \
+ 'find .git -name foo | xargs rm -rf &&
+ touch .git/refs/patches/foo &&
+ stg branch -c foo
+'
+
+test_expect_success \
+ 'Check no part of the branch was created' \
+ 'test "`find .git -name foo | tee /dev/stderr`" = ".git/refs/patches/foo" &&
+ ( grep foo .git/HEAD; test $? = 1 )
+'
+
+test_expect_failure \
+ 'Try to create an stgit branch with a spurious patches/ entry' \
+ 'find .git -name foo | xargs rm -rf &&
+ touch .git/patches/foo &&
+ stg branch -c foo
+'
+
+test_expect_success \
+ 'Check no part of the branch was created' \
+ 'test "`find .git -name foo | tee /dev/stderr`" = ".git/patches/foo" &&
+ ( grep foo .git/HEAD; test $? = 1 )
+'
+
+test_expect_failure \
+ 'Try to create an stgit branch with a spurious refs/bases/ entry' \
+ 'find .git -name foo | xargs rm -rf &&
+ touch .git/refs/bases/foo &&
+ stg branch -c foo
+'
+
+test_expect_success \
+ 'Check no part of the branch was created' \
+ 'test "`find .git -name foo | tee /dev/stderr`" = ".git/refs/bases/foo" &&
+ ( grep foo .git/HEAD; test $? = 1 )
+'
+
+
+test_expect_failure \
+ 'Try to create an stgit branch with an existing git branch by that name' \
+ 'find .git -name foo | xargs rm -rf &&
+ cp .git/refs/heads/master .git/refs/heads/foo &&
+ stg branch -c foo
+'
+
+test_expect_success \
+ 'Check no part of the branch was created' \
+ 'test "`find .git -name foo | tee /dev/stderr`" = ".git/refs/heads/foo" &&
+ ( grep foo .git/HEAD; test $? = 1 )
+'
+
+
+test_expect_failure \
+ 'Try to create an stgit branch with an invalid refs/heads/ entry' \
+ 'find .git -name foo | xargs rm -rf &&
+ touch .git/refs/heads/foo &&
+ stg branch -c foo
+'
+
+test_expect_success \
+ 'Check no part of the branch was created' \
+ 'test "`find .git -name foo | tee /dev/stderr`" = ".git/refs/heads/foo" &&
+ ( grep foo .git/HEAD; test $? = 1 )
+'
+
+test_done