Add a branch --cleanup option
authorCatalin Marinas <catalin.marinas@gmail.com>
Fri, 22 Oct 2010 15:44:13 +0000 (16:44 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Fri, 22 Oct 2010 15:44:13 +0000 (16:44 +0100)
This has been required for some time. If you need to take a branch out
of StGit control, use this option (keeps the branch but removes the
metadata).

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
stgit/commands/branch.py
stgit/stack.py

index 614c3ae..98141a6 100644 (file)
@@ -34,6 +34,7 @@ usage = ['',
          '--protect [--] [<branch>]',
          '--unprotect [--] [<branch>]',
          '--delete [--force] [--] <branch>',
+         '--cleanup [--force] [--] [<branch>]',
          '--description=<description> [--] [<branch>]']
 description = """
 Create, clone, switch between, rename, or delete development branches
@@ -100,6 +101,17 @@ options = [
 
         If you delete the current branch, you are switched to the
         "master" branch, if it exists."""),
+    opt('--cleanup', action = 'store_true',
+        short = 'Clean up the StGit metadata for a branch', long = """
+        Remove the StGit information for the current or given branch. If there
+        are patches left in the branch, StGit refuses the operation unless
+        '--force' is given.
+
+        A protected branch cannot be cleaned up; it must be unprotected first
+        (see '--unprotect' above).
+
+        A cleaned up branch can be re-initialised using the 'stg init'
+        command."""),
     opt('-d', '--description', short = 'Set the branch description'),
     opt('--force', action = 'store_true',
         short = 'Force a delete when the series is not empty')]
@@ -137,6 +149,15 @@ def __delete_branch(doomed_name, force = False):
     doomed.delete(force)
     out.done()
 
+def __cleanup_branch(name, force = False):
+    branch = stack.Series(name)
+    if branch.get_protected():
+        raise CmdExcpetion('This branch is protected. Clean up is not permitted')
+
+    out.start('Cleaning up branch "%s"' % name)
+    branch.delete(force = force, cleanup = True)
+    out.done()
+
 def func(parser, options, args):
 
     if options.create:
@@ -231,6 +252,18 @@ def func(parser, options, args):
         log.delete_log(log.default_repo(), args[0])
         return
 
+    elif options.cleanup:
+
+        if not args:
+            name = crt_series.get_name()
+        elif len(args) == 1:
+            name = args[0]
+        else:
+            parser.error('incorrect number of arguments')
+        __cleanup_branch(name, options.force)
+        log.delete_log(log.default_repo(), name)
+        return
+
     elif options.list:
 
         if len(args) != 0:
index f166aea..9cd43a3 100644 (file)
@@ -623,7 +623,7 @@ class Series(PatchSet):
         if value:
             config.set('branch.%s.stgit.parentbranch' % target_series, value)
 
-    def delete(self, force = False):
+    def delete(self, force = False, cleanup = False):
         """Deletes an stgit series
         """
         if self.is_initialised():
@@ -631,7 +631,8 @@ class Series(PatchSet):
                     self.get_hidden();
             if not force and patches:
                 raise StackException, \
-                      'Cannot delete: the series still contains patches'
+                      'Cannot %s: the series still contains patches' % \
+                        ('delete', 'clean up')[cleanup]
             for p in patches:
                 self.get_patch(p).delete()
 
@@ -663,12 +664,13 @@ class Series(PatchSet):
                 raise StackException('Series directory %s is not empty'
                                      % self._dir())
 
-        try:
-            git.delete_branch(self.get_name())
-        except git.GitException:
-            out.warn('Could not delete branch "%s"' % self.get_name())
+        if not cleanup:
+            try:
+                git.delete_branch(self.get_name())
+            except git.GitException:
+                out.warn('Could not delete branch "%s"' % self.get_name())
+            config.remove_section('branch.%s' % self.get_name())
 
-        config.remove_section('branch.%s' % self.get_name())
         config.remove_section('branch.%s.stgit' % self.get_name())
 
     def refresh_patch(self, files = None, message = None, edit = False,