Sunday, September 14, 2014

Create TFS Patch File With Pending Changes using TF and 7Zip

I recently had to do a TFS migration and upgrade and during that process needed a way to copy pending changes between the Production and Staging TFS servers. The goal was to be able to test the upgrade in a stage environment, modify the build scripts so that they worked using a new code structure, and then create a patch file that could be applied to the production instance of TFS. I checked online and found a solution that used the unified diff feature of the tf.exe program, but this didn’t work very well if you had to checkout or add many files as part of applying the patch. Also instead of creating a large TXT file I wanted a ZIP file that had full versions of all the files that were modified in the current workspaces.
After playing around with the tfvc commands I was able to write up a PowerShell script that would detect any pending changes in your current workspace and then use 7Zip to create a patch file with just those files. Here is an example of the Export-TFSPatch.ps1 script:
image
Here you can see it detected 10 files that were checked out in the workspace and created a zip file with just those files:
image
You can then copy that zip file to another system and use the Apply-TFSPatch.ps1 script to apply those changes to the new workspace. This includes checking out any existing files so they can be overwritten and adding any new files from the patch to the TFS workspace.
image
After applying the patch you can then use TFS to check in the pending changes on your workspace:
image
If you want to use these scripts you can find them at my CodeBlog Github repository.
The TF.exe command is very sensitive to which version of Visual Studio you used to create your workspace, so if it complains try changing the $tfexe variable to use 11.0 (VS2012) or 12.0 (VS2013).

Blog.TheG2.Net - Your guide to life in the Internet age.