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
1 change: 1 addition & 0 deletions mergify_cli/stack/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class LocalChange(Change):
base_branch: str
dest_branch: str
action: ActionT
reason: str = ""

@property
def commit_short_sha(self) -> str:
Expand Down
51 changes: 51 additions & 0 deletions mergify_cli/stack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,54 @@ async def open_cmd(
@utils.run_with_asyncio
async def fixup(*, commits: tuple[str, ...], dry_run: bool) -> None:
await stack_squash_mod.stack_fixup(list(commits), dry_run=dry_run)


@stack.command(help="Squash commits into a target commit")
@click.argument("tokens", nargs=-1, required=True)
@click.option(
"-m",
"--message",
"message",
default=None,
help="Final commit message (required to rename; otherwise target's is kept)",
)
@click.option(
"--dry-run",
"-n",
is_flag=True,
default=False,
help="Show the plan without rebasing",
)
@utils.run_with_asyncio
async def squash(
*,
tokens: tuple[str, ...],
message: str | None,
dry_run: bool,
) -> None:
srcs, target = _parse_squash_tokens(tokens)
await stack_squash_mod.stack_squash(
src_prefixes=srcs,
target_prefix=target,
message=message,
dry_run=dry_run,
)


def _parse_squash_tokens(tokens: tuple[str, ...]) -> tuple[list[str], str]:
"""Parse ``SRC... into TARGET`` from a flat tuple of tokens.

Raises :class:`click.BadParameter` on shape errors.
"""
into_positions = [i for i, t in enumerate(tokens) if t == "into"]
if len(into_positions) != 1:
msg = "squash requires exactly one 'into' keyword: SRC... into TARGET"
raise click.BadParameter(msg)
idx = into_positions[0]
srcs = list(tokens[:idx])
after = tokens[idx + 1 :]
if not srcs:
raise click.BadParameter("at least one source commit required before 'into'")
if len(after) != 1:
raise click.BadParameter("exactly one target commit required after 'into'")
return srcs, after[0]
Loading
Loading