Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions mergify_cli/stack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from mergify_cli.stack import session as stack_session_mod
from mergify_cli.stack import setup as stack_setup_mod
from mergify_cli.stack import skill as stack_skill_mod
from mergify_cli.stack import sync as stack_sync_mod


def trunk_type(
Expand Down Expand Up @@ -504,6 +505,38 @@ def skill() -> None:
click.echo(stack_skill_mod.get_skill_content(), nl=False)


@stack.command(help="Sync the stack: fetch trunk, remove merged commits, rebase")
@click.pass_context
@click.option(
"--dry-run",
"-n",
is_flag=True,
default=False,
help="Show what would happen without making changes",
)
@click.option(
"--trunk",
"-t",
type=click.UNPROCESSED,
default=lambda: asyncio.run(utils.get_trunk()),
callback=trunk_type,
help="Change the target branch of the stack.",
)
@utils.run_with_asyncio
async def sync(
ctx: click.Context,
*,
dry_run: bool,
trunk: tuple[str, str],
) -> None:
await stack_sync_mod.stack_sync(
github_server=ctx.obj["github_server"],
token=ctx.obj["token"],
trunk=trunk,
dry_run=dry_run,
)


@stack.command(name="list", help="List the stack's commits and their associated PRs")
@click.pass_context
@click.option(
Expand Down
3 changes: 3 additions & 0 deletions mergify_cli/stack/skill.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ A branch is a stack. Keep stacks short and focused:
```bash
mergify stack new NAME # Create a new stack/branch for new work
mergify stack push # Push and create/update PRs
mergify stack sync # Fetch trunk, remove merged commits, rebase
mergify stack list # Show commit <-> PR mapping for current stack
mergify stack list --json # Same, but machine-readable JSON output
```

Use `mergify stack sync` to bring your stack up to date. It fetches the latest trunk, detects which PRs have been merged, removes those commits from your local branch, and rebases the remaining commits. Run this before starting new work on an existing stack.

Use `mergify stack list` to see which commits have been pushed, which PRs they map to, and whether the stack is up to date with the remote. This is the go-to command to understand the current state of a stack. Use `--json` when you need to parse the output programmatically.

## CRITICAL: Check Branch Before ANY Commit
Expand Down
13 changes: 5 additions & 8 deletions mergify_cli/stack/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,11 @@ async def get_sync_status(

stack_prefix = f"{branch_prefix}/{dest_branch}" if branch_prefix else dest_branch

try:
base_commit_sha = await utils.git(
"merge-base",
"--fork-point",
f"{remote}/{base_branch}",
)
except utils.CommandError:
base_commit_sha = ""
base_commit_sha = await utils.git(
"merge-base",
"--fork-point",
f"{remote}/{base_branch}",
)
if not base_commit_sha:
console.print(
f"Common commit between `{remote}/{base_branch}` and `{dest_branch}` branches not found",
Expand Down
Loading
Loading