Skip to content

Commit 5fe7c26

Browse files
authored
Merge pull request #902 from rpjday/searching
Minor aesthetic/rewording changes to "Searching" section in Chapter 7
2 parents 098963b + fa326bb commit 5fe7c26

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

book/07-git-tools/sections/searching.asc

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[[_searching]]
22
=== Searching
33

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.
55
Git provides a couple of useful tools for looking through the code and commits stored in its database quickly and easily.
66
We'll go through a few of them.
77

88
[[_git_grep]]
99
==== Git Grep
1010

1111
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.
1313

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:
1616

1717
[source,console]
1818
----
@@ -21,45 +21,46 @@ compat/gmtime.c:3:#undef gmtime_r
2121
compat/gmtime.c:8: return git_gmtime_r(timep, &result);
2222
compat/gmtime.c:11:struct tm *git_gmtime_r(const time_t *timep, struct tm *result)
2323
compat/gmtime.c:16: ret = gmtime_r(timep, result);
24-
compat/mingw.c:606:struct tm *gmtime_r(const time_t *timep, struct tm *result)
25-
compat/mingw.h:162:struct tm *gmtime_r(const time_t *timep, struct tm *result);
26-
date.c:429: if (gmtime_r(&now, &now_tm))
27-
date.c:492: if (gmtime_r(&time, tm)) {
28-
git-compat-util.h:721:struct tm *git_gmtime_r(const time_t *, struct tm *);
29-
git-compat-util.h:723:#define gmtime_r git_gmtime_r
24+
compat/mingw.c:826:struct tm *gmtime_r(const time_t *timep, struct tm *result)
25+
compat/mingw.h:206:struct tm *gmtime_r(const time_t *timep, struct tm *result);
26+
date.c:482: if (gmtime_r(&now, &now_tm))
27+
date.c:545: if (gmtime_r(&time, tm)) {
28+
date.c:758: /* gmtime_r() in match_digit() may have clobbered it */
29+
git-compat-util.h:1138:struct tm *git_gmtime_r(const time_t *, struct tm *);
30+
git-compat-util.h:1140:#define gmtime_r git_gmtime_r
3031
----
3132

32-
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.
3334

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:
3536

3637
[source,console]
3738
----
3839
$ git grep --count gmtime_r
3940
compat/gmtime.c:4
4041
compat/mingw.c:1
4142
compat/mingw.h:1
42-
date.c:2
43+
date.c:3
4344
git-compat-util.h:2
4445
----
4546

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:
4748

4849
[source,console]
4950
----
5051
$ 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,
5253
date.c: if (gmtime_r(&now, &now_tm))
5354
date.c=static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
5455
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 */
5558
----
5659

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).
5861

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):
6364

6465
[source,console]
6566
----
@@ -95,7 +96,7 @@ As we saw in the above example, we looked for terms in an older version of the G
9596
Perhaps you're looking not for _where_ a term exists, but _when_ it existed or was introduced.
9697
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.
9798

98-
If we 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.
99100

100101
[source,console]
101102
----
@@ -104,7 +105,7 @@ e01503b zlib: allow feeding more than 4GB in one go
104105
ef49a7a zlib: zlib can only process 4GB at a time
105106
----
106107

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.
108109

109110
If you need to be more specific, you can provide a regular expression to search for with the `-G` option.
110111

@@ -154,6 +155,6 @@ diff --git a/zlib.c b/zlib.c
154155
+
155156
----
156157

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`.
159160
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

Comments
 (0)