public
Last active

HOWTO: Using FileMerge (opendiff) with Git on OSX

  • Download Gist
howto-filemerge-git-osx.md
Markdown

HOWTO: Using FileMerge (opendiff) with Git on OSX

FileMerge (opendiff) can really come in handy when you need to visually compare merging conflicts. Other times it's just a nice visual way to review your days work.

The following method works by creating a simple bash script (git-diff-cmd.sh) that sets us up with the proper command line arguments for Git to pass off files to FileMerge.

  1. Open the bash script for editing:

    vi ~/bin/git-diff-cmd.sh
    
  2. Paste the following code:

    #!/bin/sh 
    /usr/bin/opendiff "$2" "$5" -merge "$1"
    
  3. Make the bash script executable:

    chmod +x ~/bin/git-diff-cmd.sh
    
  4. Tell Git (globally) to run our bash script when 'git diff' is issued:

    git config --global diff.external ~/bin/git-diff-cmd.sh
    

Now head over to your Git-aware project directory and issue a git diff /path/to/modified/file.py and FileMerge will pop up showing you the differences against it and HEAD.

You can also do things like git diff --cached to review all the changes against HEAD.

#!/bin/sh` should be
#!/bin/sh

Thanks, yllan. Fixed.

I think the leading "\" of "#!/bin/sh" also need to be deleted. The correct version should be

#!/bin/sh
/usr/bin/opendiff "$2" "$5" -merge "$1"

Thanks. Fixed it. Not sure why that was.

This is awesome, thanks very much!

You can also use git difftool.

j-kan, blindly trying git difftool on another machine of mine did not do anything. shrug I'll keep it in mind though. Thanks.

Huh... here's what it does for me:

beton:pgm1 kan$ git difftool diagnosis.clj 
merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld kompare gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff

Viewing: 'pgm1/diagnosis.clj'
Hit return to launch 'opendiff': 

...and hitting return does exactly what it says. I'm running 1.7.3.4, if that matters....

Very confusing terminology. "Choose Left" moves the arrow from the right to the left. This seems that the code on the right goes into the chosen code on the left. - Please provide an improvement to help the user.

Anyone know what diff git natively uses? I like it's terminal output and want to make it my system default.

Hi, how do I paste the code in in step 2?
I try to paste it in, but I get an error that reads:

"E486: Pattern not found: usr"

I guess I don't understand how to use VI, it is not intuitive for me, but I want to make this work. Before I had git setup to use opendiff as the mergetool and it worked great. Somehow it stopped working and I don't know how to get it back. Thanks.

-jack

There are two mode when you're in vi. The "command" mode in which you type commands, and the edition mode. To enter the edition mode, you should type "i", or "a". Then you can paste the code. To leave the edition mode, hit the escape key. To quit and save the changes, type ":x" then the return key while in command mode.

This works great but is there any way to make the git diff command line not wait until FileMerge quits?
I made the following modification so that a copy is made of the temp file that git generates so that & can be called on opendiff.

#!/bin/sh
TMPFILE=$(mktemp /tmp/output.XXXXXXXXXX)
# Copy the temp file that git generates
# So that the command line can exit
cp $5 $TMPFILE
/usr/bin/opendiff "$2" "$TMPFILE" &

This works great when I test it directly from the shell.

./git-diff-cmd.sh ignored test.txt ignored ignored test2.txt 

but when asking git to do a diff I don't regain control of the terminal until I quit FileMerge

git diff 263f..4aaa complete.cpp 

After looking at this more closely it appears the git diff behavior is different depending on whether FileMerge is already running. So if FileMerge is running before a call to git diff then it works.

Here is a fish shell version for working with a temp file.

#!/usr/local/bin/fish

set -l TMPFILE (mktemp /tmp/output.XXXXXXXXXX)

# Copy the temp file that git generates
# So that the command line can exit
cp $argv[5] $TMPFILE

function event_git_diff --on-event git_diff_event
  /usr/bin/opendiff $argv[1] $argv[2] &
end

emit git_diff_event $argv[2] $TMPFILE

difftool did the right thing for me, too.

% git --version
git version 1.8.3.4 (Apple Git-47)

Problem for me with difftool is that it will ask for merge file when saving.
Problem for me with this script is that opendiff will open new diffs before I had time to save/quit.

If someone knows how to fix sequential diffs where git will wait for the first file to be saved/quit before diffing another file + have merge path set I'd be really greatful.

Update:
I had this behaviour with svn using filemerge and:
http://soft.vub.ac.be/svn-gen/bdefrain/fmscripts/fmdiff

But can't seem to replicate it with git.

Edit:
@mcormier I think you can just put & after the command to put in the background.

For anyone interested, this is how I solved it (using git difftool):

[diff]
    tool = opendiff
[difftool]
    prompt = false
[difftool "opendiff"]
    cmd = /usr/bin/opendiff \"$LOCAL\" \"$REMOTE\" -merge \"$MERGED\" | cat

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.