Determine in which branches a changeset in present

2019-03-31 22:38发布

问题:

I need to programmatically track changesets: determine in which branch a changeset is currently located.

Update: Let's say we got three branches for a project: Dev, Test and Release. Whenever we're ready to go live with some changesets, we first merge them to Test, and then to Release as soon as the testing is done. I need to know where a given changeset is located in this workflow (only in Dev OR in Dev + merged in Test OR in Dev + merged in Test + merged in Release).

It's already possible to do this using the Visual Studio "Tracking" interface, but I need to do it programmatically to get more customizations over it. I'm already able to get all the changesets for a specific work item using the code provided here, but I've yet to find a way to determine in which branches a changeset in present.

回答1:

I just made a tool which does the exact same thing. The call you need is TrackMerges. The good news is you can check multiple branches in one call. Here's a code snippet:

var tfsCollection = new TfsTeamProjectCollection(new Uri(server), new NetworkCredential(username, password, userDomain));
tfsCollection.EnsureAuthenticated();
VersionControlServer versionControlServer = tfsCollection.GetService<VersionControlServer>();

ExtendedMerge[] merges = versionControlServer.TrackMerges(
                                  new[] {changesetIdYouAreChecking}, 
                                  new ItemIdentifier("$/Proj/Dev"), 
                                  new [] {new ItemIdentifier("$/Proj/Test"), new ItemIdentifier("$/Proj/Release")}, 
                                  null);

foreach (ExtendedMerge extendedMerge in merges)
{
    Console.WriteLine("Branch --> " + extendedMerge.TargetItem.Item);
    Console.WriteLine("Changeset --> " + extendedMerge.TargetChangeset.ChangesetId);
}

If the merges variable does not contain the branch you are looking for, then it hasn't been merged.