Skip to content

Commit 282fb96

Browse files
committed
patching: rewrite: fully stabilize patch index stanzas as well as From lines
- `git format-patch --zero-commit` doesn't affect `index xxx...yyy` lines, only `From: ` - so use the _classy_ "use a regex with a callback" solution as git format-patch doesn't offer one - this will make _all_ patches change when rewritten, but hopefully _for the last time_ ! - we need to preserve `index 000000000000..xxx` as zeros, which indicate new file creation, thus: - new file creations are rewritten as `index 000000000000..111111111111` - non-creations are rewritten as `index 111111111111..222222222222` - this is the final version of armbian#6455
1 parent d7068fa commit 282fb96

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/tools/common/patching_utils.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@
2828
REGEX_PATCH_FILENAMES = r"^patching file \"(.+)\""
2929
log: logging.Logger = logging.getLogger("patching_utils")
3030

31+
# Magic strings and regex for rewriting patches' "index xxx....yyyy" lines
32+
index_zero = f"{'0' * 12}"
33+
index_from_zero = f"index {'0' * 12}..{'1' * 12}"
34+
index_not_zero = f"index {'1' * 12}..{'2' * 12}"
35+
index_rewrite_regexp: re.Pattern = re.compile(r"index ([0-9a-f]{12})\.\.([0-9a-f]{12})")
36+
37+
38+
# Callback used for rewriting index lines.
39+
def rewrite_indexes_callback(x: re.Match): # Preserve zero from's for new file creations.
40+
if x.group(1) == index_zero:
41+
return index_from_zero
42+
return index_not_zero
43+
3144

3245
class PatchRootDir:
3346
def __init__(self, abs_dir, root_type, patch_type, root_dir):
@@ -808,7 +821,7 @@ def export_commit_as_patch(repo: git.Repo, commit: str):
808821
'--zero-commit', # do not use the git revision, instead 000000...0000
809822
'--stat=120', # 'wider' stat output; default is 80
810823
'--stat-graph-width=10', # shorten the diffgraph graph part, it's too long
811-
'--abbrev=12', # force index length to 12
824+
'--abbrev=12', # force index length to 12 - essential for the regex below to work
812825
"-1", "--stdout", commit
813826
],
814827
cwd=repo.working_tree_dir,
@@ -823,7 +836,17 @@ def export_commit_as_patch(repo: git.Repo, commit: str):
823836
raise Exception(f"Failed to export commit {commit} to patch: {stderr_output}")
824837
if stdout_output == "":
825838
raise Exception(f"Failed to export commit {commit} to patch: no output")
826-
return stdout_output
839+
840+
# Now, massage the output. We don't want the "index 08c33ec7e9f1..528741fcc0ec 100644" lines changing every time.
841+
# We do need to preserve "0000000000.." ones as that indicates new file creation.
842+
# Use a regular expression and a callback to decide. Check the top of this file for the regex and callback.
843+
rewritten_indexes = re.sub(index_rewrite_regexp, rewrite_indexes_callback, stdout_output)
844+
845+
# If rewritten is same as original this didn't work, surely.
846+
if rewritten_indexes == stdout_output:
847+
raise Exception(f"Failed to rewrite indexes in patch output: {stdout_output}")
848+
849+
return rewritten_indexes
827850

828851

829852
# Hack

0 commit comments

Comments
 (0)