1

I am learning git and want to know how to get the exact line number of a modified line using git show commit_id. I am writing a python script to achieve some task and want the exact line number of modified line. For example consider the following:

//some extra lines here...
diff --combined describe.c
index fabadb8,cc95eb0..4866510
--- a/describe.c
+++ b/describe.c
@@@ -98,20 -98,12 +98,20 @@@
    return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
}

- static void describe(char *arg)
 -static void describe(struct commit *cmit, int last_one)
++static void describe(char *arg, int last_one)
  {
 +      unsigned char sha1[20];
 +      struct commit *cmit;
    struct commit_list *list;
    static int initialized = 0;
    struct commit_name *n;

 +      if (get_sha1(arg, sha1) < 0)
 +              usage(describe_usage);
 +      cmit = lookup_commit_reference(sha1);
 +      if (!cmit)
 +              usage(describe_usage);
 +
    if (!initialized) {
            initialized = 1;
            for_each_ref(get_name);

In this output, at line 5 from top, gives 98, but this not the line number where we made changes. How can we get the exact line number of modified line. One option is we scan the file and find the line line number by using the content of git show commit_id output, but it's not efficient.

Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
  • see http://stackoverflow.com/questions/8259851/using-git-diff-how-can-i-get-added-and-modified-lines-numbers – Paul Nov 05 '14 at 07:19
  • Thanks @Paul, for sharing it. Sorry, i did typo in question. I have updated the question. Please Help. – Amit Sharma Nov 05 '14 at 07:25
  • These probably share a similar format. – Paul Nov 05 '14 at 07:26
  • Yes, but the link you share give the solution to find line number for uncommited changes. But in my case they are commited. – Amit Sharma Nov 05 '14 at 07:27
  • Both of these procedures appear to run [`diff`](http://linux.die.net/man/1/diff) in the back end. That is why I thought the answers may be identical. Otherwise, I don't have any further tips. – Paul Nov 05 '14 at 07:29
  • You can look at [this](http://git-scm.com/docs/git-diff#_combined_diff_format) for information about the combined diff format. If things aren't lining up, perhaps it is a bug in Git. – John Szakmeister Nov 05 '14 at 13:19

0 Answers0