Repository: VirtusLab/git-machete
Pull request #230 (2021-09-13 12:39:08)

add check if head and base branch are equal


Patch:
diff --git a/git_machete/client.py b/git_machete/client.py
index f444579..b3d6ce8 100644
--- a/git_machete/client.py
+++ b/git_machete/client.py
@@ -1760,18 +1760,10 @@ class MacheteClient:
 
     def create_github_pr(self, head: str, draft: bool) -> None:
         # first make sure that head branch is synced with remote
-        try:
-            self.__sync_before_creating_pr()
-        except MacheteException as e:
-            print(e)
-            return
+        self.__sync_before_creating_pr()
         self.flush_caches()
-        base: Optional[str] = self.up_branch.get(head)
-        if not base:
-            raise MacheteException(
-                f'Branch {head} does not have a parent branch (it is a root), '
-                'base branch for the PR cannot be established')
 
+        base: Optional[str] = self.up_branch.get(head)
         org: str
         repo: str
         _, (org, repo) = self.__derive_remote_and_github_org_and_repo()
@@ -1905,8 +1897,15 @@ class MacheteClient:
         if current_branch not in self.managed_branches:
             self.add(current_branch)
 
-        if self.__is_merged_to_upstream(current_branch):
-            return
+        up_branch: Optional[str] = self.up_branch.get(current_branch)
+        if not up_branch:
+            raise MacheteException(
+                f'Branch `{current_branch}` does not have a parent branch (it is a root), '
+                'base branch for the PR cannot be established.')
+
+        if self.__git.is_ancestor_or_equal(current_branch, up_branch):
+            raise MacheteException(
+                f'All commits in `{current_branch}` branch  are already included in `{up_branch}` branch.\nCannot create pull request.')
 
         s, remote = self.__git.get_strict_remote_sync_status(current_branch)
         statuses_to_sync = (UNTRACKED, AHEAD_OF_REMOTE, DIVERGED_FROM_AND_NEWER_THAN_REMOTE)


Test patch:
diff --git a/git_machete/tests/functional/test_machete.py b/git_machete/tests/functional/test_machete.py
index 97ff59d..708486d 100644
--- a/git_machete/tests/functional/test_machete.py
+++ b/git_machete/tests/functional/test_machete.py
@@ -142,7 +142,6 @@ class MockGithubAPIRequest:
     def fill_pull_request_data(self, data: Dict[str, Any], pull: Dict[str, Any]) -> "MockGithubAPIResponse":
         index = self.get_index_or_none(pull, self.github_api_state.issues)
         for key in data.keys():
-
             if key in ('base', 'head'):
                 pull[key] = {'ref': ""}
                 pull[key]['ref'] = json.loads(self.json_data)[key]
@@ -1811,6 +1810,31 @@ class MacheteTester(unittest.TestCase):
             self.assertEqual(e.exception.parameter, expected_error_message,
                              'Verify that expected error message has appeared when given pull request to create is already created.')
 
+        # check against head branch is ancestor or equal to base branch
+        (
+            self.repo_sandbox.check_out('develop')
+            .new_branch('testing/endpoints')
+            .push()
+        )
+        self.launch_command('discover')
+
+        machete_client = MacheteClient(cli_opts, git)
+        machete_client.read_definition_file()
+        expected_error_message = "All commits in `testing/endpoints` branch  are already included in `develop` branch.\nCannot create pull request."
+        with self.assertRaises(MacheteException) as e:
+            machete_client.create_github_pr('testing/endpoints', draft=False)
+        if e:
+            self.assertEqual(e.exception.parameter, expected_error_message,
+                             'Verify that expected error message has appeared when head branch is equal or ancestor of base branch.')
+
+        self.repo_sandbox.check_out('develop')
+        expected_error_message = "Branch `develop` does not have a parent branch (it is a root), base branch for the PR cannot be established."
+        with self.assertRaises(MacheteException) as e:
+            machete_client.create_github_pr('develop', draft=False)
+        if e:
+            self.assertEqual(e.exception.parameter, expected_error_message,
+                             'Verify that expected error message has appeared when creating PR from root branch.')
+
     git_api_state_for_test_checkout_prs = MockGithubAPIState([
         {'head': {'ref': 'chore/redundant_checks'}, 'user': {'login': 'github_user'}, 'base': {'ref': 'restrict_access'}, 'number': '18', 'html_url': 'www.github.com'},
         {'head': {'ref': 'restrict_access'}, 'user': {'login': 'github_user'}, 'base': {'ref': 'allow-ownership-link'}, 'number': '17', 'html_url': 'www.github.com'},