开发者

Git line number before and after

开发者 https://www.devze.com 2023-03-06 16:38 出处:网络
I have some master commits in my git repo. Some of commits are tagged with git tags. Let\'s look at example:

I have some master commits in my git repo. Some of commits are tagged with git tags. Let's look at example:

commits:  Master -> Master -> Master -> Master
tags:       v1        v2        v3       HEAD

Now I have a report that at v2 is an error on file filename at line 45.

Is there any way to get line number based on HEAD and line number 45 in v2?

git diff HEAD v2 filename show me differences but what I really want is just "new" line number.

To be more开发者_如何学Go specific if I added 3 lines at v3 commit and than remove 1 line in latest (HEAD) commit, some git command should result me 45 + 3 - 1 = 47. Is there any?


Far easier IMHO would be to use git blame:

git blame $ref -L'/search_pattern/,+1' filename(s)

A full-on example on my zfs-fuse repository:

$ git for-each-ref --format='%(refname)' -- refs/heads |
       while read ref; 
            do git blame $ref -L'/push/,+1' zfs_operations.c; 
       done

As you can see, half the work is in getting the branch names; You can of course simply hardcode revisions if that's easier for your usage. Output from the above:

f5370a5e (Emmanuel Anne 2011-02-22 20:53:17 +0100 1492) static void push(zfsvfs_t *zfsvfs, cred_t *cred, fuse_ino_t ino, file_info_t *info, const char *buf, size_t size, off_t off)
f5370a5e (Emmanuel Anne 2011-02-22 20:53:17 +0100 1512) static void push(zfsvfs_t *zfsvfs, cred_t *cred, fuse_ino_t ino, file_info_t *info, const char *buf, size_t size, off_t off)
f5370a5e (Emmanuel Anne 2011-02-22 20:53:17 +0100 1512) static void push(zfsvfs_t *zfsvfs, cred_t *cred, fuse_ino_t ino, file_info_t *info, const char *buf, size_t size, off_t off)

Note the line numbers (1492 vs. 1512):

f5370a5e (Emmanuel Anne 2011-02-22 20:53:17 +0100 1512) static void push(zfsvfs_t *zfsvfs, cred_t *cred, fuse_ino_t ino, file_info_t *info, const char *buf, size_t size, off_t off)

Any revisions that do no match the search pattern, will show

fatal: -L parameter 'push': No match

You'll additionally want to look at the options:

 -M    (detect moved lines)
 -n    (show source line number)
 -f    (show filename, especially handy with -C)
 -p / --incremental: if you want something parsed more easily in code
0

精彩评论

暂无评论...
验证码 换一张
取 消