Programming

Git 내부에서 Winmerge를 사용하여 파일 비교

procodes 2020. 8. 23. 10:16
반응형

Git 내부에서 Winmerge를 사용하여 파일 비교


git 내부에서 Winmerge를 사용하여 Diff를 수행하는 방법이 있습니까?


6 년 후인 2015 년 6 월 업데이트 :

" git mergetool winmerge " 에서 자세히 설명했듯이 간단한 git config diff.tool winmerge것으로 충분합니다.

Git 2.5+ (2015 년 2 분기)는 이제 Winmerge를 diff 또는 merge 도구로 인식합니다!


원문 답변 (2009-2012)

(msysgit, 1.6.5, DOS 세션)

첫 번째 부분 (winmerge 사용)은 " visual diff 프로그램으로 'git diff'출력을 보는 방법은 무엇입니까? "에 설명되어 있습니다.

C:\myGitRepo>git config --replace --global diff.tool winmerge
C:\myGitRepo>git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --replace --global difftool.prompt false

으로 winmerge.sh당신의 디렉토리 부분에 저장 PATH:

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"$PROGRAMFILES/WinMerge/WinMergeU.exe" -e -u -dl "Local" -dr "Remote" "$1" "$2"

( WinMerge 명령 줄 옵션 참조 )

git difftool

이제 WinMerge가 시작됩니다. WinMerge를 시작
하려면 다음을 git diff설정하십시오.

set GIT_EXTERNAL_DIFF=winmerge.sh

그러나 진정한 부가가치는 동일한 diff 도구를 사용하여 모든 차이점을 순차적으로 표시하는 대신 한 배치 로 표시 하는 기능에서 비롯됩니다. 따라서 diff 도구 창을 한 번에 하나씩 닫아야합니다.

2012 년 6 월 업데이트 (2 년 반 후) :

파일 별 대신 디렉토리를 비교하는 것은 곧 사용할 수있을 것입니다 :
참조가 망할 놈의 1.7.11.rc1을 [ANNOUNCE] :

" git difftool" 는 파일 쌍당 한 번 외부 도구 인스턴스를 실행하는 대신 두 개의 임시 디렉토리를 채운 후 한 번에 두 개의 디렉토리 계층 구조비교할--dir-diff 수있는 외부 비교 도구를 생성 하는 " "옵션을 배웠습니다 .

자세한 내용은 " Patch difftool: difftool디렉토리 차이를 처리하도록 가르치기 "및 " Git 분기의 디렉토리 비교 "답변 을 참조하십시오.


디렉토리 스크립트 별 원본 difftool (2009 년 12 월)

으로 스바 Illingworth가 에 언급 그의 대답 , 스크립트 git-diffall.sh (또한 경로에 넣어) 그냥 할 수 있습니다 :

#!/bin/sh
git diff --name-only "$@" | while read filename; do
    git difftool "$@" --no-prompt "$filename" &
done

그러나 그것은 n 개의 파일에 대해 n 개의 창을 열 때만 작동 합니다 ( WinMerge 옵션 을 사용하려고 하면 difftool에 의해 너무 일찍 삭제되는 임시 파일 때문에 작동하지 않습니다)-s


그렇기 때문에 GitDiff.bat-power-diffing with GI 접근 방식을 좋아합니다.이 방식 을 사용하면 내부 차이점을 검사하기 위해 하나를 선택하기 전에 차이점이있는 파일 목록을 검토 할 수 있습니다.
DOS 명령 만 사용하도록 조정했습니다.

@echo off

setlocal

if "%1" == "-?" (
    echo GitDiff - enables diffing of file lists, instead of having to serially
    echo diff files without being able to go back to a previous file.
    echo Command-line options are passed through to git diff.
    echo If GIT_FOLDER_DIFF is set, it is used to diff the file lists. Default is windff.
    goto END
)

if "%GIT_DIFF_COPY_FILES%"=="" (
    rd /s /q %TEMP%\GitDiff
    mkdir %TEMP%\GitDiff
    mkdir %TEMP%\GitDiff\old
    mkdir %TEMP%\GitDiff\new

    REM This batch file will be called by git diff. This env var indicates whether it is
    REM being called directly, or inside git diff
    set GIT_DIFF_COPY_FILES=1

    set GIT_DIFF_OLD_FILES=%TEMP%\GitDiff\old
    set GIT_DIFF_NEW_FILES=%TEMP%\GitDiff\new

    set GIT_EXTERNAL_DIFF=%~dp0\GitDiff.bat
    echo Please wait and press q when you see "(END)" printed in reverse color...
    call git diff %*

    if defined GIT_FOLDER_DIFF (
        REM This command using GIT_FOLDER_DIFF just does not work for some reason.
        %GIT_FOLDER_DIFF% %TEMP%\GitDiff\old %TEMP%\GitDiff\new
        goto END
    )

    if exist "%ProgramFiles%\Beyond Compare 2\BC2.exe" (
        set GIT_FOLDER_DIFF="%ProgramFiles%\Beyond Compare 2\BC2.exe"
        "%ProgramFiles%\Beyond Compare 2\BC2.exe" %TEMP%\GitDiff\old %TEMP%\GitDiff\new
        goto END
    )

    "%ProgramFiles(x86)%\WinMerge\WinMergeU.exe" -r -e -dl "Local" -dr "Remote"  %TEMP%\GitDiff\old %TEMP%\GitDiff\new
    goto END
)

REM diff is called by git with 7 parameters:
REM     path old-file old-hex old-mode new-file new-hex new-mode
copy %TEMP%\%~nx2 %GIT_DIFF_OLD_FILES%\%1
copy %5 %GIT_DIFF_NEW_FILES%

:END

서로 다른 디렉토리에서 동일한 이름을 가진 파일을 처리 할만큼 강력하지는 않지만 가능한 일에 대한 일반적인 아이디어를 제공합니다.
여기서 내부 차이점이있는 파일 목록과 함께 하나의 WinMerge 만 열립니다. 검사하려는 항목을 클릭 ESC하면 모든 WinMerge-diff세션 이 종료됩니다 .


첫 번째 부분을 2 개소에서 사용하는데 어려움을 겪었고 다음과 같이 수정했습니다.

  1. The second command for setting up winmerge.cmd required an extra slash on cmdline (before $LOCAL and $REMOTE), else cygwin was substituting the variable in cmdline

    C:\myGitRepo>git config --replace --global difftool.winmerge.cmd "winmerge.sh \"\$LOCAL\" \"\$REMOTE\""
    
  2. changed the winmerge.sh file to (without this, was getting right-path-invalid error)

    #!/bin/sh
    echo Launching WinMergeU.exe: "$(cygpath -aw "$1")" "$(cygpath -aw "$2")"
    "$PROGRAMFILES/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$(cygpath -aw "$1")" "$(cygpath -aw "$2")"
    

Since the thread is getting confusing and bifurcated, here are consolidated instructions for the Directory Listing "--dir-diff" WinMerge method for msysgit Git Windows.

Step 1 - Create a file named winmerge.sh at a location accessible to your path (such as /home/bin/winmerge.sh) with following contents.

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"$PROGRAMFILES/WinMerge/WinMergeU.exe" -r -ub -dl "Remote" -dr "Local" "$1" "$2"

Step 2 - Type the following commands in Git Bash to instruct git to use winmerge.sh as difftool (these options get stored in /home/.gitconfig):

git config --replace --global diff.tool winmerge
git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
git config --replace --global difftool.prompt false

Step 3 - Now you can test by typing the following command in Git Bash to start your WinMerge diff:

git difftool --dir-diff

Step 4 - For quicker access, create an alias for this command by adding this line to .bashrc in your home folder (or create .bashrc file with this line if file does not already exist):

alias diffdir='git difftool --dir-diff'

Step 5 - Now you can quickly see a diff in WinMerge just by typing the following command into Git Bash

diffdir

I have a script that will set the Diff and Merge tools in the Git config with the proper parameters that doesn't require a separate .sh file to exist. It seems to be working fine for me.

git config --global diff.tool winmerge
git config --global difftool.prompt false
git config --global difftool.winmerge.cmd "\"\$PROGRAMFILES\"/WinMerge/WinMergeU.exe -r -u -e -dl \"Local\" -dr \"Remote\" \"\$LOCAL\" \"\$REMOTE\""

git config --global merge.tool winmerge
git config --global mergetool.prompt false
git config --global mergetool.winmerge.trustExitCode true
git config --global mergetool.winmerge.cmd "\"\$PROGRAMFILES\"/WinMerge/WinMergeU.exe -r -u -e -dl \"Local\" -dr \"Remote\" \"\$LOCAL\" \"\$REMOTE\" \"\$BASE\" \"\$MERGED\""

Note - the entire .cmd portion is quoted so that the parameters will be listed in the .gitconfig properly


On windows you can do it this way:

1) Open .gitconfig file. It's located at your home directory: c:\users\username.gitconfig

2) Add the lines below. Pay attention to the single quotes wrapping the path to winmerge:

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" -e "$LOCAL" "$REMOTE"
[difftool]
    prompt = false
[merge]
    tool = winmerge
[mergetool "winmerge"]
    cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" \"$MERGED\" \"$REMOTE\"
[mergetool]
    keepBackup = false
    trustExitCode = false

Without configuration:

git difftool --tool winmerge

Assuming:

  • Winmerge is installed
  • Git for windows is installed, from "git version 2.12.0.windows1" or above (although earlier versions of git may have introduced the command).

I was confused about why the solution was presented as a DOS batch file, as my Git installation came with a bash shell. I was also unable to get a DOS context running from bash, so I've attempted to adapt what was previously shared in a bash context.

Since git diff appears to run the specified command once for each file, I split my solution into two bash scripts:

First, configure gitprepdiff.sh to be the difftool as previously mentioned

#!/bin/sh
#echo ...gitprepdiff.sh
cp -v $1 "$TMP/GitDiff/old/$2"
cp -v $2 "$TMP/GitDiff/new"

I also noted that the results of the git configure commands can be found and edited directly in C:\Users\<username>\.gitconfigure

gitdiff.sh is then run at the command-line where you would normally call git diff

#!/bin/sh
#echo Running gitdiff.sh...

DIFFTEMP=$TMP/GitDiff

echo Deleting and re-creating $DIFFTEMP...
rm -rf $DIFFTEMP;
mkdir $DIFFTEMP;

echo Creating $DIFFTEMP/old...
mkdir $DIFFTEMP/old;

echo Creating $DIFFTEMP/new...
mkdir $DIFFTEMP/new;

git diff --name-only "$@" | while read filename; do
    git difftool "$@" --no-prompt "$filename";
done

"$PROGRAMFILES\WinMerge\WinMergeU.exe" -r -e -dl "Repository" -dr "Working" $LOCALAPPDATA\\Temp\\1\\GitDiff\\old $LOCALAPPDATA\\Temp\\1\\GitDiff\\new

Also worth noting is that, on my installation, /tmp (in bash) mapped to %LOCALAPPDATA%\Temp\1\ (in Windows), so that's why I'm using the latter in my call to WinMerge.


git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "\"$PROGRAMFILES\\WinMerge\\WinMergeU.exe\" -u -dl \"Local\" -dr \"Remote\" \"\$LOCAL\" \"\$REMOTE\""
git config --global difftool.prompt false

As per the WinMerge command line manual: "Parameters are prefixed with either a forward slash ( / ) or dash ( - ) character"

참고URL : https://stackoverflow.com/questions/1881594/use-winmerge-inside-of-git-to-file-diff

반응형