bash函数找到所有的Git提交其中一个文件(文件名正则表达式匹配)已经改变* * [英] Bash function to find all Git commits in which a file (whose name matches a regex) has *changed*

查看:140
本文介绍了bash函数找到所有的Git提交其中一个文件(文件名正则表达式匹配)已经改变* *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下bash函数,该搜索所有文件的存储库,它的文件名有规律的前$匹配p $ pssion。目前,它发现其中一个文件的存在的全部提交。这怎么能说是文件中改变,因此只搜索的修改的每个提交(创建,更改或删除)?

这是我对功能的初衷。我很惊讶地看到的结果是更广泛的比预期的。我试图做到这一点的原因是:我创建了一个文件,在很久以前,在现在和当时的某一点上,我意外地从它删除了一个重要的部分。我希望所有的点(承诺),在该文件改变的列表,这样我就可以快回去包含丢失部分版本,然后将其粘贴回电流commmit版本。

 :其中;< COMMENT
    在搜索包含其名称匹配的正前pression文件当前Git仓库全部提交。    用法:GITF<正则表达式>    参数是必需的,而且必须是至少一种非空白字符。    此功能的原始版本是基于GitHub的要点
     - https://gist.github.com/anonymous/62d981890eccb48a99dc
    写的堆栈溢出用户Handyman5
     - http://stackoverflow.com/users/459089/handyman5
    这是基于这样的SO问题:
     - http://stackoverflow.com/questions/372506/how-can-i-search-git-branches-for-a-file-or-directory/372654#372654    这一功能的主要部分是由堆栈溢出用户编写
    SwankSwashbucklers。
     - http://stackoverflow.com/users/2615252/swankswashbucklers
     - http://stackoverflow.com/a/28095750/2736496    简短说明:在GITF_DESC存储
评论
#GITF_DESC:对于aliaf命令(带有'F')。必须以换行符结束。
GITF_DESC =GITF [搜索关键词]:搜索匹配常规的前pression文件名保存当前Git仓库\\ n。

正文:

  GITF(){
    #Exit如果没有提供参数(如果它是空字符串)
        参数= $(回声$ 1|装饰)
        回声$参数
        如果[-z参数$] #http://tldp.org/LDP/abs/html/comparison-ops.html
        然后
          回声缺少所需参数已取消。返回
        科幻    wasFound =0;
    LOC =参/遥控器/产地#来搜索当地的分支机构只有:'裁判/头
    REF =%(refname)
    在`的Git分支的for-each-REF --format =$ REF$ LOC`;做
        在`git的转速名单$分公司提交| grep的-OP ^ \\ {7 \\}`。做
            找到= $(GIT LS-树-r --name仅$提交| grep的$参数)
            如果[$? -eq 0];然后
                回声$ {分支#$ LOC /}:$承诺:
                而读线;做
                    回声$线
                完成< ≤(回声$找到了)
                wasFound =1;
            科幻
        DONE
    DONE    如果[$ wasFound-eq0];然后
        回声没有文件在这个库中匹配'$参数'。
    科幻
}


解决方案

如果你可以用一个shell glob模式,而不是一个全面的正则表达式的生活,考虑

  git的日志-p --diff过滤= AMD --branches --tags  - 富* bar.sh

使用 -p ,看到三角洲随着提交信息,作者,SHA1等。 - 差分滤波器= AMD 选项只选择那些提交,其中有问题的文件均 A dded, M odified,或 D eleted。要搜索的遥控器以及当地的分支机构和标签,使用 - 所有,而不是 - 分支机构--tags 。最后,请注意 - ,介绍路径模式,你将要引用允许混帐执行水珠匹配

I have the following bash function, which searches for all files in a repository, whose filename matches a regular expression. It currently finds all commits in which a file exists. How can this be changed so it only searches among files that were edited (created, altered, or deleted) in each commit?

This was my original intention for the function. I was surprised to see the results being much broader than expected. The reason I'm trying to do this: I created a file a long time ago, and at some point between now and then, I accidentally deleted an important section from it. I want a list of all the points (commits) at which this file has changed, so I can quickly go back to the version containing the missing section, and paste it back into the current-commmit version.

:<<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
    - http://stackoverflow.com/users/459089/handyman5
    which is based on this SO question:
    - http://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.
    - http://stackoverflow.com/users/2615252/swankswashbucklers
    - http://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"

Body:

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
}

解决方案

If you can live with a shell glob pattern rather than a full-blown regex, consider

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

With -p, you see the deltas along with the commit message, author, SHA1, etc. The --diff-filter=AMD option selects only those commits in which the files in question were Added, Modified, or Deleted. To search remotes as well as local branches and tags, use --all rather than --branches --tags. Finally, note the -- that introduces path patterns, which you will want to quote to allow git to perform glob matching.

这篇关于bash函数找到所有的Git提交其中一个文件(文件名正则表达式匹配)已经改变* *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆