Can I use Git to search for matching filenames in a repository?

后端 未结 6 1326
盖世英雄少女心
盖世英雄少女心 2020-12-04 12:58

Just say I have a file: \"HelloWorld.pm\" in multiple subdirectories within a Git repository.

I would like to issue a command to find the full paths of all the files

6条回答
  •  一个人的身影
    2020-12-04 13:59

    Hmm, the original question was about the repository. A repository contains more than 1 commit (in the general case at least), but the answers given before search only through one commit.

    Because I could not find an answer that really searches the whole commit history I wrote a quick brute force script git-find-by-name that takes (nearly) all commits into consideration.

    #! /bin/sh
    tmpdir=$(mktemp -td git-find.XXXX)
    trap "rm -r $tmpdir" EXIT INT TERM
    
    allrevs=$(git rev-list --all)
    # well, nearly all revs, we could still check the log if we have
    # dangling commits and we could include the index to be perfect...
    
    for rev in $allrevs
    do
      git ls-tree --full-tree -r $rev >$tmpdir/$rev 
    done
    
    cd $tmpdir
    grep $1 * 
    

    Maybe there is a more elegant way.

    Please note the trivial way the parameter is passed into grep, so it will match parts of filename. If that is not desired anchor your search expression and/or add suitable grep options.

    For deep histories the output might be too noisy, I thought about a script that converts a list of revisions into a range, like the opposite of what git rev-list can do. But so far it has remained a thought.

提交回复
热议问题