Tuesday, September 21, 2010

Importing a TFS repository into a Git repository using Powershell

Git is a snapshot based system. It takes a snapshot of your entire tree every time you make a commit. The individual changes between commits can be derived from the state of the snapshots.
This simple system makes it easy to recreate an existing Team Foundation Server repository (or any other version control system) into a new Git repository. If you are able to replay the history from a Team Foundation Server repository, you can use this to create a new history in a Git repository.
Enter TFS2GIT.
TFS2GIT is a Powershell script that reads the history of a TFS repository, replays this history in a workspace and finally adds this to a Git repository.
  1. Powershell
  2. Access to the Team Foundation commandline tools (type ‘tf’ in a commandline)
  3. Access to msysgit (type ‘git –-version’ in a commandline).
To import the history of the repository $/Trunk/ApplicationFoo, use:
.\TFS2GIT.ps1 $/Trunk/ApplicationFoo

Modifying it for your own needs

The script uses a regular expression to filter all the changeset numbers out of the ‘tf history’ command. Because this script originated out of my specific need to import our own TFS repositories the regular expression will not work for your specific situation.
EDIT: The script has been updated and should work for almost all situations now.
The regular expression can be found in the GetChangeSetsFromHistory function.
Our history is as follows (part of the useraccounts / comments removed to protect the innocent).

I used the regular expression \d{1,5}(?=\s{5}BC2SC) to retrieve the changeset numbers. In the future, I’ll update it to a more generic version.
This is my first attempt at using Powershell. Feel free to send me any enhancements through the usual channels at the excellent Github site.