[git-buildpackage] [PATCH] Implement 'unidiff-zero' option

Sergio Durigan Junior sergiodj at sergiodj.net
Mon Mar 4 23:14:03 CET 2024


Some Debian packages (namely QEMU, although it has been "fixed"
recently) keep patches using the "unified=0" format, i.e., patches
that were generated without context).  For an example of such patch,
see:

https://salsa.debian.org/qemu-team/qemu/-/blob/4112b90d8217c618aec5050c2059223486150cc0/debian/patches/openbios-spelling-endianess.patch

git-apply(1) supports the "--unidiff-zero" option, which can handle
these patches.  This commit exposes this option to gbp users.

Signed-off-by: Sergio Durigan Junior <sergiodj at sergiodj.net>
---
 gbp/config.py            |  5 +++++
 gbp/git/repository.py    |  5 ++++-
 gbp/scripts/common/pq.py | 13 ++++++++-----
 gbp/scripts/pq.py        | 13 +++++++++----
 gbp/scripts/pq_rpm.py    |  8 ++++++--
 5 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/gbp/config.py b/gbp/config.py
index bad59cde..6fbd7ea2 100644
--- a/gbp/config.py
+++ b/gbp/config.py
@@ -195,6 +195,7 @@ class GbpOptionParser(OptionParser):
                 'time-machine': 1,
                 'track': 'True',
                 'track-missing': 'False',
+                'unidiff-zero': 'False',
                 'upstream-branch': 'upstream',
                 'upstream-tag': 'upstream/%(version)s',
                 'upstream-tree': 'TAG',
@@ -384,6 +385,10 @@ class GbpOptionParser(OptionParser):
         'bare':
             "whether to create a bare repository on the remote side. "
             "'Default is '%(bare)s'.",
+        'unidiff-zero':
+            "whether to apply patches using git-apply(1)'s '--unidiff-zero' "
+            "option, for patches that don't have any context (i.e., were generated "
+            "with '--unified=0'). Default is '%(unidiff-zero)'.",
         'urgency':
             "Set urgency level, default is '%(urgency)s'",
         'repo-user':
diff --git a/gbp/git/repository.py b/gbp/git/repository.py
index 972a058f..1c5750c4 100644
--- a/gbp/git/repository.py
+++ b/gbp/git/repository.py
@@ -1781,7 +1781,8 @@ class GitRepository(object):
         output, ret = self._git_getoutput('format-patch', options.args)
         return [line.strip() for line in output]
 
-    def apply_patch(self, patch, index=True, context=None, strip=None, fix_ws=False):
+    def apply_patch(self, patch, index=True, context=None, strip=None, fix_ws=False,
+                    unidiff_zero=False):
         """Apply a patch using git apply"""
         args = []
         if context:
@@ -1792,6 +1793,8 @@ class GitRepository(object):
             args.append("--whitespace=fix")
         if strip is not None:
             args += ['-p', str(strip)]
+        if unidiff_zero:
+            args += ['--unidiff-zero']
         args.append(patch)
         self._git_command("apply", args)
 
diff --git a/gbp/scripts/common/pq.py b/gbp/scripts/common/pq.py
index 1a80ec05..2591b875 100644
--- a/gbp/scripts/common/pq.py
+++ b/gbp/scripts/common/pq.py
@@ -305,13 +305,16 @@ def switch_to_pq_branch(repo, branch):
     repo.set_branch(pq_branch)
 
 
-def apply_single_patch(repo, branch, patch, fallback_author, topic=None):
+def apply_single_patch(repo, branch, patch, fallback_author, topic=None,
+                       unidiff_zero=False):
     switch_to_pq_branch(repo, branch)
-    apply_and_commit_patch(repo, patch, fallback_author, topic)
+    apply_and_commit_patch(repo, patch, fallback_author, topic,
+                           unidiff_zero=unidiff_zero)
     gbp.log.info("Applied %s" % os.path.basename(patch.path))
 
 
-def apply_and_commit_patch(repo, patch, fallback_author, topic=None, name=None):
+def apply_and_commit_patch(repo, patch, fallback_author, topic=None, name=None,
+                           unidiff_zero=False):
     """apply a single patch 'patch', add topic 'topic' and commit it"""
     author = {'name': patch.author,
               'email': patch.email,
@@ -330,10 +333,10 @@ def apply_and_commit_patch(repo, patch, fallback_author, topic=None, name=None):
             gbp.log.warn("Patch '%s' has no authorship information" % patch_fn)
 
     try:
-        repo.apply_patch(patch.path, strip=patch.strip)
+        repo.apply_patch(patch.path, strip=patch.strip, unidiff_zero=unidiff_zero)
     except GitRepositoryError:
         gbp.log.warn("Patch %s failed to apply, retrying with whitespace fixup" % patch_fn)
-        repo.apply_patch(patch.path, strip=patch.strip, fix_ws=True)
+        repo.apply_patch(patch.path, strip=patch.strip, unidiff_zero=unidiff_zero, fix_ws=True)
     tree = repo.write_tree()
     msg = "%s\n\n%s" % (patch.subject, patch.long_desc)
     if topic:
diff --git a/gbp/scripts/pq.py b/gbp/scripts/pq.py
index 78f479ca..d9750f4c 100755
--- a/gbp/scripts/pq.py
+++ b/gbp/scripts/pq.py
@@ -267,7 +267,7 @@ def safe_patches(series, repo):
 
 
 def import_quilt_patches(repo, branch, series, tries, force, pq_from,
-                         upstream_tag):
+                         upstream_tag, unidiff_zero):
     """
     apply a series of quilt patches in the series file 'series' to branch
     the patch-queue branch for 'branch'
@@ -282,6 +282,7 @@ def import_quilt_patches(repo, branch, series, tries, force, pq_from,
                     DEBIAN indicates the current branch, TAG indicates that
                     the corresponding upstream tag should be used.
     @param upstream_tag: upstream tag template to use
+    @param unidiff_zero: whether to apply the patch using '--unidiff-zero'
     """
     tmpdir = None
     series = os.path.join(repo.path, series)
@@ -332,7 +333,8 @@ def import_quilt_patches(repo, branch, series, tries, force, pq_from,
             gbp.log.debug("Applying %s" % patch.path)
             try:
                 name = os.path.basename(patch.path)
-                apply_and_commit_patch(repo, patch, maintainer, patch.topic, name)
+                apply_and_commit_patch(repo, patch, maintainer, patch.topic, name,
+                                       unidiff_zero=unidiff_zero)
             except Exception as e:
                 gbp.log.err("Failed to apply '%s': %s" % (patch.path, e))
                 repo.force_head('HEAD', hard=True)
@@ -371,7 +373,8 @@ def import_pq(repo, branch, options):
     tries = options.time_machine if (options.time_machine > 0) else 1
     num = import_quilt_patches(repo, branch, series, tries,
                                options.force, options.pq_from,
-                               options.upstream_tag)
+                               options.upstream_tag,
+                               options.unidiff_zero)
     gbp.log.info("%d patches listed in '%s' imported on '%s'" %
                  (num, series, repo.get_branch()))
 
@@ -447,6 +450,7 @@ def build_parser(name):
     parser.add_config_file_option(option_name="pq-from", dest="pq_from", choices=['DEBIAN', 'TAG'])
     parser.add_config_file_option(option_name="upstream-tag", dest="upstream_tag")
     parser.add_boolean_config_file_option(option_name="ignore-new", dest="ignore_new")
+    parser.add_boolean_config_file_option(option_name="unidiff-zero", dest="unidiff_zero")
     return parser
 
 
@@ -504,7 +508,8 @@ def main(argv):
         elif action == "apply":
             patch = Patch(patchfile)
             maintainer = get_maintainer_from_control(repo)
-            apply_single_patch(repo, current, patch, maintainer, options.topic)
+            apply_single_patch(repo, current, patch, maintainer, options.topic,
+                               options.unidiff_zero)
         elif action == "switch":
             switch_pq(repo, current, options)
     except KeyboardInterrupt:
diff --git a/gbp/scripts/pq_rpm.py b/gbp/scripts/pq_rpm.py
index fcefbb6e..6ff3980d 100755
--- a/gbp/scripts/pq_rpm.py
+++ b/gbp/scripts/pq_rpm.py
@@ -327,7 +327,8 @@ def import_spec_patches(repo, options):
                      (base, upstream_commit))
         for patch in queue:
             gbp.log.debug("Applying %s" % patch.path)
-            apply_and_commit_patch(repo, patch, packager)
+            apply_and_commit_patch(repo, patch, packager,
+                                   unidiff_zero=options.unidiff_zero)
     except (GbpError, GitRepositoryError) as err:
         repo.set_branch(base)
         repo.delete_branch(pq_branch)
@@ -406,6 +407,8 @@ def build_parser(name):
     parser.add_config_file_option(option_name="spec-file", dest="spec_file")
     parser.add_config_file_option(option_name="packaging-dir",
                                   dest="packaging_dir")
+    parser.add_boolean_config_file_option(option_name="unidiff-zero",
+                                          dest="unidiff_zero")
     return parser
 
 
@@ -465,7 +468,8 @@ def main(argv):
             rebase_pq(repo, options)
         elif action == "apply":
             patch = Patch(patchfile)
-            apply_single_patch(repo, current, patch, fallback_author=None)
+            apply_single_patch(repo, current, patch, fallback_author=None,
+                               unidiff_zero=options.unidiff_zero)
         elif action == "switch":
             switch_pq(repo, current)
     except KeyboardInterrupt:
-- 
2.43.0



More information about the git-buildpackage mailing list