在 SVN 存储库中搜索文件名

我们有一个大量的代码存储库包含数以千计的文件夹和子文件夹,我想搜索下这个存储库与文件名或一些单词。

Root folder
a\
b\
c\
d\
e\
f\ab\
f\ab\cd.txt

我想搜索 cd.txt,但是不知道它在 SVN Repository 中的哪个位置,因此我想在 SVN 的根文件夹中执行一个搜索,我将把文件名 cd.txt 放在那里,然后运行命令,检查每个文件夹并显示文件详细信息结果..。

希望的要求很明确,你能帮我一下吗。

97494 次浏览

svn list --depth infinity <your-repo-here> to get a list of files in the repo, and then svn cat to get contents. You can add --xml key to list command to make parsing a bit simpler.

If the file is in your working copy, then if you are using svn 1.5:

svn list --depth infinity | grep <filename>

or an earlier version of svn:

find . -name <filename> -not -path '*.svn*'

If you need to find the file in history (deleted or moved):

svn log -v | less

and search for it with:

\<filename><return>

The following works for me (svn version 1.4.6)

svn list -R <URL> | grep "<file_pattern>"

If you are using TortoiseSVN you can try this IF you are willing to look Project by Project - works for me:

  1. Create a blank project under your repository top level URL, call it BLANK
  2. Click on the Repo URL on left pane
  3. On the right hand pane select your BLANK project and your desired project - say trunk
  4. Right click to pop up the browser menu and select 'Compare URLs', depending on the size of your repo it may take a minute to load. But you basically get your entire project list in a 'ready-to-search' list.
  5. Enter your file name or other string in the search filter

Recently I've published my utility to list the repository, that's much faster than "svn ls --depth infinity" approach. Here're the benchmarks. It just calls a function that is available in Subversion internal API, but now accessible through a command line.

So you can run

$ svn-crawler <URL> | grep "<file_pattern>"

With access to the repo itself use (i.e on your svn host file sytem)

svnlook tree [path_to_repo] | grep [file_name]

or to search all repos (if you have mulitple repos setup).

for i in \`ls [path_to_repos_dir]`; do echo $i; svnlook tree [path_to_repos_dir]/$i | grep -i [file_or_folder_name]; done

the option --full-paths will give the full path in repo to the file (if found)

example:

for i in `ls /u01/svn-1.6.17/repos`; do echo $i; svnlook tree --full-paths /u01/svn- 1.6.17/repos/$i | grep -i somefile.txt; done

redirect output to a file if static copy is needed.
assumes using nix OS.

If your's remote repository is not huge, then an easy method is: You can do a "checkout" to get a local repository. If you are in windows machine you use "Search" or Linux machine use "Find" command.

VisualSVN Server 4.2 supports finding files by name in the web interface. Try the new feature on the demo server.

You can download VisualSVN Server 4.2.0 at https://www.visualsvn.com/server/download/pre-release/. See the Release Notes at https://www.visualsvn.com/server/download/.

enter image description here

Not sure that it's a good idea to use additional tools to filter search results like svn+grep. Such tools like grep or svn-crawler might not be available/work on Windows or other OS, you'll need to install/upgrade them.

You may solve this task using 1 single svn command with --search flag that supports glob pattern:

> svn ls -R --search "readme.md"
branches/0.13.x/readme.md
trunk/readme.md


> svn ls -R --search "*.md"
branches/0.13.x/189.md
branches/0.13.x/readme.md
trunk/189.md
trunk/readme.md

More about svn ls --search option:

> svn ls -h
list (ls): List directory entries in the repository.
usage: list [TARGET[@REV]...]


List each TARGET file and the contents of each TARGET directory as
they exist in the repository.  If TARGET is a working copy path, the
corresponding repository URL will be used. If specified, REV determines
in which revision the target is first looked up.


...


Valid options:
...
--search ARG             : use ARG as search pattern (glob syntax, case-
and accent-insensitive, may require quotation marks
to prevent shell expansion)