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
를 통해 사용할 수도 있습니다 .
colordiff를 설치하십시오 .
~ / .colordiffrc를 업데이트하십시오 (필요한 경우 먼저 / etc / colordiffrc 복사).
# be more git-like: plain=off newtext=darkgreen oldtext=darkred diffstuff=darkcyan
colordiff -u file1 file2
두 파일에 사용 하거나colordiff -ruN path1 path2
경로를 재귀 적으로 비교하는 데 사용하십시오 .
정확히 동일하지는 않지만 매우 가깝습니다.
이것은 내가 제안하는 것이고 꽤 가깝습니다.
diff -u FILE1 FILE2 | colordiff | less -R
colordiff
: 이것을 설치해야합니다-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
}
이 기능은 다음과 같이 작동합니다.
- 궁극적
diff
으로 다양한 서식 옵션을 사용하여 파일 내 변경 내용이 표시되는 방법을 지정합니다. tput
is used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replacetput setaf
withtput setf
.- The output of
diff
is piped intoless
.-R
allows ANSI colors to be preserved.-X
preventsless
from clearing the screen upon exiting.-F
preventsless
from operating as a pager if the output fits within one screen. - 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
:
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). Thanksdiff
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.- Because the lines of context are different from
git diff
, the line numbers reported by this function will also vary from those reported bygit diff
. - 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 todiff
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
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
'programing tip' 카테고리의 다른 글
UIImageView에서 코너 반경 설정이 작동하지 않습니다 (0) | 2020.07.13 |
---|---|
groovy 배열 / 해시 / 컬렉션 / 목록의 요소를 확인하는 방법은 무엇입니까? (0) | 2020.07.13 |
angularjs 약속을 반환하기 전에 해결할 수 있습니까? (0) | 2020.07.12 |
Pandas DataFrame을 사전으로 변환 (0) | 2020.07.12 |
C ++ 11의 재귀 람다 함수 (0) | 2020.07.12 |