From 0b4b9499a53813957d23adaa0e97e1cc3a3a6900 Mon Sep 17 00:00:00 2001 From: Chuck Lever Date: Thu, 6 Oct 2005 13:57:12 +0100 Subject: [PATCH] Allow user to protect some branches against "stg pull" 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 --- stgit/commands/branch.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- stgit/commands/commit.py | 3 +++ stgit/commands/pull.py | 3 +++ stgit/stack.py | 13 +++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/stgit/commands/branch.py b/stgit/commands/branch.py index 27a2e98..5ea0ede 100644 --- a/stgit/commands/branch.py +++ b/stgit/commands/branch.py @@ -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], diff --git a/stgit/commands/commit.py b/stgit/commands/commit.py index ec3de72..a3b7277 100644 --- a/stgit/commands/commit.py +++ b/stgit/commands/commit.py @@ -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), diff --git a/stgit/commands/pull.py b/stgit/commands/pull.py index f4e0005..69b8d88 100644 --- a/stgit/commands/pull.py +++ b/stgit/commands/pull.py @@ -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() diff --git a/stgit/stack.py b/stgit/stack.py index 6efee79..554ae8b 100644 --- a/stgit/stack.py +++ b/stgit/stack.py @@ -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) -- 2.11.0