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

时间:2015-01-23 21:57:07

标签: git bash

我有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
}

4 个答案:

答案 0 :(得分:4)

使用 git diff-tree -r --name-only --no-commit-id (可能使用--stdin)代替git ls-tree -r --name-only。如果您对合并感兴趣,请使用-m-c -M-C如果您想分别考虑,重命名和复制检测。

或者更好地解析git diff-tree -r的输出。

的Nb。有问题的代码是非常不理想的(其中包括你多次检查相同的提交)。

答案 1 :(得分:3)

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

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

使用-p,您可以看到增量以及提交消息,作者,SHA1等。--diff-filter=AMD选项仅选择那些有问题的文件为 A <的提交 dded, M odified,或 D eleted。要搜索遥控器以及本地分支和标签,请使用--all而不是--branches --tags。最后,请注意引入路径模式的--,您将引用它以允许git执行全局匹配。

答案 2 :(得分:2)

您可以通过并使用git diff来查看每个提交之间的更改。像这样:

for branch in `git for-each-ref --format="$ref" $LOC`;
do
    previous_commit=""
    for commit in `git rev-list $branch | grep -oP ^.\{7\}`;
    do
        if [ "$previous_commit" != "" ];
        then
            found=$(git diff --name-only $previous_commit $commit | grep "$param")
            if [ $? -eq 0 ];
            then
                echo "${branch#$LOC/}: $commit:"
                while read line;
                do
                    echo "  $line"
                done < <(echo "$found")
                echo
                wasFound="1";
            fi
        fi
        previous_commit="$commit"
    done
done

答案 3 :(得分:1)

我想出了这个基于Greg Bacon's answer的函数。我本来想要正则表达式,但是很好地适应了这个法案。我还期望需要一个循环函数,但只需要单git log行。

首先,实用功能:

#https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable#comment21953456_3232433
alias trim="sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'"

文档标题:

:<<COMMENT
   Searches all commits in the current git repository containing a file
   that has *changed*, whose name matches a glob. If the glob does not
   contain any asterisks, then it is surrounded by them on both sides.


   Usage:
      gitf "05"     #Equivalent to "*05*"
      gitf "05_*"

   Parameter is required, and must be at least one non-whitespace character.

   See:
   - https://stackoverflow.com/questions/28119379/bash-function-to-find-all-git-commits-in-which-a-file-whose-name-matches-a-rege/28120305
   - https://stackoverflow.com/questions/28094136/bash-function-to-search-git-repository-for-a-filename-that-matches-regex/28095750
   - https://stackoverflow.com/questions/372506/how-can-i-search-git-branches-for-a-file-or-directory/372654#372654

   The main "git log" line is based on this answer
   - https://stackoverflow.com/a/28119940/2736496
   by Stack Overflow user Greg Bacon
   - https://stackoverflow.com/users/123109/greg-bacon

   With thanks to SwankSwashbucklers
   - https://stackoverflow.com/users/2615252/swankswashbucklers

   Short description: Stored in GITF_DESC
COMMENT
#GITF_DESC: For "aliaf" command (with an 'f'). Must end with a newline.
GITF_DESC="gitf [glob]: Searches all commits in the current git repository containing a file that has *changed*, whose name matches a glob.\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

   #https://stackoverflow.com/questions/229551/string-contains-in-bash/229606#229606
   if [[ $param != *"*"* ]]
   then
     param="*$param*"
   fi

   echo "Searching for \"$param\"..."

   git log -p --name-only --oneline --diff-filter=AMD --branches --tags -- "$param"
}

示例输出:

$ gitf 05_
05_
Searching for "*05_*"...
14e5cdd Quick save (no message): 01-21-2015__14_36_11
non_django_files/wordpress_posts/templates/05_login_remember_me.html
2efdeb1 Part four final. Changed auth/tests in post to auth/tests_login_basic.
non_django_files/wordpress_posts/templates/05_login_remember_me.html
526ca01 Part four final. Renamed auth/tests to test_basic_login, so Java doesn't need to parse the py file in future par
non_django_files/wordpress_posts/templates/05_login_remember_me.html
7c227f3 Escaped unescaped dollar-signs in initial_script_sh snippet, and added delete-all-but-.git command in comment at
non_django_files/wordpress_posts/templates/05_login_remember_me.html
e68a30a Part four final, moved post output folder into wordpress_posts.
non_django_files/wordpress_posts/templates/05_login_remember_me.html
3c5e4ec Part two final. Corrections/minor changes to all posts.
non_django_files/wordpress_posts/templates/05_login_remember_me.html
3a7dac9 Finished part one.
non_django_files/wordpress_posts/templates/05_login_remember_me.html
f87540e Initial commit
non_django_files/wordpress_posts/templates/05_login_remember_me.html