Repository: VirtusLab/git-machete
Pull request #1170 (2024-02-08 11:37:42)

Remove the old-style `Based on PR #...` header from PR description in `github retarget-pr`


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index c7b0378..73ba1e0 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -2,6 +2,10 @@
 
 ## New in git-machete 3.23.1
 
+## New in git-machete 3.23.1
+
+- fixed: if a PR has a pre-v3.23.0 `Based on PR #...` header, then it's removed by `git machete github retarget-pr` in favor of the new extended PR chain
+
 ## New in git-machete 3.23.0
 
 - added: full chain of PRs (and not just a link to the base PR) is added to/updated in PR description by `git machete github create-pr`/`retarget-pr`/`restack-pr` (suggested by @mjgigli)
diff --git a/docs/man/git-machete.1 b/docs/man/git-machete.1
index a3da1fe..a7ed6fc 100644
--- a/docs/man/git-machete.1
+++ b/docs/man/git-machete.1
@@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
 .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
 ..
-.TH "GIT-MACHETE" "1" "Feb 07, 2024" "" "git-machete"
+.TH "GIT-MACHETE" "1" "Feb 08, 2024" "" "git-machete"
 .SH NAME
 git-machete \- git-machete 3.23.1
 .sp
diff --git a/git_machete/client.py b/git_machete/client.py
index 02935a2..7dee476 100644
--- a/git_machete/client.py
+++ b/git_machete/client.py
@@ -2305,6 +2305,9 @@ class MacheteClient:
             else:
                 lines = lines[:start_index] + lines_to_prepend[:-1] + lines[end_index + 1:]
         else:
+            # For compatibility with pre-v3.23.0 format
+            if lines and '# Based on PR #' in lines[0]:
+                lines = skip_leading_empty(lines[1:])
             lines = lines_to_prepend + lines
         return '\n'.join(lines)
 
@@ -2546,11 +2549,11 @@ class MacheteClient:
         if base_branch_found_on_remote:
             # As the description may include the reference to this PR itself (in case of a chain of >=2 PRs),
             # let's update the PR description after it's already created (so that we know the current PR's number).
-            prepend = self.__generate_text_to_prepend_to_pr_description(github_client, pr)
-            if prepend:
+            text_to_prepend = self.__generate_text_to_prepend_to_pr_description(github_client, pr)
+            if text_to_prepend:
                 if description:
-                    prepend += '\n'
-                description = prepend + description
+                    text_to_prepend += '\n'
+                description = text_to_prepend + description
                 print(f'Updating description of PR #{bold(str(pr.number))} to include the chain of PRs... ', end='', flush=True)
                 github_client.set_description_of_pull_request(pr.number, description)
                 print(fmt(ok_str))


Test patch:
diff --git a/tests/test_github_retarget_pr.py b/tests/test_github_retarget_pr.py
index e909d68..50818c3 100644
--- a/tests/test_github_retarget_pr.py
+++ b/tests/test_github_retarget_pr.py
@@ -16,7 +16,7 @@ class TestGitHubRetargetPR(BaseTest):
     def github_api_state_for_test_retarget_pr() -> MockGitHubAPIState:
         return MockGitHubAPIState(
             mock_pr_json(head='feature', base='master', number=15),
-            mock_pr_json(head='feature_1', base='master', number=20, body='# Summary\n\n'),
+            mock_pr_json(head='feature_1', base='master', number=20, body='# Based on PR #10\n\n# Summary'),
             mock_pr_json(head='feature_2', base='master', number=25, body=None),
             mock_pr_json(head='feature_3', base='master', number=30),
             mock_pr_json(head='feature_4', base='feature', number=35),
@@ -275,9 +275,7 @@ class TestGitHubRetargetPR(BaseTest):
 
             <!-- end git-machete generated -->
 
-            # Summary
-
-        ''')[1:]
+            # Summary''')[1:]
 
         # branch feature_2 is not present in any of the remotes
         (