2

I have a custom merge driver for a type of text file with a specific layout. (It solves basically the same problem as git-merge-changelog: when things are added at the same place, I have rules to decide in in what order to put them.)

This merge driver is based on heuristics and sometimes fails when the changes are too complex. When it fails, I want to fall back to git's default handling of text files.

I've set up .gitattributes:

myfile merge=my-driver

and .gitconfig:

[merge "my-driver"]
    name = My merge driver for myfile
    driver = my-merge-driver %O %A %B

This is fine when my custom merge driver is able to resolve the conflicts. When the driver program can't resolve the conflicts, it returns a non-zero code and doesn't modify %A. This causes git to report a conflict (good), but to leave the current version of the file in place. Instead, I want git to run the built-in text merge driver.

How can I use my custom merge driver, but if it fails, use the built-in driver as a fallback?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254

1 Answers1

3

A partial solution (at least for a “normal”, two-way merge):

driver = my-merge-driver %O %A %B || git-merge-file -q --marker-size=%L %A %O %B

This works in that it performs a text merge and produces conflict markers. Unfortunately, the conflict markers have temporary file names (<<<<<<< .merge_file_xxxxxx) instead of branch names. I'd prefer to have the branch names.

This may be resolved in the same way as How to retrieve branch names in a custom Git merge driver? (currently unanswered).

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • That's the right way. It's not documented and it has a pitfall if you're running an octopus merge, because then there will be multiple environment variables, one for each head. I'll answer that other question too, I guess. – torek Nov 28 '17 at 17:57
  • You can have a bit better looking names by using it like `git merge-file -L "current" -L "base" -L "incoming" --marker-size=%L %A %O %B`. – Somnium Oct 14 '19 at 13:14