Repository navigation
Distinguish a Confirmed Deletion From a Revision-Read Failure #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| import re | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import time | ||
| import urllib.error | ||
| import urllib.request | ||
|
|
@@ -1718,10 +1719,12 @@ def classify_verbatim(down_text, canon_text, past_texts): | |
| def _git_revisions(rel_path): | ||
| """Every commit that touched rel_path in the hub's history, newest first, as (date, sha, text). | ||
|
|
||
| `text` is None where `git show` failed (rare: a permission or encoding fluke, not absence, | ||
| since the commit came from `git log -- rel_path` and so the path existed at that revision). | ||
| Cached because one canonical's history is read once per fidelity/staleness check, then reused | ||
| for every audited repo's copy. | ||
| `text` is None for a confirmed deletion, checked against the revision's own tree rather than | ||
| `git show`'s stderr wording (which reads differently once rel_path exists again in a later | ||
| commit). A `git show` failure for any other reason (a permission or encoding fluke, a corrupt | ||
| object) raises instead of folding into the same None, so a real command fault cannot pass as | ||
| an ordinary deletion. Cached because one canonical's history is read once per fidelity/staleness | ||
| check, then reused for every audited repo's copy. | ||
| """ | ||
| r = subprocess.run( | ||
| ["git", "log", "--format=%cI %H", "--", rel_path], | ||
|
|
@@ -1740,6 +1743,22 @@ def _git_revisions(rel_path): | |
| out = [] | ||
| for line in r.stdout.splitlines(): | ||
| date, sha = line.split(" ", 1) | ||
| t = subprocess.run( | ||
| ["git", "ls-tree", sha, "--", rel_path], | ||
| cwd=ROOT, | ||
| capture_output=True, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
| if t.returncode != 0: | ||
| # A real lookup failure (a corrupt object, not this sha): git ls-tree exits non-zero | ||
| # only for that, never for a merely absent path (empty stdout, exit 0, below). | ||
| raise RuntimeError(f"git ls-tree failed for {sha}:{rel_path}: {t.stderr.strip()}") | ||
| if not t.stdout.strip(): | ||
| # Confirmed deletion: rel_path is absent from this revision's own tree, whether or | ||
| # not it exists again in a later commit or the current working tree. | ||
| out.append((date, sha, None)) | ||
| continue | ||
| # Decode as UTF-8 with replacement to match the downstream and canonical reads. | ||
| # A divergent decode would fabricate a mismatch. | ||
| s = subprocess.run( | ||
|
|
@@ -1750,7 +1769,9 @@ def _git_revisions(rel_path): | |
| errors="replace", | ||
| check=False, | ||
| ) | ||
| out.append((date, sha, s.stdout if s.returncode == 0 else None)) | ||
| if s.returncode != 0: | ||
| raise RuntimeError(f"git show failed for {sha}:{rel_path}: {s.stderr.strip()}") | ||
| out.append((date, sha, s.stdout)) | ||
| return out | ||
|
|
||
|
|
||
|
|
@@ -3241,6 +3262,54 @@ def _selftest(): | |
| f" {'ok ' if got == want else 'FAIL'} want={want!s:<24} got={got!s:<24} _last_effective_change: {label}" | ||
| ) | ||
|
|
||
| # _git_revisions: a deletion revision reads as None even once rel_path exists again in a | ||
| # later commit, per ptr727/ProjectTemplate#1016 and #1018. | ||
| with tempfile.TemporaryDirectory() as tmp_root: | ||
| tmp_root_path = pathlib.Path(tmp_root) | ||
| for cmd in ( | ||
| ["git", "init", "-q"], | ||
| ["git", "config", "user.email", "[email protected]"], | ||
| ["git", "config", "user.name", "test"], | ||
| ): | ||
| subprocess.run(cmd, cwd=tmp_root_path, check=True, capture_output=True) | ||
| rel = "deletion-probe.txt" | ||
| (tmp_root_path / rel).write_text("v1\n") | ||
| subprocess.run(["git", "add", rel], cwd=tmp_root_path, check=True, capture_output=True) | ||
| subprocess.run( | ||
| ["git", "commit", "-q", "-m", "add"], cwd=tmp_root_path, check=True, capture_output=True | ||
| ) | ||
| subprocess.run(["git", "rm", "-q", rel], cwd=tmp_root_path, check=True, capture_output=True) | ||
| subprocess.run( | ||
| ["git", "commit", "-q", "-m", "delete"], | ||
| cwd=tmp_root_path, | ||
| check=True, | ||
| capture_output=True, | ||
| ) | ||
| (tmp_root_path / rel).write_text("v2\n") | ||
| subprocess.run(["git", "add", rel], cwd=tmp_root_path, check=True, capture_output=True) | ||
| subprocess.run( | ||
| ["git", "commit", "-q", "-m", "readd"], | ||
| cwd=tmp_root_path, | ||
| check=True, | ||
| capture_output=True, | ||
| ) | ||
| global ROOT | ||
| saved_root = ROOT | ||
| ROOT = tmp_root_path | ||
| try: | ||
| _git_revisions.cache_clear() | ||
| revisions = _git_revisions(rel) | ||
| finally: | ||
| ROOT = saved_root | ||
| _git_revisions.cache_clear() | ||
| got = [text for _, _, text in revisions] | ||
| want = ["v2\n", None, "v1\n"] | ||
| if got != want: | ||
| ok = False | ||
| print( | ||
| f" {'ok ' if got == want else 'FAIL'} want={want!s:<24} got={got!s:<24} _git_revisions: re-added file" | ||
| ) | ||
|
|
||
| # Region extraction and hashing: a forked github-release block must hash differently from the canonical. | ||
| region = split_jobs(rel_ok).get("github-release") | ||
| forked_region = split_jobs( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: ptr727/ProjectTemplate
Length of output: 16306
🏁 Script executed:
Repository: ptr727/ProjectTemplate
Length of output: 50379
🏁 Script executed:
Repository: ptr727/ProjectTemplate
Length of output: 25447
🌐 Web query:
Git ls-tree core.quotePath non-ASCII path output documentation💡 Result:
By default, the git ls-tree command quotes "unusual" characters in pathnames to ensure safe display [1][2]. This quoting behavior is controlled by the configuration variable core.quotePath [3]. When core.quotePath is set to its default value (true), characters with values larger than 0x80—which includes many non-ASCII characters—are considered "unusual" and are escaped in the output using backslash-based octal sequences (e.g., \302\265) [3]. Additionally, control characters, double quotes, and backslashes are always escaped regardless of this setting [3]. To display non-ASCII pathnames verbatim, you have two primary options: 1. Use the -z option: Adding the -z flag to the git ls-tree command causes it to output pathnames as-is (verbatim) and uses a NUL byte as the line terminator instead of a newline [1][4]. This is generally the recommended approach for scripts or programmatic access [5][3]. 2. Configure core.quotePath: You can globally or locally disable this behavior by setting the configuration variable to false: git config core.quotePath false When set to false, bytes higher than 0x80 are no longer considered "unusual" and will not be escaped [3]. Note that double quotes, backslashes, and control characters will still remain escaped [3]. Documentation confirms that this mechanism ensures compatibility with various terminals and tools that might otherwise struggle with non-ASCII or control characters in filenames [1][3].
Citations:
Use explicit decoding for
git ls-treeoutput.When
core.quotePath=falseand the locale cannot decode a non-ASCII tracked path,text=Truecan raiseUnicodeDecodeErrorbefore_git_revisions()checkst.returncode. Setencoding="utf-8", errors="replace"or keep this subprocess output as bytes.🧰 Tools
🪛 ast-grep (0.45.2)
[error] 1745-1751: Command coming from incoming request
Context: subprocess.run(
["git", "ls-tree", sha, "--", rel_path],
cwd=ROOT,
capture_output=True,
text=True,
check=False,
)
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
(subprocess-from-request)
🤖 Prompt for AI Agents