patchutils.py: Invert logic for applying #ifdef patches.

This commit is contained in:
Sebastian Lackner
2016-07-22 17:29:14 +02:00
parent c779a2acbe
commit 4b1b7f77bc
4 changed files with 5250 additions and 3195 deletions

View File

@@ -338,6 +338,7 @@ def extract_patch(patchset, filename):
for patch in patchset.patches:
if patch.modified_file != filename:
continue
assert not patch.is_binary
for chunk in patch.read_chunks():
p.write(chunk)
m.update(chunk)
@@ -473,8 +474,7 @@ def generate_ifdefined(all_patches, skip_checks=False):
'subject': "Autogenerated #ifdef patch for %s." % patch.name }
if skip_checks:
patch.files.append(os.path.basename(filename))
patch.patches.append(patchutils.PatchObject(filename, headers))
patch.files = [os.path.basename(filename)]
continue
with open(filename, "wb") as fp:
@@ -527,9 +527,13 @@ def generate_ifdefined(all_patches, skip_checks=False):
subprocess.call(["git", "add", filename])
# Add the autogenerated file as a last patch
patch.files.append(os.path.basename(filename))
patch.files = [os.path.basename(filename)]
for p in patch.patches:
p.filename = None
p.modified_file = None
for p in patchutils.read_patch(filename):
assert p.modified_file in patch.modified_files
p.patch_author = None
patch.patches.append(p)
def generate_apply_order(all_patches, skip_checks=False):
@@ -744,6 +748,7 @@ def generate_script(all_patches, resolved):
if len(patch.patches):
lines.append("\t(\n")
for p in _unique(patch.patches, key=lambda p: (p.patch_author, p.patch_subject, p.patch_revision)):
if p.patch_author is None: continue
lines.append("\t\techo '+ { \"%s\", \"%s\", %d },';\n" %
(_escape(p.patch_author), _escape(p.patch_subject), p.patch_revision))
lines.append("\t) >> \"$patchlist\"\n")

View File

@@ -612,10 +612,10 @@ def generate_ifdef_patch(original, patched, ifdef):
intermediate.write("\n")
if len(srcdata) and len(dstdata):
intermediate.write("#if defined(%s)\n" % ifdef)
intermediate.write("\n".join(dstdata))
intermediate.write("\n#else /* %s */\n" % ifdef)
intermediate.write("#if !defined(%s)\n" % ifdef)
intermediate.write("\n".join(srcdata))
intermediate.write("\n#else /* %s */\n" % ifdef)
intermediate.write("\n".join(dstdata))
intermediate.write("\n#endif /* %s */\n" % ifdef)
elif len(srcdata):
@@ -637,9 +637,9 @@ def generate_ifdef_patch(original, patched, ifdef):
intermediate.write("\n")
intermediate.flush()
# Now we can finally compute the diff between the patched file and our intermediate file
# Now we can finally compute the diff between the original file and our intermediate file
diff = tempfile.NamedTemporaryFile(mode='w+')
exitcode = subprocess.call(["git", "diff", "--no-index", patched.name, intermediate.name],
exitcode = subprocess.call(["git", "diff", "--no-index", original.name, intermediate.name],
stdout=diff, stderr=_devnull)
if exitcode != 1: # exitcode 0 cannot (=shouldn't) happen in this situation
raise PatchDiffError("Failed to compute diff (exitcode %d)." % exitcode)
@@ -913,23 +913,7 @@ if __name__ == "__main__":
expected = ["@@ -1,9 +1,15 @@",
" line1();", " line2();", " line3();",
"+#if defined(PATCHED)",
" function(arg1, \\",
" new_arg2, \\",
" arg3);",
"+#else /* PATCHED */",
"+function(arg1, \\",
"+ arg2, \\",
"+ arg3);",
"+#endif /* PATCHED */",
" line5();", " line6();", " line7();"]
diff = generate_ifdef_patch(source1, source2, "PATCHED")
lines = diff.read().rstrip("\n").split("\n")
self.assertEqual(lines, expected)
expected = ["@@ -1,9 +1,15 @@",
" line1();", " line2();", " line3();",
"+#if defined(PATCHED)",
"+#if !defined(PATCHED)",
" function(arg1, \\",
" arg2, \\",
" arg3);",
@@ -939,6 +923,22 @@ if __name__ == "__main__":
"+ arg3);",
"+#endif /* PATCHED */",
" line5();", " line6();", " line7();"]
diff = generate_ifdef_patch(source1, source2, "PATCHED")
lines = diff.read().rstrip("\n").split("\n")
self.assertEqual(lines, expected)
expected = ["@@ -1,9 +1,15 @@",
" line1();", " line2();", " line3();",
"+#if !defined(PATCHED)",
" function(arg1, \\",
" new_arg2, \\",
" arg3);",
"+#else /* PATCHED */",
"+function(arg1, \\",
"+ arg2, \\",
"+ arg3);",
"+#endif /* PATCHED */",
" line5();", " line6();", " line7();"]
diff = generate_ifdef_patch(source2, source1, "PATCHED")
lines = diff.read().rstrip("\n").split("\n")
self.assertEqual(lines, expected)