gpt4 book ai didi

Git 前后行号

转载 作者:太空狗 更新时间:2023-10-29 14:09:27 25 4
gpt4 key购买 nike

我的 git 仓库中有一些主提交。一些提交带有 git 标签。让我们看一个例子:

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

现在我有一个报告,在 v2 中第 45 行的文件 filename 上有一个错误。

有没有办法根据 HEAD 和 v2 中的行号 45 获取行号?

git diff HEAD v2 filename 显示差异,但我真正想要的只是"new"行号。

更具体地说,如果我在 v3 提交时添加了 3 行,而不是在最新的 (HEAD) 提交中删除了 1 行,一些 git 命令应该得到 45 + 3 - 1 = 47。是否有有吗?

最佳答案

恕我直言,使用 git blame 会容易得多:

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

我的 zfs-fuse 存储库中的完整示例:

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

如您所见,一半的工作是获取分支名称;如果您的使用更方便,您当然可以简单地对修订进行硬编码。 上面的输出:

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)

注意行号(14921512):

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)

任何与搜索模式不匹配的修订都会显示

fatal: -L parameter 'push': No match

您还需要查看以下选项:

 -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

关于Git 前后行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6015509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com