mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:28:37 +00:00
merge_check: report Git blob read failures
Use the diff to identify new files and surface required blob read failures through the existing Git error exit.
This commit is contained in:
@ -105,14 +105,18 @@ def duplicates(repo, main, tree, min_length, block=3):
|
||||
legitimately."""
|
||||
found = {}
|
||||
hunks, path, added = {}, None, None
|
||||
new_file = False
|
||||
patch = git(repo, "-c", "core.quotePath=true", "diff", "--unified=0", "--no-color", "--no-renames",
|
||||
"--src-prefix=a/", "--dst-prefix=b/", "--no-relative", "--no-ext-diff", "--no-textconv",
|
||||
main, tree).stdout
|
||||
for line in patch.split("\n"):
|
||||
if line.startswith("diff --git "):
|
||||
path, added = None, None
|
||||
new_file = False
|
||||
elif added is None and line == "--- /dev/null":
|
||||
new_file = True
|
||||
elif added is None and line.startswith("+++ "):
|
||||
path = patch_destination(line)
|
||||
path = None if new_file else patch_destination(line)
|
||||
elif line.startswith("@@ ") and path:
|
||||
added = []
|
||||
hunks.setdefault(path, []).append(added)
|
||||
@ -121,15 +125,13 @@ def duplicates(repo, main, tree, min_length, block=3):
|
||||
if text:
|
||||
added.append(text)
|
||||
def windows(rev, path):
|
||||
r = git(repo, "show", f"{rev}:{path}", check=False)
|
||||
lines = [l.strip() for l in r.stdout.split("\n") if l.strip()] if r.returncode == 0 else []
|
||||
return r.returncode == 0, Counter(tuple(lines[i:i + block]) for i in range(len(lines) - block + 1))
|
||||
out = git(repo, "show", f"{rev}:{path}").stdout
|
||||
lines = [l.strip() for l in out.split("\n") if l.strip()]
|
||||
return Counter(tuple(lines[i:i + block]) for i in range(len(lines) - block + 1))
|
||||
|
||||
for path, groups in hunks.items():
|
||||
exists, before = windows(main, path)
|
||||
if not exists:
|
||||
continue # a new file: nothing to duplicate
|
||||
_, after = windows(tree, path)
|
||||
before = windows(main, path)
|
||||
after = windows(tree, path)
|
||||
same = []
|
||||
for added in groups:
|
||||
marked = [False] * len(added)
|
||||
|
||||
@ -13,7 +13,7 @@ import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
from unittest import mock
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
@ -147,6 +147,40 @@ class MergeCheckTest(unittest.TestCase):
|
||||
self.assertEqual(r["merges"], []) # another number: only the text check can see it
|
||||
self.assertEqual(r["duplicates"], {"docs/notes.md": 3})
|
||||
|
||||
def test_duplicate_check_reports_failed_blob_reads(self):
|
||||
self.append("docs/notes.md", "\n" + PARAGRAPH)
|
||||
self.commit("Existing paragraph")
|
||||
self.pr_branch("repeat", "main", "\n" + PARAGRAPH)
|
||||
tree = self.git("rev-parse", "repeat^{tree}")
|
||||
run = subprocess.run
|
||||
|
||||
for rev in ("main", tree):
|
||||
with self.subTest(revision=rev):
|
||||
read = ["show", f"{rev}:docs/notes.md"]
|
||||
|
||||
def fail_read(args, **kwargs):
|
||||
if args[3:] == read:
|
||||
return subprocess.CompletedProcess(args, 128, b"", b"fatal: could not read blob\n")
|
||||
return run(args, **kwargs)
|
||||
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with mock.patch.object(subprocess, "run", side_effect=fail_read), \
|
||||
redirect_stdout(out), redirect_stderr(err):
|
||||
code = mc.main(["--repo", self.repo, "--main", "main", "--json", "20=repeat"])
|
||||
self.assertEqual((code, out.getvalue()), (2, ""))
|
||||
self.assertEqual(err.getvalue(), f"merge_check: git {' '.join(read)}: fatal: could not read blob\n")
|
||||
|
||||
def test_a_new_path_is_clean(self):
|
||||
self.append("docs/notes.md", "\n" + PARAGRAPH)
|
||||
self.commit("Existing paragraph")
|
||||
self.git("checkout", "-q", "-b", "new-path")
|
||||
self.write("docs/new.md", PARAGRAPH)
|
||||
self.commit("Add a separate document")
|
||||
self.git("checkout", "-q", "main")
|
||||
|
||||
code, [result] = self.run_check("20=new-path")
|
||||
self.assertEqual((code, result["verdict"], result["duplicates"]), (0, "clean", {}))
|
||||
|
||||
def test_patch_paths_and_added_header_like_lines_still_count_duplicates(self):
|
||||
paths = ("docs/quoted \u65e5\u672c\u8a9e.md", "docs/notes with spaces.md", "docs/header-like.md")
|
||||
header_like = "++ " + PARAGRAPH
|
||||
|
||||
Reference in New Issue
Block a user