Skip to content

Commit e24d19a

Browse files
authored
internal: make lintdiff.py resilient against git errors (#3694)
## Why So that it works on constrained checkouts. Codex complains "❌ make (fails: lintdiff cannot diff against missing main revision)"
1 parent c248b5b commit e24d19a

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

tools/lintdiff.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
def parse_lines(cmd):
1818
# print("+ " + " ".join(cmd), file=sys.stderr, flush=True)
19-
result = subprocess.run(cmd, stdout=subprocess.PIPE, encoding="utf-8", check=True)
19+
result = subprocess.run(cmd, stdout=subprocess.PIPE, encoding="utf-8")
20+
if result.returncode != 0:
21+
return
2022
return [x.strip() for x in result.stdout.split("\n") if x.strip()]
2123

2224

@@ -36,29 +38,34 @@ def main():
3638
args.ref = "HEAD"
3739

3840
# All paths are relative to repo root, so we need to ensure we're in the right directory.
39-
gitroot = parse_lines(["git", "rev-parse", "--show-toplevel"])[0]
40-
os.chdir(gitroot)
41+
gitroot = parse_lines(["git", "rev-parse", "--show-toplevel"])
42+
if gitroot:
43+
os.chdir(gitroot[0])
4144

4245
# Get list of changed files relative to repo root.
4346
# Note: Paths are always relative to repo root, even when running from subdirectories.
4447
# Example: Running from tools/ returns 'tools/lintdiff.py' rather than just 'lintdiff.py'.
4548
changed = parse_lines(["git", "diff", "--name-only", args.ref, "--", "."])
49+
if changed is None:
50+
cmd = ["golangci-lint"] + args.args
51+
else:
52+
# We need to pass packages to golangci-lint, not individual files.
53+
# QQQ for lint we should also pass all dependent packages
54+
dirs = set()
55+
for filename in changed:
56+
if "/testdata/" in filename:
57+
continue
58+
if filename.endswith(".go"):
59+
d = os.path.dirname(filename)
60+
dirs.add(d)
61+
62+
dirs = ["./" + d for d in sorted(dirs) if os.path.exists(d)]
63+
64+
if not dirs:
65+
sys.exit(0)
66+
67+
cmd = ["golangci-lint"] + args.args + dirs
4668

47-
# We need to pass packages to golangci-lint, not individual files.
48-
dirs = set()
49-
for filename in changed:
50-
if "/testdata/" in filename:
51-
continue
52-
if filename.endswith(".go"):
53-
d = os.path.dirname(filename)
54-
dirs.add(d)
55-
56-
dirs = ["./" + d for d in sorted(dirs) if os.path.exists(d)]
57-
58-
if not dirs:
59-
sys.exit(0)
60-
61-
cmd = ["golangci-lint"] + args.args + dirs
6269
print("+ " + " ".join(cmd), file=sys.stderr, flush=True)
6370
os.execvp(cmd[0], cmd)
6471

0 commit comments

Comments
 (0)