Skip to content

Commit 43de26c

Browse files
committed
Supported multiple same named branches.
1 parent b25e555 commit 43de26c

File tree

9 files changed

+86
-18
lines changed

9 files changed

+86
-18
lines changed

FSharp.GitReader/Primitive/RepositoryExtension.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module public RepositoryExtension =
2626
PrimitiveRepositoryFacade.GetBranchHeadReferenceAsync(
2727
repository, branchName, unwrapCT ct) |> Async.AwaitTask
2828

29+
member repository.getBranchAllHeadReference(branchName: string, ?ct: CancellationToken) =
30+
PrimitiveRepositoryFacade.GetBranchAllHeadReferenceAsync(
31+
repository, branchName, unwrapCT ct) |> Async.AwaitTask
32+
2933
member repository.getTagReference(tagName: string, ?ct: CancellationToken) =
3034
PrimitiveRepositoryFacade.GetTagReferenceAsync(
3135
repository, tagName, unwrapCT ct) |> Async.AwaitTask

FSharp.GitReader/Structures/RepositoryExtension.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module public RepositoryExtension =
9191
(match repository.head with
9292
| null -> None
9393
| _ -> Some repository.head),
94-
repository.branches,
94+
repository.branchesAll,
9595
repository.tags)
9696

9797
let (|Branch|) (branch: Branch) =

GitReader.Core/Primitive/PrimitiveRepositoryFacade.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ remoteResults is { } rr ?
8888
throw new ArgumentException($"Could not find a branch: {branchName}");
8989
}
9090

91+
public static async Task<PrimitiveReference[]> GetBranchAllHeadReferenceAsync(
92+
Repository repository,
93+
string branchName,
94+
CancellationToken ct)
95+
{
96+
var path = $"refs/heads/{branchName}";
97+
var remotePath = $"refs/remotes/{branchName}";
98+
var (results, remoteResults) = await Utilities.Join(
99+
RepositoryAccessor.ReadHashAsync(repository, path, ct),
100+
RepositoryAccessor.ReadHashAsync(repository, remotePath, ct));
101+
return (results is { } r ?
102+
[ new PrimitiveReference(branchName, path, r.Hash) ] : Utilities.Empty<PrimitiveReference>()).
103+
Concat(remoteResults is { } rr ?
104+
[ new PrimitiveReference(branchName, remotePath, rr.Hash) ] : Utilities.Empty<PrimitiveReference>()).
105+
ToArray();
106+
}
107+
91108
public static async Task<PrimitiveTagReference> GetTagReferenceAsync(
92109
Repository repository,
93110
string tagName, CancellationToken ct)

GitReader.Core/Structures/StructuredRepository.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using GitReader.IO;
1212
using System;
1313
using System.ComponentModel;
14+
using System.Linq;
15+
using System.Threading;
1416

1517
namespace GitReader.Structures;
1618

@@ -23,10 +25,12 @@ internal interface IRepositoryReference
2325
public sealed class StructuredRepository : Repository
2426
{
2527
internal Branch? head;
26-
internal ReadOnlyDictionary<string, Branch> branches = null!;
28+
internal ReadOnlyDictionary<string, Branch[]> branchesAll = null!;
2729
internal ReadOnlyDictionary<string, Tag> tags = null!;
2830
internal ReadOnlyArray<Stash> stashes = null!;
2931

32+
private ReadOnlyDictionary<string, Branch>? branches;
33+
3034
internal StructuredRepository(
3135
string repositoryPath,
3236
IFileSystem fileSystem) :
@@ -37,10 +41,26 @@ internal StructuredRepository(
3741
public Branch? Head =>
3842
this.head;
3943

40-
public ReadOnlyDictionary<string, Branch> Branches =>
41-
this.branches;
44+
public ReadOnlyDictionary<string, Branch> Branches
45+
{
46+
get
47+
{
48+
if (this.branches == null)
49+
{
50+
// Ignored race condition.
51+
this.branches = this.branchesAll.ToDictionary(
52+
entry => entry.Key, entry => entry.Value[0]);
53+
}
54+
return this.branches;
55+
}
56+
}
57+
4258
public ReadOnlyDictionary<string, Tag> Tags =>
43-
this.tags;
59+
this.tags;
60+
61+
public ReadOnlyDictionary<string, Branch[]> BranchesAll =>
62+
this.branchesAll;
63+
4464
public ReadOnlyArray<Stash> Stashes =>
4565
this.stashes;
46-
}
66+
}

GitReader.Core/Structures/StructuredRepositoryFacade.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static RepositoryReferenceExtracted GetRelatedRepository(
7373

7474
//////////////////////////////////////////////////////////////////////////
7575

76-
private static async Task<ReadOnlyDictionary<string, Branch>> GetStructuredBranchesAsync(
76+
private static async Task<ReadOnlyDictionary<string, Branch[]>> GetStructuredBranchesAsync(
7777
StructuredRepository repository,
7878
WeakReference rwr,
7979
CancellationToken ct)
@@ -89,7 +89,8 @@ private static async Task<ReadOnlyDictionary<string, Branch>> GetStructuredBranc
8989
return references.
9090
Select(r => new Branch(rwr, r.Name, r.Target, false)).
9191
Concat(remoteReferences.Select(r => new Branch(rwr, r.Name, r.Target, true))).
92-
ToDictionary(b => b.Name);
92+
GroupBy(b => b.Name).
93+
ToDictionary(g => g.Key, g => g.ToArray());
9394
}
9495

9596
private static async Task<ReadOnlyDictionary<string, Tag>> GetStructuredTagsAsync(
@@ -135,8 +136,8 @@ private static async Task<ReadOnlyDictionary<string, Tag>> GetStructuredTagsAsyn
135136

136137
return tags.
137138
Where(tag => tag != null).
138-
DistinctBy(tag => tag!.Name).
139-
ToDictionary(tag => tag!.Name, tag => tag!);
139+
DistinctBy(tag => tag.Name).
140+
ToDictionary(tag => tag.Name);
140141
}
141142

142143
private static async Task<Stash[]> GetStructuredStashesAsync(
@@ -188,14 +189,14 @@ private static async Task<StructuredRepository> InternalOpenStructuredAsync(
188189

189190
// Read all other requirements.
190191
var rwr = new WeakReference(repository);
191-
var (head, branches, tags, stashes) = await Utilities.Join(
192+
var (head, branchesAll, tags, stashes) = await Utilities.Join(
192193
GetCurrentHeadAsync(repository, rwr, ct),
193194
GetStructuredBranchesAsync(repository, rwr, ct),
194195
GetStructuredTagsAsync(repository, rwr, ct),
195196
GetStructuredStashesAsync(repository, rwr, ct));
196197

197198
repository.head = head;
198-
repository.branches = branches;
199+
repository.branchesAll = branchesAll;
199200
repository.tags = tags;
200201
repository.stashes = stashes;
201202

GitReader/Primitive/RepositoryExtension.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static Task<PrimitiveReference> GetBranchHeadReferenceAsync(
2727
string branchName, CancellationToken ct = default) =>
2828
PrimitiveRepositoryFacade.GetBranchHeadReferenceAsync(repository, branchName, ct);
2929

30+
public static Task<PrimitiveReference[]> GetBranchAllHeadReferenceAsync(
31+
this PrimitiveRepository repository,
32+
string branchName, CancellationToken ct = default) =>
33+
PrimitiveRepositoryFacade.GetBranchAllHeadReferenceAsync(repository, branchName, ct);
34+
3035
public static Task<PrimitiveTagReference> GetTagReferenceAsync(
3136
this PrimitiveRepository repository,
3237
string tagName, CancellationToken ct = default) =>

GitReader/Structures/RepositoryExtension.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,31 @@ public static void Deconstruct(
9595
out string gitPath,
9696
out ReadOnlyDictionary<string, string> remoteUrls,
9797
out Branch? head,
98-
out ReadOnlyDictionary<string, Branch> branches,
98+
out ReadOnlyDictionary<string, Branch[]> branchesAll,
9999
out ReadOnlyDictionary<string, Tag> tags)
100100
{
101101
gitPath = repository.GitPath;
102102
remoteUrls = repository.RemoteUrls;
103103
head = repository.head;
104-
branches = repository.branches;
104+
branchesAll = repository.branchesAll;
105105
tags = repository.tags;
106106
}
107107

108+
public static void Deconstruct(
109+
this StructuredRepository repository,
110+
out string gitPath,
111+
out ReadOnlyDictionary<string, string> remoteUrls,
112+
out Branch? head,
113+
out ReadOnlyDictionary<string, Branch> branches,
114+
out ReadOnlyDictionary<string, Tag> tags)
115+
{
116+
gitPath = repository.GitPath;
117+
remoteUrls = repository.RemoteUrls;
118+
head = repository.head;
119+
branches = repository.Branches;
120+
tags = repository.Tags;
121+
}
122+
108123
public static void Deconstruct(
109124
this Branch branch,
110125
out string name,

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ It primarily fits the purpose of easily extracting commit information from a Git
6868

6969
### Target .NET platforms
7070

71-
* .NET 8.0 to 5.0
71+
* .NET 9.0 to 5.0
7272
* .NET Core 3.1 to 2.0
7373
* .NET Standard 2.1 to 1.6
7474
* .NET Framework 4.8.1 to 3.5
@@ -78,7 +78,7 @@ It primarily fits the purpose of easily extracting commit information from a Git
7878
F# 5.0 or upper, it contains F# friendly signature definition.
7979
(Asynchronous operations with `Async` type, elimination of null values with `Option`, etc.)
8080

81-
* .NET 8.0 to 5.0
81+
* .NET 9.0 to 5.0
8282
* .NET Core 3.1 to 2.0
8383
* .NET Standard 2.1, 2.0
8484
* .NET Framework 4.8.1 to 4.6.1
@@ -173,6 +173,9 @@ Console.WriteLine($"Subject: {commit.Subject}");
173173
Console.WriteLine($"Body: {commit.Body}");
174174
```
175175

176+
Use `BranchesAll` instead of `Branches` when there may be branches with the same name but different references.
177+
Using `Branches` will give you only the first branch available from all branches of the same name.
178+
176179
### Get a tag
177180

178181
```csharp

README_ja.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ if (repository.Head is { } head)
6969

7070
### 対応する.NETプラットフォーム
7171

72-
* .NET 8.0 to 5.0
72+
* .NET 9.0 to 5.0
7373
* .NET Core 3.1 to 2.0
7474
* .NET Standard 2.1 to 1.6
7575
* .NET Framework 4.8.1 to 3.5
@@ -79,7 +79,7 @@ if (repository.Head is { } head)
7979
F# 5.0以上が対象で、F#フレンドリーなシグネチャ定義が含まれています。
8080
(`Async`型による非同期操作、`Option`によるnull値排除など)
8181

82-
* .NET 8.0 to 5.0
82+
* .NET 9.0 to 5.0
8383
* .NET Core 3.1 to 2.0
8484
* .NET Standard 2.1, 2.0
8585
* .NET Framework 4.8.1 to 4.6.1
@@ -171,6 +171,9 @@ Console.WriteLine($"Subject: {commit.Subject}");
171171
Console.WriteLine($"Body: {commit.Body}");
172172
```
173173

174+
同じ名前を持つ、異なる参照のブランチが存在する可能性がある場合は、`Branches` の代わりに `BranchesAll` を使います。
175+
`Branches` を使うと、同名の全てのブランチから得られる最初のブランチのみが得られます。
176+
174177
### 指定されたタグの情報を取得
175178

176179
```csharp

0 commit comments

Comments
 (0)