Skip to content

Commit e0dfd93

Browse files
Generate authors file if missing and auto-populate with changset authors
1 parent 6c6de02 commit e0dfd93

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/TfvcMigrator/Program.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.CommandLine;
22
using System.CommandLine.NamingConventionBinder;
33
using System.Globalization;
4-
using System.Linq;
54
using System.Text;
65
using LibGit2Sharp;
76
using Microsoft.TeamFoundation.SourceControl.WebApi;
@@ -13,7 +12,7 @@ namespace TfvcMigrator;
1312

1413
public static class Program
1514
{
16-
public static Task<int> Main(string[] args)
15+
public static Task<int> Main(string[] args)
1716
{
1817
var command = new RootCommand("Migrates TFVC source history to idiomatic Git history while preserving branch topology.")
1918
{
@@ -117,17 +116,20 @@ pat is not null
117116
var unmappedAuthors = changesets.Select(c => c.Author)
118117
.Concat(changesets.Select(c => c.CheckedInBy))
119118
.Concat(allLabels.Select(l => l.Owner))
120-
.Select(identity => identity.UniqueName)
121-
.Where(name => !authorsLookup.ContainsKey(name))
122-
.Distinct(StringComparer.OrdinalIgnoreCase)
119+
.Where(identity => !authorsLookup.ContainsKey(identity.UniqueName))
120+
.GroupBy(i => i.UniqueName)
121+
.Select(grouping => grouping.First())
123122
.ToList();
124123

125124
if (unmappedAuthors.Any())
126125
{
127-
Console.WriteLine("An entry must be added to the authors file for each of the following TFVC users:");
126+
Console.WriteLine("A valid email address must be added to the authors file for each of the following TFVC users:");
128127
foreach (var user in unmappedAuthors)
129-
Console.WriteLine(user);
130-
return 1;
128+
{
129+
Console.WriteLine(user.UniqueName);
130+
await File.AppendAllTextAsync(authors, $"{user.UniqueName} = {user.DisplayName} <email>{Environment.NewLine}");
131+
}
132+
return 1;
131133
}
132134

133135
Console.WriteLine("Downloading changesets and converting to commits...");
@@ -546,6 +548,13 @@ private static ImmutableDictionary<string, Identity> LoadAuthors(string authorsP
546548
{
547549
var builder = ImmutableDictionary.CreateBuilder<string, Identity>(StringComparer.OrdinalIgnoreCase);
548550

551+
if (!File.Exists(authorsPath))
552+
{
553+
Console.WriteLine($"Authors file not found. Creating: {authorsPath}");
554+
File.Create(authorsPath);
555+
return builder.ToImmutable();
556+
}
557+
549558
using (var reader = File.OpenText(authorsPath))
550559
{
551560
while (reader.ReadLine() is { } line)
@@ -565,6 +574,8 @@ private static ImmutableDictionary<string, Identity> LoadAuthors(string authorsP
565574
var name = gitIdentity[..openingAngleBracketIndex].TrimEnd().ToString();
566575
var email = gitIdentity[(openingAngleBracketIndex + 1)..^1].Trim().ToString();
567576

577+
if (email == "email") throw new NotImplementedException("<email> in authors file must be replaced with a valid email address");
578+
568579
builder.Add(tfvcIdentity, new Identity(name, email));
569580
}
570581
}

0 commit comments

Comments
 (0)