diff mbox series

[v2,07/15] gentree: try to make patch operation idempotent in case of --clean

Message ID 20220513160523.2944694-8-thomas@adapt-ip.com (mailing list archive)
State New, archived
Headers show
Series Resurrect Integration Mode v2 | expand

Commit Message

Thomas Pedersen May 13, 2022, 4:05 p.m. UTC
The patch utility will not just skip a patch if it has
already been applied. In the case of --integrate --clean
we may need to repatch the kernel Makefile, which wasn't
removed along with backports/. To support reapplying
patches without error, revert a failed patch with -R, then
reapply it.

Signed-off-by: Thomas Pedersen <thomas@adapt-ip.com>
---
 gentree.py | 56 +++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/gentree.py b/gentree.py
index 4705ffa08fc4..0576e14d6fe5 100755
--- a/gentree.py
+++ b/gentree.py
@@ -484,6 +484,31 @@  def upload_release(args, rel_prep, logwrite=lambda x:None):
         kup_cmd = "kup put /\n\t\t%s /\n\t\t%s /\n\t\t%s" % (gzip_name, tar_name + '.asc', korg_path)
         logwrite("kup-test: skipping cmd: %s" % kup_cmd)
 
+def do_patch(args, logwrite, pfile, target_dir, reverse=False):
+    cmd = 'patch -p1'.split()
+
+    if reverse:
+        cmd.append('-R')
+    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                               stderr=subprocess.STDOUT, stdin=subprocess.PIPE,
+                               close_fds=True, universal_newlines=True,
+                               cwd=target_dir)
+    output = process.communicate(input=open(pfile, 'r').read())[0]
+    output = output.split('\n')
+    if output[-1] == '':
+        output = output[:-1]
+    if args.verbose:
+        for line in output:
+            logwrite('> %s' % line)
+    else:
+        logwrite("Failed to apply changes from %s" % print_name)
+        if not args.verbose:
+            for line in output:
+                logwrite('> %s' % line)
+
+    return process.returncode
+
+
 def apply_patches(args, desc, source_dir, patch_src, target_dir, logwrite=lambda x:None):
     """
     Given a path of a directories of patches and SmPL patches apply
@@ -539,23 +564,20 @@  def apply_patches(args, desc, source_dir, patch_src, target_dir, logwrite=lambda
                 fullfn = os.path.join(target_dir, patched_file)
                 shutil.copyfile(fullfn, fullfn + '.orig_file')
 
-        process = subprocess.Popen(['patch', '-p1'], stdout=subprocess.PIPE,
-                                   stderr=subprocess.STDOUT, stdin=subprocess.PIPE,
-                                   close_fds=True, universal_newlines=True,
-                                   cwd=target_dir)
-        output = process.communicate(input=open(pfile, 'r').read())[0]
-        output = output.split('\n')
-        if output[-1] == '':
-            output = output[:-1]
-        if args.verbose:
-            for line in output:
-                logwrite('> %s' % line)
-        if process.returncode != 0:
-            if not args.verbose:
-                logwrite("Failed to apply changes from %s" % print_name)
-                for line in output:
-                    logwrite('> %s' % line)
-            return 2
+        err = do_patch(args, logwrite, pfile, target_dir)
+        if err > 0:
+            if not args.clean or err == 2:
+                return 2
+
+            # in case of clean, try to make patch idempotent
+            # try the reverse
+            err = do_patch(args, logwrite, pfile, target_dir, reverse=True)
+            if err != 0:
+                return 2
+            # that worked, now reapply..
+            err = do_patch(args, logwrite, pfile, target_dir)
+            if err != 0:
+                return 2
 
         if args.refresh:
             pfilef = open(pfile + '.tmp', 'a')