Python – Git merge failed unexpectedly

Git merge failed unexpectedly… here is a solution to the problem.

Git merge failed unexpectedly

I’ve implemented an automatic merge script in Python. If there are some merge conflicts, it should automatically merge the branch and create a pull request.

Part of my script looks like this:

from subprocess import check_call, CalledProcessError

# E.g. destination_branch = "master", source_branch = "release"
try:
    check_call(['git', 'checkout', '%s' % destination_branch])
    check_call(['git', 'merge', '--no-ff', '%s' % source_branch])
except CalledProcessError:
    # Creating pull request.

It looks like everything is fine, but there are some problems here.
After some automatic merges, I got the following error:
Error: You need to parse the current index first
Dockerfile: Requires merge
. Also, I’m printing the status code for both steps. The status code is 1, which is not good.

As a result, I see too many pull requests, most of which don’t have any merge conflicts.

Is there something wrong here?

Update:

Before merging something, I also have some stuff for branch updates (to keep the latest version).
It looks like:

try:
    check_call(['git', 'checkout', str(source_branch)])
    # branch successfully checked out
    check_call(['git', 'pull', 'origin', str(source_branch)])
except CalledProcessError:
    # Logging an errors.

One important thing to add:

As guys: @torek, @MarkAdelsberger mentioned in their comment that I also tried their solution to add the git merge --abort command after the merge fails for some reason.
So, this doesn’t help. It failed with another error:
check_call([GIT_CMD, 'merge', '--abort'])
The file "/usr/lib/python2.7/subprocess.py" in check_call, line 540
Raises CalledProcessError(retcode,cmd)
subprocess. CalledProcessError: The command '['git', 'merge', '--abort']' returns a non-zero exit status of 128

So, I’m looking for some solutions again….

Idea?

Solution

An error message of the form you need to resolve your current index first means that you started a regular (non-fast-forward) merge earlier, but failed due to a merge conflict.

When a git merge fails due to a merge conflict, Git build status 1 exits. check_call code will raise the CalledProcessError if that particular failure merge occurs as part of this code, but all we know is that there was such a failed merge earlier, not that it happened here:

subprocess. CalledProcessError: Command '...' returned non-zero exit status 1

Once this happens – either as part of Python code or by other means – the repository’s working tree and indexes will be in a “merge failed and manual processing required” state. If you don’t know the state of the repository beforehand, you should find out before you start, for example, using git status –porcelain or git status --porcelain=v2 (see the documentation for git status ; Note that this web version does not show the new v2 format, which is the first version in Git 2.11).

If you have put the working tree and index into this partially merged, intermediate, manual state and want to revert it to the pre-merge state, simply run git merge --abort.

Narrator: ‘%s' % expr is silly, just use str(expr), or expr itself if it is known to be a string.


1. Say “half-zhang” three times. 🙂

Related Problems and Solutions