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

Custom merge drivers: accessing the pathnames and revisions of the files being merged

From
Antonin Delpeuch <antonin@delpeuch.eu>
Date
Jan 16, 2024, 08:44 UTC
Message-ID
<8bb5e41e-4db9-4527-8492-3aca6a0f40bf@delpeuch.eu>
Hello,

Custom merge drivers [1] provide a convenient way to extend Git's merge algorithms. But as far as I can tell, there is no way to access the pathnames and revisions of the files to merge. Those would be very useful to generate informative conflict markers, like the built-in merge strategies do.

For instance, when merging with the ort strategy and diff3 conflict style, I get markers such as:

<<<<<<< HEAD:main/my_file.txt
…
||||||| 194c4190b:main/my_file.txt
…
=======
…
 >>>>>>> origin/branch:main/my_renamed_file.txt

Those strings at the end of conflict markers, with the revision and the pathname, are very useful to understand which parts are coming from where.

When implementing a custom merge driver, I don't see how to access this information to include it in the conflict markers. Custom merge drivers are typically invoked on temporary files generated by Git with uninformative paths, such as ".merge_file_NgiKjJ".

Therefore, my merge driver is only able to generate conflicts which look like this:

<<<<<<< .merge_file_NgiKjJ
…
||||||| .merge_file_D1XtCW
…
=======
…
 >>>>>>> .merge_file_WbmrBA

Of course, in a given rebase/merge session, the order in which the conflicting parts will be presented will remain consistent, so I will generally be able to remember which revision each part corresponds to, but it's still a mental load I would ideally try to avoid. Also, if the rename detection heuristics have false positives, then I can get merge conflicts which come from unrelated files: in that case it is very useful to see the pathnames, to understand this situation better.

So, I wonder: would people be open to exposing additional parameters to merge drivers? For instance we could add parameters "%X", "%Y" "%Z" to expose those "revision:pathname" strings for each part. (I think colons cannot be part of revision names, so this can be parsed unambiguously by the custom merge driver to recover the revision and pathname independently, if needed.)

I would be happy to submit a patch for this if you think this makes sense. If it is already possible to access this information in another way, I would like to work on documenting that.

Best wishes, Antonin

[1]: https://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver
Next: Junio C Hamano
Message 1 of 3 in “Custom merge drivers: accessing the pathnames and revisions of the files being merged”
  1. Antonin DelpeuchJan 16, 2024
  2. Junio C HamanoJan 16, 2024
  3. Antonin DelpeuchJan 16, 2024

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.