Skip to content

Commit

Permalink
Merge pull request #9 from Techsola/use_previously_created_empty_repo
Browse files Browse the repository at this point in the history
Use previously created empty repo
  • Loading branch information
jnm2 committed Apr 15, 2023
2 parents d36251a + 91d0f11 commit d497828
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
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

0 comments on commit d497828

Please sign in to comment.