git diff with line numbers and proper code alignment/indentation

不想你离开。 提交于 2020-05-23 11:58:49

问题


I obtained this code sample from someone else here:

  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}'

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("\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~/^-/{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("\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}'

.

{
    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
}

回答1:


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

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


来源:https://stackoverflow.com/questions/61932427/git-diff-with-line-numbers-and-proper-code-alignment-indentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!