TFVC Repository Migration To GIT Repository

There are two types of projects maintained in TFS (Microsoft).
  1. TFVC (Scrum)
  2. Agile
Git-tfs is an open source two-way bridge between Microsoft Team Foundation Server (TFS) and GIT, similar to git-svn. It fetches TFS commits into a GIT repository and lets you push your updates back to TFS.
 
What if you want to move your Source Control Repository and the content to GIT?
 
This functionality is provided in TFS where a person selects the Repository type and then Repository name. Then, you have to select the Repository you want to move and click on the "OK" button to migrate all the content and the branches along with the history from Source Control (TFVC) to GIT.
 
But what if a user wants to move it through the code itself? Yes, there is a way in .NET where you can migrate your repository; not only migrate but you can create your own repository.
 
In NuGet Packages, you can find a NuGet related to Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.SourceControl.WebApi.
 
You have to install the packages as well as all the dependent packages. The following code will help you in creating and migrating the repository.
  1. VssCredentials creds = new VssClientCredentials();  
  2. creds.Storage = new VssClientCredentialStorage();  
  3. VssConnection connection = new VssConnection(new Uri(c_collectionUri), creds);  
  4. GitHttpClient gitClient = connection.GetClient < GitHttpClient > ();  
  5. //The below code will Create New Repository and returns you the respository's ID  
  6. Guid targetProjectGuid = new Guid(cbp._Key);  
  7. TeamProjectReference teamProjectReference = new TeamProjectReference();  
  8. teamProjectReference.Id = targetProjectGuid;  
  9. GitRepositoryCreateOptions gitRepositoryCreateOptions = new GitRepositoryCreateOptions();  
  10. gitRepositoryCreateOptions.Name = textBox2.Text; //New Repository Name    
  11. gitRepositoryCreateOptions.ProjectReference = teamProjectReference;  
  12. var obj = gitClient.CreateRepositoryAsync(gitRepositoryCreateOptions).Result; //Create new repository in target  Project    
  13. Guid targetRepositoryGuid = obj.Id;  
  14. GitImportRequest gitImportRequest = new GitImportRequest();  
  15. GitRepository gitRepository = new GitRepository();  
  16. gitRepository.Id = targetRepositoryGuid;  
  17. gitImportRequest.Repository = gitRepository;  
  18. GitImportTfvcSource gitImportTfvcSource = new GitImportTfvcSource();  
  19. gitImportTfvcSource.Path = "$/" + cbpSource._Value.ToString();  
  20. gitImportTfvcSource.ImportHistory = false;  
  21. gitImportTfvcSource.ImportHistoryDurationInDays = 30;  
  22. GitImportRequestParameters gitImportRequestParameters = new GitImportRequestParameters();  
  23. gitImportRequestParameters.TfvcSource = gitImportTfvcSource;  
  24. gitImportRequestParameters.GitSource = null;  
  25. gitImportRequest.Parameters = gitImportRequestParameters;  
  26. var resultBack = gitClient.CreateImportRequestAsync(gitImportRequest, targetProjectGuid, targetRepositoryGuid).Result; 
The above "resultBack" will be the response after the migration of the repository and its content.
 
References Involved
  1. using Microsoft.TeamFoundation.Client;  
  2. using Microsoft.TeamFoundation.Core.WebApi;  
  3. using Microsoft.TeamFoundation.SourceControl.WebApi;  
  4. using Microsoft.TeamFoundation.WorkItemTracking.Client;  
  5. using Microsoft.VisualStudio.Services.Client;  
  6. using Microsoft.VisualStudio.Services.Common;  
  7. using Microsoft.VisualStudio.Services.WebApi;  
Next Recommended Reading C# Repository Pattern