Last active
August 29, 2015 14:09
-
-
Save mika76/9a1c83387ece9128f6d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tfsServer = new Uri("http://127.0.0.1:8080/tfs"); | |
var branch = "$/Some Project"; | |
var fromVersion = new ChangesetVersionSpec(1); | |
var toVersion = VersionSpec.Latest; | |
var tfs = TfsTeamProjectCollectionFactory | |
.GetTeamProjectCollection(tfsServer); | |
tfs.EnsureAuthenticated(); | |
var versionControl = tfs.GetService<VersionControlServer>(); | |
//var history = versionControl.QueryHistory(branch, VersionSpec.Latest, 0, | |
// RecursionType.Full, null, fromVersion, toVersion, int.MaxValue, false, | |
// false, false); | |
// | |
//history.Dump(); | |
var r = versionControl.QueryRootBranchObjects(RecursionType.Full); | |
//r.Dump(); | |
// GET BRANCHES | |
// From http://msdn.microsoft.com/en-us/library/bb138961.aspx | |
//public IEnumerable QueryHistory( | |
// string path, | |
// VersionSpec version, | |
// int deletionId, | |
// RecursionType recursion, | |
// string user, | |
// VersionSpec versionFrom, | |
// VersionSpec versionTo, | |
// int maxCount, | |
// bool includeChanges, | |
// bool slotMode, | |
// bool includeDownloadInfo | |
//) | |
var s = from b in r | |
let Changes = from c in versionControl.QueryHistory(b.Properties.RootItem.Item, VersionSpec.Latest, 0, RecursionType.Full, null, fromVersion, toVersion, int.MaxValue, false, false, false).OfType<Changeset>() | |
orderby c.ChangesetId ascending | |
select | |
new | |
{ | |
ChangesetId = c.ChangesetId, | |
Comment = c.Comment, | |
Committer = c.Committer, | |
CommitterDisplayName = c.CommitterDisplayName, | |
CreationDate = c.CreationDate | |
} | |
where b.Properties.RootItem.Item.Contains(branch) | |
orderby b.Properties.RootItem.Item ascending // order by name | |
select new { b, Changes}; | |
// Add row indexes | |
var s2 = s.Select((x, index) => new | |
{ | |
Id = index, | |
Name = x.b.Properties.RootItem.Item, | |
ParentName = x.b.Properties.ParentBranch == null ? "" : x.b.Properties.ParentBranch.Item, // root if null | |
DateCreated = x.b.DateCreated, | |
//Properties = x.b.Properties, | |
Current = ((ChangesetVersionSpec)x.b.Properties.RootItem.Version).ChangesetId, | |
Created = x.Changes.First().ChangesetId, | |
Changes = x.Changes.ToList() | |
}) | |
.ToList(); | |
// Link each row to it's parent | |
var branches = s2.Select(x => new | |
{ | |
Id = x.Id, | |
Name = x.Name, | |
ParentName = x.ParentName, | |
DateCreated = x.DateCreated, | |
Current = x.Current, | |
Created = x.Created, | |
Changes = x.Changes, | |
ParentId = s2.Where(y => y.Name == x.ParentName).Select(y => y.Id).FirstOrDefault() | |
}) | |
.ToList() | |
.OrderBy(x => x.ParentId) // Put in order of parents so that the graph works in a nice downwards waterfall way | |
.ThenBy(x=> x.Created) | |
.ThenBy(x=> x.Id) | |
.ToList(); | |
//branches.Dump(); | |
var minChange = branches.SelectMany(x => x.Changes).Select(x => x.ChangesetId).Min(); | |
var maxChange = branches.SelectMany(x => x.Changes).Select(x => x.ChangesetId).Max(); | |
var sb = new StringBuilder(); | |
sb.AppendFormat("Branch graph for {0} and {1}\r\n\r\n", minChange, maxChange); | |
sb.Append(" +++++----- "); | |
for(int i = minChange; i <= maxChange; i++) | |
sb.AppendFormat("{0,5}", i); | |
sb.Append("\r\n"); | |
foreach(var b in branches) | |
{ | |
sb.AppendFormat("{0,-100} {1,5}{2,5} ", b.Name, b.Changes.Select(x => x.ChangesetId).Min(), b.Changes.Select(x => x.ChangesetId).Max()); | |
for(int i = minChange; i <= maxChange; i++) | |
{ | |
//Console.Write(string.Format("{0} {1} Min {2} Created {3} Current {4} Changesets {5}", b.Name, i, b.Changes.Select(x => x.ChangesetId).Min(), b.Created, b.Current, string.Join(",", b.Changes.Select(x => x.ChangesetId.ToString()).ToArray()))); | |
//Draw checkin | |
var ch = b.Changes.Select(x => x.ChangesetId).ToArray(); | |
if (i >= b.Created && i <= b.Current) // this changeset is within this branch | |
{ | |
if (ch.Min() == i) | |
{ | |
//Console.WriteLine("is min"); | |
if (b.ParentId == 0) | |
sb.Append(" ■"); | |
else | |
sb.Append(" └"); | |
} | |
else if (ch.Contains(i)) | |
{ | |
//Console.WriteLine("contains"); | |
// Changeset was committed in this branch | |
sb.Append("────■"); | |
} | |
else | |
{ | |
//Console.WriteLine("within"); | |
if (branches.Any(br => br.Created == i&& br.ParentId != 00 && br.ParentId < b.Id && br.Id > b.Id)) | |
{ | |
sb.Append("────│"); | |
} | |
else | |
{ | |
if (branches.Any(br => br.Created == i&& br.ParentId != b.Id)) | |
sb.Append("────┬"); | |
else | |
sb.Append("─────"); | |
} | |
} | |
} | |
else | |
{ | |
//Console.WriteLine("not in"); | |
//sb.Append(" "); | |
if (branches.Any(br => br.Created == i && br.ParentId != 00 && br.ParentId < b.Id && br.Id > b.Id)) | |
{ | |
sb.Append(" │"); | |
} | |
else | |
{ | |
sb.Append(" "); | |
} | |
} | |
} | |
sb.Append("\r\n"); | |
} | |
//Console.WriteLine(sb.ToString()); | |
System.IO.File.WriteAllText("c:\\test_graph.txt", sb.ToString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment