From 307637d8b61ef12c5d1ee4c3ffd859ffef339bf5 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Wed, 20 Jul 2016 11:04:59 +0100 Subject: [PATCH] Record upstream branch in import merge commit Ensure correct upstream branch is used for the finish command when working with branches named differently to defaults, otherwise the merge commit message will reference the wrong branch. Change-Id: I5b6dee8f45e86f42caf56c7e3e4c1527f89925bc --- git_upstream/commands/import.py | 2 +- git_upstream/lib/importupstream.py | 11 ++++++++--- .../commands/import/scenarios/import_finish.yaml | 1 + .../scenarios/import_same_as_previous_upstream.yaml | 1 + .../import_same_previous_with_same_additional.yaml | 1 + git_upstream/tests/commands/import/test_import.py | 10 ++++++++++ 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/git_upstream/commands/import.py b/git_upstream/commands/import.py index a741b66..3a21b57 100644 --- a/git_upstream/commands/import.py +++ b/git_upstream/commands/import.py @@ -113,7 +113,7 @@ class ImportCommand(LogDedentMixin, GitUpstreamCommand): def finalize(self): """Perform additional parsing of args""" - if self.args.finish: + if self.args.finish and not self.args.upstream_branch: self.args.upstream_branch = self.args.import_branch def _finish(self, import_upstream): diff --git a/git_upstream/lib/importupstream.py b/git_upstream/lib/importupstream.py index 4426ba9..852a346 100644 --- a/git_upstream/lib/importupstream.py +++ b/git_upstream/lib/importupstream.py @@ -369,6 +369,11 @@ class ImportUpstream(LogDedentMixin, GitMixin): self.git.checkout(self.branch) current_sha = self.git.rev_parse("HEAD") + commit_message = ( + "Merge branch '{0}' into {1}\n\n" + "Import of '{2}' into '{1}'." + ).format(self.import_branch, self.branch, self.upstream) + try: self.log.info( """ @@ -390,9 +395,9 @@ class ImportUpstream(LogDedentMixin, GitMixin): self.log.info( """ Committing merge commit: - git commit --no-edit - """) - self.git.commit(no_edit=True) + git commit -m "%s" + """, commit_message.replace('\n', '\n' + ' ' * 4)) + self.git.commit(m=commit_message) # finally test that everything worked correctly by comparing if # the tree object id's match if self.git.rev_parse("HEAD^{tree}") != \ diff --git a/git_upstream/tests/commands/import/scenarios/import_finish.yaml b/git_upstream/tests/commands/import/scenarios/import_finish.yaml index cdb7eca..828b5e2 100644 --- a/git_upstream/tests/commands/import/scenarios/import_finish.yaml +++ b/git_upstream/tests/commands/import/scenarios/import_finish.yaml @@ -62,3 +62,4 @@ - --finish - --import-branch=import/F - --into=master + - custom/master diff --git a/git_upstream/tests/commands/import/scenarios/import_same_as_previous_upstream.yaml b/git_upstream/tests/commands/import/scenarios/import_same_as_previous_upstream.yaml index 1d7ba74..1fda1df 100644 --- a/git_upstream/tests/commands/import/scenarios/import_same_as_previous_upstream.yaml +++ b/git_upstream/tests/commands/import/scenarios/import_same_as_previous_upstream.yaml @@ -41,3 +41,4 @@ - --into=master expect_found: [] + check_merge: False diff --git a/git_upstream/tests/commands/import/scenarios/import_same_previous_with_same_additional.yaml b/git_upstream/tests/commands/import/scenarios/import_same_previous_with_same_additional.yaml index 4779b8a..4a1a796 100644 --- a/git_upstream/tests/commands/import/scenarios/import_same_previous_with_same_additional.yaml +++ b/git_upstream/tests/commands/import/scenarios/import_same_previous_with_same_additional.yaml @@ -53,3 +53,4 @@ - additional/2 expect_found: [] + check_merge: False diff --git a/git_upstream/tests/commands/import/test_import.py b/git_upstream/tests/commands/import/test_import.py index c3dff15..43f6f5a 100644 --- a/git_upstream/tests/commands/import/test_import.py +++ b/git_upstream/tests/commands/import/test_import.py @@ -52,6 +52,8 @@ class TestImportCommand(TestWithScenarios, BaseTestCase): "import command failed to complete successfully") expected = getattr(self, 'expect_found', None) + # even if empty want to confirm that find no changes applied, + # otherwise confirm we find the expected number of changes. if expected is not None: if len(list(Commit.new(self.repo, target_branch).parents)) > 1: changes = list(Commit.iter_items( @@ -82,6 +84,14 @@ class TestImportCommand(TestWithScenarios, BaseTestCase): subject, commit.hexsha, node_subject, node)) + # allow disabling of checking the merge commit contents + # as some tests won't result in an import + if getattr(self, 'check_merge', True): + commit_message = self.git.log(target_branch, n=1) + self.assertThat(commit_message, + Contains("of '%s' into '%s'" % (upstream_branch, + target_branch))) + # allow additional test specific verification methods below extra_test_func = getattr(self, '_verify_%s' % self.name, None) if extra_test_func: