We have hundreds of repositories and receive patches regularly from the upstream. A job applies these patches with git apply --check <patch>
. If there is no error, the patch is applied with git apply <patch>
and the changes are committed. If any error, the patch is labeled as conflict
. Then the errors and conflicted patches are delivered to our repository maintainers. They use git apply --reject <patch>
to apply patches and solve the conflicts.
To my previous understanding, git apply --reject
was reliable. However, one maintainer reports that a patch is applied in a completely wrong way. Some new lines are inserted to a chunk in an unexpected function, which happens to have the same context. And there are some other wrong chunks.
For example, the chunk in the patch is
@@ -1757,9 +1757,9 @@ def FunctionAAA()
print('hi')
}
+ print('hello world')
print('good day')
return True
But in the applied file, the chunk is
@@ -1927,9 +1997,9 @@ def FunctionBBB() ---> in another function
print('hi')
}
+ print('hello world')
print('good day')
return True
It's very likely that the maintainer doesn't notice the misplaced lines and it would result in build errors or even worse hidden bugs. I let the maintainer try git apply --3way <patch>
and the patch is applied as expected although there are still conflicts.
I think git apply --reject
and git apply --3way
behave differently because they use different algorithms. From the result, I guess we need to adopt git apply --3way
. But I'm also worried that --3way
could work unexpectedly in some cases.
Why does git apply --reject
work in a seemingly wrong way instead of considering the chunk as conflicted? Which is better in our case? Is there any better solution to apply patches? Thanks.
git version 2.31.1
ubuntu 4.15.0-76-generic