import-orig-rpm: implement --create-missing-branches option
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Wed, 12 Feb 2014 13:11:30 +0000 (15:11 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 5 Jun 2014 11:20:08 +0000 (14:20 +0300)
Create the upstream branch if it does not exist. Use the same option
name that import-srpm has.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/scripts/import_orig_rpm.py
tests/component/rpm/test_import_orig_rpm.py

index 8eacfb2..a8f874c 100755 (executable)
@@ -159,6 +159,9 @@ def parse_args(argv):
                             help="Upstream VCS tag add to the merge commit")
     branch_group.add_boolean_config_file_option(option_name="merge", dest="merge")
     branch_group.add_config_file_option(option_name="packaging-dir", dest="packaging_dir")
+    branch_group.add_boolean_config_file_option(
+                      option_name="create-missing-branches",
+                      dest="create_missing_branches")
 
     tag_group.add_boolean_config_file_option(option_name="sign-tags",
                       dest="sign_tags")
@@ -214,9 +217,14 @@ def main(argv):
         initial_branch = repo.get_branch()
         is_empty = False if initial_branch else True
 
-        if not repo.has_branch(options.upstream_branch) and not is_empty:
-            gbp.log.err(no_upstream_branch_msg % options.upstream_branch)
-            raise GbpError
+        if not repo.has_branch(options.upstream_branch):
+            if options.create_missing_branches:
+                gbp.log.info("Will create missing branch '%s'" %
+                             options.upstream_branch)
+            elif is_empty:
+                options.create_missing_branches = True
+            else:
+                raise GbpError(no_upstream_branch_msg % options.upstream_branch)
 
         (sourcepackage, version) = detect_name_and_version(repo, source, options)
 
@@ -262,11 +270,10 @@ def main(argv):
                 parents = None
 
             commit = repo.commit_dir(unpacked_orig,
-                                     msg=msg,
-                                     branch=options.upstream_branch,
-                                     other_parents=parents,
-                                     create_missing_branch=True,
-                                     )
+                        msg=msg,
+                        branch=options.upstream_branch,
+                        other_parents=parents,
+                        create_missing_branch=options.create_missing_branches)
             if options.pristine_tar and pristine_orig:
                 gbp.log.info("Pristine-tar: commiting %s" % pristine_orig)
                 repo.pristine_tar.commit(pristine_orig, options.upstream_branch)
index e2de8b6..d26f5eb 100644 (file)
@@ -102,6 +102,15 @@ class ImportOrigTestBase(ComponentTestBase):
 class TestImportOrig(ImportOrigTestBase):
     """Basic tests for git-import-orig-rpm"""
 
+    @staticmethod
+    def _init_repo_with_dummy_packaging():
+        """Create a dummy packaging branch with one commit"""
+        repo = GitRepository.create('.')
+        shutil.copy2('.git/HEAD', 'foobar')
+        repo.add_files('.')
+        repo.commit_all('First commit')
+        return repo
+
     def test_invalid_args(self):
         """
         See that import-orig-rpm fails gracefully when called with invalid args
@@ -188,10 +197,7 @@ class TestImportOrig(ImportOrigTestBase):
     def test_import_to_existing(self):
         """Test importing of to an existing repo"""
         # Create new repo and add dummy files
-        repo = GitRepository.create('.')
-        shutil.copy2('.git/HEAD', 'foobar')
-        repo.add_files('.')
-        repo.commit_all('First commit')
+        repo = self._init_repo_with_dummy_packaging()
         sha1 = repo.rev_parse('HEAD^0')
 
         # Test missing upstream branch
@@ -301,6 +307,21 @@ class TestImportOrig(ImportOrigTestBase):
         eq_(mock_import(['--no-interactive', orig_renamed], stdin_data=''), 1)
         self._check_log(-1, "gbp:error: Couldn't determine upstream version")
 
+    def test_option_create_missing(self):
+        """Test importing of to an existing repo"""
+        # Create new repo and add dummy files
+        repo = self._init_repo_with_dummy_packaging()
+
+        # Test missing upstream branch
+        orig = os.path.join(DATA_DIR, 'gbp-test2-2.0.tar.gz')
+        eq_(mock_import([orig]), 1)
+        self._check_log(1, 'Repository does not have branch')
+
+        # Try again, with --create-missing-branches
+        eq_(mock_import(['--create-missing-branches', orig]), 0)
+        self._check_repo_state(repo, 'master', ['master', 'upstream'])
+        eq_(len(repo.get_commits(until='upstream')), 1)
+
     def test_misc_options(self):
         """Test various options of git-import-orig-rpm"""
         repo = GitRepository.create('.')