Wednesday, January 20, 2010

Quickly check out all files in TFS that contain a certain string

We use Team Foundation Server at my place of gainful employment for source control and bug tracking.  It's adequate for both tasks, but isn't what I would prefer.  One of the reasons I don't really care for it is that it doesn't integrate well with most of the tools I use.

If I'm working on the C++ dll then it actually works rather well, as I'm already using Visual Studio as my IDE.  Any time I need to change a file it automatically checks it out.  Sadly I don't get any of that automation when working in netbeans or flex builder (yes, there's an eclipse plugin for TFS, but it's $200 and I'm not quite _that_ annoyed with it).

Today I needed to refactor a class in my Flex code that's used in a number of places.  The refactor included moving the class to a new package and renaming it.  FlexBuilder could handle the refactor, but couldn't handle checking out all the files it needed to modify.  Doing so manually was also annoying, as I'd first have to grep for all the files it would need to change and then find them in TFS to check them out.

My first thought was to just open up cygwin and write a nice one-line command to find and check out all the files.  Here's what I tried:

grep -l -r 'ClassIWantedToRename' . | xargs -L1 tf edit

Sadly, the tf command had problems resolving the paths that cygwin was passing it, so that didn't work. Then I remembered I have powershell installed.  I figured powershell should have the same basic capabilities, so I started poking at that.

As someone who has only used the most basic powershell commands before now I was pretty much starting from scratch.  I couple google searches for grep told me to either use FINDSTR or Select-String, though I had trouble getting either of those to work.

Eventually I got the solution (huge thanks to Keith Hill on stackoverflow). Here's the powershell version of that bash command above:

gci -r | Select-String 'ClassIWantedToRename' | %{tf edit $_.Path}

Powershell command to quickly check out all files from TFS that contain a particular string:

No comments:

Post a Comment