How can a custom merge tool get access to file shell variables?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm in a situation where I have to fairly regularly merge two different
trees where multiple files are being simultaneously edited in both trees
and whitespace in some of the files is different in both trees as well.
I would like to be able to use git mergetool to walk me through
resolving conflicts in merges.  My preferred graphical merge tool is
xxdiff but in 3-way mode it doesn't allow you to ignore whitespace while
doing the diff, only while displaying.  As a result, all these files
appear to have one giant diff and separating out what has really changed
is a giant pain.  Since my use-case is simple, I thought I would create
a custom mergetool as a shell script that invokes xxdiff in 2-way mode
and moves around files to get what I need.  I've pasted that script at
the end of the message.

I've added this script as custom mergetool as indicated in comments at
the top of the script, but it does not work.  It appears to me that the
problem is that the shell variables containing the required filenames
(BASE, LOCAL, REMOTE, MERGED) are not set.  Looking
at /usr/bin/git-mergetool, it appears that custom merge tools are called
without arguments in a subshell and those variables are not exported
anywhere so they are not in the environment of the subshell.  Given
this, how can a custom mergetool ever get access to this information to
do its work?

If there is another better way to try to accomplish this (other than
forcing whitespace to be the same in both trees for now!) I'd appreciate
it if someone would clue me in!

             --- Keith


#!/bin/bash
#
# Perform a merge for GIT in which whitespace is ignored.  Because the
# underlying diff tools don't seem to be able to ignore whitespace on
# a three-way merge, a 2-way merge is done where the first file is a
# copy of the remote file to be merged and the second file is copy of
# the current file from the branch into which files are being merged.
# The success of the merge is determined by whether a merged file is
# written out from xxdiff.  If you don't save the merged file, the
# merge will be aborted.
#
# This script should be configured as a merge tool in git as follows:
#
# git-config --global --add mergetool.xxdiff-2way-ignorews.cmd
xxdiff-2way-ignorews-git-mergetool
# git-config --global --add mergetool.xxdiff-2way-ignorews.trustExitCode
true
# 

rtmpfile=""
ltmpfile=""

function remove_tmpfiles () {
    if [ -n "$ltmpfile" ]; then
        rm -f "$ltmpfile"
    fi
    if [ -n "$rtmpfile" ]; then
        rm -f "$rtmpfile"
        rm -f "$rtmpfile.merge"
    fi
}

function die () {
    printf "%b" "$1"
    remove_tmpfiles
    exit 1
}

[ -f "$LOCAL" ] || die "Local file does not exists: $LOCAL\n"
[ -f "$REMOTE" ] || die "Remote file does not exists: $REMOTE\n"

# Be really paranoid and copy git's temp files to new ones so we aren't
# playing around with git's files...
ltmpfile=$(mktemp --tmpdir xxdiff-2way-tmp.remote.XXXXXXXXXX)
cp "$LOCAL" "$ltmpfile" || die "Could not copy local file to temporary
merge file.\nLocal file is: $LOCAL\nTemporary merge file is: $ltmpfile
\n"

rtmpfile=$(mktemp --tmpdir xxdiff-2way-tmp.local.XXXXXXXXXX)
cp "$REMOTE" "$rtmpfile" || die "Could not copy remote file to temporary
merge result file.\nLocal file is: $LOCAL\nTemporary merge file is:
$tmpfile\n"

xxdiff -w "$rtmpfile" "$ltmpfile"

if [ -f "$rtmpfile.merge" ]; then
    if [ -f "$MERGED" ]; then
        printf "Merged file already exists, removing so merge result can
be renamed.\n"
        rm -f "$MERGED"
    fi
    mv "$rtmpfile.merge" "$MERGED" || die "Could not rename merge result
to merged file: $MERGED\n"
    remove_tmpfiles
else
    die "Did not find merge output file from xxdiff, aborting merge.\n"
fi


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux