chiark / gitweb /
Generalize branch renaming
authorChuck Lever <cel@netapp.com>
Sat, 5 Nov 2005 22:47:06 +0000 (17:47 -0500)
committerCatalin Marinas <catalin.marinas@gmail.com>
Wed, 9 Nov 2005 21:31:19 +0000 (21:31 +0000)
The Series object already has the path names of all the pieces we need to
rename, so let's reuse that information.  Extract the rename branch logic
from the "stg branch" command and put it in stack.py.

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

index 9b880f15a0d5b2617439f64719d1bada32392d39..8997e2078f90a61783c0fc54110c72d878d72472 100644 (file)
@@ -95,28 +95,6 @@ def delete_branch(doomed_name, force = False):
 
     print 'Branch "%s" has been deleted.' % doomed_name
 
-def rename_branch(from_name, to_name):
-    if from_name == 'master':
-        raise CmdException, 'Renaming the master branch is not allowed'
-
-    to_patchdir = os.path.join(git.base_dir, 'patches', to_name)
-    if os.path.isdir(to_patchdir):
-        raise CmdException, '"%s" already exists' % to_patchdir
-    to_base = os.path.join(git.base_dir, 'refs', 'bases', to_name)
-    if os.path.isfile(to_base):
-        raise CmdException, '"%s" already exists' % to_base
-
-    git.rename_branch(from_name, to_name)
-
-    from_patchdir = os.path.join(git.base_dir, 'patches', from_name)
-    if os.path.isdir(from_patchdir):
-        os.rename(from_patchdir, to_patchdir)
-    from_base = os.path.join(git.base_dir, 'refs', 'bases', from_name)
-    if os.path.isfile(from_base):
-        os.rename(from_base, to_base)
-
-    print 'Renamed branch "%s" as "%s".' % (from_name, to_name)
-
 def func(parser, options, args):
 
     if options.create:
@@ -177,7 +155,11 @@ def func(parser, options, args):
 
         if len(args) != 2:
             parser.error('incorrect number of arguments')
-        rename_branch(args[0], args[1])
+
+        stack.Series(args[0]).rename(args[1])
+
+        print 'Renamed branch "%s" as "%s".' % (args[0], args[1])
+
         return
 
     elif options.unprotect:
index edabb271ff299bbcf5f4d09fadd7213c06c19bf9..1ffeaee0f9bf9a9bf1b8a1d38a033b2ee2455856 100644 (file)
@@ -400,6 +400,24 @@ class Series:
         create_empty_file(self.__descr_file)
         self.__begin_stack_check()
 
+    def rename(self, to_name):
+        """Renames a series
+        """
+        to_stack = Series(to_name)
+        if os.path.isdir(to_stack.__patch_dir):
+            raise StackException, '"%s" already exists' % to_stack.__patch_dir
+        if os.path.isfile(to_stack.__base_file):
+            raise StackException, '"%s" already exists' % to_stack.__base_file
+
+        git.rename_branch(self.__name, to_name)
+
+        if os.path.isdir(self.__patch_dir):
+            os.rename(self.__patch_dir, to_stack.__patch_dir)
+        if os.path.isfile(self.__base_file):
+            os.rename(self.__base_file, to_stack.__base_file)
+
+        self.__init__(to_name)
+
     def delete(self, force = False):
         """Deletes an stgit series
         """