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