我有以下bash函数,它搜索存储库中的所有文件,其文件名与正则表达式匹配.它当前查找文件存在的所有提交.如何更改,以便只在每次提交中编辑(创建,更改或删除)的文件中进行搜索?
这是我对该功能的初衷.我惊讶地发现结果比预期的要广泛得多.我之所以这样做是因为:我很久以前创建了一个文件,而且从现在到现在之间的某个时刻,我不小心删除了一个重要的部分.我想要一个包含此文件更改的所有点(提交)的列表,因此我可以快速返回包含缺失部分的版本,并将其粘贴回当前提交版本.
:<<COMMENT
Searches all commits in the current git repository containing a file whose name matches a regular expression.
Usage: gitf <regex>
Parameter is required, and must be at least one non-whitespace character.
The original version of this function was based on the GitHub gist
- https://gist.github.com/anonymous/62d981890eccb48a99dc
written by Stack Overflow user Handyman5
- https://stackoverflow.com/users/459089/handyman5
which is based on this SO question:
- https://stackoverflow.com/questions/372506/how-can-i-search-git-branches-for-a-file-or-directory/372654#372654
The main section of this function was authored by Stack Overflow user
SwankSwashbucklers.
- https://stackoverflow.com/users/2615252/swankswashbucklers
- https://stackoverflow.com/a/28095750/2736496
Short description: Stored in GITF_DESC
COMMENT
#GITF_DESC: For "aliaf" command (with an 'f'). Must end with a newline.
GITF_DESC="gitf [searchterm]: Searches the current git repository for the file name that matches a regular expression.\n"
Run Code Online (Sandbox Code Playgroud)
身体:
gitf() {
#Exit if no parameter is provided (if it's the empty string)
param=$(echo "$1" | trim)
echo "$param"
if [ -z "$param" ] #http://tldp.org/LDP/abs/html/comparison-ops.html
then
echo "Required parameter missing. Cancelled"; return
fi
wasFound="0";
LOC=refs/remotes/origin # to search local branches only: 'refs/heads'
ref="%(refname)"
for branch in `git for-each-ref --format="$ref" $LOC`; do
for commit in `git rev-list $branch | grep -oP ^.\{7\}`; do
found=$(git ls-tree -r --name-only $commit | grep "$param")
if [ $? -eq 0 ]; then
echo "${branch#$LOC/}: $commit:"
while read line; do
echo " $line"
done < <(echo "$found")
wasFound="1";
fi
done
done
if [ "$wasFound" -eq "0" ]; then
echo "No files in this repository match '$param'."
fi
}
Run Code Online (Sandbox Code Playgroud)
如果您可以接受 shell glob 模式而不是成熟的正则表达式,请考虑
git log -p --diff-filter=AMD --branches --tags -- "foo*bar.sh"
Run Code Online (Sandbox Code Playgroud)
使用-p,您可以看到增量以及提交消息、作者、SHA1 等。该选项仅选择添加、修改或删除--diff-filter=AMD相关文件的提交。要搜索远程以及本地分支和标签,请使用而不是。最后,请注意引入路径模式的 ,您需要引用该路径模式以允许 git 执行全局匹配。--all--branches --tags--
| 归档时间: |
|
| 查看次数: |
347 次 |
| 最近记录: |