* How to automatically correct an almost correct auto-merge?
@ 2011-12-16 20:32 SZEDER Gábor
2011-12-16 20:39 ` Seth Robertson
2011-12-16 21:32 ` Neal Kreitzinger
0 siblings, 2 replies; 4+ messages in thread
From: SZEDER Gábor @ 2011-12-16 20:32 UTC (permalink / raw)
To: git
Hi,
Briefly:
Neighboring areas of a file are modified in two branches. Git merges
the two branches without conflicts, but the result is not semantically
correct. How can I teach git to produce the correct merge result when
performing the same merge later on?
Longer:
The following commands create a file and two branches, both of them
modifying the file by adding lines in the same area:
git init
cat >file <<\EOF
1
2
EOF
git add file
git commit -m file
git apply <<\EOF
diff --git a/file b/file
index 1c3e7efc..121366a2 100644
--- a/file
+++ b/file
@@ -1,3 +1,5 @@
1
+a
+
2
EOF
git commit -a -m a
git checkout -b branch HEAD^
git apply <<\EOF
diff --git a/file b/file
index 1c3e7efc..f2e91d4f 100644
--- a/file
+++ b/file
@@ -1,3 +1,6 @@
1
+b
+
+c
2
EOF
git commit -a -m 'b c'
git checkout master
At this point I merge 'branch' and git produces the following result:
$ git merge branch
Auto-merging file
Merge made by the 'recursive' strategy.
file | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
$ cat file
1
b
c
a
2
Now, these changes and the merge above are the minimal receipe which
corresponds to a real merge I'm having trouble with at dayjob. Just
imagine that '1' and '2' are the beginning and end of a function, 'b'
is the declaration of a new local variable, and 'a' and 'c' are new
code blocks. As it happens, the semantically correct result would be
the following:
1
b
a
c
2
i.e. 'a' must be executed before 'c'.
I corrected the merge result manually, but these two branches are
merged a couple of times a day into an integration branch, and they
will likely cook for a few weeks, which means a lot of merges, and a
lot of manual corrections. So I'm looking for a way to teach git to
produce the semantically correct merge result. Something like
'rerere' would be great, but of course I can't use 'rerere' in this
case, because there are no merge conflicts at all...
Any ideas? Did someone deal with similar issues before?
Thanks,
Gábor
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: How to automatically correct an almost correct auto-merge?
2011-12-16 20:32 How to automatically correct an almost correct auto-merge? SZEDER Gábor
@ 2011-12-16 20:39 ` Seth Robertson
2011-12-17 11:07 ` SZEDER Gábor
2011-12-16 21:32 ` Neal Kreitzinger
1 sibling, 1 reply; 4+ messages in thread
From: Seth Robertson @ 2011-12-16 20:39 UTC (permalink / raw)
To: SZEDER Gábor; +Cc: git
> 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.
-Seth Robertson
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to automatically correct an almost correct auto-merge?
2011-12-16 20:32 How to automatically correct an almost correct auto-merge? SZEDER Gábor
2011-12-16 20:39 ` Seth Robertson
@ 2011-12-16 21:32 ` Neal Kreitzinger
1 sibling, 0 replies; 4+ messages in thread
From: Neal Kreitzinger @ 2011-12-16 21:32 UTC (permalink / raw)
To: SZEDER Gábor; +Cc: git
On 12/16/2011 2:32 PM, SZEDER Gábor wrote:
> Hi,
>
>
> Briefly:
>
> Neighboring areas of a file are modified in two branches. Git
> merges the two branches without conflicts, but the result is not
> semantically correct. How can I teach git to produce the correct
> merge result when performing the same merge later on?
>
>
> Longer:
>
> The following commands create a file and two branches, both of them
> modifying the file by adding lines in the same area:
>
> git init cat>file<<\EOF 1
>
> 2 EOF git add file git commit -m file git apply<<\EOF diff --git
> a/file b/file index 1c3e7efc..121366a2 100644 --- a/file +++ b/file
> @@ -1,3 +1,5 @@ 1
>
> +a + 2 EOF git commit -a -m a git checkout -b branch HEAD^ git
> apply<<\EOF diff --git a/file b/file index 1c3e7efc..f2e91d4f 100644
> --- a/file +++ b/file @@ -1,3 +1,6 @@ 1 +b + +c
>
> 2 EOF git commit -a -m 'b c' git checkout master
>
>
> At this point I merge 'branch' and git produces the following
> result:
>
> $ git merge branch Auto-merging file Merge made by the 'recursive'
> strategy. file | 3 +++ 1 files changed, 3 insertions(+), 0
> deletions(-) $ cat file 1 b
>
> c
>
> a
>
> 2
>
>
> Now, these changes and the merge above are the minimal receipe which
> corresponds to a real merge I'm having trouble with at dayjob. Just
> imagine that '1' and '2' are the beginning and end of a function,
> 'b' is the declaration of a new local variable, and 'a' and 'c' are
> new code blocks. As it happens, the semantically correct result
> would be the following:
>
> 1 b
>
> a
>
> c
>
> 2
>
> i.e. 'a' must be executed before 'c'.
>
> I corrected the merge result manually, but these two branches are
> merged a couple of times a day into an integration branch, and they
> will likely cook for a few weeks, which means a lot of merges, and a
> lot of manual corrections. So I'm looking for a way to teach git to
> produce the semantically correct merge result. Something like
> 'rerere' would be great, but of course I can't use 'rerere' in this
> case, because there are no merge conflicts at all...
>
> Any ideas? Did someone deal with similar issues before?
>
You can produce conflicts by implementing keyword expansion on "line 1"
(or whatever the first commentable line is in your language) of your
source changes during your pre-commit hook. We do a keyword expansion
on "user" (whomai) and "date" (date) keywords. This will cause "line 1"
to conflict on same file edits in a merge. As for trusting rerere, we
don't. We do it manually with kdiff3 as the mergetool. If rerere works
for you reliably in this scenario then I'd like to know about it.
v/r,
neal
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to automatically correct an almost correct auto-merge?
2011-12-16 20:39 ` Seth Robertson
@ 2011-12-17 11:07 ` SZEDER Gábor
0 siblings, 0 replies; 4+ messages in thread
From: SZEDER Gábor @ 2011-12-17 11:07 UTC (permalink / raw)
To: Seth Robertson; +Cc: git
On Fri, Dec 16, 2011 at 03:39:27PM -0500, Seth Robertson wrote:
>
> > 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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-12-17 11:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-16 20:32 How to automatically correct an almost correct auto-merge? SZEDER Gábor
2011-12-16 20:39 ` Seth Robertson
2011-12-17 11:07 ` SZEDER Gábor
2011-12-16 21:32 ` Neal Kreitzinger
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).