You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/07-git-tools/sections/searching.asc
+25-24Lines changed: 25 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
[[_searching]]
2
2
=== Searching
3
3
4
-
With just about any size codebase, you'll often need to find where a function is called or defined, or find the history of a method.
4
+
With just about any size codebase, you'll often need to find where a function is called or defined, or display the history of a method.
5
5
Git provides a couple of useful tools for looking through the code and commits stored in its database quickly and easily.
6
6
We'll go through a few of them.
7
7
8
8
[[_git_grep]]
9
9
==== Git Grep
10
10
11
11
Git ships with a command called `grep` that allows you to easily search through any committed tree or the working directory for a string or regular expression.
12
-
For these examples, we'll look through the Git source code itself.
12
+
For the examples that follow, we'll search through the source code for Git itself.
13
13
14
-
By default, it will look through the files in your working directory.
15
-
You can pass `-n` to print out the line numbers where Git has found matches.
14
+
By default, `git grep` will look through the files in your working directory.
15
+
As a first variation, you can use either of the `-n` or `--line-number` options to print out the line numbers where Git has found matches:
There are a number of interesting options you can provide the `grep` command.
33
+
In addition to the basic search shown above, `git grep` supports a plethora of other interesting options.
33
34
34
-
For instance, instead of the previous call, you can have Git summarize the output by just showing you which files matched and how many matches there were in each file with the `--count` option:
35
+
For instance, instead of printing all of the matches, you can ask `git grep` to summarize the output by showing you only which files contained the search string and how many matches there were in each file with the `-c` or `--count` option:
35
36
36
37
[source,console]
37
38
----
38
39
$ git grep --count gmtime_r
39
40
compat/gmtime.c:4
40
41
compat/mingw.c:1
41
42
compat/mingw.h:1
42
-
date.c:2
43
+
date.c:3
43
44
git-compat-util.h:2
44
45
----
45
46
46
-
If you want to see what method or function it thinks it has found a match in, you can pass `-p`:
47
+
If you're interested in the _context_ of a search string, you can display the enclosing method or function for each matching string with either of the `-p` or `--show-function` options:
47
48
48
49
[source,console]
49
50
----
50
51
$ git grep -p gmtime_r *.c
51
-
date.c=static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
52
+
date.c=static int match_multi_number(timestamp_t num, char c, const char *date,
52
53
date.c: if (gmtime_r(&now, &now_tm))
53
54
date.c=static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
54
55
date.c: if (gmtime_r(&time, tm)) {
56
+
date.c=int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
57
+
date.c: /* gmtime_r() in match_digit() may have clobbered it */
55
58
----
56
59
57
-
So here we can see that `gmtime_r` is called in the `match_multi_number` and `match_digit` functions in the `date.c` file.
60
+
As you can see, the `gmtime_r` routine is called from both the `match_multi_number` and `match_digit` functions in the `date.c` file (the third match displayed represents just the string appearing in a comment).
58
61
59
-
You can also look for complex combinations of strings with the `--and` flag, which makes sure that multiple matches are in the same line.
60
-
For instance, let's look for any lines that define a constant with either the strings ``LINK'' or ``BUF_MAX'' in them in the Git codebase in an older 1.8.0 version.
61
-
62
-
Here we'll also use the `--break` and `--heading` options which help split up the output into a more readable format.
62
+
You can also search for complex combinations of strings with the `--and` flag, which ensures that multiple matches must occur in the same line of text.
63
+
For instance, let's look for any lines that define a constant whose name contains _either_ of the substrings ``LINK'' or ``BUF_MAX'', specifically in an older version of the Git codebase represented by the tag `v1.8.0` (we'll throw in the `--break` and `--heading` options which help split up the output into a more readable format):
63
64
64
65
[source,console]
65
66
----
@@ -95,7 +96,7 @@ As we saw in the above example, we looked for terms in an older version of the G
95
96
Perhaps you're looking not for _where_ a term exists, but _when_ it existed or was introduced.
96
97
The `git log` command has a number of powerful tools for finding specific commits by the content of their messages or even the content of the diff they introduce.
97
98
98
-
Ifwe want to find out for example when the `ZLIB_BUF_MAX` constant was originally introduced, we can use the `-S` option (colloquially referred to as the Git "pickaxe" option) to tell Git to show us only those commits that changed the number of occurrences of that string.
99
+
If, for example, we want to find out when the `ZLIB_BUF_MAX` constant was originally introduced, we can use the `-S` option (colloquially referred to as the Git ``pickaxe'' option) to tell Git to show us only those commits that changed the number of occurrences of that string.
99
100
100
101
[source,console]
101
102
----
@@ -104,7 +105,7 @@ e01503b zlib: allow feeding more than 4GB in one go
104
105
ef49a7a zlib: zlib can only process 4GB at a time
105
106
----
106
107
107
-
If we look at the diff of those commits we can see that in `ef49a7a` the constant was introduced and in `e01503b` it was modified.
108
+
If we look at the diff of those commits, we can see that in `ef49a7a` the constant was introduced and in `e01503b` it was modified.
108
109
109
110
If you need to be more specific, you can provide a regular expression to search for with the `-G` option.
110
111
@@ -154,6 +155,6 @@ diff --git a/zlib.c b/zlib.c
154
155
+
155
156
----
156
157
157
-
If Git can't figure out how to match a function or method in your programming language, you can also provide it a regex.
158
-
For example, this would have done the same thing: `git log -L '/unsigned long git_deflate_bound/',/^}/:zlib.c`.
158
+
If Git can't figure out how to match a function or method in your programming language, you can also provide it with a regular expression (or _regex_).
159
+
For example, this would have done the same thing as the example above: `git log -L '/unsigned long git_deflate_bound/',/^}/:zlib.c`.
159
160
You could also give it a range of lines or a single line number and you'll get the same sort of output.
0 commit comments