chiark / gitweb /
Add a stack method for determining whether a branch is initialised
authorChuck Lever <cel@netapp.com>
Wed, 2 Nov 2005 21:55:46 +0000 (16:55 -0500)
committerCatalin Marinas <catalin.marinas@gmail.com>
Fri, 4 Nov 2005 21:46:53 +0000 (21:46 +0000)
Let's be consistent about how we decide whether a branch is ready for use
by stgit.

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

index 8420ec6393b0071383946993c8ec06fd49d7c630..9b880f15a0d5b2617439f64719d1bada32392d39 100644 (file)
@@ -70,7 +70,7 @@ def print_branch(branch_name):
 
     branch = stack.Series(branch_name)
 
-    if os.path.isdir(os.path.join(git.base_dir, 'patches', branch_name)):
+    if branch.is_initialised():
         initialized = 's'
     if is_current_branch(branch_name):
         current = '>'
@@ -156,19 +156,19 @@ def func(parser, options, args):
     elif options.protect:
 
         if len(args) == 0:
-            branch = git.get_head_file()
+            branch_name = git.get_head_file()
         elif len(args) == 1:
-            branch = args[0]
+            branch_name = args[0]
         else:
             parser.error('incorrect number of arguments')
+        branch = stack.Series(branch_name)
 
-        base = os.path.join(git.base_dir, 'refs', 'bases', branch)
-        if not os.path.isfile(base):
-            raise CmdException, 'Branch "%s" is not controlled by StGit' % branch
+        if not branch.is_initialised():
+            raise CmdException, 'Branch "%s" is not controlled by StGit' % branch_name
 
-        print 'Protecting branch "%s"...' % branch,
+        print 'Protecting branch "%s"...' % branch_name,
         sys.stdout.flush()
-        stack.Series(branch).protect()
+        branch.protect()
         print 'done'
 
         return
@@ -183,19 +183,19 @@ def func(parser, options, args):
     elif options.unprotect:
 
         if len(args) == 0:
-            branch = git.get_head_file()
+            branch_name = git.get_head_file()
         elif len(args) == 1:
-            branch = args[0]
+            branch_name = args[0]
         else:
             parser.error('incorrect number of arguments')
+        branch = stack.Series(branch_name)
 
-        base = os.path.join(git.base_dir, 'refs', 'bases', branch)
-        if not os.path.isfile(base):
-            raise CmdException, 'Branch "%s" is not controlled by StGit' % branch
+        if not branch.is_initialised():
+            raise CmdException, 'Branch "%s" is not controlled by StGit' % branch_name
 
-        print 'Unprotecting branch "%s"...' % branch,
+        print 'Unprotecting branch "%s"...' % branch_name,
         sys.stdout.flush()
-        stack.Series(branch).unprotect()
+        branch.unprotect()
         print 'done'
 
         return
index 23f3039eedbe4ea3f197d4dfc19245f5919c4a1f..edabb271ff299bbcf5f4d09fadd7213c06c19bf9 100644 (file)
@@ -378,12 +378,17 @@ class Series:
             return True
         return git.get_head() == Patch(crt, self.__patch_dir).get_top()
 
+    def is_initialised(self):
+        """Checks if series is already initialised
+        """
+        return os.path.isdir(self.__patch_dir)
+
     def init(self):
         """Initialises the stgit series
         """
         bases_dir = os.path.join(git.base_dir, 'refs', 'bases')
 
-        if os.path.isdir(self.__patch_dir):
+        if self.is_initialised():
             raise StackException, self.__patch_dir + ' already exists'
         os.makedirs(self.__patch_dir)
 
@@ -398,7 +403,7 @@ class Series:
     def delete(self, force = False):
         """Deletes an stgit series
         """
-        if os.path.isdir(self.__patch_dir):
+        if self.is_initialised():
             patches = self.get_unapplied() + self.get_applied()
             if not force and patches:
                 raise StackException, \