Steven Lynn’s Blog

some thoughts about patch in packaging

2024-10-24258 words1 min read
It’s a common usage to modify the code when pkgbuild the package.
Here are several ways.

sed

de facto way
Pros
  1. easy to get, so old that stable, one of GNU toolkit
  1. is turing complete, so in theory has high limitation
Cons
  1. very hard to use, learn and maintain. simple usage like sed -i 's/a/b/g' is already hard to read. If with regex, it’s hell

patch

de facto way in git based work flow
Pros
  1. easy to get
  1. easy to generate, just diff a b > patch
  1. easy to use, patch -Np1 < patch
Cons
  1. stick to the origin file, not possible to apply to many files.
  1. if many replaces, patch will be huge, even if they are the same rules.

ast-grep

Pros
  1. easy to use
  1. easy to generate
Cons
  1. not built-in
  1. only support some languages
  1. added by author: pre-compiled version doesn’t support glibc < 2.31, or you can compile it yourself.

Example

code base need to change a function name

there are so many function calls there
  1. sed, just replace, but will replace ALL, even in string. The problem of using part of text is that
hard to write correct rule.
  1. patch, patch may be huge, but is accepted
  1. ast-grep, replace the function call

Now consider the update of software update

consider a thing:
The upstream ignore a request, so maintainers have to keep sync and do the patch.
  • sed: need to change even if var names have changed.
  • patch: every time repatch.
  • ast-grep: just keep the same replace, if the api and code shape doesn’t change. Even if changed, still easy to write.

So may be I could give ast-grep a try, when doing packaging.
 
Loading...