rpm-ch: implement --commit-msg option
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 26 Feb 2015 14:03:31 +0000 (16:03 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 3 Mar 2015 08:07:47 +0000 (10:07 +0200)
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/scripts/rpm_ch.py
tests/component/rpm/test_rpm_ch.py

index 0759830d39bf82965ae96149b6e987fce13c0dc7..3ceb81ee3059b30ce418263899751b69c73d1cee 100755 (executable)
@@ -316,11 +316,23 @@ def update_changelog(changelog, entries, repo, spec, options):
     for entry in entries:
         top_section.append_entry(entry)
 
-def commit_changelog(repo, changelog, author, committer, edit):
+def create_commit_message(spec, options):
+    """Generate commit message"""
+    fields = spec.version
+    fields['version'] = version=compose_version_str(spec.version)
+    fields['vendor'] = options.vendor
+    fields['pkg'] = spec.name
+    try:
+        return options.commit_msg % fields
+    except KeyError as err:
+        raise GbpError("Unknown key %s in commit-msg string, "
+                       "only %s are accepted" % (err, fields.keys()))
+
+def commit_changelog(repo, changelog, message, author, committer, edit):
     """Commit changelog to Git"""
     repo.add_files(changelog.path)
-    repo.commit_staged("Update changelog", author_info=author,
-                        committer_info=committer, edit=edit)
+    repo.commit_staged(message, author_info=author, committer_info=committer,
+                       edit=edit)
 
 
 def build_parser(name):
@@ -401,6 +413,8 @@ def build_parser(name):
     # Commit group options
     commit_grp.add_option("-c", "--commit", action="store_true",
                     help="commit changes")
+    commit_grp.add_config_file_option(option_name="commit-msg",
+                                      dest="commit_msg")
     return parser
 
 def parse_args(argv):
@@ -461,7 +475,8 @@ def main(argv):
 
         if options.commit:
             edit = True if editor_cmd else False
-            commit_changelog(repo, ch_file, None, None, edit)
+            msg = create_commit_message(spec, options)
+            commit_changelog(repo, ch_file, msg, None, None, edit)
 
     except (GbpError, GitRepositoryError, ChangelogError, NoSpecError) as err:
         if len(err.__str__()):
index d342273e0b3e821ed592cbb9b9e0e7bcbc6c91cb..59ce39bcd821a3165f1f235da6bdc46d52a30bc2 100644 (file)
@@ -277,6 +277,17 @@ class TestRpmCh(RpmRepoTestBase):
         eq_(repo.get_commit_info('HEAD')['files'],
             {'M': ['foo.txt', 'gbp-test.spec']})
 
+    def test_option_commit_msg(self):
+        """Test the --commit-msg cmdline option"""
+        repo = self.init_test_repo('gbp-test2')
+
+        eq_(mock_ch(['--commit', '--since=HEAD^', '--commit-msg=Foo']), 0)
+        eq_(repo.get_commit_info('HEAD')['subject'], 'Foo')
+
+        # Unknown key in format string causes failure
+        eq_(mock_ch(['--commit', '--since=HEAD^', '--commit-msg=%(foo)s']), 1)
+        self._check_log(-1, "gbp:error: Unknown key 'foo' in commit-msg string")
+
     def test_option_editor_cmd(self):
         """Test the --editor-cmd and --spawn-editor cmdline options"""
         repo = self.init_test_repo('gbp-test-native')