|
1 |
| -##Finding in Git Grep |
| 1 | +## Finding with Git Grep ## |
| 2 | + |
| 3 | +Finding files with words or phrases in Git is really easy with the |
| 4 | +linkgit:git-grep[1] command. It is possible to do this with the normal |
| 5 | +unix 'grep' command, but with 'git grep' you can also search through |
| 6 | +previous versions of the project without having to check them out. |
| 7 | + |
| 8 | +For example, if I wanted to see every place that used the 'xmmap' call in |
| 9 | +my git.git repository, I could run this: |
| 10 | + |
| 11 | + $ git grep xmmap |
| 12 | + config.c: contents = xmmap(NULL, contents_sz, PROT_READ, |
| 13 | + diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 14 | + git-compat-util.h:extern void *xmmap(void *start, size_t length, int prot, int fla |
| 15 | + read-cache.c: mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
| 16 | + refs.c: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0); |
| 17 | + sha1_file.c: map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0); |
| 18 | + sha1_file.c: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 19 | + sha1_file.c: win->base = xmmap(NULL, win->len, |
| 20 | + sha1_file.c: map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, f |
| 21 | + sha1_file.c: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 22 | + wrapper.c:void *xmmap(void *start, size_t length, |
| 23 | + |
| 24 | +If I wanted to see the line number of each match as well, I can add the '-n' |
| 25 | +option: |
| 26 | + |
| 27 | + $>git grep -n xmmap |
| 28 | + config.c:1016: contents = xmmap(NULL, contents_sz, PROT_READ, |
| 29 | + diff.c:1833: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, |
| 30 | + git-compat-util.h:291:extern void *xmmap(void *start, size_t length, int prot, int |
| 31 | + read-cache.c:1178: mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_ |
| 32 | + refs.c:1345: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0); |
| 33 | + sha1_file.c:377: map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0); |
| 34 | + sha1_file.c:479: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd |
| 35 | + sha1_file.c:780: win->base = xmmap(NULL, win->len, |
| 36 | + sha1_file.c:1076: map = xmmap(NULL, *size, PROT_READ, MAP_PR |
| 37 | + sha1_file.c:2393: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd |
| 38 | + wrapper.c:89:void *xmmap(void *start, size_t length, |
| 39 | + |
| 40 | +If we're only interested in the filename, we can pass the '--name-only' option: |
| 41 | + |
| 42 | + $>git grep --name-only xmmap |
| 43 | + config.c |
| 44 | + diff.c |
| 45 | + git-compat-util.h |
| 46 | + read-cache.c |
| 47 | + refs.c |
| 48 | + sha1_file.c |
| 49 | + wrapper.c |
| 50 | + |
| 51 | +We could also see how many line matches we have in each file with the '-c' |
| 52 | +option: |
| 53 | + |
| 54 | + $>git grep -c xmmap |
| 55 | + config.c:1 |
| 56 | + diff.c:1 |
| 57 | + git-compat-util.h:1 |
| 58 | + read-cache.c:1 |
| 59 | + refs.c:1 |
| 60 | + sha1_file.c:5 |
| 61 | + wrapper.c:1 |
| 62 | + |
| 63 | +Now, if I wanted to see where that was used in a specific version of git, I |
| 64 | +could add the tag reference to the end, like this: |
| 65 | + |
| 66 | + $ git grep xmmap v1.5.0 |
| 67 | + v1.5.0:config.c: contents = xmmap(NULL, st.st_size, PROT_READ, |
| 68 | + v1.5.0:diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, |
| 69 | + v1.5.0:git-compat-util.h:static inline void *xmmap(void *start, size_t length, |
| 70 | + v1.5.0:read-cache.c: cache_mmap = xmmap(NULL, cache_mmap_size, |
| 71 | + v1.5.0:refs.c: log_mapped = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd |
| 72 | + v1.5.0:sha1_file.c: map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, |
| 73 | + v1.5.0:sha1_file.c: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd |
| 74 | + v1.5.0:sha1_file.c: win->base = xmmap(NULL, win->len, |
| 75 | + v1.5.0:sha1_file.c: map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, |
| 76 | + v1.5.0:sha1_file.c: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd |
| 77 | + |
| 78 | +We can see that there are some differences between the current lines and these |
| 79 | +lines in version 1.5.0, one of which is that xmmap is now used in wrapper.c where |
| 80 | +it was not back in v1.5.0. |
| 81 | + |
| 82 | +We can also combine search terms in grep. Say we wanted to search for where |
| 83 | +SORT_DIRENT is defined in our repository: |
| 84 | + |
| 85 | + $ git grep -e '#define' --and -e SORT_DIRENT |
| 86 | + builtin-fsck.c:#define SORT_DIRENT 0 |
| 87 | + builtin-fsck.c:#define SORT_DIRENT 1 |
| 88 | + |
| 89 | +We can also search for every file that has *both* search terms, but display |
| 90 | +each line that has *either* of the terms in those files: |
| 91 | + |
| 92 | + $ git grep --all-match -e '#define' -e SORT_DIRENT |
| 93 | + builtin-fsck.c:#define REACHABLE 0x0001 |
| 94 | + builtin-fsck.c:#define SEEN 0x0002 |
| 95 | + builtin-fsck.c:#define ERROR_OBJECT 01 |
| 96 | + builtin-fsck.c:#define ERROR_REACHABLE 02 |
| 97 | + builtin-fsck.c:#define SORT_DIRENT 0 |
| 98 | + builtin-fsck.c:#define DIRENT_SORT_HINT(de) 0 |
| 99 | + builtin-fsck.c:#define SORT_DIRENT 1 |
| 100 | + builtin-fsck.c:#define DIRENT_SORT_HINT(de) ((de)->d_ino) |
| 101 | + builtin-fsck.c:#define MAX_SHA1_ENTRIES (1024) |
| 102 | + builtin-fsck.c: if (SORT_DIRENT) |
| 103 | + |
| 104 | +We can also search for lines that have one term and either of two other terms, |
| 105 | +for example, if we wanted to see where we defined constants that had either |
| 106 | +PATH or MAX in the name: |
| 107 | + |
| 108 | + $ git grep -e '#define' --and \( -e PATH -e MAX \) |
| 109 | + abspath.c:#define MAXDEPTH 5 |
| 110 | + builtin-blame.c:#define MORE_THAN_ONE_PATH (1u<<13) |
| 111 | + builtin-blame.c:#define MAXSG 16 |
| 112 | + builtin-describe.c:#define MAX_TAGS (FLAG_BITS - 1) |
| 113 | + builtin-fetch-pack.c:#define MAX_IN_VAIN 256 |
| 114 | + builtin-fsck.c:#define MAX_SHA1_ENTRIES (1024) |
| 115 | + ... |
| 116 | + |
| 117 | + |
| 118 | + |
0 commit comments