chiark / gitweb /
Allow user to protect some branches against "stg pull"
authorChuck Lever <cel@netapp.com>
Thu, 6 Oct 2005 12:57:12 +0000 (13:57 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Thu, 6 Oct 2005 12:57:12 +0000 (13:57 +0100)
Sometimes we want to develop against a branch that doesn't evolve (eg.
2.6.12).  To prevent an accidental "stg pull", provide two new options to
stg: "--protect" and "--unprotect".  This also prevents deleting any
patches in the series.

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

index 27a2e98d59847929dc942767f3fc16eab3cd47e5..5ea0ede65556d147d250402d42935c9e354e2e2f 100644 (file)
@@ -49,8 +49,14 @@ options = [make_option('-c', '--create',
            make_option('-l', '--list',
                        help = 'list branches contained in this repository',
                        action = 'store_true'),
+           make_option('-p', '--protect',
+                       help = 'prevent "stg pull" from modifying this branch',
+                       action = 'store_true'),
            make_option('-r', '--rename',
                        help = 'rename an existing development branch',
+                       action = 'store_true'),
+           make_option('-u', '--unprotect',
+                       help = 'allow "stg pull" to modify this branch',
                        action = 'store_true')]
 
 
@@ -60,13 +66,19 @@ def is_current_branch(branch_name):
 def print_branch(branch_name):
     initialized = ' '
     current = ' '
+    protected = ' '
     if os.path.isdir(os.path.join(git.base_dir, 'patches', branch_name)):
         initialized = 's'
     if is_current_branch(branch_name):
         current = '>'
-    print '%s %s\t%s' % (current, initialized, branch_name)
+    if stack.Series(branch_name).get_protected():
+        protected = 'p'
+    print '%s %s%s\t%s' % (current, initialized, protected, branch_name)
 
 def delete_branch(doomed_name, force = False):
+    if stack.Series(doomed_name).get_protected():
+        raise CmdException, 'This branch is protected. Delete is not permitted'
+
     if is_current_branch(doomed_name) and doomed_name != 'master':
         git.switch_branch('master')
 
@@ -135,6 +147,23 @@ def func(parser, options, args):
             print_branch(i)
         return
 
+    elif options.protect:
+
+        if len(args) == 0:
+            branch = git.get_head_file()
+        elif len(args) == 1:
+            branch = args[0]
+        else:
+            parser.error('incorrect number of arguments')
+
+        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
+
+        print 'Protecting branch "%s"...' % branch
+        stack.Series(branch).protect()
+        return
+
     elif options.rename:
 
         if len(args) != 2:
@@ -142,6 +171,23 @@ def func(parser, options, args):
         rename_branch(args[0], args[1])
         return
 
+    elif options.unprotect:
+
+        if len(args) == 0:
+            branch = git.get_head_file()
+        elif len(args) == 1:
+            branch = args[0]
+        else:
+            parser.error('incorrect number of arguments')
+
+        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
+
+        print 'Unprotecting branch "%s"...' % branch
+        stack.Series(branch).unprotect()
+        return
+
     elif len(args) == 1:
 
         print 'Switching to branch "%s"...' % args[0],
index ec3de72462fe42858fe934c89068415fc3b8446e..a3b72770e474a982293ff67726fbfdd87f7738b4 100644 (file)
@@ -49,6 +49,9 @@ def func(parser, options, args):
     if not applied:
         raise CmdException, 'No patches applied'
 
+    if crt_series.get_protected():
+        raise CmdException, 'This branch is protected.  Commit is not permitted'
+
     crt_head = git.get_head()
 
     print 'Committing %d patches...' % len(applied),
index f4e0005e9793dc995391e90caeb34b91e7732f58..69b8d88f114bc33272a5e71a69d4ec5c9e37329a 100644 (file)
@@ -54,6 +54,9 @@ def func(parser, options, args):
     if len(args) == 2:
         refspec = args[1]
 
+    if crt_series.get_protected():
+        raise CmdException, 'This branch is protected. Pulls are not permitted'
+
     check_local_changes()
     check_conflicts()
     check_head_top_equal()
index 6efee79b032e300dde13bf835a5b7dc0281c516f..554ae8bd64fef072bd5e3211882829285571b8bc 100644 (file)
@@ -314,6 +314,19 @@ class Series:
     def get_base_file(self):
         return self.__base_file
 
+    def get_protected(self):
+        return os.path.isfile(os.path.join(self.__patch_dir, 'protected'))
+
+    def protect(self):
+        protect_file = os.path.join(self.__patch_dir, 'protected')
+        if not os.path.isfile(protect_file):
+            create_empty_file(protect_file)
+
+    def unprotect(self):
+        protect_file = os.path.join(self.__patch_dir, 'protected')
+        if os.path.isfile(protect_file):
+            os.remove(protect_file)
+
     def __patch_is_current(self, patch):
         return patch.get_name() == read_string(self.__current_file)