Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable using an existing empty repository created in a previous try #9

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ dotnet_diagnostic.SA1641.severity = none
dotnet_diagnostic.SA1642.severity = none
dotnet_diagnostic.SA1643.severity = none
dotnet_diagnostic.SA1649.severity = none

# Workaround for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503
dotnet_diagnostic.SA1010.severity = none
5 changes: 5 additions & 0 deletions src/TfvcMigrator/InterceptableCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public static Delegate Intercept(Delegate @delegate)
{
generator.Emit(OpCodes.Call, typeof(Task).GetProperty(nameof(Task.CompletedTask), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)!.GetMethod!);
}
else if (@delegate.Method.ReturnType == typeof(Task<int>))
{
generator.Emit(OpCodes.Ldc_I4_0);
generator.Emit(OpCodes.Call, typeof(Task).GetMethod(nameof(Task.FromResult), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)!.MakeGenericMethod(typeof(int)));
}
else
{
throw new NotImplementedException();
Expand Down
44 changes: 33 additions & 11 deletions src/TfvcMigrator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace TfvcMigrator;

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

public static async Task MigrateAsync(
public static async Task<int> MigrateAsync(
Uri projectCollectionUrl,
string rootPath,
string authors,
Expand All @@ -74,17 +74,12 @@ private static RootPathChange ParseRootPathChange(string token)
new[] { outDir, PathUtils.GetLeaf(rootPath), projectCollectionUrl.Segments.LastOrDefault() }
.First(name => !string.IsNullOrEmpty(name))!);

Directory.CreateDirectory(outputDirectory);
if (Directory.GetFileSystemEntries(outputDirectory).Any())
{
Console.WriteLine($"Cannot create Git repository at {outputDirectory} because the directory is not empty.");
return;
}
using var repo = InitRepository(outputDirectory);
if (repo is null)
return 1;

var authorsLookup = LoadAuthors(authors);

using var repo = new Repository(Repository.Init(outputDirectory));

Console.WriteLine("Connecting...");

using var connection = new VssConnection(
Expand Down Expand Up @@ -131,7 +126,7 @@ private static RootPathChange ParseRootPathChange(string token)
Console.WriteLine("An entry must be added to the authors file for each of the following TFVC users:");
foreach (var user in unmappedAuthors)
Console.WriteLine(user);
return;
return 1;
}

Console.WriteLine("Downloading changesets and converting to commits...");
Expand Down Expand Up @@ -336,6 +331,33 @@ private static RootPathChange ParseRootPathChange(string token)
}

Console.WriteLine($"\rAll {changesets.Count} changesets migrated successfully.");
return 0;
}

private static Repository? InitRepository(string outputDirectory)
{
Directory.CreateDirectory(outputDirectory);

var existingFileSystemEntries = Directory.GetFileSystemEntries(outputDirectory);
if (!existingFileSystemEntries.Any())
return new Repository(Repository.Init(outputDirectory));

if (existingFileSystemEntries is not [var singleFileSystemEntry]
|| !".git".Equals(Path.GetFileName(singleFileSystemEntry), StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"Cannot create Git repository at {outputDirectory} because the directory is not empty.");
return null;
}

var repository = new Repository(singleFileSystemEntry);
if (repository.ObjectDatabase.Any())
{
repository.Dispose();
Console.WriteLine($"A Git repository at {outputDirectory} already exists and is not empty.");
return null;
}

return repository;
}

private static ImmutableDictionary<BranchIdentity, ImmutableArray<(string GitRepositoryPath, TfvcItem DownloadSource)>> MapItemsToDownloadSources(
Expand Down
1 change: 1 addition & 0 deletions src/TfvcMigrator/TfvcMigrator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Expand Down