-
Notifications
You must be signed in to change notification settings - Fork 10
feat(stack): add move command #1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jd
wants to merge
1
commit into
main
Choose a base branch
from
devs/jd/feat/stack-reorder-move/Id3e08ba58709c88ac9eaf8fcf4137f630ba2f383
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+470
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # | ||
| # Copyright © 2021-2026 Mergify SAS | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| from mergify_cli import console | ||
| from mergify_cli import utils | ||
| from mergify_cli.stack.reorder import display_plan | ||
| from mergify_cli.stack.reorder import get_stack_commits | ||
| from mergify_cli.stack.reorder import match_commit | ||
| from mergify_cli.stack.reorder import run_rebase | ||
|
|
||
|
|
||
| async def stack_move( | ||
| commit_prefix: str, | ||
| position: str, | ||
| target_prefix: str | None, | ||
| *, | ||
| dry_run: bool, | ||
| ) -> None: | ||
| os.chdir(await utils.git("rev-parse", "--show-toplevel")) | ||
| trunk = await utils.get_trunk() | ||
| base = await utils.git("merge-base", trunk, "HEAD") | ||
| commits = get_stack_commits(base) | ||
|
|
||
| if not commits: | ||
| console.print("No commits in the stack", style="green") | ||
| return | ||
|
|
||
| commit = match_commit(commit_prefix, commits) | ||
|
|
||
| if position in {"before", "after"}: | ||
| if target_prefix is None: | ||
| console.print( | ||
| f"error: '{position}' requires a target commit", | ||
| style="red", | ||
| ) | ||
| sys.exit(1) | ||
| target = match_commit(target_prefix, commits) | ||
| if commit[0] == target[0]: | ||
| console.print( | ||
| "error: commit and target are the same", | ||
| style="red", | ||
| ) | ||
| sys.exit(1) | ||
| elif position in {"first", "last"}: | ||
| if target_prefix is not None: | ||
| console.print( | ||
| f"error: '{position}' does not accept a target commit", | ||
| style="red", | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| # Compute new order | ||
| remaining = [c for c in commits if c[0] != commit[0]] | ||
|
|
||
| if position == "first": | ||
| new_order = [commit, *remaining] | ||
| elif position == "last": | ||
| new_order = [*remaining, commit] | ||
| elif position == "before": | ||
| target_idx = next(i for i, c in enumerate(remaining) if c[0] == target[0]) | ||
| new_order = [*remaining[:target_idx], commit, *remaining[target_idx:]] | ||
| elif position == "after": | ||
| target_idx = next(i for i, c in enumerate(remaining) if c[0] == target[0]) | ||
| new_order = [ | ||
| *remaining[: target_idx + 1], | ||
| commit, | ||
| *remaining[target_idx + 1 :], | ||
| ] | ||
jd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Check if order changed | ||
| current_shas = [c[0] for c in commits] | ||
| new_shas = [c[0] for c in new_order] | ||
| if current_shas == new_shas: | ||
| console.print( | ||
| "Commit is already in the requested position", | ||
| style="green", | ||
| ) | ||
| return | ||
|
|
||
| display_plan("Move plan:", new_order) | ||
|
|
||
| if dry_run: | ||
| console.print("Dry run — no changes made", style="green") | ||
| return | ||
|
|
||
| run_rebase(base, new_shas) | ||
| console.print("Commit moved successfully", style="green") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.