+ """Parse the string and return a verified SHA1 id
+ """
+ try:
+ return _output_one_line(['git-rev-parse', '--verify', git_id])
+ except GitException:
+ raise GitException, 'Unknown revision: %s' % git_id
+
+def branch_exists(branch):
+ """Existence check for the named branch
+ """
+ branch = os.path.join('refs', 'heads', branch)
+ 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):
+ """Create a new branch in the git repository
+ """
+ if branch_exists(new_branch):
+ raise GitException, 'Branch "%s" already exists' % new_branch
+
+ 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)
+
+ if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
+ os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
+
+def switch_branch(new_branch):
+ """Switch to a git branch
+ """
+ global __head
+
+ if not branch_exists(new_branch):
+ raise GitException, 'Branch "%s" does not exist' % new_branch
+
+ tree_id = rev_parse(os.path.join('refs', 'heads', new_branch)
+ + '^{commit}')
+ if tree_id != get_head():
+ refresh_index()
+ if __run('git-read-tree -u -m', [get_head(), tree_id]) != 0:
+ raise GitException, 'git-read-tree failed (local changes maybe?)'
+ __head = tree_id
+ set_head_file(new_branch)
+
+ if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
+ os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
+
+def delete_branch(name):
+ """Delete a git branch
+ """
+ if not branch_exists(name):
+ raise GitException, 'Branch "%s" does not exist' % name
+ remove_file_and_dirs(os.path.join(basedir.get(), 'refs', 'heads'),
+ name)
+
+def rename_branch(from_name, to_name):
+ """Rename a git branch