跨所有新文件和修改过的文件的Git grep(在提交之前) [英] Git grep across all new and modified files (before commit)

查看:116
本文介绍了跨所有新文件和修改过的文件的Git grep(在提交之前)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Windows Git Bash中运行什么命令,该命令将向我显示代码中写有 TODO的所有位置的文件名,行预览(上下文)和行号,仅限于新的文件和修改的文件?

What command can I run in my Windows Git Bash that will show me the file names, line preview (context), and line numbers of all of the places there is "TODO" written in my code, limited to new files and modified files?

这很笨重,不会打印行号:

This is clunky and doesn't print line number:

function __greptodo {
    QUERY="TODO"
    for FILE in `git diff --name-only`; do
        grep "$QUERY" $FILE 2>&1 >/dev/null
        if [ $? -eq 0 ]; then
            echo '———————————————'
            echo $FILE 'contains' $QUERY
            grep "$QUERY" $FILE 2>&1
        fi
    done
}
alias greptodo=__greptodo



方法2不足(摘自此处



此更好(显示上下文,包括新文件和已修改的文件),但仍不打印行号:

Inadequate Approach 2 (from here)

This is much better (shows context and includes new files and modified files) but still doesn't print line numbers:

grep -s TODO $(git ls-files -m)

推荐答案

-n 标志告诉 grep 显示行号,所以您很近。尝试:

The -n flag tells grep to show the line number, so you were close. Try:

grep -sn "TODO" $(git ls-files -m)

要包含未跟踪的文件,请使用-others -o )标志和-exclude-standard 标志以排除通常被Git忽略的文件:

To include untracked files, use the --others (-o) flag, and the --exclude-standard flag to exclude the files usually ignored by Git:

grep -sn "TODO" $(git ls-files -mo --exclude-standard)

这篇关于跨所有新文件和修改过的文件的Git grep(在提交之前)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆