2

I need to know the changed line numbers between two commits (HEAD~1 and HEAD) to a repo

git diff HEAD~1 HEAD

returns information in a confusing diff format

I was able to identify newly added files or deleted files by looking at their markers in git diff (ie --- /dev/null and +++/dev/null meaning added, deleted)

Is it possible to identify the lines numbers in the modified files

The need for me is

path/to/file/MyClass1.java
23-56
59-60
72-74

path/to/file/MyClass2.java
1-34
54-56

23-56 , 72-74 and 59-60 are the line/range of lines (denoted as line number) changed between HEAD~1 and HEAD

PS: few of the leads which I found on other SO questions seems to be outdated Any help will be deeply appreciated!

Paolo
  • 21,270
  • 6
  • 38
  • 69
anon-sAI
  • 53
  • 1
  • 9
  • https://www.atlassian.com/git/tutorials/saving-changes/git-diff – symlink Apr 25 '20 at 15:06
  • @symlink I had in past visited the same link and my difficulty is not in generating diff rather it's getting it in the format I want which I explained in my question – anon-sAI Apr 25 '20 at 16:09
  • 1
    You might think about the fact that the unified diff format that Git uses is the product of some 40 years of evolution, going from an original very-terse and fragile format that is remarkably similar to what you're asking for, to the current comprehensive and fairly robust format. In other words, you might find `git diff` output confusing (many do at first) but it's what people using these tools in practice for many decades have settled on, more or less, as best practice. – torek Apr 25 '20 at 23:22

1 Answers1

5

This should give the diff for modified files between the two revisions HEAD~1 and HEAD

git diff --unified=0 --diff-filter=M HEAD~1 HEAD 

Using grep utility, the modified lines and index can be removed from the output

git diff --unified=0 --diff-filter=M HEAD~1 HEAD  | grep -v -e '^[+-]' -e '^index'

The output :

diff --git a/some/file b/some/file
@@ -startline1,count1 +startline2,count2 @@
...

On further processing using sed utility, the final command is :

git diff --unified=0 --diff-filter=M HEAD~1 HEAD | \
grep -v -e '^[+-]' -e '^index' | \
sed 's/diff --git a.* b\//\//g; s/.*@@\(.*\)@@.*/\1/g; s/^ -//g; s/,[0-9]*//g; s/\(^[0-9]*\) +/\1-/g;'

and the output should look like this

/some/file1
startline1-startline2
/some/file2
startline3-startline4
...
Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • Thanks a ton man! Answer verified with git version 2.25.0 Working as I wanted – anon-sAI Apr 26 '20 at 08:13
  • 1
    @anon-sAI Please note that it is quite possible that it might fail in some cases , I recommend you to use the second command `git diff --unified=0 --diff-filter=M HEAD~1 HEAD | grep -v -e '^[+-]' -e '^index'` if you want to be on the safer side, but in the interest of the output format you have suggested, I have asked a question [here](https://unix.stackexchange.com/q/582559/408757) to see if it can be further improved and to find out any case it might fail – Saurabh P Bhandari Apr 26 '20 at 08:31
  • Noted! Would update the failure cases in this thread if I come across any – anon-sAI Apr 26 '20 at 08:34
  • 2
    @anon-sAI safer alternative as it doesn't involve post-processing : https://unix.stackexchange.com/a/582649/408757 – Saurabh P Bhandari Apr 27 '20 at 05:41
  • Better one @Saurabh – anon-sAI Apr 27 '20 at 14:12