我需要搜索过去20位提交(或范围)中的文本"text"
我需要在匹配的地方查看文件名和行号。
注:
我想搜索提交内容,但不想像git grep
那样搜索该提交的项目。
我所说的提交内容是指使用git diff HEAD^^^..HEAD
可以看到的内容
我得到的最接近的是使用git log --raw -GTEXT
,它向我显示提交内容和显示文件名中包含"TEXT"的提交。但是仍然没有行号。
还有一些管道git diff $(git log -n 20 --pretty=format:%h -GTEXT) | grep -E 'TEXT|+++|@@s'
它仍然有点冗长,噪音很大,如果你有更好的解决方案,请回答。
获取最后20个提交:
git log -n 20
将每个数组关联到一个数组:
declare -A COMMITS # Declare associative array
COMMITNUMBER=0
while read -r line; do # For each line, do
# Add 1 to COMMITNUMBER, if the current line contains "commit [0-9a-f]* (e.g., new associative array index for each commit)
# As this'll happen straight way, our array is technically 1-indexed (starts from "${COMMITS[1]}", not "${COMMITS[0]}")
REGEX="commits[0-9a-f]*"
[[ "$line" =~ $REGEX ]] && COMMITNUMBER=$(( COMMITNUMBER+1 ))
# Append the commit line to the index
COMMITS[$COMMITNUMBER]="${COMMITS[$COMMITNUMBER]} $line"
done < <(git log -n 20)
然后迭代:
for i in "${!COMMITS[@]}"
do
echo "key : $i"
echo "value: ${COMMITS[$i]}"
done
这给出了类似于以下内容的输出:
key : 1
value: commit 778f88ec8ad4f454aa5085cd0c8d51441668207c Author: Nick <nick.bull@jgregan.co.uk> Date: Sun Aug 7 11:43:24 2016 +0100 Second commit
key : 2
value: commit a38cd7b2310038af180a548c03b086717d205a61 Author: Nick <nick.bull@jgregan.co.uk> Date: Sun Aug 7 11:25:31 2016 +0100 Some commit
现在,您可以使用grep
或任何东西搜索循环中的每个,并匹配您需要的内容:
for i in "${!COMMITS[@]}"
do
REGEX="Date:[s]*SunsAug7"
if [[ "${COMMITS[$i]}" =~ $REGEX ]]; then
echo "The following commit matched '$REGEX':"
echo "${COMMITS[$i]}"
fi
done
总计:
search_last_commits() {
[[ -z "$1" ]] && echo "Arg #1: No search pattern specified" && return 1
[[ -z "$2" ]] && echo "Arg #2: Number required for number of commits to return" && return 1
declare -A COMMITS # Declare associative array
COMMITNUMBER=0
while read -r line; do # For each line, do
# Add 1 to COMMITNUMBER, if the current line contains "commit [0-9a-f]* (e.g., new associative array index for each commit)
# As this'll happen straight way, our array is technically 1-indexed (starts from "${COMMITS[1]}", not "${COMMITS[0]}")
REGEX="commits[0-9a-f]*"
[[ "$line" =~ $REGEX ]] && COMMITNUMBER=$(( COMMITNUMBER+1 ))
# Append the commit line to the index
COMMITS[$COMMITNUMBER]="${COMMITS[$COMMITNUMBER]} $line"
done < <(git log -n $2)
for i in "${!COMMITS[@]}"
do
REGEX="$1"
if [[ "${COMMITS[$i]}" =~ $REGEX ]]; then
echo "The following commit matched '$REGEX':"
echo "${COMMITS[$i]}"
fi
done
}
编辑:
用法:
search_last_commits <search-term> <last-commits-quantity>
示例:
search_last_commits "Aug" 20
如果你想在这个范围内搜索修改过的文件(并且不关心那些提交中没有实际修改的文件的匹配行),你可以运行:
git grep TEXT -- $(git diff --name-only RANGE)
最简单的方法:GIT_SEQUENCE_EDITOR=: git rebase --exec "git rev-parse HEAD; git --no-pager grep -n 'TEXT_TO_FIND'; echo " -i HEAD~20
详细信息:
git rev-parse HEAD
-显示当前sha1
git --no-pager grep -n 'TEXT_TO_FIND'
-查找文本的grep命令,您可以在这里使用任何其他命令(如常规grep
等)
echo
-在每次提交之间打印空行的命令,并确保返回值始终为OK
HEAD~20
-你想搜索多少提交(这里我做了20)
一些有用的答案:link1 link2
试试这个:
echo `git log --raw --pretty=format:%h -GTEXT | xargs | awk '{print $NF}'` ;grep -ni 'MAX_STR_LEN' $(git log --raw --pretty=format:%h -GMAX_STR_LEN | xargs | awk '{print $NF}')
这适用于我的单一提交版本。也许你可以循环n次来实现你的目标。
git grep SEARCH `git rev-list HEAD~20..HEAD`
指定您需要的范围。
结果将是每个修订的grep。也就是说,您可以在每个修订中看到所有的出现。您看不到相对于上一次修订所做的更改。(通过将您自己与上一次修订中列出的行进行比较)
而且速度很慢。因为它对范围内的每个修订都进行了完整的grep。
或者,您可以将其限制为(在范围内)与搜索词相关的更改。
git grep SEARCH `git log -G SEARCH --format=%H HEAD~20..HEAD`
虽然仅限于具有相关搜索的修订,但在每个此类修订中,您都会得到所有行。