Ehud Grand
Ehud Grand

Reputation: 3693

git merge programmatically detect conflict

I need to merge from on branch to another during a CI CD. I', looking for a way to detect conflicts after the merge thus stopping the process and alerting it. This is what I have for now:

#! /bin/bash 

echo 'start'

function do_merge() {
    git merge auto
}

function is_merge_ok(){
    local merge_response = $(do_merge)
    local SUB = 'error'
    if [[ "$merge_response" == "*$SUB*" ]]; then
        echo 'false'
    else
        echo 'true'
    fi
}


local was_merge = $(is_merge_ok)
if [[ "$was_merge" == "false" ]]; then
    exit(1)
else    
    exit(0)
fi

But it is not getting the error. Is there a way to achieve it? Thanks

Upvotes: 2

Views: 1079

Answers (1)

LeGEC
LeGEC

Reputation: 51780

git merge already has an exit code which indicates success or failure.

Either use git merge auto directly in your if :

if git merge auto; then
   echo "sucess !"
else
   exitcode=$?
   echo "failure"
   exit $exitcode
fi

or store the exit code right after executing git merge auto for later comparison :


git merge auto
exitcode=$?

... # do something else

if [ $exitcode -eq 0 ]; then
   echo "success"
else
   echo "failure"
fi

Your current script is roughly equivalent to :

if git merge auto > /dev/null; then
    exit 0;
else
    exit 1;
fi

Upvotes: 6

Related Questions