Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

linux - git diff with line numbers and proper code alignment/indentation

I obtained this code sample from someone else here:

  git diff --color=always | 
    gawk '{bare=$0;gsub("33[[][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("^(33[[][0-9]*m)?(.)","\2\1",1,$0)};
      bare~/^-/{print "-"left++ ":" line;next};
      bare~/^[+]/{print "+"right++ ":" line;next};
      {print "("left++","right++"):"line;next}'

and would like to have it output properly-aligned lines. Unfortunately, it might output line numbers in your git diff like this:

+240:+ some code here
(241,257): some code here

rather than this to force alignment:

+240     :+some code here
(241,257): some code here

This is one thing I've tried, thinking printf might do the trick (ex: printf "-%-8s:"):

  git diff HEAD~..HEAD --color=always | 
    gawk '{bare=$0;gsub("33[[][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("^(33[[][0-9]*m)?(.)","\2\1",1,$0)};
      bare~/^-/{printf "-%-8s:" left++ line;next};
      bare~/^[+]/{printf "+%-8s:" right++ line;next};
      {print "("left++","right++"): "line;next}'

but it produces this error:

gawk: cmd. line:5: (FILENAME=- FNR=9) fatal: not enough arguments to satisfy format string
    `-%-8s:151-    STR_GIT_LOG="" #######'
        ^ ran out for this one

This bash script is just way over my head at the moment and I've been tinkering on it for quite some time. Perhaps someone can help me out?

Additionally, the numbers and +/- signs should be green and red, respectively, like in normal git diff output.


EDIT by Ed Morton - making the OPs code readable by pretty-printing it using gawk -o- with gawk 5.0.1:

$ gawk -o- '{bare=$0;gsub("33[[][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("^(33[[][0-9]*m)?(.)","\2\1",1,$0)};
  bare~/^-/{print "-"left++ ":" line;next};
  bare~/^[+]/{print "+"right++ ":" line;next};
  {print "("left++","right++"):"line;next}'

.

{
    bare = $0
    gsub("33[[][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("^(33[[][0-9]*m)?(.)", "\2\1", 1, $0)
}

bare ~ /^-/ {
    print "-" left++ ":" line
    next
}

bare ~ /^[+]/ {
    print "+" right++ ":" line
    next
}

{
    print "(" left++ "," right++ "):" line
    next
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It should be a minor typo (most likely) because printf() in awk expects a , after the format specifiers

printf "-%-8s:", left++ line
#             ^^^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...