Skip to content

Commit a38d9db

Browse files
fix: git thought some php files were binary and refused to diff (#928)
* fix: git thought some php files were binary and refused to diff Pass the --binary flag to git diff so it can diff files no matter if they're binary or text. Fixes #918 * fix: add the --binary flag to one more 'git diff' statement About 1/3 of the APIs are still failing due to binary files. * fix: don't attempt to parse the result of git diff as text * fix: update regex code to work with binary * fix: yet another binary-utf8 issue
1 parent d1bb917 commit a38d9db

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

autosynth/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def patch_merge(
145145
"""
146146
with open(patch_file_path, "wb+") as patch_file:
147147
executor.check_call(
148-
["git", "diff", "HEAD", branch_name], stdout=patch_file, cwd=git_repo_dir
148+
["git", "diff", "--binary", "HEAD", branch_name], stdout=patch_file, cwd=git_repo_dir
149149
)
150150
if os.stat(patch_file_path).st_size:
151151
executor.check_call(["git", "apply", patch_file_path], cwd=git_repo_dir)

autosynth/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def synthesize_library(
124124
logger.error(f"Synthesis failed for {library['name']}")
125125
return {
126126
"name": library["name"],
127-
"output": output.decode("utf-8"),
127+
"output": output.decode("utf-8", errors="ignore"),
128128
"error": error,
129129
"skipped": skipped,
130130
}

autosynth/synth_toolbox.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ def _compose_pr_title(
337337

338338
def git_branches_differ(branch_a: str, branch_b: str, metadata_path: str) -> bool:
339339
# Check to see if any files besides synth.metadata were added, modified, deleted.
340-
diff_cmd = ["git", "diff", f"{branch_a}..{branch_b}"]
340+
diff_cmd = ["git", "diff", "--binary", f"{branch_a}..{branch_b}"]
341341
diff_cmd.extend(["--", ".", f":(exclude){metadata_path}"])
342-
proc = executor.run(diff_cmd, stdout=subprocess.PIPE, universal_newlines=True)
342+
proc = executor.run(diff_cmd, stdout=subprocess.PIPE)
343343
proc.check_returncode()
344344
if bool(proc.stdout):
345345
return True

synthtool/transforms.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,28 @@ def move(
224224

225225

226226
def _replace_in_file(path, expr, replacement):
227-
with path.open("r+") as fh:
228-
content = fh.read()
229-
content, count = expr.subn(replacement, content)
230-
231-
# Don't bother writing the file if we didn't change
232-
# anything.
233-
if count:
234-
fh.seek(0)
235-
fh.write(content)
236-
fh.truncate()
237-
return count
227+
try:
228+
with path.open("r+") as fh:
229+
return _replace_in_file_handle(fh, expr, replacement)
230+
except UnicodeDecodeError:
231+
pass # It's a binary file. Try again with a binary regular expression.
232+
flags = expr.flags & ~re.UNICODE
233+
expr = re.compile(expr.pattern.encode(), flags)
234+
with path.open("rb+") as fh:
235+
return _replace_in_file_handle(fh, expr, replacement.encode())
236+
237+
238+
def _replace_in_file_handle(fh, expr, replacement):
239+
content = fh.read()
240+
content, count = expr.subn(replacement, content)
241+
242+
# Don't bother writing the file if we didn't change
243+
# anything.
244+
if count:
245+
fh.seek(0)
246+
fh.write(content)
247+
fh.truncate()
248+
return count
238249

239250

240251
def replace(

0 commit comments

Comments
 (0)