是否可以使用winmerge从cygwin查看git diffs?

时间:2010-08-29 05:07:34

标签: windows git windows-xp cygwin winmerge

我喜欢在cygwin上使用git,但我唯一的缺点就是当我想git difftool我不能使用任何有用的东西时。 git diff大部分时间都适合我,但有时候我想使用winmerge通过git difftool查看这些差异是否有某种方法来设置它?

1 个答案:

答案 0 :(得分:9)

对于cygwin,SO问题“How can I configure Mercurial to use WinMerge for merges, under cygwin?”可以应用于git(将以下winmerge脚本与旧问题“use Winmerge inside of Git to file diff”结合使用时)

#!/bin/sh
"/cygdrive/c/Program Files/WinMerge/WinMergeU.EXE" /e /ub /dl other /dr local `cygpath -aw $1` `cygpath -aw $2` `cygpath -aw $3`

过程

您在本文“Git + WinMerge + Cygwin”(来自Thiru Thirunavukarasu)中有更多详细信息:
(但我仍然会推荐上面的WinMergeU.exe选项)

  

将以下行添加到.gitconfig文件中:

  [diff]
    tool = winmerge
  [difftool "winmerge"]
    cmd = git-difftool-winmerge-wrapper.sh \"$LOCAL\" \"$REMOTE\"
  [difftool]
    prompt = false
  

最后一个选项(prompt = false)是可选的。如果你希望Git在打开每个差异之前提示你,你可以省略它。

     

创建名为 git-difftool-winmerge-wrapper.sh 的文件并将其放在您的路径中   我把它放在我的主目录中。您可以通过修改.bash_profile文件(也在主目录中)并添加PATH=${PATH}:${HOME}

来更改默认的Cygwin路径以包含您的主目录      

将以下内容添加到git-difftool-winmerge-wrapper.sh

  #!/bin/sh
  echo "Launching WinMergeU.exe \"$(cygpath -aw "$1")\" \"$(cygpath -aw "$2")\""
  if [ -f "$1" -a -f "$2" ]
  then
    "C:/Program Files (x86)/WinMerge/WinMergeU.exe" -e -u -wl -dl "Base" -dr "Mine" "$(cygpath -aw "$1")" "$(cygpath -aw "$2")"
  else
    echo "skipping as one file doesn't exist"
  fi

(再次,使用那些WinMerge选项进行一些测试)


脚本

如果你想使用diff和merge git函数的“all WinMerge”,你可以从gist page to follow得到ecerulm (Ruben Laguna)

  

diffmerge-diff.sh

#!/bin/sh
# Use SourceGear DiffMerge as mergetool for git in cygwin.
#   git config --global mergetool.diffmerge.cmd "diffmergetool.sh \"\$LOCAL\" \"\$REMOTE\" \"\$BASE\" \"\$MERGED\""
#   git config --global mergetool.diffmerge.trustExitCode false
#   git difftool -t diffmerge branch1..branch2

# Reference: http://www.tldp.org/LDP/abs/abs-guide.pdf

library=githelperfunctions.sh

#[ -f $library ] && . $library
. $library

echo Launching DiffMerge.exe - diffmerge-diff.sh: 

set_path_vars "$1" "$2" "$3" "$4"

echo "$diffmergewinpath" -t1=FROM_VERSION -t2=TO_VERSION --caption=$caption $localwinpath $remotewinpath
"$diffmergewinpath" -t1=FROM_VERSION -t2=TO_VERSION --caption="$caption" "$localwinpath" "$remotewinpath"
  

diffmerge-merge.sh

#!/bin/sh
# Use SourceGear DiffMerge as mergetool for git in cygwin.
#   git config --global mergetool.diffmerge.cmd "diffmergetool.sh \"\$LOCAL\" \"\$REMOTE\" \"\$BASE\" \"\$MERGED\""
#   git config --global mergetool.diffmerge.trustExitCode false
#   git mergetool -t diffmerge

# Reference: http://www.tldp.org/LDP/abs/abs-guide.pdf

library=githelperfunctions.sh

#[ -f $library ] && . $library
. $library

echo Launching DiffMerge.exe - diffmerge-merge.sh: 

set_path_vars "$1" "$2" "$3" "$4"

"$diffmergewinpath" --merge -t1=FROM_VERSION -t2=MERGED -t3=TO_VERSION --result="$mergedwinpath" --caption="$caption" "$localwinpath" "$basewinpath" "$remotewinpath"

unix2dos "$merged"
  

githelperfunctions.sh

# Helper functions


convert_path () {
    file=$1

    if [ "$file" == '/dev/null' ] || [ ! -e "$file" ]
        then 
           file="/tmp/nulla"
           `echo "">$file`
    fi
    echo `cygpath -w -a "$file"`
}


set_path_vars () {
    local=$1
    remote=$2
    base=$3
    merged=$4

    echo ========= Cygwin paths =======
    echo "LOCAL   :  $local"
    echo "REMOTE  :  $remote"
    echo "BASE    :  $base"
    echo "MERGED  :  $merged"

    localwinpath=$(convert_path "$local")
    remotewinpath=$(convert_path "$remote")
    basewinpath=$(convert_path "$base")
    mergedwinpath=$(convert_path "$merged")

    echo ========= Win paths =======
    echo "LOCAL   :  $localwinpath"
    echo "REMOTE  :  $remotewinpath"
    echo "BASE    :  $basewinpath"
    echo "MERGED  :  $mergedwinpath"

    caption=`basename "$merged"`
    diffmergewinpath="C:/Program Files/SourceGear/DiffMerge/DiffMerge.exe"
    winmergewinpath="C:/Program Files/WinMerge/WinMergeU.exe"
#   diffmergewinpath=`cygpath -u C:/Program Files/SourceGear/DiffMerge/DiffMerge.exe`
#   winmergewinpath=`cygpath -u \"C:\Program Files\WinMerge\WinMergeU.exe\"`
}
  

winmerge-diff.sh

#!/bin/sh
# Use winmerge as mergetool for git in cygwin.
#   git config --global difftool.winmerge.cmd "winmerge-diff.sh \"\$LOCAL\" \"\$REMOTE\" \"\$BASE\""
#   git config --global mergetool.winmerge.trustExitCode false
#   git difftool -t winmerge branch1..branch2   

# Reference: http://www.tldp.org/LDP/abs/abs-guide.pdf
# Reference: http://winmerge.org/docs/manual/CommandLine.html


library=githelperfunctions.sh

#[ -f $library ] && . $library
. $library

echo Launching winmerge.exe - winmerge-diff.sh: 

set_path_vars "$1" "$2" "$3" "$4"

"$winmergewinpath" /dl "LOCAL.$caption" /dr "TO_VERSION.$caption" "$localwinpath" "$remotewinpath" 
  

winmerge-merge.sh

#!/bin/sh
# Use winmerge as mergetool for git in cygwin.
#   git config --global mergetool.winmerge.cmd "winmerge-merge.sh \"\$LOCAL\" \"\$REMOTE\" \"\$BASE\" \"\$MERGED\""
#   git config --global mergetool.winmerge.trustExitCode false
#   git mergetool -t diffmerge

# Reference: http://www.tldp.org/LDP/abs/abs-guide.pdf
# Reference: http://winmerge.org/docs/manual/CommandLine.html

library=githelperfunctions.sh

#[ -f $library ] && . $library
. $library

echo Launching winmerge.exe - winmerge-merge.sh: 


set_path_vars "$1" "$2" "$3" "$4"

# -- use WinMergeU conflictFile
"$winmergewinpath" "$mergedwinpath"