Skip to content

Commit 226e2c1

Browse files
committed
Collected alternative path list.
1 parent 3b4365a commit 226e2c1

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

GitReader.Core/Internal/RepositoryAccessor.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ public HashResults(Hash hash, string[] names)
9292

9393
internal static class RepositoryAccessor
9494
{
95+
public readonly record struct CandidateRepositoryPaths(
96+
string GitPath,
97+
string[] AlternativePaths);
98+
9599
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
96-
public static async ValueTask<string> DetectLocalRepositoryPathAsync(
100+
public static async ValueTask<CandidateRepositoryPaths> DetectLocalRepositoryPathAsync(
97101
string startPath, IFileSystem fileSystem, CancellationToken ct)
98102
#else
99-
public static async Task<string> DetectLocalRepositoryPathAsync(
103+
public static async Task<CandidateRepositoryPaths> DetectLocalRepositoryPathAsync(
100104
string startPath, IFileSystem fileSystem, CancellationToken ct)
101105
#endif
102106
{
@@ -111,7 +115,7 @@ public static async Task<string> DetectLocalRepositoryPathAsync(
111115

112116
if (await fileSystem.IsFileExistsAsync(fileSystem.Combine(candidatePath, "config"), ct))
113117
{
114-
return candidatePath;
118+
return new(candidatePath, Utilities.Empty<string>());
115119
}
116120

117121
// Issue #11
@@ -137,7 +141,19 @@ public static async Task<string> DetectLocalRepositoryPathAsync(
137141

138142
if (await fileSystem.IsFileExistsAsync(fileSystem.Combine(candidatePath, "config"), ct))
139143
{
140-
return candidatePath;
144+
return new(candidatePath, Utilities.Empty<string>());
145+
}
146+
147+
// Worktree
148+
var commonDirPath = fileSystem.Combine(candidatePath, "commondir");
149+
if (await fileSystem.IsFileExistsAsync(commonDirPath, ct))
150+
{
151+
using var fs2 = await fileSystem.OpenAsync(commonDirPath, false, ct);
152+
var tr2 = new AsyncTextReader(fs2);
153+
154+
var relativePath = (await tr2.ReadToEndAsync(ct)).Trim();
155+
var gitPath = fileSystem.GetFullPath(fileSystem.Combine(candidatePath, relativePath));
156+
return new(gitPath, [candidatePath]);
141157
}
142158

143159
break;

GitReader.Core/Primitive/PrimitiveRepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ namespace GitReader.Primitive;
1414
public sealed class PrimitiveRepository : Repository
1515
{
1616
internal PrimitiveRepository(
17-
string repositoryPath,
17+
string gitPath,
18+
string[] alternativePaths,
1819
IFileSystem fileSystem) :
19-
base(repositoryPath, fileSystem)
20+
base(gitPath, alternativePaths, fileSystem)
2021
{
2122
}
2223
}

GitReader.Core/Primitive/PrimitiveRepositoryFacade.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ internal static class PrimitiveRepositoryFacade
2121
{
2222
private static async Task<PrimitiveRepository> InternalOpenPrimitiveAsync(
2323
string repositoryPath,
24+
string[] alternativePaths,
2425
IFileSystem fileSystem,
2526
CancellationToken ct)
2627
{
27-
var repository = new PrimitiveRepository(repositoryPath, fileSystem);
28+
var repository = new PrimitiveRepository(repositoryPath, alternativePaths, fileSystem);
2829

2930
try
3031
{
@@ -52,9 +53,9 @@ public static async Task<PrimitiveRepository> OpenPrimitiveAsync(
5253
IFileSystem fileSystem,
5354
CancellationToken ct)
5455
{
55-
var repositoryPath = await RepositoryAccessor.DetectLocalRepositoryPathAsync(
56+
var (gitPath, alternativePaths) = await RepositoryAccessor.DetectLocalRepositoryPathAsync(
5657
path, fileSystem, ct);
57-
return await InternalOpenPrimitiveAsync(repositoryPath, fileSystem, ct);
58+
return await InternalOpenPrimitiveAsync(gitPath, alternativePaths, fileSystem, ct);
5859
}
5960

6061
//////////////////////////////////////////////////////////////////////////
@@ -140,6 +141,6 @@ public static async Task<PrimitiveRepository> OpenSubModuleAsync(
140141
throw new ArgumentException("Submodule repository does not exist.");
141142
}
142143

143-
return await InternalOpenPrimitiveAsync(repositoryPath, repository.fileSystem, ct);
144+
return await InternalOpenPrimitiveAsync(repositoryPath, [], repository.fileSystem, ct);
144145
}
145146
}

GitReader.Core/Repository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public abstract class Repository : IDisposable
2626

2727
private protected Repository(
2828
string gitPath,
29+
string[] alternativeGitPaths,
2930
IFileSystem fileSystem)
3031
{
3132
this.GitPath = gitPath;
33+
this.GitPaths = [..alternativeGitPaths, gitPath];
3234
this.fileSystem = fileSystem;
3335
this.fileStreamCache = new(this.fileSystem);
3436
this.objectAccessor = new(this.pool, this.fileSystem, this.fileStreamCache, gitPath);
@@ -47,6 +49,8 @@ public void Dispose()
4749
}
4850

4951
public string GitPath { get; }
52+
53+
internal string[] GitPaths { get; }
5054

5155
public ReadOnlyDictionary<string, string> RemoteUrls =>
5256
this.remoteUrls;

GitReader.Core/Structures/StructuredRepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public sealed class StructuredRepository : Repository
3232
private ReadOnlyDictionary<string, Branch>? branches;
3333

3434
internal StructuredRepository(
35-
string repositoryPath,
35+
string gitPath,
36+
string[] alternativePaths,
3637
IFileSystem fileSystem) :
37-
base(repositoryPath, fileSystem)
38+
base(gitPath, alternativePaths, fileSystem)
3839
{
3940
}
4041

GitReader.Core/Structures/StructuredRepositoryFacade.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,11 @@ public static async Task<ReflogEntry[]> GetHeadReflogsAsync(
170170

171171
private static async Task<StructuredRepository> InternalOpenStructuredAsync(
172172
string repositoryPath,
173+
string[] alternativePaths,
173174
IFileSystem fileSystem,
174175
CancellationToken ct)
175176
{
176-
var repository = new StructuredRepository(repositoryPath, fileSystem);
177+
var repository = new StructuredRepository(repositoryPath, alternativePaths, fileSystem);
177178

178179
try
179180
{
@@ -214,9 +215,9 @@ public static async Task<StructuredRepository> OpenStructuredAsync(
214215
IFileSystem fileSystem,
215216
CancellationToken ct)
216217
{
217-
var repositoryPath = await RepositoryAccessor.DetectLocalRepositoryPathAsync(
218+
var (gitPath, alternativePaths) = await RepositoryAccessor.DetectLocalRepositoryPathAsync(
218219
path, fileSystem, ct);
219-
return await InternalOpenStructuredAsync(repositoryPath, fileSystem, ct);
220+
return await InternalOpenStructuredAsync(gitPath, alternativePaths, fileSystem, ct);
220221
}
221222

222223
//////////////////////////////////////////////////////////////////////////
@@ -438,7 +439,7 @@ public static async Task<StructuredRepository> OpenSubModuleAsync(
438439
throw new ArgumentException("Submodule repository does not exist.");
439440
}
440441

441-
return await InternalOpenStructuredAsync(repositoryPath, repository.fileSystem, ct);
442+
return await InternalOpenStructuredAsync(repositoryPath, [], repository.fileSystem, ct);
442443
}
443444

444445
public static Task<Stream> OpenBlobAsync(

0 commit comments

Comments
 (0)