1

I'm trying to write a merge driver for git that used the kdiff3 for merge and conflict resolution.

I need a way to know if kdiff3 was successful in the merge or not.

So far I've got this:

#!/bin/bash

Kdiff3 "${1}" "${2}" "${3}" -o "${1}" --L1 "Working Copy" --L2 "Nearest Common ancestor" --L3 "Version from Other Branch"

exit 1

Since the exit 1 tells git whether or not the merge succeed or not, I'd like to set it based on the success of kdiff3.

Is there a way to do this that I am completely missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TechplexEngineer
  • 1,836
  • 2
  • 30
  • 48
  • 1
    Leave off `exit 1` but you shouldn't be using kdiff3 as a merge driver, it's an interactive conflict resolution tool supported natively by `git mergetool`. What are you trying to achieve with you merge driver? – CB Bailey Nov 10 '12 at 10:33
  • Hey Chris, I'm trying to make a merge driver that will show me all the changes it wants to make. I've found that Git's internal automerge isn't the best at detecting moves, so I'd like to review all the code and changes in a gui before I accept them. The above script doe nearly exactly what I want it to do, I'd just like the exit to be based on whether the user successfully merged the files or not. I was just thinking of sha1 the file before then after. Thoughts? – TechplexEngineer Nov 11 '12 at 17:27

1 Answers1

1

You can tell if the auto-merge worked by checking if the output has been written by kdiff3, as mentioned in this thread

If you don't specify an output file explicitly (via -o) then the third (last) file will be the destination.

The exit status is 0 when after a merge the output file was saved successfully.
And 1 if you exit without saving.

That is what this script is testing (here as an example), but using diff3:
(With kdiff3 command-line options, you should need to add the -auto option to your command-line)

    echo "Attempting to auto-merge ${baseFileName}" 1>&2
    diff3 -L $labelMine -L $labelOlder -L $labelTheirs -Em $mine $older $theirs > $output
    if [ $? = 1 ]; then
        #Can't auto merge
        rm -f $output
        $VDIFF3 $older $mine $theirs --L1 $labelOlder --L2 $labelMine --L3 $labelTheirs -o $output --auto 1>&2
        bLoop=1
        if [ -f $output ]; then
            if [ -s $output ]; then
                #output succesfully written
                bLoop=0
            fi
        fi
        if [ $bLoop = 0 ]; then
            cat $output
            rm -f $output
            exit 0
        else
            echo "Merge failed, try again" 1>&2
        fi
    else
        #We can automerge, and we already did it
        cat $output
        rm -f $output
        exit 0
    fi
    ;;
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250