Skip to content

Commit d497828

Browse files
authored
Merge pull request #9 from Techsola/use_previously_created_empty_repo
Use previously created empty repo
2 parents d36251a + 91d0f11 commit d497828

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,6 @@ dotnet_diagnostic.SA1641.severity = none
9090
dotnet_diagnostic.SA1642.severity = none
9191
dotnet_diagnostic.SA1643.severity = none
9292
dotnet_diagnostic.SA1649.severity = none
93+
94+
# Workaround for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503
95+
dotnet_diagnostic.SA1010.severity = none

src/TfvcMigrator/InterceptableCommandHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public static Delegate Intercept(Delegate @delegate)
8686
{
8787
generator.Emit(OpCodes.Call, typeof(Task).GetProperty(nameof(Task.CompletedTask), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)!.GetMethod!);
8888
}
89+
else if (@delegate.Method.ReturnType == typeof(Task<int>))
90+
{
91+
generator.Emit(OpCodes.Ldc_I4_0);
92+
generator.Emit(OpCodes.Call, typeof(Task).GetMethod(nameof(Task.FromResult), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)!.MakeGenericMethod(typeof(int)));
93+
}
8994
else
9095
{
9196
throw new NotImplementedException();

src/TfvcMigrator/Program.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace TfvcMigrator;
1212

1313
public static class Program
1414
{
15-
public static Task Main(string[] args)
15+
public static Task<int> Main(string[] args)
1616
{
1717
var command = new RootCommand("Migrates TFVC source history to idiomatic Git history while preserving branch topology.")
1818
{
@@ -58,7 +58,7 @@ private static RootPathChange ParseRootPathChange(string token)
5858
return new RootPathChange(changeset, token[(colonIndex + 1)..]);
5959
}
6060

61-
public static async Task MigrateAsync(
61+
public static async Task<int> MigrateAsync(
6262
Uri projectCollectionUrl,
6363
string rootPath,
6464
string authors,
@@ -74,17 +74,12 @@ public static async Task MigrateAsync(
7474
new[] { outDir, PathUtils.GetLeaf(rootPath), projectCollectionUrl.Segments.LastOrDefault() }
7575
.First(name => !string.IsNullOrEmpty(name))!);
7676

77-
Directory.CreateDirectory(outputDirectory);
78-
if (Directory.GetFileSystemEntries(outputDirectory).Any())
79-
{
80-
Console.WriteLine($"Cannot create Git repository at {outputDirectory} because the directory is not empty.");
81-
return;
82-
}
77+
using var repo = InitRepository(outputDirectory);
78+
if (repo is null)
79+
return 1;
8380

8481
var authorsLookup = LoadAuthors(authors);
8582

86-
using var repo = new Repository(Repository.Init(outputDirectory));
87-
8883
Console.WriteLine("Connecting...");
8984

9085
using var connection = new VssConnection(
@@ -131,7 +126,7 @@ pat is not null
131126
Console.WriteLine("An entry must be added to the authors file for each of the following TFVC users:");
132127
foreach (var user in unmappedAuthors)
133128
Console.WriteLine(user);
134-
return;
129+
return 1;
135130
}
136131

137132
Console.WriteLine("Downloading changesets and converting to commits...");
@@ -336,6 +331,33 @@ pat is not null
336331
}
337332

338333
Console.WriteLine($"\rAll {changesets.Count} changesets migrated successfully.");
334+
return 0;
335+
}
336+
337+
private static Repository? InitRepository(string outputDirectory)
338+
{
339+
Directory.CreateDirectory(outputDirectory);
340+
341+
var existingFileSystemEntries = Directory.GetFileSystemEntries(outputDirectory);
342+
if (!existingFileSystemEntries.Any())
343+
return new Repository(Repository.Init(outputDirectory));
344+
345+
if (existingFileSystemEntries is not [var singleFileSystemEntry]
346+
|| !".git".Equals(Path.GetFileName(singleFileSystemEntry), StringComparison.OrdinalIgnoreCase))
347+
{
348+
Console.WriteLine($"Cannot create Git repository at {outputDirectory} because the directory is not empty.");
349+
return null;
350+
}
351+
352+
var repository = new Repository(singleFileSystemEntry);
353+
if (repository.ObjectDatabase.Any())
354+
{
355+
repository.Dispose();
356+
Console.WriteLine($"A Git repository at {outputDirectory} already exists and is not empty.");
357+
return null;
358+
}
359+
360+
return repository;
339361
}
340362

341363
private static ImmutableDictionary<BranchIdentity, ImmutableArray<(string GitRepositoryPath, TfvcItem DownloadSource)>> MapItemsToDownloadSources(

src/TfvcMigrator/TfvcMigrator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<LangVersion>11</LangVersion>
67
<Nullable>enable</Nullable>
78
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
89

0 commit comments

Comments
 (0)