加载中...

使用Git Grep进行搜索


用git grep 命令查找Git库里面的某段文字是很方便的. 当然, 你也可以用unix下的'grep'命令进行搜索, 但是'git grep'命令能让你不用签出(checkout)历史文件, 就能查找它们.

例如, 你要看 git.git 这个仓库里每个使用'xmmap'函数的地方, 你可以运行下面的命令:

  1. $ git grep xmmap
  2. config.c: contents = xmmap(NULL, contents_sz, PROT_READ,
  3. diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
  4. git-compat-util.h:extern void *xmmap(void *start, size_t length, int prot, int fla
  5. read-cache.c: mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
  6. refs.c: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
  7. sha1_file.c: map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
  8. sha1_file.c: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
  9. sha1_file.c: win->base = xmmap(NULL, win->len,
  10. sha1_file.c: map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, f
  11. sha1_file.c: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  12. wrapper.c:void *xmmap(void *start, size_t length,

如果你要显示行号, 你可以添加'-n'选项:

  1. $>git grep -n xmmap
  2. config.c:1016: contents = xmmap(NULL, contents_sz, PROT_READ,
  3. diff.c:1833: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd,
  4. git-compat-util.h:291:extern void *xmmap(void *start, size_t length, int prot, int
  5. read-cache.c:1178: mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_
  6. refs.c:1345: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
  7. sha1_file.c:377: map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
  8. sha1_file.c:479: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd
  9. sha1_file.c:780: win->base = xmmap(NULL, win->len,
  10. sha1_file.c:1076: map = xmmap(NULL, *size, PROT_READ, MAP_PR
  11. sha1_file.c:2393: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd
  12. wrapper.c:89:void *xmmap(void *start, size_t length,

如果我们想只显示文件名, 我们可以使用'--name-onley'选项:

  1. $>git grep --name-only xmmap
  2. config.c
  3. diff.c
  4. git-compat-util.h
  5. read-cache.c
  6. refs.c
  7. sha1_file.c
  8. wrapper.c

我们可以用'-c'选项,可以查看每个文件里有多少行匹配内容(line matches):

  1. $>git grep -c xmmap
  2. config.c:1
  3. diff.c:1
  4. git-compat-util.h:1
  5. read-cache.c:1
  6. refs.c:1
  7. sha1_file.c:5
  8. wrapper.c:1

现在, 如果我们要查找git仓库里某个特定版本里的内容, 我们可以像下面一样在命令行末尾加上标签名(tag reference):

  1. $ git grep xmmap v1.5.0
  2. v1.5.0:config.c: contents = xmmap(NULL, st.st_size, PROT_READ,
  3. v1.5.0:diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd,
  4. v1.5.0:git-compat-util.h:static inline void *xmmap(void *start, size_t length,
  5. v1.5.0:read-cache.c: cache_mmap = xmmap(NULL, cache_mmap_size,
  6. v1.5.0:refs.c: log_mapped = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd
  7. v1.5.0:sha1_file.c: map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd,
  8. v1.5.0:sha1_file.c: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd
  9. v1.5.0:sha1_file.c: win->base = xmmap(NULL, win->len,
  10. v1.5.0:sha1_file.c: map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd,
  11. v1.5.0:sha1_file.c: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd

我可以看到"1.5.0版"和当前版本间一些区别: 在“1.5.0版"中, xmmap没有在wrapper.c中出现.

我们也可以组合一些搜索条件, 下面的命令就是查找我们在仓库的哪个地方定义了'SORT_DIRENT'.

  1. $ git grep -e '#define' --and -e SORT_DIRENT
  2. builtin-fsck.c:#define SORT_DIRENT 0
  3. builtin-fsck.c:#define SORT_DIRENT 1

我不但可以进行“与"(both)条件搜索操作,也可以进行"或"(either)条件搜索操作.

  1. $ git grep --all-match -e '#define' -e SORT_DIRENT
  2. builtin-fsck.c:#define REACHABLE 0x0001
  3. builtin-fsck.c:#define SEEN 0x0002
  4. builtin-fsck.c:#define ERROR_OBJECT 01
  5. builtin-fsck.c:#define ERROR_REACHABLE 02
  6. builtin-fsck.c:#define SORT_DIRENT 0
  7. builtin-fsck.c:#define DIRENT_SORT_HINT(de) 0
  8. builtin-fsck.c:#define SORT_DIRENT 1
  9. builtin-fsck.c:#define DIRENT_SORT_HINT(de) ((de)->d_ino)
  10. builtin-fsck.c:#define MAX_SHA1_ENTRIES (1024)
  11. builtin-fsck.c: if (SORT_DIRENT)

我们也可以查找出符合一个条件(term)且符合两个条件(terms)之一的文件行. 例如我们要找出名字中含有‘PATH'或是'MAX'的常量定义:

  1. $ git grep -e '#define' --and \( -e PATH -e MAX \)
  2. abspath.c:#define MAXDEPTH 5
  3. builtin-blame.c:#define MORE_THAN_ONE_PATH (1u<<13)
  4. builtin-blame.c:#define MAXSG 16
  5. builtin-describe.c:#define MAX_TAGS (FLAG_BITS - 1)
  6. builtin-fetch-pack.c:#define MAX_IN_VAIN 256
  7. builtin-fsck.c:#define MAX_SHA1_ENTRIES (1024)
  8. ...

译者注: 就是"与"条件搜索和"或"条件搜索可以组合使用。


还没有评论.