There are two types of projects maintained in TFS (Microsoft).
- TFVC (Scrum)
- 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.
- VssCredentials creds = new VssClientCredentials();
- creds.Storage = new VssClientCredentialStorage();
- VssConnection connection = new VssConnection(new Uri(c_collectionUri), creds);
- GitHttpClient gitClient = connection.GetClient < GitHttpClient > ();
-
- Guid targetProjectGuid = new Guid(cbp._Key);
- TeamProjectReference teamProjectReference = new TeamProjectReference();
- teamProjectReference.Id = targetProjectGuid;
- GitRepositoryCreateOptions gitRepositoryCreateOptions = new GitRepositoryCreateOptions();
- gitRepositoryCreateOptions.Name = textBox2.Text;
- gitRepositoryCreateOptions.ProjectReference = teamProjectReference;
- var obj = gitClient.CreateRepositoryAsync(gitRepositoryCreateOptions).Result;
- Guid targetRepositoryGuid = obj.Id;
- GitImportRequest gitImportRequest = new GitImportRequest();
- GitRepository gitRepository = new GitRepository();
- gitRepository.Id = targetRepositoryGuid;
- gitImportRequest.Repository = gitRepository;
- GitImportTfvcSource gitImportTfvcSource = new GitImportTfvcSource();
- gitImportTfvcSource.Path = "$/" + cbpSource._Value.ToString();
- gitImportTfvcSource.ImportHistory = false;
- gitImportTfvcSource.ImportHistoryDurationInDays = 30;
- GitImportRequestParameters gitImportRequestParameters = new GitImportRequestParameters();
- gitImportRequestParameters.TfvcSource = gitImportTfvcSource;
- gitImportRequestParameters.GitSource = null;
- gitImportRequest.Parameters = gitImportRequestParameters;
- 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
- using Microsoft.TeamFoundation.Client;
- using Microsoft.TeamFoundation.Core.WebApi;
- using Microsoft.TeamFoundation.SourceControl.WebApi;
- using Microsoft.TeamFoundation.WorkItemTracking.Client;
- using Microsoft.VisualStudio.Services.Client;
- using Microsoft.VisualStudio.Services.Common;
- using Microsoft.VisualStudio.Services.WebApi;