章节 ▾ 第二版

7.5 Git 工具 - 搜索

搜索

对于任何规模的代码库,你经常需要查找函数被调用或定义的位置,或者显示方法的历史记录。Git 提供了一些有用的工具,可以快速轻松地浏览存储在其数据库中的代码和提交。我们将介绍其中一些工具。

Git Grep

Git 自带一个名为 grep 的命令,可以让你轻松地在任何已提交的树、工作目录甚至索引中搜索字符串或正则表达式。对于接下来的示例,我们将在 Git 自身的源代码中进行搜索。

默认情况下,git grep 会在你的工作目录中的文件里查找。作为第一个变体,你可以使用 -n--line-number 选项来打印 Git 找到匹配项的行号

$ git grep -n gmtime_r
compat/gmtime.c:3:#undef gmtime_r
compat/gmtime.c:8:      return git_gmtime_r(timep, &result);
compat/gmtime.c:11:struct tm *git_gmtime_r(const time_t *timep, struct tm *result)
compat/gmtime.c:16:     ret = gmtime_r(timep, result);
compat/mingw.c:826:struct tm *gmtime_r(const time_t *timep, struct tm *result)
compat/mingw.h:206:struct tm *gmtime_r(const time_t *timep, struct tm *result);
date.c:482:             if (gmtime_r(&now, &now_tm))
date.c:545:             if (gmtime_r(&time, tm)) {
date.c:758:             /* gmtime_r() in match_digit() may have clobbered it */
git-compat-util.h:1138:struct tm *git_gmtime_r(const time_t *, struct tm *);
git-compat-util.h:1140:#define gmtime_r git_gmtime_r

除了上面显示的基本搜索,git grep 还支持大量其他有趣的选项。

例如,你可以不打印所有匹配项,而是要求 git grep 通过使用 -c--count 选项,仅显示哪些文件包含搜索字符串以及每个文件中有多少匹配项来汇总输出

$ git grep --count gmtime_r
compat/gmtime.c:4
compat/mingw.c:1
compat/mingw.h:1
date.c:3
git-compat-util.h:2

如果你对搜索字符串的上下文感兴趣,可以使用 -p--show-function 选项显示每个匹配字符串的包含方法或函数

$ git grep -p gmtime_r *.c
date.c=static int match_multi_number(timestamp_t num, char c, const char *date,
date.c:         if (gmtime_r(&now, &now_tm))
date.c=static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
date.c:         if (gmtime_r(&time, tm)) {
date.c=int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
date.c:         /* gmtime_r() in match_digit() may have clobbered it */

如你所见,gmtime_r 例程在 date.c 文件中的 match_multi_numbermatch_digit 函数中都被调用(显示的第三个匹配项仅表示字符串出现在注释中)。

你还可以使用 --and 标志搜索字符串的复杂组合,这可以确保多个匹配项必须出现在同一行文本中。例如,让我们查找任何定义常量且其名称包含子字符串“LINK”或“BUF_MAX”的行,特别是在由标签 v1.8.0 表示的旧版 Git 代码库中(我们将添加 --break--heading 选项,这有助于将输出分解成更易读的格式)

$ git grep --break --heading \
    -n -e '#define' --and \( -e LINK -e BUF_MAX \) v1.8.0
v1.8.0:builtin/index-pack.c
62:#define FLAG_LINK (1u<<20)

v1.8.0:cache.h
73:#define S_IFGITLINK  0160000
74:#define S_ISGITLINK(m)       (((m) & S_IFMT) == S_IFGITLINK)

v1.8.0:environment.c
54:#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS

v1.8.0:strbuf.c
326:#define STRBUF_MAXLINK (2*PATH_MAX)

v1.8.0:symlinks.c
53:#define FL_SYMLINK  (1 << 2)

v1.8.0:zlib.c
30:/* #define ZLIB_BUF_MAX ((uInt)-1) */
31:#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */

git grep 命令比普通的搜索命令(如 grepack)有几个优势。首先是它速度非常快,其次是你可以搜索 Git 中的任何树,而不仅仅是工作目录。正如我们在上面的示例中看到的,我们在旧版 Git 源代码中搜索术语,而不是当前检出的版本。

Git 日志搜索

也许你不是在寻找一个术语在哪里存在,而是在寻找它何时存在或被引入。git log 命令有许多强大的工具,可以根据提交消息的内容甚至它们引入的差异内容来查找特定的提交。

例如,如果我们要找出 ZLIB_BUF_MAX 常量何时最初引入,我们可以使用 -S 选项(俗称 Git “鹤嘴锄”选项)来告诉 Git 只显示那些更改了该字符串出现次数的提交。

$ git log -S ZLIB_BUF_MAX --oneline
e01503b zlib: allow feeding more than 4GB in one go
ef49a7a zlib: zlib can only process 4GB at a time

如果我们查看这些提交的差异,我们可以看到在 ef49a7a 中引入了常量,在 e01503b 中修改了常量。

如果你需要更具体,可以使用 -G 选项提供一个正则表达式进行搜索。

另一个相当高级的日志搜索是行历史搜索,它非常有用。只需使用 -L 选项运行 git log,它将显示代码库中函数或代码行的历史记录。

例如,如果我们要查看 zlib.c 文件中 git_deflate_bound 函数的所有更改,我们可以运行 git log -L :git_deflate_bound:zlib.c。这将尝试找出该函数的范围,然后遍历历史记录并显示对该函数所做的所有更改,作为一系列补丁,追溯到该函数首次创建时。

$ git log -L :git_deflate_bound:zlib.c
commit ef49a7a0126d64359c974b4b3b71d7ad42ee3bca
Author: Junio C Hamano <gitster@pobox.com>
Date:   Fri Jun 10 11:52:15 2011 -0700

    zlib: zlib can only process 4GB at a time

diff --git a/zlib.c b/zlib.c
--- a/zlib.c
+++ b/zlib.c
@@ -85,5 +130,5 @@
-unsigned long git_deflate_bound(z_streamp strm, unsigned long size)
+unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
 {
-       return deflateBound(strm, size);
+       return deflateBound(&strm->z, size);
 }


commit 225a6f1068f71723a910e8565db4e252b3ca21fa
Author: Junio C Hamano <gitster@pobox.com>
Date:   Fri Jun 10 11:18:17 2011 -0700

    zlib: wrap deflateBound() too

diff --git a/zlib.c b/zlib.c
--- a/zlib.c
+++ b/zlib.c
@@ -81,0 +85,5 @@
+unsigned long git_deflate_bound(z_streamp strm, unsigned long size)
+{
+       return deflateBound(strm, size);
+}
+

如果 Git 无法识别你的编程语言中的函数或方法的匹配方式,你也可以提供一个正则表达式(或regex)。例如,这会做与上面示例相同的事情:git log -L '/unsigned long git_deflate_bound/',/^}/:zlib.c。你也可以提供一个行范围或单个行号,你将得到相同类型的输出。