|
|
@ -45,7 +45,7 @@ def fetch_content(host: str, repo: str, ref: str, file: str) -> bytes:
|
|
|
|
return base64.b64decode(response.read())
|
|
|
|
return base64.b64decode(response.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def git_diff(src: str | None, dest: str | None, unified: int | None) -> str:
|
|
|
|
def git_diff(src: str | None, dest: str | None) -> str:
|
|
|
|
"""Returns the result of `git diff --no-index` between two paths.
|
|
|
|
"""Returns the result of `git diff --no-index` between two paths.
|
|
|
|
|
|
|
|
|
|
|
|
If a path is not specified, the diff is against /dev/null. At least one of
|
|
|
|
If a path is not specified, the diff is against /dev/null. At least one of
|
|
|
@ -54,23 +54,13 @@ def git_diff(src: str | None, dest: str | None, unified: int | None) -> str:
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
src: Source path.
|
|
|
|
src: Source path.
|
|
|
|
dest: Destination path.
|
|
|
|
dest: Destination path.
|
|
|
|
unified: Number of lines of context. If None, git diff uses 3 as
|
|
|
|
|
|
|
|
the default value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
A string containing the git diff.
|
|
|
|
A string containing the git diff.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
args = ["git", "diff", "--no-index"]
|
|
|
|
return subprocess2.capture(
|
|
|
|
if unified is not None:
|
|
|
|
["git", "diff", "--no-index", "--", src or DEV_NULL, dest
|
|
|
|
# git diff doesn't error out even if it's given a negative <n> value.
|
|
|
|
or DEV_NULL]).decode("utf-8")
|
|
|
|
# e.g., --unified=-3323, -U-3
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# It just ignores the value and treats it as 0.
|
|
|
|
|
|
|
|
# hence, this script doesn't bother validating the <n> value.
|
|
|
|
|
|
|
|
args.append(f"-U{unified}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args.extend(["--", src or DEV_NULL, dest or DEV_NULL])
|
|
|
|
|
|
|
|
return subprocess2.capture(args).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _process_diff(diff: str, src_root: str, dst_root: str) -> str:
|
|
|
|
def _process_diff(diff: str, src_root: str, dst_root: str) -> str:
|
|
|
@ -109,12 +99,7 @@ def _process_diff(diff: str, src_root: str, dst_root: str) -> str:
|
|
|
|
return header
|
|
|
|
return header
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_diff(host: str,
|
|
|
|
def _create_diff(host: str, repo: str, ref: str, root: str, file: str) -> str:
|
|
|
|
repo: str,
|
|
|
|
|
|
|
|
ref: str,
|
|
|
|
|
|
|
|
root: str,
|
|
|
|
|
|
|
|
file: str,
|
|
|
|
|
|
|
|
unified: int | None = None) -> str:
|
|
|
|
|
|
|
|
new_file = os.path.join(root, file)
|
|
|
|
new_file = os.path.join(root, file)
|
|
|
|
if not os.path.exists(new_file):
|
|
|
|
if not os.path.exists(new_file):
|
|
|
|
new_file = None
|
|
|
|
new_file = None
|
|
|
@ -132,12 +117,12 @@ def _create_diff(host: str,
|
|
|
|
raise RuntimeError(f"Could not access file {file} from {root} "
|
|
|
|
raise RuntimeError(f"Could not access file {file} from {root} "
|
|
|
|
f"or from {host}/{repo}:{ref}.")
|
|
|
|
f"or from {host}/{repo}:{ref}.")
|
|
|
|
|
|
|
|
|
|
|
|
diff = git_diff(old_file, new_file, unified)
|
|
|
|
diff = git_diff(old_file, new_file)
|
|
|
|
return _process_diff(diff, tmp_root, root)
|
|
|
|
return _process_diff(diff, tmp_root, root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_diffs(host: str, repo: str, ref: str, root: str, files: list[str],
|
|
|
|
def create_diffs(host: str, repo: str, ref: str, root: str,
|
|
|
|
unified: int | None) -> dict[str, str]:
|
|
|
|
files: list[str]) -> dict[str, str]:
|
|
|
|
"""Calculates diffs of files in a directory against a commit.
|
|
|
|
"""Calculates diffs of files in a directory against a commit.
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
@ -146,8 +131,6 @@ def create_diffs(host: str, repo: str, ref: str, root: str, files: list[str],
|
|
|
|
ref: Gerrit commit.
|
|
|
|
ref: Gerrit commit.
|
|
|
|
root: Path of local directory containing modified files.
|
|
|
|
root: Path of local directory containing modified files.
|
|
|
|
files: List of file paths relative to root.
|
|
|
|
files: List of file paths relative to root.
|
|
|
|
unified: Number of lines of context. If None, git diff uses 3 as
|
|
|
|
|
|
|
|
the default value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
A dict mapping file paths to diffs.
|
|
|
|
A dict mapping file paths to diffs.
|
|
|
@ -159,8 +142,7 @@ def create_diffs(host: str, repo: str, ref: str, root: str, files: list[str],
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(
|
|
|
|
max_workers=MAX_CONCURRENT_CONNECTION) as executor:
|
|
|
|
max_workers=MAX_CONCURRENT_CONNECTION) as executor:
|
|
|
|
futures_to_file = {
|
|
|
|
futures_to_file = {
|
|
|
|
executor.submit(_create_diff, host, repo, ref, root, file, unified):
|
|
|
|
executor.submit(_create_diff, host, repo, ref, root, file): file
|
|
|
|
file
|
|
|
|
|
|
|
|
for file in files
|
|
|
|
for file in files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for future in concurrent.futures.as_completed(futures_to_file):
|
|
|
|
for future in concurrent.futures.as_completed(futures_to_file):
|
|
|
@ -183,20 +165,15 @@ def main(argv):
|
|
|
|
parser.add_argument("--root",
|
|
|
|
parser.add_argument("--root",
|
|
|
|
required=True,
|
|
|
|
required=True,
|
|
|
|
help="Folder containing modified files.")
|
|
|
|
help="Folder containing modified files.")
|
|
|
|
parser.add_argument("-U",
|
|
|
|
|
|
|
|
"--unified",
|
|
|
|
|
|
|
|
required=False,
|
|
|
|
|
|
|
|
type=int,
|
|
|
|
|
|
|
|
help="generate diffs with <n> lines context",
|
|
|
|
|
|
|
|
metavar='<n>')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
parser.add_argument(
|
|
|
|
"files",
|
|
|
|
"files",
|
|
|
|
nargs="+",
|
|
|
|
nargs="+",
|
|
|
|
help="List of changed files. Paths are relative to the repo root.",
|
|
|
|
help="List of changed files. Paths are relative to the repo root.",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
options = parser.parse_args(argv)
|
|
|
|
options = parser.parse_args(argv)
|
|
|
|
|
|
|
|
|
|
|
|
diffs = create_diffs(options.host, options.repo, options.ref, options.root,
|
|
|
|
diffs = create_diffs(options.host, options.repo, options.ref, options.root,
|
|
|
|
options.files, options.unified)
|
|
|
|
options.files)
|
|
|
|
|
|
|
|
|
|
|
|
unified_diff = "\n".join([d for d in diffs.values() if d])
|
|
|
|
unified_diff = "\n".join([d for d in diffs.values() if d])
|
|
|
|
if options.output:
|
|
|
|
if options.output:
|
|
|
|