Skip to content

Commit fcf3358

Browse files
authored
Sanitise file URIs for git blame (#54)
Sanitise file URIs for git blame
1 parent 4bf8cad commit fcf3358

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- Multiple occurrences of a single issue are now sorted by location in the Word report.
3030
- Improved debug and version reporting for when multiple versions are installed.
3131
- For the copy operation, "invocation" in the resulting sarif is changed to an object to match the spec.
32+
- #53 Fix the `blame` command for `file:///` URL locations.
3233

3334
### Compatibility
3435

sarif/operations/blame_op.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import os
77
import subprocess
88
import sys
9+
import urllib.parse
10+
import urllib.request
911

1012
from sarif.sarif_file import SarifFileSet
1113

@@ -94,10 +96,20 @@ def _enhance_with_blame(input_files, repo_path):
9496
print(f"Found blame information for {blame_info_count} of {item_count} results")
9597

9698

99+
def _make_path_git_compatible(file_path):
100+
try:
101+
path_as_url = urllib.parse.urlparse(file_path)
102+
if path_as_url.scheme == "file":
103+
return urllib.request.url2pathname(path_as_url.path)
104+
return file_path
105+
except ValueError:
106+
return file_path
107+
108+
97109
def _run_git_blame_on_files(files_to_blame, repo_path):
98110
file_blame_info = {}
99111
for file_path in files_to_blame:
100-
cmd = ["git", "blame", "--porcelain", file_path]
112+
cmd = ["git", "blame", "--porcelain", _make_path_git_compatible(file_path)]
101113
with subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=repo_path) as proc:
102114
blame_info = {"commits": {}, "line_to_commit": {}}
103115
file_blame_info[file_path] = blame_info
@@ -131,6 +143,7 @@ def _run_git_blame_on_files(files_to_blame, repo_path):
131143
if proc.returncode:
132144
cmd_str = " ".join(cmd)
133145
sys.stderr.write(
134-
f"WARNING: Command `{cmd_str}` failed with exit code {proc.returncode} in {repo_path}\n"
146+
f"WARNING: Command `{cmd_str} "
147+
f"failed with exit code {proc.returncode} in {repo_path}\n"
135148
)
136149
return file_blame_info

0 commit comments

Comments
 (0)