Repository: VirtusLab/git-machete
Pull request #1223 (2024-04-19 21:07:15)

Make `git{hub,lab} restack-{pr,mr}` first retarget, then push


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 3663f80..dfd6d6f 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -2,6 +2,8 @@
 
 ## New in git-machete 3.25.1
 
+- fixed: `git machete git{hub,lab} restack-{pr,mr}` now first retargets, then pushes (so that certain CIs see the correct base branch in env vars)
+
 ## New in git-machete 3.25.0
 
 - added: GitLab support via `git machete gitlab` (first suggested by @mikeynap, partly contributed by @max-nicholson)
diff --git a/git_machete/client.py b/git_machete/client.py
index 9fef5c3..33bd5a3 100644
--- a/git_machete/client.py
+++ b/git_machete/client.py
@@ -2268,6 +2268,9 @@ class MacheteClient:
                 if converted_to_draft:
                     print(f'{pr.display_text()} has been temporarily marked as draft')
 
+                # Note that retarget should happen BEFORE push, see issue #1222
+                self.retarget_pr(spec, head, ignore_if_missing=False)
+
                 if s == SyncToRemoteStatuses.AHEAD_OF_REMOTE:
                     assert remote is not None
                     self.__handle_ahead_state(
@@ -2303,8 +2306,6 @@ class MacheteClient:
                     opt_no_detect_squash_merges=False)
                 self.__print_new_line(False)
 
-                self.retarget_pr(spec, head, ignore_if_missing=False)
-
                 if converted_to_draft:
                     code_hosting_client.set_draft_status_of_pull_request(prs[0].number, target_draft_status=False)
                     print(f'{pr.display_text()} has been marked as ready for review again')


Test patch:
diff --git a/tests/test_github_restack_pr.py b/tests/test_github_restack_pr.py
index 3204512..bd1d9cb 100644
--- a/tests/test_github_restack_pr.py
+++ b/tests/test_github_restack_pr.py
@@ -108,13 +108,13 @@ class TestGitHubRestackPR(BaseTest):
         assert_success(
             ['github', 'restack-pr'],
             """
+            Base branch of PR #14 has been switched to master
             Pushing untracked branch feature_1 to origin...
 
               master (untracked)
               |
-              o-feature_1 *
+              o-feature_1 *  PR #14 (some_other_user)
 
-            Base branch of PR #14 has been switched to master
             """
         )
         pr = github_api_state.get_pull_by_number(14)
@@ -149,15 +149,15 @@ class TestGitHubRestackPR(BaseTest):
             ['github', 'restack-pr'],
             """
             PR #15 has been temporarily marked as draft
+            Base branch of PR #15 has been switched to master
+            Description of PR #15 has been updated
             Branch feature diverged from (and has newer commits than) its remote counterpart origin/feature.
             Pushing feature with force-with-lease to origin...
 
               master (untracked)
               |
-              o-feature *
+              o-feature *  PR #15 (some_other_user)
 
-            Base branch of PR #15 has been switched to master
-            Description of PR #15 has been updated
             PR #15 has been marked as ready for review again
             """
         )
@@ -192,14 +192,14 @@ class TestGitHubRestackPR(BaseTest):
             ['github', 'restack-pr'],
             """
             PR #15 has been temporarily marked as draft
+            Base branch of PR #15 has been switched to master
+            Description of PR #15 has been updated
             Pushing feature to origin...
 
               master (untracked)
               |
-              o-feature *
+              o-feature *  PR #15 (some_other_user)
 
-            Base branch of PR #15 has been switched to master
-            Description of PR #15 has been updated
             PR #15 has been marked as ready for review again
             """
         )
diff --git a/tests/test_gitlab_restack_mr.py b/tests/test_gitlab_restack_mr.py
index cb71581..0315295 100644
--- a/tests/test_gitlab_restack_mr.py
+++ b/tests/test_gitlab_restack_mr.py
@@ -108,13 +108,13 @@ class TestGitLabRestackMR(BaseTest):
         assert_success(
             ['gitlab', 'restack-mr'],
             """
+            Target branch of MR !14 has been switched to master
             Pushing untracked branch feature_1 to origin...
 
               master (untracked)
               |
-              o-feature_1 *
+              o-feature_1 *  MR !14 (some_other_user)
 
-            Target branch of MR !14 has been switched to master
             """
         )
         mr = gitlab_api_state.get_pull_by_number(14)
@@ -152,15 +152,15 @@ class TestGitLabRestackMR(BaseTest):
             ['gitlab', 'restack-mr'],
             """
             MR !15 has been temporarily marked as draft
+            Target branch of MR !15 has been switched to master
+            Description of MR !15 has been updated
             Branch feature diverged from (and has newer commits than) its remote counterpart origin/feature.
             Pushing feature with force-with-lease to origin...
 
               master (untracked)
               |
-              o-feature *
+              o-feature *  MR !15 (some_other_user)
 
-            Target branch of MR !15 has been switched to master
-            Description of MR !15 has been updated
             MR !15 has been marked as ready for review again
             """
         )
@@ -196,14 +196,14 @@ class TestGitLabRestackMR(BaseTest):
             ['gitlab', 'restack-mr'],
             """
             MR !15 has been temporarily marked as draft
+            Target branch of MR !15 has been switched to master
+            Description of MR !15 has been updated
             Pushing feature to origin...
 
               master (untracked)
               |
-              o-feature *
+              o-feature *  MR !15 (some_other_user)
 
-            Target branch of MR !15 has been switched to master
-            Description of MR !15 has been updated
             MR !15 has been marked as ready for review again
             """
         )