搜索代码中当前存在且在过去6个月内添加的术语?

huangapple go评论78阅读模式
英文:

How to search for a term currently in code AND added within the last 6 months?

问题

我想在源代码控制中搜索包含特定术语并符合以下条件的文件:

  1. 必须位于我正在搜索的分支中的最新代码版本中。因此,3个月前添加但在1个月前被删除的实例不应计入其中。

  2. 必须在过去的6个月内添加。因此,目前在代码中的实例,如果是在2020年添加的,不应计入其中。

是否有一种相当直接的方式可以使用命令行或任何可视化工具进行此类搜索?

英文:

I want to search source control for files with a specific term and with these conditions:

  1. It has to be in the latest version of the code in the branch I'm searching. So an instance that was added 3 months ago but was removed 1 month ago should NOT count.

  2. It has to have been added within the last 6 months. So an instance currently in the code that was added in 2020 should NOT count.

Is there a reasonably straightforward way to do a search like this using either the command line or any visual tools?

答案1

得分: 2

git log --all --since=6.months.ago -p --word-diff=porcelain -S term \
| awk '/^diff/ { file=substr($NF,3) } /^+/ && /term/ { print file }' | sort -u

将会获取到在过去六个月内添加了该术语的文件列表。使用 git grep -l 列出仍然包含该术语的文件,共同的条目即为您的有效载荷。如果裸子串匹配过于混乱,您可以添加 --pickaxe-regex,日志将把 term 视为正则表达式,以增加选择性。

英文:
git log --all --since=6.months.ago -p --word-diff=porcelain -S term \
| awk '/^diff/ { file=substr($NF,3) } /^+/ && /term/ { print file }' | sort -u

will get you the list of files that have had that term added in the last six months. List the files that still have it with git grep -l and the common entries are your payload. You can add --pickaxe-regex and the log will treat term as a regex so you can beef up the selectivity if the bare substring match gets too much cruft.

答案2

得分: 2

以下是您要翻译的内容:

如果您想要字符串仍然在这里,git grep 将会有所帮助
然后您可以责备 git grep 的结果
您可以根据日期进行筛选

ggb.sh:

ggb() {
    limit=$(date +%s -d '-6 months' )
    echo $limit
    git grep -n $1 | while IFS=: read i j k; do
    git blame -L $j,$j $i -p|awk -v limit="$limit" -v file="$i"  'NR==1 {commit=$1} NR==8 {date=$2} END { if (date > limit) print file " " commit}'
done
}
 ggb $1

用法:

ggb.sh "foo"

将输出包含字符串的文件名和提交记录

详情:

使用 while IFS=: read i j k; do 循环读取 git grep 的输出。

然后使用 -p 参数执行 git blame 以获得工具输出。

git blame 工具输出格式在此处进行了描述。

使用 awk 获取第一行的第一个单词 NR==1 {commit=$1}

然后获取第8行的第二个单词 NR==8 {date=$2} 以获取 committer-time

时间限制通过 date +%s -d '-6 months' 计算。

然后,路径和限制与 awk 命令一起使用,使用 -v 参数:

awk -v limit="$limit" -v file="$i"

英文:

if you want string still here, git grep will help
then you blame the result of the git grep
you filter on the date

ggb.sh:

ggb() {
    limit=$(date +%s -d '-6 months' )
    echo $limit
    git grep -n $1 | while IFS=: read i j k; do
    git blame -L $j,$j $i -p|awk -v limit="$limit" -v file="$i"  'NR==1 {commit=$1} NR==8 {date=$2} END { if (date > limit) print file " " commit}'
done
}
 ggb $1

usage:

ggb.sh "foo"

Will output filename and commit adding the string

Details:

git grep output is read with while IFS=: read i j k; do loop.

Then git blame is used with -p to get porcelain output

git blame porcelain format is described here

with awk we get the first word of the first line NR==1 {commit=$1}

then second work of line 8 NR==8 {date=$2} to get the committer-time

The time limit is computed with date +%s -d '-6 months'
then the path and the limit is used into awk command with -v:

awk -v limit="$limit" -v file="$i"

huangapple
  • 本文由 发表于 2023年6月30日 01:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定