Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 3 additions & 32 deletions src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ internal static partial class GitHelpers
/// <inheritdoc cref="IVersionControl.GetBranchNames(string?)"/>
public static Task<BranchItem[]> GetBranchNames(string? path) => _implementation.GetBranchNames(path);

/// <inheritdoc cref="IVersionControl.GetRepositoryHead(string?)"/>
public static Task<BranchItem?> GetRepositoryHead(string? path) => _implementation.GetRepositoryHead(path);

#region Legacy implementation

// Property already moved into abstraction
Expand Down Expand Up @@ -91,38 +94,6 @@ private set
// Event handler already moved into abstraction
public static event EventHandler? GitFetchCompleted;

public static async Task<BranchItem?> GetRepositoryHead(string? path)
{
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
return null;

var (_, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem?)>(() =>
{
BranchItem? head = null;
try
{
using var repository = new Repository(path);
var branch = GetValidBranches(repository.Branches).FirstOrDefault(b => b.IsCurrentRepositoryHead);
if (branch is not null)
head = new BranchItem(
branch.FriendlyName,
branch.IsCurrentRepositoryHead,
branch.IsRemote,
TryGetTrackingDetails(branch)?.AheadBy ?? 0,
TryGetTrackingDetails(branch)?.BehindBy ?? 0
);
}
catch
{
return (GitOperationResult.GenericError, head);
}

return (GitOperationResult.Success, head);
}, true);

return returnValue;
}

public static async Task<bool> Checkout(string? repositoryPath, string? branch)
{
// Re-enable when Metris feature is available again
Expand Down
32 changes: 32 additions & 0 deletions src/Files.App/Utils/Git/LibGit2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,38 @@ public async Task<BranchItem[]> GetBranchNames(string? path)
return returnValue;
}

public async Task<BranchItem?> GetRepositoryHead(string? path)
{
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
return null;

var (_, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem?)>(() =>
{
BranchItem? head = null;
try
{
using var repository = new Repository(path);
var branch = GetValidBranches(repository.Branches).FirstOrDefault(b => b.IsCurrentRepositoryHead);
if (branch is not null)
head = new BranchItem(
branch.FriendlyName,
branch.IsCurrentRepositoryHead,
branch.IsRemote,
TryGetTrackingDetails(branch)?.AheadBy ?? 0,
TryGetTrackingDetails(branch)?.BehindBy ?? 0
);
}
catch
{
return (GitOperationResult.GenericError, head);
}

return (GitOperationResult.Success, head);
}, true);

return returnValue;
}

private static bool IsRepoValid(string path)
{
return SafetyExtensions.IgnoreExceptions(() => Repository.IsValid(path));
Expand Down
Loading