Saturday, June 23, 2007

Shelve all pending changes

One of the major advantages of the "Shelf" feature in Team System is the ability to backup all your current code the the central server for backup, even before checking in complete changes.
To do this programmatically you first need to get the name of the TFS server, and get the version control service:

TeamFoundationServer tfs = new TeamFoundationServer(tfsName);
VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));


Now you can call the VersionControlServer.QueryWorkspaces method, which receives 3 parameters (WorkspaceName, WorkspaceOwner, WorkspaceComputer) when each of them (or all) may be null.
Using the workspace you need to call the Shelve method, supplying the pending changes with the GetPendingChanges method.

Here is the complete code:

public static void ShelveAll(string tfsName, string workspaceName, string workspaceOwner, string workspaceComputer)
{
TeamFoundationServer tfs = new TeamFoundationServer(tfsName);
VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
foreach (Workspace ws in versionControl.QueryWorkspaces(workspaceName, workspaceOwner, workspaceComputer))
{
PendingChange[] changes = ws.GetPendingChanges();
if (changes.Length > 0)
{
Shelveset set = new Shelveset(versionControl, DateTime.Now.ToString("yyyyMMdd HHmm"), ws.OwnerName);
ws.Shelve(set, ws.GetPendingChanges(), ShelvingOptions.None);
}
}
}

No comments: