Reputation: 2982
I'm wondering if I can tell git to resolve conflicts by appending "their" code blocks after "ours". Does he "know" at all about C-language code blocks ?
Example:
//====== ours ======
....
if(cond1)
{
do_smth();
}
....
//====== their ======
....
if(cond2)
{
do_smth_else();
}
....
Normally such a merge results in a conflict which I would like to resolve automatically as :
//======= merge result ========
if(cond1)
{
do_smth();
}
if(cond2)
{
do_smth_else();
}
Thank you !
Upvotes: 2
Views: 372
Reputation: 1846
You can using the union
gitattribute
union
Run 3-way file level merge for text files, but take lines from both versions, instead of leaving conflict markers. This tends to leave the added lines in the resulting file in random order and the user should verify the result. Do not use this if you do not understand the implications.
As @Greyson says, it's not a good idea for C code, though. See my answer to a similar question for an example.
Upvotes: 0
Reputation: 468241
No, I'm afraid that git's merge algorithms don't understand the structure of C programs, nor any other programming language. There are options to git's recursive merge strategy for automatically resolving conflicting hunks in favour of ours
or theirs
, but as far as I know there is no such option to automatically include both.
Upvotes: 0
Reputation: 3688
I understand that flagging this as a conflict seems like an annoyance, but it is by design. Consider the following situation, you add:
if( cond1 || cond2 )
{
perform_action1();
}
and they add:
if( cond1 )
{
perform_action1();
}
The portion that they added probably does not match what you were trying to accomplish. If their code were appended to yours, then perform_action1()
would be called twice.
In short, there is no way to merge automatically because the few times it would be a bad idea, it would be a really bad idea.
Upvotes: 2