-
1. 開始
-
2. Git 基礎
-
3. Git 分支
-
4. 伺服器上的 Git
- 4.1 通訊協定
- 4.2 在伺服器上取得 Git
- 4.3 產生您的 SSH 公開金鑰
- 4.4 設定伺服器
- 4.5 Git Daemon
- 4.6 智慧型 HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 第三方託管選項
- 4.10 總結
-
5. 分散式 Git
-
A1. 附錄 A:其他環境中的 Git
- A1.1 圖形介面
- A1.2 Visual Studio 中的 Git
- A1.3 Visual Studio Code 中的 Git
- A1.4 IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine 中的 Git
- A1.5 Sublime Text 中的 Git
- A1.6 Bash 中的 Git
- A1.7 Zsh 中的 Git
- A1.8 PowerShell 中的 Git
- A1.9 總結
-
A2. 附錄 B:將 Git 嵌入您的應用程式
-
A3. 附錄 C:Git 命令
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
還支援許多其他有趣的選項。
例如,您可以使用 -c
或 --count
選項,要求 git grep
摘要輸出,只顯示哪些檔案包含搜尋字串,以及每個檔案中有多少個符合項目,而不是列印所有符合項目
$ 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_number
和 match_digit
函數呼叫的(顯示的第三個符合項目僅表示字串出現在註解中)。
您也可以使用 --and
旗標搜尋複雜的字串組合,以確保多個符合項目必須出現在同一行文字中。 例如,讓我們尋找在 Git 程式碼庫的較舊版本(以標籤 v1.8.0
表示)中,名稱包含「LINK」或「BUF_MAX」子字串任一的常數的任何定義行(我們將加入 --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 */
與 grep
和 ack
等一般搜尋命令相比,git grep
命令具有一些優勢。 第一個是它真的很快,第二個是您可以搜尋 Git 中的任何樹狀結構,而不僅僅是工作目錄。 正如我們在上面的範例中所見,我們在舊版的 Git 原始碼中尋找詞彙,而不是目前已簽出的版本。
Git 記錄搜尋
也許您不是在尋找某個詞彙存在的位置,而是在尋找它存在或引入的時間。 git log
命令有許多強大的工具,可以透過其訊息內容,甚至是他們引入的 diff 內容來尋找特定的提交。
例如,如果我們想找出 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
。您也可以給定一個行範圍或單一行號,您將會得到相同的輸出結果。