Skip to content

Commit

Permalink
Enable using an existing empty repository created in a previous try
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Apr 15, 2023
1 parent 0753830 commit 91d0f11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 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
35 changes: 28 additions & 7 deletions src/TfvcMigrator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
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 @@ -339,6 +334,32 @@ private static RootPathChange ParseRootPathChange(string token)
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(
ImmutableArray<(BranchIdentity Branch, RepositoryBranchMapping Mapping)> branchMappingsInDependentOperationOrder,
ImmutableArray<TfvcItem> currentItems)
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 91d0f11

Please sign in to comment.