章节 ▾ 第二版

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”中的任意一个子字符串,特别是在 Git 代码库的旧版本(以标签 v1.8.0 表示)中(我们将添加 --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 */

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

Git 日志搜索

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

例如,如果我们想查明 ZLIB_BUF_MAX 常量最初是何时引入的,我们可以使用 -S 选项(俗称 Git 的“pickaxe”选项)来告诉 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 选项提供一个正则表达式来进行搜索。

另一种非常高级且极其有用的日志搜索是行历史搜索。只需在运行 git log 时加上 -L 选项,它就会显示代码库中某个函数或代码行的历史记录。

例如,如果我们想查看对 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 无法确定如何匹配你所用编程语言中的函数或方法,你也可以为其提供一个正则表达式。例如,执行 git log -L '/unsigned long git_deflate_bound/',/^}/:zlib.c 会得到与上述示例相同的结果。你也可以指定行范围或单个行号,同样能得到类似的结果。