{"thread":{"id":"29187","subject":"How to automatically correct an almost correct auto-merge?","startedAt":"2011-12-16T20:32:15Z","lastAt":"2011-12-17T11:07:06Z","messageCount":4,"participants":["SZEDER Gábor","Seth Robertson","Neal Kreitzinger"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"181331","messageId":"20111216203215.GG1868@goldbirke","threadId":"29187","inReplyTo":null,"subject":"How to automatically correct an almost correct auto-merge?","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2011-12-16T20:32:15Z","receivedAt":"2011-12-16T20:32:15Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Hi,\n\n\nBriefly:\n\nNeighboring areas of a file are modified in two branches.  Git merges\nthe two branches without conflicts, but the result is not semantically\ncorrect.  How can I teach git to produce the correct merge result when\nperforming the same merge later on?\n\n\nLonger:\n\nThe following commands create a file and two branches, both of them\nmodifying the file by adding lines in the same area:\n\ngit init\ncat >file <<\\EOF\n1\n\n2\nEOF\ngit add file\ngit commit -m file\ngit apply <<\\EOF\ndiff --git a/file b/file\nindex 1c3e7efc..121366a2 100644\n--- a/file\n+++ b/file\n@@ -1,3 +1,5 @@\n 1\n\n+a\n+\n 2\nEOF\ngit commit -a -m a\ngit checkout -b branch HEAD^\ngit apply <<\\EOF\ndiff --git a/file b/file\nindex 1c3e7efc..f2e91d4f 100644\n--- a/file\n+++ b/file\n@@ -1,3 +1,6 @@\n 1\n+b\n+\n+c\n\n 2\nEOF\ngit commit -a -m 'b c'\ngit checkout master\n\n\nAt this point I merge 'branch' and git produces the following result:\n\n$ git merge branch\nAuto-merging file\nMerge made by the 'recursive' strategy.\n file |    3 +++\n 1 files changed, 3 insertions(+), 0 deletions(-)\n$ cat file\n1\nb\n\nc\n\na\n\n2\n\n\nNow, these changes and the merge above are the minimal receipe which\ncorresponds to a real merge I'm having trouble with at dayjob.  Just\nimagine that '1' and '2' are the beginning and end of a function, 'b'\nis the declaration of a new local variable, and 'a' and 'c' are new\ncode blocks.  As it happens, the semantically correct result would be\nthe following:\n\n1\nb\n\na\n\nc\n\n2\n\ni.e. 'a' must be executed before 'c'.\n\nI corrected the merge result manually, but these two branches are\nmerged a couple of times a day into an integration branch, and they\nwill likely cook for a few weeks, which means a lot of merges, and a\nlot of manual corrections.  So I'm looking for a way to teach git to\nproduce the semantically correct merge result.  Something like\n'rerere' would be great, but of course I can't use 'rerere' in this\ncase, because there are no merge conflicts at all...\n\nAny ideas?  Did someone deal with similar issues before?\n\n\nThanks,\nGábor\n"},{"id":"181333","messageId":"201112162039.pBGKdR8H012831@no.baka.org","threadId":"29187","inReplyTo":"20111216203215.GG1868@goldbirke","subject":"Re: How to automatically correct an almost correct auto-merge?","fromName":"Seth Robertson","fromEmail":"in-gitvger@baka.org","sentAt":"2011-12-16T20:39:27Z","receivedAt":"2011-12-16T20:39:27Z","isPatch":false,"sender":{"key":"in-gitvger@baka.org","avatar":null},"body":"\n> How can I teach git to produce the correct merge result when\n> performing the same merge later on?\n\nCustom merge driver.\n\nType `man gitattributes` and search for \"custom merge driver\"\n\nYour merge driver can have whatever policy you can program, on a per\nfile basis.\n\n\t\t\t\t\t-Seth Robertson\n"},{"id":"181338","messageId":"4EEBB8D1.2050509@gmail.com","threadId":"29187","inReplyTo":"20111216203215.GG1868@goldbirke","subject":"Re: How to automatically correct an almost correct auto-merge?","fromName":"Neal Kreitzinger","fromEmail":"nkreitzinger@gmail.com","sentAt":"2011-12-16T21:32:01Z","receivedAt":"2011-12-16T21:32:01Z","isPatch":false,"sender":{"key":"nkreitzinger@gmail.com","avatar":null},"body":"On 12/16/2011 2:32 PM, SZEDER Gábor wrote:\n> Hi,\n>\n>\n> Briefly:\n>\n> Neighboring areas of a file are modified in two branches.  Git\n> merges the two branches without conflicts, but the result is not\n> semantically correct.  How can I teach git to produce the correct\n> merge result when performing the same merge later on?\n>\n>\n> Longer:\n>\n> The following commands create a file and two branches, both of them\n> modifying the file by adding lines in the same area:\n>\n> git init cat>file<<\\EOF 1\n>\n> 2 EOF git add file git commit -m file git apply<<\\EOF diff --git\n> a/file b/file index 1c3e7efc..121366a2 100644 --- a/file +++ b/file\n> @@ -1,3 +1,5 @@ 1\n>\n> +a + 2 EOF git commit -a -m a git checkout -b branch HEAD^ git\n> apply<<\\EOF diff --git a/file b/file index 1c3e7efc..f2e91d4f 100644\n> --- a/file +++ b/file @@ -1,3 +1,6 @@ 1 +b + +c\n>\n> 2 EOF git commit -a -m 'b c' git checkout master\n>\n>\n> At this point I merge 'branch' and git produces the following\n> result:\n>\n> $ git merge branch Auto-merging file Merge made by the 'recursive'\n> strategy. file |    3 +++ 1 files changed, 3 insertions(+), 0\n> deletions(-) $ cat file 1 b\n>\n> c\n>\n> a\n>\n> 2\n>\n>\n> Now, these changes and the merge above are the minimal receipe which\n> corresponds to a real merge I'm having trouble with at dayjob.  Just\n> imagine that '1' and '2' are the beginning and end of a function,\n> 'b' is the declaration of a new local variable, and 'a' and 'c' are\n> new code blocks.  As it happens, the semantically correct result\n> would be the following:\n>\n> 1 b\n>\n> a\n>\n> c\n>\n> 2\n>\n> i.e. 'a' must be executed before 'c'.\n>\n> I corrected the merge result manually, but these two branches are\n> merged a couple of times a day into an integration branch, and they\n> will likely cook for a few weeks, which means a lot of merges, and a\n> lot of manual corrections.  So I'm looking for a way to teach git to\n> produce the semantically correct merge result.  Something like\n> 'rerere' would be great, but of course I can't use 'rerere' in this\n> case, because there are no merge conflicts at all...\n>\n> Any ideas?  Did someone deal with similar issues before?\n>\nYou can produce conflicts by implementing keyword expansion on \"line 1\"\n(or whatever the first commentable line is in your language) of your \nsource changes during your pre-commit hook.  We do a keyword expansion \non \"user\" (whomai) and \"date\" (date) keywords.  This will cause \"line 1\" \nto conflict on same file edits in a merge.  As for trusting rerere, we \ndon't.  We do it manually with kdiff3 as the mergetool.  If rerere works \nfor you reliably in this scenario then I'd like to know about it.\n\nv/r,\nneal\n"},{"id":"181394","messageId":"20111217110706.GH1868@goldbirke","threadId":"29187","inReplyTo":"201112162039.pBGKdR8H012831@no.baka.org","subject":"Re: How to automatically correct an almost correct auto-merge?","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2011-12-17T11:07:06Z","receivedAt":"2011-12-17T11:07:06Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Fri, Dec 16, 2011 at 03:39:27PM -0500, Seth Robertson wrote:\n> \n> > How can I teach git to produce the correct merge result when\n> > performing the same merge later on?\n> \n> Custom merge driver.\n> \n> Type `man gitattributes` and search for \"custom merge driver\"\n> \n> Your merge driver can have whatever policy you can program, on a per\n> file basis.\n\nThanks for the pointer, I knew it must be possible with git somehow.\n\nFor future reference, here's what I did to handle the minimal receipe\nposted in my earlier email:\n\nI stored the diff between the original and semantically almost correct\nmerge result and the fixed version in the file \"patch\", then put\ntogether the little script below and configured it as custom merge\ndriver for the file \"file\", as described in the documentation.  The\nscript uses 'git merge-file' to perform the real merge, and then\napplies the fixup patch (after rewriting the paths, because during the\nmerge the patch should be applied to the current file the merge driver\nis working with instead of the original \"file\").  Of course, \"file\"\nmight be modified by other merges before or after the problematic\nmerge, but the fixup patch should not be applied in those cases.  The\ntwo greps and the conditions make sure that the patch is applied only\nif both \"code blocks\" are in the file and they are in the wrong order.\n\nUnfortunately, applying a patch is more sensitive to matching context,\nso it will fail easily when there are other changes in the\nneighborhood, but at least it will fail loudly, and then I'll know I\nhave to fix it up manually.  We'll see, how it will cope with real\nsource code.\n\n\nThanks,\nGábor\n\n\n\n#!/bin/sh\n\nf=\"file\"\n\ngit merge-file \"$1\" \"$2\" \"$3\"\n\na_line=\"$(grep -n '^a$' \"$1\" |cut -d: -f1)\"\nc_line=\"$(grep -n '^c$' \"$1\" |cut -d: -f1)\"\n\nif [ -n \"$a_line\" -a -n \"$c_line\" ] && [ \"$c_line\" -lt \"$a_line\" ]; then \n\techo \"Fixing incorrect auto-merge result of '$f'\"\n\tsed -e \"s%^diff --git a/$f b/$f%diff --git a/$1 b/$1%\" \\\n\t\t-e \"s%^--- a/$f%--- a/$1%\" \\\n\t\t-e \"s%^+++ b/$f%+++ b/$1%\" patch \\\n\t\t|git apply\nfi\n"}]}