击功能搜索Git仓库为正则表达式匹配的文件名 [英] Bash function to search git repository for a filename that matches regex

查看:1891
本文介绍了击功能搜索Git仓库为正则表达式匹配的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现 Handyman5的要点(这是对的这个答案,在<一个href=\"http://stackoverflow.com/questions/372506/how-can-i-search-git-branches-for-a-file-or-directory\">this问题)在我的.bashrc文件bash函数。

I'm attempting to implement Handyman5's gist (which is a comment on this answer, in this question) as a bash function in my .bashrc file.

:<<COMMENT
   Searches all commits in the current git repository for a file that matches a regular expression.

   Usage: gitf <regex>

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

   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

   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()  {
   set +x
   #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
      found=$(git ls-tree -r --name-only $branch | grep "$param")
      if [ $? -eq 0 ]; then
         echo "${branch#$LOC/}: $ref:"; echo " $found"
         wasFound="1";
      fi
   done

   if ["$wasFound" -eq 0]; then
      echo "No files in this repository match '$param'."
   fi
   set -x
}

我试图把它打印出来提交它在,这样我就可以迅速检查出来。但它只是打印出的文件路径,尽管回声。任何指导我缺少的是什么吗?

I'm trying to get it to print out the commit it's in, so I can quickly check it out. But it's only printing out the file path, despite the echos. Any guidance to what I'm missing?

GITF05电流输出:

$ gitf "05"
+ gitf 05
+ set +x
05
master:
non_django_files/wordpress_posts/build_java/BuildPart05.java
non_django_files/wordpress_posts/templates/05_login_remember_me.html
[1: command not found
++ history -a

我是pretty确保最后两行是一个不相关的.bashrc或者.inputrc文件的问题,但我包括他们在情况下,他们是相关的。

I'm pretty sure the last two lines are an unrelated .bashrc or .inputrc issue, but I'm including them in case they're relevant.

@SwankSwashbucklers:谢谢你基本上是做我的工作,我(我是新来的这两个Bash和Git的,所以我说,真正的)。输出是现在很清楚,有用的。我也注意到,它缺少一些点击。这里是一个<一个href=\"http://aliteralmind.com/docs/computer/programming/z_other/stackoverflow_post_data/djauth_lifecycle_final.zip\"相对=nofollow>例如小库(2MB ZIP)。搜索05与响应

@SwankSwashbucklers: Thank you for basically doing my work for me (I'm new to both Bash and Git, so I say that genuinely). The output is now very clear and useful. I do notice that it's missing some hits. Here is a small repository (2mb zip) for instance. Searching for "05" responds with

$ gitf 05
05
master: 14e5cdd:
  non_django_files/wordpress_posts/build_java/BuildPart05.java
  non_django_files/wordpress_posts/templates/05_login_remember_me.html
master: 2efdeb1:
  non_django_files/wordpress_posts/build_java/BuildPart05.java
  non_django_files/wordpress_posts/templates/05_login_remember_me.html

这两个提交的最近的两个,如果这意味着什么。 05_login_remember_me.html 也存在于至少 f87540e (最先提交), 3a7dac9 3c5e4ec

These two commits are the most recent two if that means anything. 05_login_remember_me.html also exists in at least f87540e (the very first commit), 3a7dac9, and 3c5e4ec.

推荐答案

,因为它现在似乎只是透过树枝被搜索的脚本。这听起来像你希望它透过树枝和这些分支的所有提交搜索。要做到这一点,你需要为循环添加另一个脚本,通过在每个你虽然搜索分支的迭代提交。事情是这样的:

The script as it is right now just seems to be searching through the branches. It sounds like you want it to search through the branches and all the commits on those branches. To do that you need to add another for loop to your script that iterates through the commits on each of the branches you are searching though. Something like this:

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:"; echo " $found"
            wasFound="1";
        fi
    done
done

您还可以清理你的脚本的输出通过缩进的路径的文件,并通过在不同的提交间增加空间。像这样的:

You could also clean up the output of your script by indenting the paths to the files, and by adding space in between the different commits. Like this:

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];

And, as mentioned in the comments, you need to add space after and before the brackets of your if statement. Like this: if [ "$wasFound" -eq 0 ];

这篇关于击功能搜索Git仓库为正则表达式匹配的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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