How can I determine programatically which branch this file belongs to? I've spent 3 hours trying to figure this out with no results. I found this topic but it's not what I want: How to programmatically get information about branches in TFS?
Asked
Active
Viewed 1,880 times
5
-
Are you referring to a file in your workspace, or a file in a source control path? – John Saunders Aug 08 '11 at 14:45
-
Maybe I will describe my prblem in more details, what I want to do is to have custom policy which determine if all files in changeset come from single branch, so answering Your question I don't think this really matters as PendingChange have bot paths, LocalItem - workspace path, ServerItem - source control path. – Krzysiek Aug 09 '11 at 08:07
-
Thanks. Please add that description to your question. The more information you provide, the better. However, I'm concerned that you want to require all files in the changeset to come from a branch point higher in the source control tree than all of them. Does your source control start with "$/Main" and branch from there? – John Saunders Aug 09 '11 at 16:18
2 Answers
4
I had a very similar problem. I found a solution to it, here is the code:
...
// get all branches
VersionControlServer vcs = tfs.GetService(typeof(VersionControlServer));
BranchObject[] allBranches = vcs.QueryRootBranchObjects(RecursionType.Full);
string myItem = "$/My Project/some Path including the branch/myFile.txt";
foreach(BranchObject branch in allBranches)
{
if(myItem.Contains(branch.Properties.RootItem.Item))
{
// branch is the branch to which the item belongs! :)
}
}
...
I hope this helps someone with this problem, I think the op has already solved it (it's been a while since he asked the question).

Christian
- 4,345
- 5
- 42
- 71
2
The only way I've found to get branch info for a specific file is to use VersionControlServer.QueryBranchObjects to query each possible branch in a folder structure all the way up to the root.
However you can make a few assumptions and do it fairly efficiently - as a branch cannot be in another branch in tfs 2010. Find the common subset of paths of all files being checked in and test those, if none are branches then they don't belong to same branch.

Betty
- 9,109
- 2
- 34
- 48