When resolving merge conflicts, most of the time it's because two people inserted at the same point of a file. And I always do the same: Use the new code from the left side, copy the new code from the right side and append it after the code from the left side.
This got me wondering, if I always do the same, it should be possible to automate this. Can I tell Git to trust that both new chunks can just be used one after the other?
I think, if the insertions would be a few lines apart (a situation Git resolves automatically), it wouldn't be any less likely to introduce bugs. I am going to check the result anyway.
(Currently I am using DiffMerge as my mergetool, if that makes any difference.)
You would need to declare a merge driver for that:
This is assigned in a
.gitattributes
done in the destination branch (the one where you are doing the merge)(replace
yourFiles
by the pattern of files you want to see that merge resolution applied)But it also needs to be configured locally:
The trick is on the
addTheirs
scripts, called with%O
,%A
,%B
(ancestor, ours, their)A
diff -u %A %B
will give you hunks of diff like:Even though the diff header is missing, a
patch
would still be able to add new lines and removes old one (and what you want is to keep ours, and add theirs).On Windows, you can take a gnu
patch
, but you need to add a manifest.You could try to filter out the deletions from the patch before applying it.
But the hunk header (
@@ -1,11 +1,11 @@
) would no longer match the number of lines expected (if you only add 2 lines and removes 0, it should end with+1,13
, nit+1,11
)You need to process your patch in order to:
That means the
addTheirs.sh
(to put anywhere in your path, even on Windows) could be:(
diff
is part of the 200+ unix commands part of thegit-for-windows
package, so again, all of this works on Windows or on Unix)The 'padd' (patch add) utility is a script removing any deletion line from each hunk, and updating the hunk header to keep track of the actual line number.
I made mine in Go (https://golang.org/, simply unzip a go distro anywhere you want and add it to your
PATH
)Copy the following in a
padd.go
file, and typego build padd.go
: you get thepadd
executable that the merge driver can call to adjust the patch.