From 3f5219963f3a14f16b3d5aa16968c4ac3c7187d4 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 30 Jan 2013 11:09:53 +0000 Subject: branch and merge: combine checkout and branch logic --- morphlib/plugins/branch_and_merge_plugin.py | 85 +++++++++++++---------------- 1 file changed, 37 insertions(+), 48 deletions(-) (limited to 'morphlib/plugins') diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py index 8d0e3eef..87565570 100644 --- a/morphlib/plugins/branch_and_merge_plugin.py +++ b/morphlib/plugins/branch_and_merge_plugin.py @@ -505,27 +505,15 @@ class BranchAndMergePlugin(cliapp.Plugin): os.mkdir(os.path.join(dirname, '.morph')) self.app.status(msg='Initialized morph workspace', chatty=True) - def branch(self, args): - '''Create a new system branch.''' - - if len(args) not in [2, 3]: - raise cliapp.AppException('morph branch needs name of branch ' - 'as parameter') - - repo = args[0] - new_branch = args[1] - commit = 'master' if len(args) == 2 else args[2] - - self.lrc, self.rrc = morphlib.util.new_repo_caches(self.app) - if self.lrc.get_repo(repo).ref_exists(new_branch): - raise cliapp.AppException('branch %s already exists in ' - 'repository %s' % (new_branch, repo)) - - # Create the system branch directory. - workspace = self.deduce_workspace() - branch_dir = os.path.join(workspace, new_branch) + def _create_branch(self, workspace, branch_name, repo, original_ref): + '''Create a branch called branch_name based off original_ref. + + NOTE: self.lrc and self.rrc need to be initialized before + calling since clone_to_directory uses them indirectly via + get_cached_repo + ''' + branch_dir = os.path.join(workspace, branch_name) os.makedirs(branch_dir) - try: # Create a .morph-system-branch directory to clearly identify # this directory as a morph system branch. @@ -533,7 +521,7 @@ class BranchAndMergePlugin(cliapp.Plugin): # Remember the system branch name and the repository we branched # off from initially. - self.set_branch_config(branch_dir, 'branch.name', new_branch) + self.set_branch_config(branch_dir, 'branch.name', branch_name) self.set_branch_config(branch_dir, 'branch.root', repo) # Generate a UUID for the branch. We will use this for naming @@ -542,15 +530,36 @@ class BranchAndMergePlugin(cliapp.Plugin): # Clone into system branch directory. repo_dir = os.path.join(branch_dir, self.convert_uri_to_path(repo)) - self.clone_to_directory(repo_dir, repo, commit) + self.clone_to_directory(repo_dir, repo, original_ref) # Create a new branch in the local morphs repository. - self.app.runcmd(['git', 'checkout', '-b', new_branch, commit], - cwd=repo_dir) + if original_ref != branch_name: + self.app.runcmd(['git', 'checkout', '-b', branch_name, + original_ref], cwd=repo_dir) except: - self.remove_branch_dir_safe(workspace, new_branch) + self.remove_branch_dir_safe(workspace, branch_name) raise + def branch(self, args): + '''Create a new system branch.''' + + if len(args) not in [2, 3]: + raise cliapp.AppException('morph branch needs name of branch ' + 'as parameter') + + repo = args[0] + new_branch = args[1] + commit = 'master' if len(args) == 2 else args[2] + + self.lrc, self.rrc = morphlib.util.new_repo_caches(self.app) + if self.lrc.get_repo(repo).ref_exists(new_branch): + raise cliapp.AppException('branch %s already exists in ' + 'repository %s' % (new_branch, repo)) + + # Create the system branch directory. + workspace = self.deduce_workspace() + self._create_branch(workspace, new_branch, repo, commit) + def checkout(self, args): '''Check out an existing system branch.''' @@ -561,31 +570,11 @@ class BranchAndMergePlugin(cliapp.Plugin): repo = args[0] system_branch = args[1] - # Create the system branch directory. - workspace = self.deduce_workspace() - branch_dir = os.path.join(workspace, system_branch) - os.makedirs(branch_dir) self.lrc, self.rrc = morphlib.util.new_repo_caches(self.app) - try: - # Create a .morph-system-branch directory to clearly identify - # this directory as a morph system branch. - os.mkdir(os.path.join(branch_dir, '.morph-system-branch')) - - # Remember the system branch name and the repository we - # branched off from. - self.set_branch_config(branch_dir, 'branch.name', system_branch) - self.set_branch_config(branch_dir, 'branch.root', repo) - - # Generate a UUID for the branch. - self.set_branch_config(branch_dir, 'branch.uuid', uuid.uuid4().hex) - - # Clone into system branch directory. - repo_dir = os.path.join(branch_dir, self.convert_uri_to_path(repo)) - self.clone_to_directory(repo_dir, repo, system_branch) - except: - self.remove_branch_dir_safe(workspace, system_branch) - raise + # Create the system branch directory. + workspace = self.deduce_workspace() + self._create_branch(workspace, system_branch, repo, system_branch) def checkout_repository(self, branch_dir, repo, ref, parent_ref=None): '''Make a chunk or stratum repository available for a system branch -- cgit v1.2.1