chiark / gitweb /
Add the ability to rename a git branch
authorChuck Lever <cel@netapp.com>
Thu, 6 Oct 2005 10:19:22 +0000 (11:19 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Thu, 6 Oct 2005 10:19:22 +0000 (11:19 +0100)
To keep StGit-specific functionality separate from git functionality,
create a git.rename_branch function to do appropriate sanity checking and
rename branch heads.

Signed-off-by: Chuck Lever <cel@netapp.com>
stgit/git.py

index 7bb41c56d58be9c6d492ab33bbf89643d942ba46..55f06c119843927ef00288c34ec43ad5ec352508 100644 (file)
@@ -315,6 +315,20 @@ def delete_branch(name):
         raise GitException, 'Branch "%s" does not exist' % name
     os.remove(os.path.join(base_dir, branch_head))
 
+def rename_branch(from_name, to_name):
+    """Rename a git branch
+    """
+    from_head = os.path.join(base_dir, 'refs', 'heads', from_name)
+    if not branch_exists(from_head):
+        raise GitException, 'Branch "%s" does not exist' % from_name
+    to_head = os.path.join(base_dir, 'refs', 'heads', to_name)
+    if branch_exists(to_head):
+        raise GitException, 'Branch "%s" already exists' % to_name
+
+    if get_head_file() == from_name:
+        set_head_file(os.path.join('refs', 'heads', to_name))
+    os.rename(from_head, to_head)
+
 def add(names):
     """Add the files or recursively add the directory contents
     """