programing

줄 번호가있는 Git diff (줄 번호가있는 Git 로그)

nasanasas 2020. 9. 22. 08:17
반응형

줄 번호가있는 Git diff (줄 번호가있는 Git 로그)


a git diff또는 a를 수행 할 때 git log -p출력과 인라인 된 소스 파일의 줄 번호를 어떻게 얻습니까?

나는 그것을 찾아 man git-diff | grep "line numbers"보려고 노력했고 인터넷 검색을 시도했지만 빨리 아무것도 얻지 못했습니다.


사람이 읽을 수있는 줄 번호를 얻을 수 없습니다. git diff

현재 행 번호를 옆에 세로로 표시하는 옵션이 없습니다 git diff.

통합 차이 형식

해당 정보는 diff의 각 변경 사항에 대한 (c) hunk 헤더에서 사용할 수 있지만 통합 된 diff 형식입니다 .

@@ -start,count +start,count @@

파일의 원래 상태는로 표시되고 -새 상태는로 표시됩니다 +(덩어리 헤더의 추가 및 삭제를 의미하지는 않습니다. start파일의 각 버전의 시작 줄 번호를 나타내며 포함 된 줄 수를 count나타냅니다.) , 시작 지점에서 시작합니다.

diff --git a/osx/.gitconfig b/osx/.gitconfig
index 4fd8f04..fcd220c 100644
--- a/osx/.gitconfig
+++ b/osx/.gitconfig
@@ -11,7 +11,7 @@ <== HERE!
 [color "branch"]
        upstream = cyan
 [color "diff"]
-       meta = yellow
+       meta = cyan
        plain = white dim
        old = red bold
        new = green bold

덩어리 헤더

@@ -11,7 +11,7 @@

파일의 이전 버전이 11 행에서 시작하고 7 행이 포함되어 있다고 말합니다.

11  [color "branch"]
12         upstream = cyan
13  [color "diff"]
14 -       meta = yellow
14 +       meta = cyan
15         plain = white dim
16         old = red bold
17         new = green bold

파일의 다음 버전도 11 행에서 시작하며 7 행도 포함합니다.

Unified-diff 형식은 실제로 사람이 사용할 수 없습니다.

아시다시피, 통합 차이 형식은 줄 번호를 쉽게 알아낼 수 없습니다 (적어도 기계가 아닌 경우). 읽을 수있는 줄 번호를 원하면 줄 번호를 표시하는 diffing 도구를 사용해야합니다.

Additional Reading


Here's two more solutions, expanding on Andy Talkowski's code.

Plain text:

  git diff | gawk 'match($0,"^@@ -([0-9]+),[0-9]+ [+]([0-9]+),[0-9]+ @@",a){left=a[1];right=a[2];next};\
   /^(---|\+\+\+|[^-+ ])/{print;next};\
   {line=substr($0,2)};\
   /^-/{print "-" left++ ":" line;next};\
   /^[+]/{print "+" right++ ":" line;next};\
   {print "(" left++ "," right++ "):"line}'

Colored text, assuming \033[66m is the format for color codes:

  git diff --color=always | \
    gawk '{bare=$0;gsub("\033[[][0-9]*m","",bare)};\
      match(bare,"^@@ -([0-9]+),[0-9]+ [+]([0-9]+),[0-9]+ @@",a){left=a[1];right=a[2];next};\
      bare ~ /^(---|\+\+\+|[^-+ ])/{print;next};\
      {line=gensub("^(\033[[][0-9]*m)?(.)","\\2\\1",1,$0)};\
      bare~/^-/{print "-"left++ ":" line;next};\
      bare~/^[+]/{print "+"right++ ":" line;next};\
      {print "("left++","right++"):"line;next}'

The code changes lines that start with - or + to -1:- or +1:+, and lines that start with to (5,6):. The numbers are the line numbers from the respective file.


Here is a script that attempts to fix this - not tested it in anger but it seems ok. It relies on the records git diff produces and uses awk to maintain line counts.

# Massage the @@ counts so they are usable
function prep1() {
   cat | awk -F',' 'BEGIN { convert = 0; }
       /^@@ / { convert=1; }
       /^/  { if ( convert == 1 ) { print $1,$2,$3;
              } else { print $0;
              }
              convert=0;
             }'
}

# Extract all new changes added with the line count
function prep2() {
  cat | awk 'BEGIN { display=0; line=0; left=0; out=1;}
     /^@@ / { out=0; inc=0; line=$4; line--; display=line; left=line;        }
     /^[-]/   { left++; display=left; inc=0; }
     /^[+]/   { line++; display=line; inc=0; }
     /^[-+][-+][-+] / { out=0; inc=0; }
     /^/    { 
               line += inc;
               left += inc;
               display += inc;
               if ( out == 1 ) {
                   print display,$0;
               } else {
                   print $0;
               }
               out = 1;
               inc = 1;
               display = line;
            }'
} 

git diff $1 | prep1 | prep2 

You can try

git blame

on the file. It shows you the committer, commit id, and line number for each line in the file.


You can use git difftool to do the diff with an external editor that will display line numbers. Here's how to do it with vim / vimdiff:

  1. Set vimdiff as git's difftool:

    git config --global diff.tool vimdiff
    
  2. Configure ~/.vimrc to automatically show line numbers when using vimdiff:

    if &diff
        set number
    endif
    
  3. Run git difftool, which will use vimdiff with line numbers:

    git difftool
    

A quick way is to use git diff -U0. That will set the lines of context to 0, which will make the @@ values match the actual changed lines. By default, the @@ values include 3 lines of before/after context, which is not convenient for humans.

Example:

git diff # default
@@ -10,8 +10,8 @@

This is hard to calculate the line numbers of the changed lines because line 10 refers to the first line of the before context. The actual line number of the first changed line is 10+3=13. To calculate the number of changed lines, then you have to also subtract the before and after context: 8-3-3=2.

git diff -U0
@@ -13,2 +13,2 @@

As you can see, setting context = 0 makes the @@ values easier for humans to read. You can see that the changed lines start at line 13, and there are 2 changed lines.

This isn't perfect, since it only shows the line number for each block. If you want to see line numbers for every line, then use difftool for an external editor. See https://stackoverflow.com/a/50049752


I like to use git difftool with meld as my difftool. It's easier to look at than git diff, has a nice side-by-side gui comparison, and shows line numbers.

참고URL : https://stackoverflow.com/questions/24455377/git-diff-with-line-numbers-git-log-with-line-numbers

반응형