Skip to content

Commit 7e248d0

Browse files
authored
Code Quality: Moved GetRepositoryHead operation into Git abstraction (#18505)
1 parent a7326cf commit 7e248d0

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

src/Files.App/Utils/Git/GitHelpers.cs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ internal static partial class GitHelpers
2525
/// <inheritdoc cref="IVersionControl.GetBranchNames(string?)"/>
2626
public static Task<BranchItem[]> GetBranchNames(string? path) => _implementation.GetBranchNames(path);
2727

28+
/// <inheritdoc cref="IVersionControl.GetRepositoryHead(string?)"/>
29+
public static Task<BranchItem?> GetRepositoryHead(string? path) => _implementation.GetRepositoryHead(path);
30+
2831
#region Legacy implementation
2932

3033
// Property already moved into abstraction
@@ -91,38 +94,6 @@ private set
9194
// Event handler already moved into abstraction
9295
public static event EventHandler? GitFetchCompleted;
9396

94-
public static async Task<BranchItem?> GetRepositoryHead(string? path)
95-
{
96-
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
97-
return null;
98-
99-
var (_, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem?)>(() =>
100-
{
101-
BranchItem? head = null;
102-
try
103-
{
104-
using var repository = new Repository(path);
105-
var branch = GetValidBranches(repository.Branches).FirstOrDefault(b => b.IsCurrentRepositoryHead);
106-
if (branch is not null)
107-
head = new BranchItem(
108-
branch.FriendlyName,
109-
branch.IsCurrentRepositoryHead,
110-
branch.IsRemote,
111-
TryGetTrackingDetails(branch)?.AheadBy ?? 0,
112-
TryGetTrackingDetails(branch)?.BehindBy ?? 0
113-
);
114-
}
115-
catch
116-
{
117-
return (GitOperationResult.GenericError, head);
118-
}
119-
120-
return (GitOperationResult.Success, head);
121-
}, true);
122-
123-
return returnValue;
124-
}
125-
12697
public static async Task<bool> Checkout(string? repositoryPath, string? branch)
12798
{
12899
// Re-enable when Metris feature is available again

src/Files.App/Utils/Git/LibGit2.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,38 @@ public async Task<BranchItem[]> GetBranchNames(string? path)
127127
return returnValue;
128128
}
129129

130+
public async Task<BranchItem?> GetRepositoryHead(string? path)
131+
{
132+
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
133+
return null;
134+
135+
var (_, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem?)>(() =>
136+
{
137+
BranchItem? head = null;
138+
try
139+
{
140+
using var repository = new Repository(path);
141+
var branch = GetValidBranches(repository.Branches).FirstOrDefault(b => b.IsCurrentRepositoryHead);
142+
if (branch is not null)
143+
head = new BranchItem(
144+
branch.FriendlyName,
145+
branch.IsCurrentRepositoryHead,
146+
branch.IsRemote,
147+
TryGetTrackingDetails(branch)?.AheadBy ?? 0,
148+
TryGetTrackingDetails(branch)?.BehindBy ?? 0
149+
);
150+
}
151+
catch
152+
{
153+
return (GitOperationResult.GenericError, head);
154+
}
155+
156+
return (GitOperationResult.Success, head);
157+
}, true);
158+
159+
return returnValue;
160+
}
161+
130162
private static bool IsRepoValid(string path)
131163
{
132164
return SafetyExtensions.IgnoreExceptions(() => Repository.IsValid(path));

0 commit comments

Comments
 (0)