From af3995ecda153f6a80ae11406a91a5a134c61204 Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Tue, 9 Jun 2026 12:42:50 +0200 Subject: [PATCH 1/3] Fix #133 -- Handle git diff mnemonic prefixes --- relint/parse.py | 9 +++++++-- tests/test_parse.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/relint/parse.py b/relint/parse.py index c14c9bc..8ddf9af 100644 --- a/relint/parse.py +++ b/relint/parse.py @@ -14,8 +14,13 @@ from rich.syntax import Syntax GIT_DIFF_LINE_NUMBERS_PATTERN = re.compile(r"@ -\d+(,\d+)? \+(\d+)(,)?(\d+)? @") -GIT_DIFF_FILENAME_PATTERN = re.compile(r"(?:\n|^)diff --git a\/.* b\/(.*)(?:\n|$)") -GIT_DIFF_SPLIT_PATTERN = re.compile(r"(?:\n|^)diff --git a\/.* b\/.*(?:\n|$)") +GIT_DIFF_PREFIX_PATTERN = r"[^/\n]+/" +GIT_DIFF_FILENAME_PATTERN = re.compile( + rf"(?:\n|^)diff --git {GIT_DIFF_PREFIX_PATTERN}.* {GIT_DIFF_PREFIX_PATTERN}(.*)(?:\n|$)" +) +GIT_DIFF_SPLIT_PATTERN = re.compile( + rf"(?:\n|^)diff --git {GIT_DIFF_PREFIX_PATTERN}.* {GIT_DIFF_PREFIX_PATTERN}.*(?:\n|$)" +) def lint_file(filename, tests): diff --git a/tests/test_parse.py b/tests/test_parse.py index 941afbe..f3b79ae 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -34,6 +34,10 @@ def test_line_numbers_when_only_one_line_was_changed(self): "diff --git a/pardal/authserver/app.py b/pardal/authserver/app.py", "pardal/authserver/app.py", ), + ( + "diff --git c/sql/very-important-sql.sql i/sql/very-important-sql.sql", + "sql/very-important-sql.sql", + ), ], ) def test_parse_filenames(self, output, expected_filename): @@ -118,6 +122,21 @@ def test_parse_complete_diff(self): assert parsed_content == expected + def test_parse_diff_with_mnemonic_prefixes(self): + output = ( + "diff --git c/test_parse.py i/test_parse.py\n" + "index 9c7f392..9bde2ad 100644\n" + "--- c/test_parse.py\n" + "+++ i/test_parse.py\n" + "@@ -1,0 +2 @@\n" + "+# TODO: I'll do it later, promise\n" + ) + + parsed_content = parse_diff(output) + expected = {"test_parse.py": [2]} + + assert parsed_content == expected + def test_empty_config_file(self, tmpdir): tmpdir.join(".relint.yml").write("") tmpdir.join("dummy.py").write("") @@ -166,6 +185,25 @@ def test_git_diff(self, capsys, tmpdir, fixture_dir): assert "0" in str(exc_info.value) + def test_git_diff_with_mnemonic_prefixes(self, capsys, tmpdir, fixture_dir): + with (fixture_dir / ".relint.yml").open() as fs: + config = fs.read() + tmpdir.join(".relint.yml").write(config) + tmpdir.join("dummy.py").write("# TODO do something") + subprocess.check_call(["git", "init"], cwd=tmpdir.strpath) # noqa: S607 + subprocess.check_call( # noqa: S607 + ["git", "config", "diff.mnemonicPrefix", "true"], cwd=tmpdir.strpath + ) + subprocess.check_call(["git", "add", "dummy.py"], cwd=tmpdir.strpath) # noqa: S607 + + with tmpdir.as_cwd(): + with pytest.raises(SystemExit) as exc_info: + main(["dummy.py", "--git-diff"]) + + out, _ = capsys.readouterr() + assert "Get it done right away!" in out + assert exc_info.value.code == 0 + def test_no_unicode(capsys, tmpdir, fixture_dir): with (fixture_dir / ".relint.yml").open() as fs: From b546a1dc4eebb202ebed721154bdaafa640c3e6e Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Tue, 9 Jun 2026 12:47:22 +0200 Subject: [PATCH 2/3] Fix ruff --- tests/test_parse.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_parse.py b/tests/test_parse.py index f3b79ae..aaf1a76 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -191,8 +191,9 @@ def test_git_diff_with_mnemonic_prefixes(self, capsys, tmpdir, fixture_dir): tmpdir.join(".relint.yml").write(config) tmpdir.join("dummy.py").write("# TODO do something") subprocess.check_call(["git", "init"], cwd=tmpdir.strpath) # noqa: S607 - subprocess.check_call( # noqa: S607 - ["git", "config", "diff.mnemonicPrefix", "true"], cwd=tmpdir.strpath + subprocess.check_call( + ["git", "config", "diff.mnemonicPrefix", "true"], # noqa: S607 + cwd=tmpdir.strpath, ) subprocess.check_call(["git", "add", "dummy.py"], cwd=tmpdir.strpath) # noqa: S607 From 6634a1a3006731dc6ff6878193f463fec5986301 Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Tue, 9 Jun 2026 16:04:35 +0200 Subject: [PATCH 3/3] Narrow diff prefix parsing --- relint/parse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relint/parse.py b/relint/parse.py index 8ddf9af..bb54e09 100644 --- a/relint/parse.py +++ b/relint/parse.py @@ -14,7 +14,7 @@ from rich.syntax import Syntax GIT_DIFF_LINE_NUMBERS_PATTERN = re.compile(r"@ -\d+(,\d+)? \+(\d+)(,)?(\d+)? @") -GIT_DIFF_PREFIX_PATTERN = r"[^/\n]+/" +GIT_DIFF_PREFIX_PATTERN = r"[abciow]/" GIT_DIFF_FILENAME_PATTERN = re.compile( rf"(?:\n|^)diff --git {GIT_DIFF_PREFIX_PATTERN}.* {GIT_DIFF_PREFIX_PATTERN}(.*)(?:\n|$)" )