Skip to content
Merged
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
14 changes: 12 additions & 2 deletions mergify_cli/stack/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import sys
import typing

import rich.markup

from mergify_cli import console
from mergify_cli import console_error
from mergify_cli import utils
Expand Down Expand Up @@ -582,8 +584,16 @@ async def stack_push(
with console.status("Fetching old PR heads for comparison..."):
try:
await fetch_old_pr_heads(remote, updated_pr_numbers)
except utils.CommandError:
pass # Non-fatal: change type will be "unknown"
except utils.CommandError as exc:
# Non-fatal: change type will be "unknown" — but surface
# the underlying error so the user can fix it. Escape the
# exception text since it can contain `[`/`]` that Rich
# would otherwise interpret as markup tags.
console.log(
f"[orange]Could not fetch old PR heads; revision-history "
f"change types will fall back to 'unknown': "
f"{rich.markup.escape(str(exc))}[/]",
)
Comment thread
jd marked this conversation as resolved.

# Detect change types before force-push overwrites refs
change_types: dict[str, str] = {}
Expand Down
7 changes: 7 additions & 0 deletions mergify_cli/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
import pathlib


def test_command_error_str_handles_non_utf8_stdout() -> None:
# Some git invocations (e.g. legacy locales) can emit non-UTF-8 bytes;
# str(CommandError) must not raise — error paths depend on it.
error = utils.CommandError(("git", "show", "abc"), 1, b"\xff\xfe broken")
assert "failed to run `git show abc`" in str(error)


@pytest.mark.usefixtures("_git_repo")
async def test_get_branch_name() -> None:
assert await utils.git_get_branch_name() == "main"
Expand Down
8 changes: 7 additions & 1 deletion mergify_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ class CommandError(Exception):
stdout: bytes

def __str__(self) -> str:
return f"failed to run `{' '.join(self.command_args)}`: {self.stdout.decode()}"
# ``errors="replace"`` so str(CommandError) never raises on
# non-UTF-8 process output — callers in error paths (warnings,
# CLI top-level handler) rely on this being safe.
return (
f"failed to run `{' '.join(self.command_args)}`: "
f"{self.stdout.decode(errors='replace')}"
)


class MergifyError(click.ClickException):
Expand Down
Loading