On git version 2.17.1
, there isn't a built-in flag to achieve this purpose.
Here's an example command to filter out the filename and line numbers from an unified diff:
git diff --unified=0 | grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? @@@)' | paste -s -d':'
For example, the unified diff:
$ git diff --unified=0
diff --cc foobar
index b436f31,df63c58..0000000
--- a/foobar
+++ b/foobar
@@@ -1,2 -1,2 +1,6 @@@ Line abov
++<<<<<<< HEAD
+bar
++=======
+ foo
++>>>>>>> Commit message
Will result in:
? git diff --unified=0 | grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? @@@)' | paste -s -d':'
foobar:1
To match the output of commands in common grep match results:
$ git diff --unified=0 | grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? )| @@@.*' | sed -e '0~3{s/ @@@[ ]\?//}' | sed '2~3 s/$/\n1/g' | sed "N;N;N;s/\n/:/g"
foobar:1:1:Line abov
grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? )
: Match filename from diff --cc <filename>
OR Match line number from @@@ <from-file-range> <from-file-range> <to-file-range>
OR Match remaining text after @@@
.sed -e '0~3{s/ @@@[ ]\?//}'
: Remove @@@[ ]\?
from every 3rd line to get the optional 1 line context before ++<<<<<<< HEAD
.sed '2~3 s/$/\n1/g'
: Add \n1
every 3 lines between the 2nd and 3rd line for the column number.sed "N;N;N;s/\n/:/g"
: Join every 3 lines with a :
.