Skip to content

Instantly share code, notes, and snippets.

@tbolon
Created June 11, 2015 12:33
Show Gist options
  • Save tbolon/dc8f924738f6eadf1dff to your computer and use it in GitHub Desktop.
Save tbolon/dc8f924738f6eadf1dff to your computer and use it in GitHub Desktop.
custom tf vc branch program
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TfsBranchExplorer
{
class Program
{
static VersionControlServer vc;
static void Main(string[] args)
{
var collection = Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection"));
vc = collection.GetService<VersionControlServer>();
// ex: TfsBranchExplorer.exe $/TestTfs/Main
DisplayBranchHistory(args[0]);
}
private static void DisplayBranchHistory(string itemSpec)
{
int num;
string str;
VersionSpec[] versionSpecArray;
VersionSpec.ParseVersionedFileSpec(itemSpec, null, out num, out str, out versionSpecArray);
var spec = new ItemSpec(str, RecursionType.None, num);
var branchHistory = vc.GetBranchHistory(new[] { spec }, VersionSpec.Latest);
for (int i = 0; i < (int)branchHistory.Length; i++)
{
var itemArray = branchHistory[i];
if (itemArray.Length != 0)
{
for (int j = 0; j < itemArray.Length; j++)
{
DisplayBranchHistoryItem(itemArray[j]);
}
}
}
}
private static void DisplayBranchHistoryItem(BranchHistoryTreeItem item, int level = 0)
{
var relative = item.Relative;
Console.Write(new string('\t', level));
var branchTo = relative.BranchToItem;
var fromItem = relative.BranchFromItem;
if (branchTo == null)
{
return;
}
Console.Write(branchTo.ServerItem);
Console.Write('\t');
if (relative.RelativeFromItemId != 0)
{
Console.Write("{0} => {1}", fromItem.ChangesetId, branchTo.ChangesetId);
}
else
{
Console.Write(branchTo.ChangesetId);
}
Console.Write('\t');
if (relative.RelativeFromItemId != 0)
{
if (fromItem == null)
{
Console.Write((relative.BranchToChangeType & ChangeType.Rename) == ChangeType.Rename ? "RenameSourceUnreadable" : "BranchUnreadable");
}
else if ((relative.BranchToChangeType & ChangeType.Rename) == ChangeType.Rename)
{
Console.Write("Renamed in version {0}", fromItem.ChangesetId);
}
else
{
Console.Write("Branched from version {0}", fromItem.ChangesetId);
}
}
Console.WriteLine();
foreach (BranchHistoryTreeItem child in item.Children)
{
DisplayBranchHistoryItem(child, level + 1);
}
}
private static BranchObject GetBranchObject(string itemSpec)
{
return vc.QueryBranchObjects(new ItemIdentifier(itemSpec, VersionSpec.Latest), RecursionType.None).FirstOrDefault();
}
private static void DisplayBranches(string header, IEnumerable<BranchObject> branches)
{
Console.WriteLine();
Console.WriteLine(header);
Console.WriteLine(new string('=', header.Length));
Console.WriteLine();
foreach (var branch in branches)
{
DisplayBranch(branch, 0);
}
}
private static void DisplayBranch(string itemSpec, VersionSpec version = null)
{
version = version ?? VersionSpec.Latest;
var itemId = new ItemIdentifier(itemSpec, version);
DisplayBranches(string.Format("Branch {0}", itemSpec), vc.QueryBranchObjects(itemId, RecursionType.Full));
}
private static void DisplayBranch(BranchObject branch, int level = 0)
{
Console.Write(new string('\t', level));
Console.Write(" ");
Console.WriteLine(branch.Properties.RootItem.Item);
foreach (var child in branch.ChildBranches)
{
var childBranch = vc.QueryBranchObjects(child, RecursionType.None);
DisplayBranch(childBranch[0], level + 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment