sedpatchast-grepExamplecode base need to change a function nameNow consider the update of software update
It’s a common usage to modify the code when pkgbuild the package.
Here are several ways.
sed
de facto way
Pros
- easy to get, so old that stable, one of GNU toolkit
- is turing complete, so in theory has high limitation
Cons
- 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
- easy to get
- easy to generate, just
diff a b > patch
- easy to use,
patch -Np1 < patch
Cons
- stick to the origin file, not possible to apply to many files.
- if many replaces, patch will be huge, even if they are the same rules.
ast-grep
Pros
- easy to use
- easy to generate
Cons
- not built-in
- only support some languages
- 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
- sed, just replace, but will replace ALL, even in string. The problem of using part of text is that
hard to write correct rule.
- patch, patch may be huge, but is accepted
- 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.