加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash函数用于查找文件(其名称与正则表达式匹配)已更改*的所有Git

发布时间:2020-12-15 22:43:12 所属栏目:安全 来源:网络整理
导读:我有 the following bash function,它搜索存储库中的所有文件,其文件名与正则表达式匹配.它当前查找文件存在的所有提交.如何更改,以便只在每次提交中编辑(创建,更改或删除)的文件中进行搜索? 这是我对该功能的初衷.我惊讶地发现结果比预期的要广泛得多.我之
我有 the following bash function,它搜索存储库中的所有文件,其文件名与正则表达式匹配.它当前查找文件存在的所有提交.如何更改,以便只在每次提交中编辑(创建,更改或删除)的文件中进行搜索?

这是我对该功能的初衷.我惊讶地发现结果比预期的要广泛得多.我之所以这样做是因为:我很久以前创建了一个文件,而且从现在到现在之间的某个时刻,我不小心删除了一个重要的部分.我想要一个包含此文件更改的所有点(提交)的列表,因此我可以快速返回包含缺失部分的版本,并将其粘贴回当前提交版本.

:<<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"

身体:

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
}

解决方法

如果您可以使用shell glob模式而不是完整的正则表达式,请考虑

git log -p --diff-filter=AMD --branches --tags -- "foo*bar.sh"

使用-p,可以看到增量以及提交消息,作者,SHA1等.– diff-filter = AMD选项仅选择那些有问题的文件被添加,修改或删除的提交.要搜索遥控器以及本地分支和标签,请使用–all而不是–branches –tags.最后,请注意 – 引入路径模式,您将引用它以允许git执行glob匹配.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读