Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(ci): make duplication lint advisory (#100614)

Paul D'Ambrae87740663490
diff --git a/.github/workflows/ci-duplication.yml b/.github/workflows/ci-duplication.yml
index 5492d2cc4211..26de48f4bdde 100644
--- a/.github/workflows/ci-duplication.yml
+++ b/.github/workflows/ci-duplication.yml
@@ -26,9 +26,6 @@ jobs:
name: Duplication lint
runs-on: depot-ubuntu-24.04
timeout-minutes: 10
- # Advisory while the gate proves itself; promote to blocking once it
- # has run clean across open PRs for a while.
- continue-on-error: true
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Fetch PR base for the clone baseline
@@ -52,6 +49,8 @@ jobs:
with:
node-version-file: .nvmrc
- name: Lint for new duplication
+ # Keep the job successful while GitHub marks this failed step as a warning.
+ continue-on-error: true
env:
BASE_REF: ${{ github.event.pull_request.base.ref || 'master' }}
run: |
diff --git a/bin/lint_duplication.py b/bin/lint_duplication.py
index 896c21c656a7..36d3aa4f8c48 100644
--- a/bin/lint_duplication.py
+++ b/bin/lint_duplication.py
@@ -201,6 +201,17 @@ def resolve_baseline(base: str, repo: Path) -> str:
return base
+def changed_files_between(baseline: str, current: str, repo: Path) -> set[str]:
+ return set(
+ filter(
+ None,
+ subprocess.check_output(["git", "diff", "--name-only", "-z", baseline, current], cwd=repo, text=True).split(
+ "\0"
+ ),
+ )
+ )
+
+
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base", default="origin/master", help="git ref to compare clones against")
@@ -242,42 +253,37 @@ def main() -> int:
scan_failed = False
with tempfile.TemporaryDirectory(prefix="jscpd-") as tmp:
tmp_path = Path(tmp)
+ current_worktree = tmp_path / "current-worktree"
baseline_worktree = tmp_path / "baseline-worktree"
+ scan_worktrees = ((current_worktree, "HEAD"), (baseline_worktree, baseline))
# Registrations from runs killed mid-scan point at paths that no
# longer exist; drop them before adding a fresh one.
subprocess.run(["git", "worktree", "prune"], capture_output=True, cwd=repo)
- add = subprocess.run(
- ["git", "worktree", "add", "--detach", str(baseline_worktree), baseline],
- capture_output=True,
- text=True,
- cwd=repo,
- )
- if add.returncode != 0:
- print(add.stderr[-2000:])
- print(f"duplication lint could not check out the baseline {baseline}")
- return 2
try:
- current_clones = run_jscpd(repo, tmp_path / "current-report")
+ for worktree, revision in scan_worktrees:
+ add = subprocess.run(
+ ["git", "worktree", "add", "--detach", str(worktree), revision],
+ capture_output=True,
+ text=True,
+ cwd=repo,
+ )
+ if add.returncode != 0:
+ print(add.stderr[-2000:])
+ print(f"duplication lint could not check out {revision}")
+ return 2
+ current_clones = run_jscpd(current_worktree, tmp_path / "current-report")
baseline_clones = run_jscpd(baseline_worktree, tmp_path / "baseline-report")
except SystemExit:
scan_failed = True
current_clones = []
finally:
- subprocess.run(
- ["git", "worktree", "remove", "--force", str(baseline_worktree)], capture_output=True, cwd=repo
- )
+ for worktree, _ in scan_worktrees:
+ subprocess.run(["git", "worktree", "remove", "--force", str(worktree)], capture_output=True, cwd=repo)
if scan_failed:
return 2
- changed_files = set(
- subprocess.check_output(["git", "diff", "--name-only", "-z", baseline], cwd=repo, text=True).split("\0")
- )
- changed_files.update(
- subprocess.check_output(["git", "ls-files", "--others", "--exclude-standard", "-z"], cwd=repo, text=True).split(
- "\0"
- )
- )
+ changed_files = changed_files_between(baseline, "HEAD", repo)
mark_new_clones(current_clones, baseline_clones, changed_files)
print(
f"{len(current_clones)} clones in this tree, {sum(1 for c in current_clones if c['isNew'])} not in the baseline"
diff --git a/bin/test/test_lint_duplication.py b/bin/test/test_lint_duplication.py
index d6ec8025f835..56522ab242b3 100644
--- a/bin/test/test_lint_duplication.py
+++ b/bin/test/test_lint_duplication.py
@@ -1,6 +1,12 @@
#!/usr/bin/env python3
+import sys
+import tempfile
+import subprocess
+from pathlib import Path
+
import unittest
+from unittest.mock import patch
from parameterized import parameterized
@@ -8,10 +14,12 @@ from bin.lint_duplication import (
APP_MAX_NEW_CLONE_TOKENS,
TEST_MAX_NEW_CLONE_TOKENS,
build_findings,
+ changed_files_between,
clone_key,
clone_language,
find_gate_failures,
is_test_file,
+ main,
mark_new_clones,
)
@@ -200,5 +208,51 @@ class TestMarkNewClones(unittest.TestCase):
self.assertEqual(current[0]["isNew"], expected_is_new)
+class TestChangedFilesBetween(unittest.TestCase):
+ def test_ignores_untracked_files(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ repo = Path(temp_dir)
+ subprocess.run(["git", "init", "--quiet"], check=True, cwd=repo)
+ subprocess.run(["git", "config", "user.email", "[email protected]"], check=True, cwd=repo)
+ subprocess.run(["git", "config", "user.name", "Test User"], check=True, cwd=repo)
+ (repo / "tracked.py").write_text("before\n")
+ subprocess.run(["git", "add", "tracked.py"], check=True, cwd=repo)
+ subprocess.run(["git", "commit", "--quiet", "-m", "base"], check=True, cwd=repo)
+ baseline = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True, cwd=repo).strip()
+ (repo / "tracked.py").write_text("after\n")
+ subprocess.run(["git", "commit", "--quiet", "-am", "change tracked file"], check=True, cwd=repo)
+ (repo / "untracked.py").write_text("generated\n")
+
+ self.assertEqual(changed_files_between(baseline, "HEAD", repo), {"tracked.py"})
+
+
+class TestCommittedScanTree(unittest.TestCase):
+ def test_scans_head_without_worktree_changes(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ repo = Path(temp_dir)
+ subprocess.run(["git", "init", "--quiet"], check=True, cwd=repo)
+ subprocess.run(["git", "config", "user.email", "[email protected]"], check=True, cwd=repo)
+ subprocess.run(["git", "config", "user.name", "Test User"], check=True, cwd=repo)
+ (repo / "tracked.py").write_text("before\n")
+ subprocess.run(["git", "add", "tracked.py"], check=True, cwd=repo)
+ subprocess.run(["git", "commit", "--quiet", "-m", "base"], check=True, cwd=repo)
+ baseline = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True, cwd=repo).strip()
+ (repo / "tracked.py").write_text("committed\n")
+ subprocess.run(["git", "commit", "--quiet", "-am", "change tracked file"], check=True, cwd=repo)
+ (repo / "tracked.py").write_text("worktree\n")
+ (repo / "untracked.py").write_text("untracked\n")
+ scans: list[dict[str, str]] = []
+
+ def record_scan(scan_root: Path, _out_dir: Path) -> list[dict]:
+ scans.append({path.name: path.read_text() for path in scan_root.glob("*.py")})
+ return []
+
+ with patch("bin.lint_duplication.run_jscpd", side_effect=record_scan):
+ with patch.object(sys, "argv", ["lint_duplication.py", "--base", baseline, "--path", str(repo)]):
+ self.assertEqual(main(), 0)
+
+ self.assertEqual(scans, [{"tracked.py": "committed\n"}, {"tracked.py": "before\n"}])
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/docs/internal/duplication-lint.md b/docs/internal/duplication-lint.md
index b158911b308b..55c07d097cdd 100644
--- a/docs/internal/duplication-lint.md
+++ b/docs/internal/duplication-lint.md
@@ -1,8 +1,8 @@
# Duplication lint
Run `python3 bin/lint_duplication.py --base origin/master` to check for new Python and TypeScript duplication.
-The check compares the working tree with the branch's merge base.
-It includes uncommitted changes and untracked files that Git does not ignore.
+The check compares the branch's committed `HEAD` with its merge base.
+It ignores uncommitted and untracked files.
A reported pair must include at least one changed file to count as new duplication.
Removing a third copy can make the scanner pair two unchanged files differently, which must not fail the check.