У меня следующая функция 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 Qaru user Handyman5
- https://stackoverflow.com/users/459089/handyman5
which is based on this SO question:
- https://stackoverflow.com/info/372506/how-can-i-search-git-branches-for-a-file-or-directory/372654#372654
The main section of this function was authored by Qaru user
SwankSwashbucklers.
- https://stackoverflow.com/users/2615252/swankswashbucklers
- /info/22704/bash-function-to-search-git-repository-for-a-filename-that-matches-regex/166158#166158
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"
Тело
gitf() {
#Exit if no parameter is provided (if it 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
}