rpm: give patches in the order they are applied in the spec
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Wed, 15 May 2013 06:51:54 +0000 (09:51 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 5 Jun 2014 11:20:07 +0000 (14:20 +0300)
Fixes patch importing in situations when patch tag numbering in the spec
differs from the order they are applied.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/rpm/__init__.py
tests/test_rpm.py
tests/test_rpm_data/specs/gbp-test-quirks.spec

index 3d11d0d..16cec8e 100644 (file)
@@ -655,20 +655,29 @@ class SpecFile(object):
         series = PatchSeries()
         if 'patch' in self._tags:
             tags = self._patches()
-            macros = {}
+            applied = []
             for macro in self._special_directives['patch']:
-                macros[macro['id']] = macro['args']
-            ignored = [] if ignored else self.ignorepatches
-
-            for num, tag in sorted(tags.iteritems()):
-                strip = 0
-                if num in macros:
-                    opts = self._patch_macro_opts(macros[num])
+                if macro['id'] in tags:
+                    applied.append((macro['id'], macro['args']))
+            ignored = set() if ignored else set(self.ignorepatches)
+
+            # Put all patches that are applied first in the series
+            for num, args in applied:
+                if num not in ignored:
+                    opts = self._patch_macro_opts(args)
                     strip = int(opts.strip) if opts.strip else 0
-                if (unapplied or (num in macros)) and num not in ignored:
-                    filename = os.path.basename(tag['linevalue'])
+                    filename = os.path.basename(tags[num]['linevalue'])
                     series.append(Patch(os.path.join(self.specdir, filename),
                                         strip=strip))
+            # Finally, append all unapplied patches to the series, if requested
+            if unapplied:
+                applied_nums = set([num for num, _args in applied])
+                unapplied = set(tags.keys()).difference(applied_nums)
+                for num in sorted(unapplied):
+                    if num not in ignored:
+                        filename = os.path.basename(tags[num]['linevalue'])
+                        series.append(Patch(os.path.join(self.specdir,
+                                                         filename), strip=0))
         return series
 
     def _guess_orig_prefix(self, orig):
index a85f41f..ca1cc6a 100644 (file)
@@ -278,7 +278,29 @@ class TestSpecFile(object):
         assert len(spec.patchseries(ignored=True)) == 1
         series = spec.patchseries(unapplied=True, ignored=True)
         assert len(series) == 2
-        assert os.path.basename(series[-1].path) == '4.patch'
+        assert os.path.basename(series[-1].path) == '1.patch'
+
+    def test_patch_series_quirks(self):
+        """Patches are applied in order different from the patch numbering"""
+        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test-quirks.spec')
+        spec = SpecFileTester(spec_filepath)
+
+        # Check series is returned in the order the patches are applied
+        files = [os.path.basename(patch.path) for patch in spec.patchseries()]
+        assert files == ['05.patch', '01.patch']
+        # Also ignored patches are returned in the correct order
+        files = [os.path.basename(patch.path) for patch in
+                    spec.patchseries(ignored=True)]
+        assert files == ['05.patch', '02.patch', '01.patch']
+        # Unapplied patches are added to the end of the series
+        files = [os.path.basename(patch.path) for patch in
+                    spec.patchseries(unapplied=True)]
+        assert files == ['05.patch', '01.patch', '03.patch']
+        # Return all patches (for which tag is found)
+        files = [os.path.basename(patch.path) for patch in
+                    spec.patchseries(unapplied=True, ignored=True)]
+        assert files == ['05.patch', '02.patch', '01.patch', '03.patch',
+                         '04.patch']
 
 
 class TestUtilityFunctions(object):
index 64db07f..bb56b00 100644 (file)
@@ -8,6 +8,12 @@ Version:        0.1
 Release:        1.2
 License:        GPLv2
 Source1:        foobar.tar.gz
+# Gbp-Ignore-Patches: 2 4 888
+Patch1:         01.patch
+Patch2:         02.patch
+Patch3:         03.patch
+Patch4:         04.patch
+Patch5:         05.patch
 
 %description
 Spec for testing some quirks of spec parsing. No intended for building an RPM.
@@ -16,3 +22,9 @@ Spec for testing some quirks of spec parsing. No intended for building an RPM.
 # We don't have Source0 so rpmbuild would fail, but gbp shouldn't crash
 %setup -q
 
+# Patches are applied out-of-order wrt. numbering
+%patch5
+%patch2
+%patch1
+# Patch 999 does not exist, rpmbuild would fail but GBP should not
+%patch999