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

Re: How to automatically correct an almost correct auto-merge?

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Dec 17, 2011, 11:07 UTC
Message-ID
<20111217110706.GH1868@goldbirke>
In-Reply-To
<201112162039.pBGKdR8H012831@no.baka.org>
On Fri, Dec 16, 2011 at 03:39:27PM -0500, Seth Robertson wrote:
Show 10 quoted lines
> 
> > How can I teach git to produce the correct merge result when
> > performing the same merge later on?
> 
> Custom merge driver.
> 
> Type `man gitattributes` and search for "custom merge driver"
> 
> Your merge driver can have whatever policy you can program, on a per
> file basis.
Thanks for the pointer, I knew it must be possible with git somehow.

For future reference, here's what I did to handle the minimal receipe posted in my earlier email:

I stored the diff between the original and semantically almost correct merge result and the fixed version in the file "patch", then put together the little script below and configured it as custom merge driver for the file "file", as described in the documentation. The script uses 'git merge-file' to perform the real merge, and then applies the fixup patch (after rewriting the paths, because during the merge the patch should be applied to the current file the merge driver is working with instead of the original "file"). Of course, "file" might be modified by other merges before or after the problematic merge, but the fixup patch should not be applied in those cases. The two greps and the conditions make sure that the patch is applied only if both "code blocks" are in the file and they are in the wrong order.

Unfortunately, applying a patch is more sensitive to matching context, so it will fail easily when there are other changes in the neighborhood, but at least it will fail loudly, and then I'll know I have to fix it up manually. We'll see, how it will cope with real source code.

Thanks, Gábor

#!/bin/sh
f="file"
git merge-file "$1" "$2" "$3"

a_line="$(grep -n '^a$' "$1" |cut -d: -f1)" c_line="$(grep -n '^c$' "$1" |cut -d: -f1)"

if [ -n "$a_line" -a -n "$c_line" ] && [ "$c_line" -lt "$a_line" ]; then 
	echo "Fixing incorrect auto-merge result of '$f'"
	sed -e "s%^diff --git a/$f b/$f%diff --git a/$1 b/$1%" \
		-e "s%^--- a/$f%--- a/$1%" \
		-e "s%^+++ b/$f%+++ b/$1%" patch \
		|git apply
fi
Previous: Seth RobertsonNext: Neal Kreitzinger
Message 3 of 4 in “How to automatically correct an almost correct auto-merge?”
  1. SZEDER GáborDec 16, 2011
  2. Seth RobertsonDec 16, 2011
  3. SZEDER GáborDec 17, 2011
  4. Neal KreitzingerDec 16, 2011

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.