可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question is similar to: Find in Files: Search all code in Team Foundation Server However it was asked 4 years ago. I'm asking specifically about TFS 2010 with VS 2010.
Has Microsoft finally got around to adding back the searching of files for code snippets like they had way back in Visual Source Safe? If so, how to enable it and use it?
I've tried adding in Microsoft's Power Tools. While it has a search feature, the search is only for file names and ignores looking in the content in the files. :(
回答1:
I had the same problem and found this thread whilst looking for the solution. I then found that this functionality is available in a Visual Studio 2010 add-on called 'TFS Administrators Toolkit' in the Visual Studio Gallery. It seems to work!
MSDN links:
TFS Administrators Toolkit for VS 2010
TFS Administrators Toolkit for VS 2012
TFS Administrators Toolkit for VS 2013
回答2:
There's nothing inbuilt, and it would be difficult to do this at the database level due to the way that TFS stores files as deltas from previous versions.
The way I see it, there are two viable options, but both only work for the "tip" revision of the code. The easiest way is to do a "get" of all of your source, then use either the findstr
command in a command window, or use something like Windows Grep to do the searching.
Alternatively, you could do what one of our more enterprising developers did, and use Lucene to index your code, then put a nice front-end around the results. We have this set up to get our MAIN branch and DEV branch on a regular basis, so we can get near-real-time search results.
There are also products like CAST which do impact analysis, and allow you to do some degree of searching, but at that point, you're looking at dropping some decent gold pieces to get the product.
回答3:
I'm not aware of anything that will do this.
The SourceSafe storage approach of 20 years ago was very basic, and the databases were limited to what we would now regard as "tiny" amounts of data. TFS stores compact differences between file versions, and there is no longer any "great big lump of text" that anyone can trivially search.
Any such tool would need to Get every version of every file to rebuild the original text to be searched, which would hammer your server for days or weeks. Add the testing overhead of producing a robust tool, and the chance of many people actually needing it, and it's not hard to see why it may never reach the top of the MS priority list - especially when they are still working on getting basic features like a usable pending checkins window, merge tools, and hopefully renaming of files(!) working for VS11.
It could be argued that with the automation model, or even just a console app calling tf.exe, writing a few lines of code yourself to iterate through all the versions of every file and apply a RegEx would be a relatively trivial task, if you really needed to apply such a search.
回答4:
In case you do not want to roll your own solution, I've worked with Krugle for TFS before (only testing and evaluating it) at a previous company and found it to be a very good solution.
回答5:
I know how I did it years ago using PVCS Version manager. I had a trigger on checkin and add archive. This queued up a worker process that would pull the source code and use the cygwin / unix command updatedb to create an index of all the files. The building of the database would go to a temp database and then swap out the read database super fast once it was built.
Then I had a website with a search function that called locatedb to pull back the names of files that contained the text.
TFS has subscriptions that can invoke webserivces.
回答6:
I check out the entire library (Latest Revision) and run this Powershell script:
C:\Users\username>powershell
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.
PS C:\Users\username> cd C:\LocalDevelopment
PS C:\LocalDevelopment> get-childitem -include *.* -rec | select-string -pattern "a string" > a_string.txt
which is: Do a recursive search on all files in c:\LocalDevelopment for "a string" (add your string there, of course) and output the results to a text file.
回答7:
TFS doesn't provide that function out of the box, but you can use Powershell to combine existing TFS functions to get what you need.
My plan is to first use tf dir
get a list of files, then use tf view
to get files contents and finally feed the contents to Select-String
to find the string (or regex) we are looking for.
You can start with:
tf dir /recursive $/
but that list is probably going to be huge, so try to restrict your search to a small scope, like:
tf dir /recursive $/some/path/*.cs
Next step is to transform the results of the previous step into a format that we can then feed into tf view
. Here is a powershell script that does that:
(tf dir /recursive $/) -join "+" -replace ":", "" -replace "\+\+", "`n" -split "`n" | %{ $arr = $_ -split "\+"; $arr | select -Skip 1 | %{ $arr[0] + '/' + $_ } }
Now let's pipe the list into tf view
and then Select-String
:
... | %{ $file = $_ ; if (tf view "$file" /console | Select-String "some string") { Write-Host $file } }
Don't forget to change the part that says "some string"
.
Altogether, you get:
(tf dir /recursive $/) -join "+" -replace ":", "" -replace "\+\+", "`n" -split "`n" | %{ $arr = $_ -split "\+"; $arr | select -Skip 1 | %{ $arr[0] + '/' + $_ } } | %{ $file = $_ ; if (tf view "$file" /console | Select-String "some string") { Write-Host $file } }
Oh, and don't forget to replace the part that says "some string"
with your actual search query.