programing tip

diff를 git-diff처럼 작동시키는 방법?

itbloger 2020. 7. 12. 10:15
반응형

diff를 git-diff처럼 작동시키는 방법?


의 출력 형식이 마음에 듭니다 git diff. 색상 및 +/ -선 사이의 변화의 표현은 GNU은 diff보다 쉽게 읽을 수 있습니다.

git repo 외부에서 플래그를 git diff사용하여 실행할 수 있으며 --no-index정상적으로 작동합니다. 그러나 --exclude재귀에서 파일이나 하위 디렉터리를 제외 하는 옵션 이없는 것으로 보입니다 diff.

두 세계를 모두 활용할 수있는 방법이 있습니까? ( GNU diff 색상 옵션 및 +/ -형식 git diff--exclude옵션).

나는 실험을 colordiff했지만 여전히 출력 형식을 선호합니다.git diff


나는 색상을 만드는 방법을 모르지만 이것은 +/-대신에 할 것 입니다.<>

diff -u file1 file2

맨 페이지git diff --no-index -- A B 를 통해 사용할 수도 있습니다 .


  1. colordiff를 설치하십시오 .

  2. ~ / .colordiffrc를 업데이트하십시오 (필요한 경우 먼저 / etc / colordiffrc 복사).

    # be more git-like:
    plain=off
    newtext=darkgreen
    oldtext=darkred
    diffstuff=darkcyan
    
  3. colordiff -u file1 file2두 파일에 사용 하거나 colordiff -ruN path1 path2경로를 재귀 적으로 비교하는 데 사용하십시오 .

정확히 동일하지는 않지만 매우 가깝습니다.


이것은 내가 제안하는 것이고 꽤 가깝습니다.

diff -u FILE1 FILE2 | colordiff | less -R
  • colordiff: 이것을 설치해야합니다
    • brew install colordiff 내 Mac에서.
    • port install colordiff 일부 Mac에서는.
    • sudo apt-get install colordiff 데비안 또는 우분투에서
    • 다른 플랫폼의 경우 메인 페이지 또는 GitHub 에서 소스를 다운로드 하고 설치 지침을 따르십시오
  • -R: 이것은 원시 코드 대신 색상을 표시하도록 Less에 지시합니다.

나는 -w공백의 차이를보고 싶지 않기 때문에 궁극적으로 사용했습니다 .

diff -w -u FILE1 FILE2 | colordiff | less -R

편집 : 의견에서 @Ciprian Tomoiaga가 제안한 것처럼이 기능을 사용하여 ~/.bashrc파일에 넣을 수도 있습니다.

function gdiff () { diff -u $@ | colordiff | less -R; }

만을 사용하여 bash, diff, tput, 그리고 less, 우리가 밀접의 출력에 근접 할 수 있습니다 git diff. 그러나 diff프로그래머의 근시안으로 인해 눈에 띄는 차이점이 있습니다 .

사용자 계정이 자동으로 제공하는 일부 파일에 다음 Bash 함수 정의를 넣으면 명령 줄에서 함수에 액세스 할 수 있습니다.

function gdiff()
{
    local REG=`tput op`
    local GRP=`tput setaf 6`
    local ADD=`tput setaf 2`
    local REM=`tput setaf 1`

    local NL=$'\n'
    local GRP_LABEL="${GRP}@@ %df,%dn +%dF,%dN @@${REG}"

    local UNCH_GRP_FMT=''

    [[ "${1}" == '@full' ]] && {

        UNCH_GRP_FMT="${GRP_LABEL}${NL}%="
        shift
    }

    diff \
        --new-line-format="${ADD}+%L${REG}" \
        --old-line-format="${REM}-%L${REG}" \
        --unchanged-line-format=" %L${REG}" \
        --new-group-format="${GRP_LABEL}${NL}%>" \
        --old-group-format="${GRP_LABEL}${NL}%<" \
        --changed-group-format="${GRP_LABEL}${NL}%<%>" \
        --unchanged-group-format="${UNCH_GRP_FMT}" \
            "${@}" | less -FXR
}

이 기능은 다음과 같이 작동합니다.

  1. 궁극적 diff으로 다양한 서식 옵션을 사용하여 파일 내 변경 내용이 표시되는 방법을 지정합니다.
  2. tput is used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replace tput setaf with tput setf.
  3. The output of diff is piped into less. -R allows ANSI colors to be preserved. -X prevents less from clearing the screen upon exiting. -F prevents less from operating as a pager if the output fits within one screen.
  4. If the first parameter is @full, the function will display all unchanged lines in addition to added and removed lines.

Note the following differences between this approach and git diff:

  1. git diff reports three lines of context surrounding each change. Unfortunately, diff seems to complain and exit if you want to specify the number of context lines while also simultaneously specifying formatting options. (At least it does in Mac OS X Yosemite). Thanks diff programmers. Therefore, you can either request no lines of context surrounding each change, which is the default behavior, or you can request that all unchanged lines within the file are also reported, by specifying @full as the first parameter.
  2. Because the lines of context are different from git diff, the line numbers reported by this function will also vary from those reported by git diff.
  3. You may see the presence of single-line changes reported, which is the correct behavior, but annoying when your changed file contains the insertion of single empty lines. I think git diff deals with this better, via its lines of context. You could try passing different options to diff to better deal with whitespace, if you prefer.

You are looking for colordiff:

sudo apt-get install colordiff

Place this in your .bashrc or .zshrc :

diff() { git diff --no-index "$1" "$2" | colordiff; }

requirments : git and colordiff should be installed before-hand.

usage : diff file1 file2

example : for $diff .tmux.conf .zshrc.pre-oh-my-zsh

diff function example


GNU diff has a --color option since version 3.4 in late 2016 according to this answer on the Unix SE. That alongside -u should be enough to mimic the output of git diff:

diff -u --color=always file1 file2 | less -r

--color must be always when used in a pipe, auto will turn off color in pipes.

I've only tried this with Git Bash on Windows, where less -R would only color the first line of a hunk. less -r fixed it for me in that case.


The other option is to do it from outside the repository so git knows to diff between files. eg. a shell function something like:

gdiff() {
    (
        dir=`pwd`
        cd ./$(git rev-parse --show-cdup)/..
        git diff  $dir/$1 $dir/$2
    )
}

Use colordiff:

Installation:

sudo apt-get install colordiff

Usage:

colordiff -u file_one file_two

Gives exactly same difference as shown by git diff.


If you don't have colordiff or git diff, you can get color by vim.

cdiff() { diff -u $@ | vim -R -; }

or simply

cdiff() { diff -u $@ | view -; }

I think the config setting :

[color]
     ui = true

combined with "diff" command's --relative=<path> option would do what you wanted. Did you try ?

참고URL : https://stackoverflow.com/questions/4857310/how-to-get-diff-working-like-git-diff

반응형