Skip to content

Commit

Permalink
Support individual patch application in %autopatch
Browse files Browse the repository at this point in the history
There are cases where "apply 'em all" doesn't cut it, and passing
-m and -M is cumbersome for individual patches. Add support for
applying individual patches by passing their numbers as arguments,
update documentation.
  • Loading branch information
pmatilai committed Jun 18, 2021
1 parent 3287848 commit f101c8c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
25 changes: 25 additions & 0 deletions docs/manual/autosetup.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ Note that the exact behavior of `-S` option depends on the used VCS: for
example quilt only controls patches whereas git and mercurial control the
entire source repository.

## %autopatch

Sometimes you need more control than just "apply all", in which case you
can call `%autopatch` directly. By default it simply applies all patches
in the order declared in the spec, but you can additionally control the
range with options, or pass patch numbers as arguments. The supported
options are

* `-v` verbose operation
* `-p<number>` argument to control patch prefix stripping (same as
`-p` to `%patch`, normally passed down from `%autosetup`)
* `-m<number>` Apply patches starting from `<number>` range
* `-M<number>` Apply patches up to `<number>` range

Some examples:

# Apply patches with number >= 100
`%autopatch -m 100`
# Apply patches with number <= 400
`%autopatch -M 400`
# Apply patches 80 to 99
`%autopatch -m 80 -99`
# Apply patches 1, 4 and 6
`%autopatch 1 4 6`

## Automating patch (and source) declarations

While typically patch and source names tend to be descriptive for humans,
Expand Down
50 changes: 33 additions & 17 deletions macros.in
Original file line number Diff line number Diff line change
Expand Up @@ -1225,23 +1225,39 @@ else\
print("echo 'Cannot read "..file.."'; exit 1;".."\\n")\
end}

# Automatically apply all patches
# Patches are applied in the order they are listed in the spec file
# not by their number!
# -m<min> Apply patches with number >= min only
# -M<max> Apply patches with number <= max only
%autopatch(vp:m:M:)\
%{lua:\
local options = rpm.expand("%{!-v:-q} %{-p:-p%{-p*}} ")\
local low_limit = tonumber(rpm.expand("%{-m:%{-m*}}"))\
local high_limit = tonumber(rpm.expand("%{-M:%{-M*}}"))\
for i, p in ipairs(patches) do\
local inum = patch_nums[i]\
if ((not low_limit or inum>=low_limit) and (not high_limit or inum<=high_limit)) \
then\
print(rpm.expand("%__apply_patch -m %{basename:"..p.."} "..options..p.." "..i.."\\n")) \
end\
end}
# Apply patches using %autosetup configured SCM.
# Typically used with no arguments to apply all patches in the order
# introduced in the spec, but alternatively can be used to apply indvidual
# patches in arbitrary order by passing them as arguments.
# -v Verbose
# -p<N> Prefix strip (ie patch -p argument)
# -m<min> Apply patches with number >= min only (if no arguments)
# -M<max> Apply patches with number <= max only (if no arguments)
%autopatch(vp:m:M:) %{lua:
if #arg == 0 then
local lo = tonumber(rpm.expand("%{-m:%{-m*}}"))
local hi = tonumber(rpm.expand("%{-M:%{-M*}}"))
for i, n in ipairs(patch_nums) do
if ((not lo or n >= lo) and (not hi or n <= hi)) then
table.insert(arg, n)
end
end
end
local options = rpm.expand("%{!-v:-q} %{-p:-p%{-p*}} ")
local bynum = {}
for i, p in ipairs(patches) do
bynum[patch_nums[i]] = p
end
for i, a in ipairs(arg) do
local p = bynum[a]
if p then
print(rpm.expand("%__apply_patch -m %{basename:"..p.."} "..options..p.." "..i.."\\n"))
else
macros.error({"no such patch "..a})
end
end
}


# One macro to (optionally) do it all.
# -S<scm name> Sets the used patch application style, eg '-S git' enables
Expand Down

0 comments on commit f101c8c

Please sign in to comment.