diff options
-rw-r--r-- | morphlib/cachedrepo.py | 9 | ||||
-rw-r--r-- | morphlib/cachedrepo_tests.py | 6 | ||||
-rw-r--r-- | morphlib/plugins/branch_and_merge_plugin.py | 11 |
3 files changed, 20 insertions, 6 deletions
diff --git a/morphlib/cachedrepo.py b/morphlib/cachedrepo.py index 312f580f..0a085bb2 100644 --- a/morphlib/cachedrepo.py +++ b/morphlib/cachedrepo.py @@ -104,6 +104,15 @@ class CachedRepo(object): self.path = path self.is_mirror = not url.startswith('file://') + def ref_exists(self, ref): + '''Returns True if the given ref exists in the repo''' + + try: + self._rev_parse(ref) + except cliapp.AppException: + return False + return True + def resolve_ref(self, ref): '''Attempts to resolve a ref into its SHA1 and tree SHA1. diff --git a/morphlib/cachedrepo_tests.py b/morphlib/cachedrepo_tests.py index 0c1ec5de..9251a473 100644 --- a/morphlib/cachedrepo_tests.py +++ b/morphlib/cachedrepo_tests.py @@ -129,6 +129,12 @@ class CachedRepoTests(unittest.TestCase): self.assertEqual(self.repo.url, self.repo_url) self.assertEqual(self.repo.path, self.repo_path) + def test_ref_exists(self): + self.assertEqual(self.repo.ref_exists('master'), True) + + def test_ref_does_not_exist(self): + self.assertEqual(self.repo.ref_exists('non-existant-ref'), False) + def test_resolve_named_ref_master(self): sha1, tree = self.repo.resolve_ref('master') self.assertEqual(sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9') diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py index 31b661b6..e6b78475 100644 --- a/morphlib/plugins/branch_and_merge_plugin.py +++ b/morphlib/plugins/branch_and_merge_plugin.py @@ -516,11 +516,15 @@ class BranchAndMergePlugin(cliapp.Plugin): 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) 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 @@ -540,11 +544,6 @@ class BranchAndMergePlugin(cliapp.Plugin): repo_dir = os.path.join(branch_dir, self.convert_uri_to_path(repo)) self.clone_to_directory(repo_dir, repo, commit) - # Check if branch already exists locally or in a remote - if self.resolve_ref(repo_dir, new_branch) is not None: - raise cliapp.AppException('branch %s already exists in ' - 'repository %s' % (new_branch, repo)) - # Create a new branch in the local morphs repository. self.app.runcmd(['git', 'checkout', '-b', new_branch, commit], cwd=repo_dir) |