Git另类技巧:快速准确多文件查找 git-grep

Git另类技巧:快速准确多文件查找 git-grep

目录

介绍

使用场景很简单, 就是从多个文件中查找给定的字符串, 字符串可以以正则式表示. 主要用于在源代码中查找相关信息, 当然也可以应用于其他类型的多文件查找. 话说作者一直在寻找简单而又强大的多文件查找方法, 试过多种方法, 最近发现 git grep 可能是最符合作者要求的.

使用环境也很简单:

  • 安装好 git
  • 把要查找的文件目录用 git init 命令设置为 git 代码仓库

简单 git grep 命令

最简单的查找:显示行数

最简单的参数就是 -n, 它显示要查找字符串所在文件的行数, 具体命令如下:

git grep -n "要查找的字符串"

假设我们要在一个 git 仓库中查找函数 format 的定义在哪个文件里, 那么我们需要搜索的字符串就是 (defun format, 查找命令为:

git grep -n "(defun format "

显示结果如下:

Air:CLISP-2.49 admin$ git grep -n "(defun format "
src/format.lisp:344:(defun format (destination control-string &rest arguments)
Air:CLISP-2.49 admin$ 

表示在当前目录下的 src/format.lisp 文件的第 344 行查到了要查的信息

  • 补充:如果该文件夹还没用被设置为 git 仓库, 可以顺序执行如下命令:
Air:CLISP-2.49 admin$ git grep -n "(defun format "
fatal: Not a git repository (or any of the parent directories): .git
Air:CLISP-2.49 admin$ git init
Initialized empty Git repository in /Users/admin/LispBox-0.93/CLISP-2.49/.git/
Air:CLISP-2.49 admin$ git add .

只显示概况

使用 --count 参数, 只会显示在哪个文件里有几个要查找的字符串, 如下:

Air:CLISP-2.49 admin$ git grep --count "(defun format "
src/format.lisp:1
Air:CLISP-2.49 admin$ 

美化显示结果

  • --break
  • --heading

命令如下:

git grep --break --heading -n "(defun format "

显示结果如下:

Air:CLISP-2.49 admin$ git grep --break --heading -n "format "
build-dir/configure
7559:    lt_cv_deplibs_check_method='file_magic file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)'
7566:  lt_cv_deplibs_check_method='file_magic file format pe-arm-.*little(.*architecture: arm)?'

build-dir/libtool
3292:       $EGREP 'file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' >/dev/null; then
3741:# Convert paths to host format when used with build tools.
3810:# Convert pathlists to host format when used with build tools.
3819:# Path separators are also converted from $build format to
7522:             $ECHO "*** with $libname and none of the candidates passed a file format test"
7576:             $ECHO "*** with $libname and none of the candidates passed a file format test"
8953:     # object format with DLL files.  See the long comment at the top of

build-dir/makemake
3002:    echotab "\$(RUN) -q -M lispinit.mem -x '(progn (dolist (s (quote (*terminal-io* *standard-output* *error-output* *query-io* *debug-io* *tr
3005:        echotab "\$(RUN) -q -M lispinit.mem -x '(progn (format ${stream1} \"~&Line1 to ${stream1}\") (format ${stream2} \"~&Line2 to ${stream2
3009:    echotab "\$(RUN) -q -M lispinit.mem -x '(progn (dolist (s (quote (*terminal-io* *standard-output* *error-output* *query-io* *debug-io* *tr
3012:        echotab "\$(RUN) -q -M lispinit.mem -x '(progn (format ${stream1} \"~&Line1 to ${stream1}\") (format ${stream2} \"~&Line2 to ${stream2
3994:# without changing the bytecode format and the tables in

不带这两个参数的显示结果如下:

Air:CLISP-2.49 admin$ git grep -n "format "
build-dir/configure:7559:    lt_cv_deplibs_check_method='file_magic file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)'
build-dir/configure:7566:  lt_cv_deplibs_check_method='file_magic file format pe-arm-.*little(.*architecture: arm)?'
build-dir/libtool:3292:       $EGREP 'file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' >/dev/null; then
build-dir/libtool:3741:# Convert paths to host format when used with build tools.
build-dir/libtool:3810:# Convert pathlists to host format when used with build tools.
build-dir/libtool:3819:# Path separators are also converted from $build format to
build-dir/libtool:7522:           $ECHO "*** with $libname and none of the candidates passed a file format test"
build-dir/libtool:7576:           $ECHO "*** with $libname and none of the candidates passed a file format test"
build-dir/libtool:8953:   # object format with DLL files.  See the long comment at the top of
build-dir/makemake:3002:    echotab "\$(RUN) -q -M lispinit.mem -x '(progn (dolist (s (quote (*terminal-io* *standard-output* *error-output* *query
build-dir/makemake:3005:        echotab "\$(RUN) -q -M lispinit.mem -x '(progn (format ${stream1} \"~&Line1 to ${stream1}\") (format ${stream2} \"~
build-dir/makemake:3009:    echotab "\$(RUN) -q -M lispinit.mem -x '(progn (dolist (s (quote (*terminal-io* *standard-output* *error-output* *query

使用举例

查找函数上下文

  • -W 查找函数上下文

还以上面的查找 format 函数定义的情况为例, 如果想查找 format 函数的上下文, 可以理解为就是查找函数 format 完全的定义代码--虽然实际上会把文件内容全部显示.

命令如下:

git grep -nW "(defun format "

显示如下:

Air:CLISP-2.49 admin$ git grep -nW "(defun format "
src/format.lisp-1-;; FORMAT - and company.
src/format.lisp-2-;; Bruno Haible 22.06.1988
src/format.lisp-3-;; CLISP-Version 16.08.1988, 03.09.1988, 04.08.1989
src/format.lisp-4-;; Major revision by Bruno Haible  14.02.1990-15.02.1990
src/format.lisp-5-;; Further revised and wrote FORMATTER 9.4.1995-11.4.1995
src/format.lisp-6-;; German comments translated into English: Stefan Kain 2001-09-09
src/format.lisp-7-;; formatter pprint-logical-block ~:> support: John Boyland 2003
src/format.lisp-8-;; Sam Steingold 1999-2009
src/format.lisp-9-
src/format.lisp-10-;; FORMAT is a mechanism for producing string output conveniently by,
src/format.lisp-11-;; basically, taking a pre-determined string with placeholders and
src/format.lisp-12-;; substituting computed values or strings for those placeholders --
src/format.lisp-13-;; though it became much more complex than this because the placeholders
src/format.lisp-14-;; included iteration primitives for producing lists of results,
src/format.lisp-15-;; plurals, and other such exotica.  It may be loosely characterized as
src/format.lisp-16-;; FORTRAN FORMAT statements gone berserk.
src/format.lisp-17-;; -- Guy L. Steele Jr. and Richard P. Gabriel in "The Evolution of Lisp"
src/format.lisp-18-
src/format.lisp-19-(in-package "SYSTEM")
src/format.lisp-20-
src/format.lisp-21-;;; ---------------------------------------------------------------------------
src/format.lisp-22-
......

更全的参数

可以使用 git help grep 来查看帮助

转载于:https://my.oschina.net/freeblues/blog/626334

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值