[我承认,这有点滥用评论,但是我还不能发表评论,以为我会改善@ uwe-geuder的答案。]
#!/bin/bash
#
#
# I'm using a fixed string here, not a regular expression, but you can easily
# use a regular expression by altering the call to grep below.
name="$1"
# Verify usage.
if [[ -z "$name" ]]
then
echo "Usage: $(basename "$0") <file name>" 1>&2
exit 100
fi
# Search all revisions; get unique results.
while IFS= read rev
do
# Find $name in $rev's tree and only use its path.
grep -F -- "$name" \
<(git ls-tree --full-tree -r "$rev" | awk '{ print $4 }')
done < \
<(git rev-list --all) \
| sort -u
同样,对@ uwe-geuder +1可以得到一个很好的答案。
如果您对BASH本身感兴趣:
除非可以确保在for循环中分词(例如使用数组:),否则for item in "${array[@]}"我强烈建议while IFS= read var ; do ... ; done < <(command)在要循环的命令输出用换行符分隔时使用(或read -d''时当输出用。空字符串$'\0')。虽然git rev-list --all可以保证使用40字节的十六进制字符串(不带空格),但我从不喜欢冒险。我现在可以轻松地将命令从更改为git rev-list --all产生行的任何命令
我还建议使用内置的BASH机制来注入输入和过滤输出,而不是临时文件。