我需要搜索过去20 Git提交(或范围)中的文本“文本”我需要看到文件名和行号,在这里我有一个匹配项。注:我想搜索提交内容,但不想像git grep那样在提交上进行项目。通过提交内容,我指的是使用git diff HEAD^^^..HEAD我最接近的方法是使用git log --raw -GTEXT,它显示commit,commit内容中包含“text”,并显示文件名。但仍然没有行号。还有一些管道它仍然有些冗长,而且噪音很大,如果你有更好的解决办法,请回答。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 获取最后20个提交:git log -n 20将每个数组关联起来:declare -A COMMITS # Declare associative arrayCOMMITNUMBER=0while 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="commit\s[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 : 1value: commit 778f88ec8ad4f454aa5085cd0c8d51441668207c Author: Nick <[email protected]> Date: Sun Aug 7 11:43:24 2016 +0100 Second commitkey : 2value: commit a38cd7b2310038af180a548c03b086717d205a61 Author: Nick <[email protected]> Date: Sun Aug 7 11:25:31 2016 +0100 Some commit现在,您可以使用grep或任何方法搜索循环中的每一个,并匹配您需要的内容:for i in "${!COMMITS[@]}"do REGEX="Date:[\s]*Sun\sAug\7" if [[ "${COMMITS[$i]}" =~ $REGEX ]]; then echo "The following commit matched '$REGEX':" echo "${COMMITS[$i]}" fidone总共: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 1declare -A COMMITS # Declare associative arrayCOMMITNUMBER=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="commit\s[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 (adsbygoogle = window.adsbygoogle || []).push({});
07-24 13:32