I have the following function that is being called in a loop on two new projects to add to source control. Each iteration of the loop gets the source code, copies it to the folder, creates a tfs team project, creates a workspace for that project, and then tries to add the code to source control.
static void Main(string[] args) {
var tfsWorkItems = _<IWorkItems>();
var workItems = tfsWorkItems.GetProjectsToMigrate();
var tfs = _<ITfs>();
var logFilePath = new DirectoryInfo("C:\\log");
var workingDirectory = new DirectoryInfo("C:\\m");
Cleanup(workItems, tfs, logFilePath, workingDirectory);
var svn = _<ISvn>();
var app = _<IApplication>();
foreach (var workItem in workItems)
{
var root = Path.Combine(workingDirectory.FullName, workItem.Id.ToString());
var svnBase = Path.Combine(root, "s");
var localWorkspacePath = Path.Combine(root, "t");
var tfsBase = Path.Combine(localWorkspacePath, workItem.TfsProjectName, "Main");
var tfsProject = workItem.ProjectType.ToLower() == "php" ? Path.Combine(tfsBase, "src")
: tfsBase;
svn.CheckoutFromSvn(workItem.SvnLocation, svnBase);
app.CopyToTfsFolderStructure(svnBase, tfsProject);
tfs.CreateTeamProject(workItem.TfsProjectName, logFilePath);
tfs.CreateWorkspace(workItem.WorkspaceName, localWorkspacePath);
tfs.AddToSourceControl(workItem.WorkspaceName, localWorkspacePath, workItem.TfsProjectName);
}
}
There are two projects. The first project works correctly, but the second does not. The second project creates the project and workspace, but in AddToSourceControl
public void AddToSourceControl(string workspaceName, string localPath, string projectName) {
var tfs = new TfsTeamProjectCollection(_collection);
var vcs = tfs.GetService<VersionControlServer>();
var user = vcs.AuthorizedUser;
var workspace = vcs.GetWorkspace(workspaceName, user);
var serverPath = workspace.GetServerItemForLocalItem(Path.Combine(localPath, projectName, "Main"));
var itemSpec = new ItemSpec[] {
new ItemSpec(serverPath, RecursionType.Full)
};
workspace.PendAdd(serverPath, true);
// doesn't return anything
var pendingSets = vcs.QueryPendingSets(
itemSpec, workspace.Name, user, true);
var pendingChanges = pendingSets.Aggregate(new List<PendingChange>(), (acc, item) => {
acc.AddRange(item.PendingChanges);
return acc;
});
var parameters = new WorkspaceCheckInParameters(pendingChanges, "svn to tfs migration") {
OverrideGatedCheckIn = ((CheckInOptions2)vcs.SupportedFeatures & CheckInOptions2.OverrideGatedCheckIn) == CheckInOptions2.OverrideGatedCheckIn,
PolicyOverride = new PolicyOverrideInfo("migration triggered check-in", null),
SuppressEvent = true,
};
workspace.CheckIn(parameters);
}
workspace.PendAdd(serverPath, true)
always returns zero for the second project, regardless of which project is second. The first project always completes properly. It doesn't matter which project is second. The second project always returns zero items. I obviously want all projects to be added to source control correctly. What is going on here?
I have figured out a way around this problem. I have to create a separate console application that contained the code in the AddToSourceControl
method and call the console application via the System.Diagnostics.Process
class. Something internally in the TFS API must not be getting released properly until the whole entire program exits.