merge_check: exit 2 when git cannot be run (MERGE-CHECK-GIT-MISSING) #610

Manually merged
github merged 1 commit from gh-pr/610/head into gh-pr/610/base 2026-11-26 09:11:35 +00:00
Owner

@⁠TuroYT on GitHub · 2026-11-24 09:54 UTC · original
TuroYT:fix/merge-check-git-missing → main · +31 −1 · 2 files · 1 commits · merged 2026-11-24 12:44 UTC

Claimed in the coordination issue as: MERGE-CHECK-GIT-MISSING (#14)

The docstring of scripts/merge_check.py (line 21) says exit 2 on a usage or git error. But git() (line 45) calls subprocess.run with no handling for a missing executable. With git not on PATH, that raises FileNotFoundError. main catches only GitError, so the script printed a traceback and exited 1.

The change: git() turns the OSError from subprocess.run into GitError("cannot run git: <strerror>"). main already prints a GitError to stderr and returns 2. git() is the only place the script runs git, so this covers every caller (prior_merges, cherry, merged_tree, rev_tree, duplicates).

Before: env PATH=/nonexistent /usr/bin/python3 -I scripts/merge_check.py 1=HEAD printed a traceback and exited 1.
After: it prints merge_check: cannot run git: No such file or directory and exits 2.

Tests added to tests/test_merge_check.py:

  • subprocess.run patched to raise FileNotFoundError; asserts exit 2, the exact message on stderr, no traceback.
  • PATH set to os.devnull; same assertions.
  • A folder that is not a repository: git exits 128, and the script still returns 2. This one passes on the unfixed script too; it guards the existing nonzero-exit path.

The first two fail on the unfixed script with 2 errors (FileNotFoundError).

Overlap with #242 and #300: both change scripts/merge_check.py outside git(). #242 edits lines 65, 110-113 and 132 (argument order for log, diff, show). #300 edits line 66 (splitlines to split). This PR changes lines 44-51 only, so the hunks do not overlap. I did not check the tests/test_merge_check.py hunks of #242 and #300. This PR appends tests after test_usage_errors; if either of them also appends at the end of the class, that one will need a rebase.

Tested:

  • python3 -m compileall -q scripts tests: exit 0.
  • python3 -I tests/test_merge_check.py: Ran 23 tests, OK.
  • Same test file with scripts/merge_check.py from origin/main: 2 errors, both FileNotFoundError, as expected.
  • env PATH=/nonexistent /usr/bin/python3 -I scripts/merge_check.py 1=HEAD: merge_check: cannot run git: No such file or directory, exit 2 (was a traceback and exit 1).
  • python3 -I scripts/merge_check.py with no arguments: argparse usage message, exit 2.
  • python3 -I scripts/merge_check.py --repo /tmp 1=HEAD: git reports "not a git repository", exit 2.
  • env -u PATH /usr/bin/python3 -I scripts/merge_check.py 1=HEAD: git is still found through the default search path, exit 1. This is not a missing-git case; the empty-PATH test covers that.
  • python3 -I tests/test_text_write_line_endings.py: Ran 3 tests, OK.
  • python3 -I scripts/check_no_game_data.py scripts/merge_check.py tests/test_merge_check.py: exit 0.

Not tested: Windows (the mocked test does not depend on the platform, but the empty-PATH test checks only the message prefix there); CI on other Python versions; this branch together with #242 and #300.

AI-assisted (Claude Code).

**@⁠TuroYT** on GitHub · 2026-11-24 09:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/610) `TuroYT:fix/merge-check-git-missing` → `main` · +31 −1 · 2 files · 1 commits · merged 2026-11-24 12:44 UTC Claimed in the coordination issue as: MERGE-CHECK-GIT-MISSING (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) The docstring of `scripts/merge_check.py` (line 21) says exit 2 on a usage or git error. But `git()` (line 45) calls `subprocess.run` with no handling for a missing executable. With git not on PATH, that raises `FileNotFoundError`. `main` catches only `GitError`, so the script printed a traceback and exited 1. The change: `git()` turns the `OSError` from `subprocess.run` into `GitError("cannot run git: <strerror>")`. `main` already prints a `GitError` to stderr and returns 2. `git()` is the only place the script runs git, so this covers every caller (`prior_merges`, `cherry`, `merged_tree`, `rev_tree`, `duplicates`). Before: `env PATH=/nonexistent /usr/bin/python3 -I scripts/merge_check.py 1=HEAD` printed a traceback and exited 1. After: it prints `merge_check: cannot run git: No such file or directory` and exits 2. Tests added to `tests/test_merge_check.py`: - `subprocess.run` patched to raise `FileNotFoundError`; asserts exit 2, the exact message on stderr, no traceback. - `PATH` set to `os.devnull`; same assertions. - A folder that is not a repository: git exits 128, and the script still returns 2. This one passes on the unfixed script too; it guards the existing nonzero-exit path. The first two fail on the unfixed script with 2 errors (`FileNotFoundError`). Overlap with #242 and #300: both change `scripts/merge_check.py` outside `git()`. #242 edits lines 65, 110-113 and 132 (argument order for `log`, `diff`, `show`). #300 edits line 66 (`splitlines` to `split`). This PR changes lines 44-51 only, so the hunks do not overlap. I did not check the `tests/test_merge_check.py` hunks of #242 and #300. This PR appends tests after `test_usage_errors`; if either of them also appends at the end of the class, that one will need a rebase. Tested: - `python3 -m compileall -q scripts tests`: exit 0. - `python3 -I tests/test_merge_check.py`: Ran 23 tests, OK. - Same test file with `scripts/merge_check.py` from origin/main: 2 errors, both `FileNotFoundError`, as expected. - `env PATH=/nonexistent /usr/bin/python3 -I scripts/merge_check.py 1=HEAD`: `merge_check: cannot run git: No such file or directory`, exit 2 (was a traceback and exit 1). - `python3 -I scripts/merge_check.py` with no arguments: argparse usage message, exit 2. - `python3 -I scripts/merge_check.py --repo /tmp 1=HEAD`: git reports "not a git repository", exit 2. - `env -u PATH /usr/bin/python3 -I scripts/merge_check.py 1=HEAD`: git is still found through the default search path, exit 1. This is not a missing-git case; the empty-PATH test covers that. - `python3 -I tests/test_text_write_line_endings.py`: Ran 3 tests, OK. - `python3 -I scripts/check_no_game_data.py scripts/merge_check.py tests/test_merge_check.py`: exit 0. Not tested: Windows (the mocked test does not depend on the platform, but the empty-PATH test checks only the message prefix there); CI on other Python versions; this branch together with #242 and #300. AI-assisted (Claude Code).
The docstring promises exit 2 on a git error. When git is not on PATH,
subprocess.run raised FileNotFoundError, which escaped main() as a
traceback with exit 1. The git() helper now turns the OSError into a
GitError, so the message reads "merge_check: cannot run git: ..." and
the exit code is 2.

Tests cover a patched FileNotFoundError, an empty PATH, and a folder
that is not a repository (git exits nonzero; still exit 2).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:11:31 +00:00
github manually merged commit 5abe553f5f into gh-pr/610/base 2026-11-26 09:11:35 +00:00
Author
Owner

@⁠Dezmonius on GitHub · 2026-11-24 10:24 UTC · original

Independent Windows 10 22H2 check at 5abe553 against main 21625bc, using CPython 3.14.7 and Git for Windows 2.56.0.windows.4. All 23 test_merge_check.py tests passed.

I also ran the CLI with --json in separate Python processes:

Scenario Main PR
PATH points to an empty directory exit 1, WinError 2 traceback exit 2, merge_check: cannot run git:
PATH contains only a synthetic unlaunchable git.exe exit 1, WinError 216 traceback exit 2, merge_check: cannot run git:
Git available, target is not a repository exit 2 with Git's diagnostic same

The PR leaves stdout empty in all three failure cases and emits no traceback. I also combined the test change from #621 locally: all 23 tests still passed with docs/ in the isolated profile's global ignore file.

Synthetic repositories and fixture only. AI-assisted (Codex).

**@⁠Dezmonius** on GitHub · 2026-11-24 10:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/610#issuecomment-7128735990) Independent Windows 10 22H2 check at `5abe553` against main `21625bc`, using CPython 3.14.7 and Git for Windows 2.56.0.windows.4. All 23 `test_merge_check.py` tests passed. I also ran the CLI with `--json` in separate Python processes: | Scenario | Main | PR | | --- | --- | --- | | PATH points to an empty directory | exit 1, WinError 2 traceback | exit 2, `merge_check: cannot run git:` | | PATH contains only a synthetic unlaunchable `git.exe` | exit 1, WinError 216 traceback | exit 2, `merge_check: cannot run git:` | | Git available, target is not a repository | exit 2 with Git's diagnostic | same | The PR leaves stdout empty in all three failure cases and emits no traceback. I also combined the test change from #621 locally: all 23 tests still passed with `docs/` in the isolated profile's global ignore file. Synthetic repositories and fixture only. AI-assisted (Codex).
Author
Owner

@⁠madvillain91 on GitHub · 2026-11-24 10:24 UTC · original · reviewed

Tested locally on Windows 11 — all 23 tests in \ ests/test_merge_check.py\ pass (including \ est_git_missing_from_an_empty_path_is_a_git_error\ with \PATH=os.devnull). The \OSError\ handling in \git()\ and the --json\ empty-stdout check look clean. LGTM.

**@⁠madvillain91** on GitHub · 2026-11-24 10:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/610#pullrequestreview-6037497963) · **reviewed** Tested locally on Windows 11 — all 23 tests in \ ests/test_merge_check.py\ pass (including \ est_git_missing_from_an_empty_path_is_a_git_error\ with \PATH=os.devnull\). The \OSError\ handling in \git()\ and the \--json\ empty-stdout check look clean. LGTM.
This discussion has been locked. Commenting is limited to contributors.
No description provided.