There's nothing built in, but if you set merge.conflictstyle
to diff3
, it would be relatively easy to write a program (in perl or python perhaps, or I'll write a cheesy version in awk) that checks whether the "original" section is empty—this will detect the "both added" situation—and if so, simply removes the conflict markers:
good morning
good evening
<<<<<<< HEAD
g'day
|||||||
=======
aloha
>>>>>>> branch1
hello
hi
Here's my awk script (which I don't claim to be good, but it works on the sample input). Note that it won't handle "nested conflicts" very well (i.e., if the two original conflicting files contain what look like conflict markers, this will go wrong).
BEGIN { in_conflict = retained_left = retained_mid = retained_right = 0 }
function handle_retained(is_eof) {
# If the section between ||||||| and ======= is empty,
# retained_mid+1 == retained_right. Otherwise print
# all the retained conflict lines.
if (retained_mid + 1 == retained_right) {
s1 = retained_left + 1 # after <<<<<<<
e1 = retained_mid - 1 # before |||||||
s2 = retained_right + 1 # after =======
e2 = NR - 1 # before >>>>>>>
} else {
s1 = retained_left; e1 = NR
s2 = 1; e2 = 0
}
for (i = s1; i <= e1; i++)
print retained[i]
for (i = s2; i <= e2; i++)
print retained[i]
delete retained
if (is_eof) {
# this should never happen!
print "WARNING: ended input while still in conflict marker"
exit(1)
}
}
/^<<<<<<</ { in_conflict = 1; retained_left = NR }
{
if (!in_conflict)
print
else
retained[NR] = $0
}
/^\|\|\|\|\|\|\|/ { if (in_conflict) retained_mid = NR }
/^=======/ { if (in_conflict) retained_right = NR }
/^>>>>>>>/ { if (in_conflict) handle_retained(0); in_conflict = 0 }
END { if (in_conflict) handle_retained(1) }