git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Programmatic patches (transform commits)

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Aug 19, 2022, 10:15 UTC
Message-ID
<n7prrp62-41o1-r280-qo55-9q7rnqs723p1@tzk.qr>
In-Reply-To
<CAED_RJPh28o8bFsBEEzAYvp1DXhff2JWiTk3zHh3VbaHZephgg@mail.gmail.com>
Hi Mark,
On Thu, 18 Aug 2022, Mark Fulton wrote:
Show 12 quoted lines
> Is there a way to commit a transform script that programmatically
> applies file changes rather than committing the file changes directly?
>
> e.g. Imagine in a large repository that a contributor wants to replace
> certain instances of "abc" with "xyz". A transform script might be
> like the following:
>
> ```sh
> #!/bin/sh
>
> sed -i 's/abc/xyz/g' $(find .)
> ```

I do have a script to do such a thing, and I am sure other users do, too. It would be a simpler script if it did not have to accommodate for `sed`'s `-i` option behaving differently between GNU and BSD variants. For the GNU variant, I use

	git grep -zl "$regex" | xargs -0r sed -i "s/$regex/$replacement/g"

and for the BSD variant I use `sed -i ''` (i.e. an extra, empty argument) and skip the `-r` option because BSD `xargs` does not understand it.

Show 6 quoted lines
> Applying such a "programmatic patch" will potentially edit many files.
> Doing a code review on such a change is error prone due to authors
> resolving merge conflicts manually, etc. while reviewing the patch in
> some circumstances is much easier (especially tools for specifically
> this type of file transformations are used to make it easy to parse
> code, traverse abstract syntax trees, make edits, etc.).

In the Git project, we do have a track record of mentioning the exact commands for automated transformation in the commit message, see e.g. https://github.com/git/git/commit/8dcf73c5c940.

The idea is that any reviewer could run the same command, verify that the outcome is the same, and give their ACK. Or, in case of merge conflicts, they would re-run the command and commit the result using the original commit message.

Ciao, Dscho

Previous: Ævar Arnfjörð Bjarmason
Message 3 of 3 in “Programmatic patches (transform commits)”
  1. Mark FultonAug 18, 2022
  2. Ævar Arnfjörð BjarmasonAug 18, 2022
  3. Johannes SchindelinAug 19, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.