Skip to content
Draft
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
36 changes: 4 additions & 32 deletions src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ internal static partial class GitHelpers
/// <inheritdoc cref="IVersionControl.GetOriginRepositoryName(string?)"/>
public static string GetOriginRepositoryName(string? path) => _implementation.GetOriginRepositoryName(path);

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

#region Legacy implementation

private static readonly StatusCenterViewModel StatusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
Expand Down Expand Up @@ -71,38 +74,6 @@ private set

public static event EventHandler? GitFetchCompleted;

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

var (result, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem[])>(() =>
{
var branches = Array.Empty<BranchItem>();
var result = GitOperationResult.Success;
try
{
using var repository = new Repository(path);

branches = GetValidBranches(repository.Branches)
.OrderByDescending(b => b.Tip?.Committer.When)
.GroupBy(b => b.IsRemote)
.SelectMany(g => g.Take(MAX_NUMBER_OF_BRANCHES))
.OrderByDescending(b => b.IsCurrentRepositoryHead)
.Select(b => new BranchItem(b.FriendlyName, b.IsCurrentRepositoryHead, b.IsRemote, TryGetTrackingDetails(b)?.AheadBy ?? 0, TryGetTrackingDetails(b)?.BehindBy ?? 0))
.ToArray();
}
catch (Exception)
{
result = GitOperationResult.GenericError;
}

return (result, branches);
});

return returnValue;
}

public static async Task<BranchItem?> GetRepositoryHead(string? path)
{
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
Expand Down Expand Up @@ -821,6 +792,7 @@ private static bool IsAuthorizationException(Exception ex)
ex.Message.Contains("authentication replays", StringComparison.OrdinalIgnoreCase);
}

// Method already moved into abstraction
private static async Task<T?> DoGitOperationAsync<T>(Func<object> payload, bool useSemaphore = false)
{
if (useSemaphore)
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Utils/Git/IVersionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal interface IVersionControl
/// <returns>
/// A task producing an array of branches; returns an empty array when the repository is invalid or unavailable.
/// </returns>
Task<BranchItem[]> GetBranchesNames(string? path);
Task<BranchItem[]> GetBranchNames(string? path);

/// <summary>
/// Gets the current repository HEAD reference.
Expand Down
67 changes: 67 additions & 0 deletions src/Files.App/Utils/Git/LibGit2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,75 @@
return repositoryName[..repositoryName.LastIndexOf(".git")];
}

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

var (result, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem[])>(() =>

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Cannot infer the type of implicitly-typed deconstruction variable 'returnValue'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Cannot infer the type of implicitly-typed deconstruction variable 'result'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Cannot infer the type of implicitly-typed deconstruction variable 'returnValue'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Cannot infer the type of implicitly-typed deconstruction variable 'result'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Cannot infer the type of implicitly-typed deconstruction variable 'returnValue'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Cannot infer the type of implicitly-typed deconstruction variable 'result'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Cannot infer the type of implicitly-typed deconstruction variable 'returnValue'.

Check failure on line 70 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Cannot infer the type of implicitly-typed deconstruction variable 'result'.
{
var branches = Array.Empty<BranchItem>();
var result = GitOperationResult.Success;
try
{
using var repository = new Repository(path);

branches = GetValidBranches(repository.Branches)
.OrderByDescending(b => b.Tip?.Committer.When)
.GroupBy(b => b.IsRemote)
.SelectMany(g => g.Take(MAX_NUMBER_OF_BRANCHES))

Check failure on line 81 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The name 'MAX_NUMBER_OF_BRANCHES' does not exist in the current context

Check failure on line 81 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

The name 'MAX_NUMBER_OF_BRANCHES' does not exist in the current context

Check failure on line 81 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

The name 'MAX_NUMBER_OF_BRANCHES' does not exist in the current context

Check failure on line 81 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The name 'MAX_NUMBER_OF_BRANCHES' does not exist in the current context
.OrderByDescending(b => b.IsCurrentRepositoryHead)
.Select(b => new BranchItem(b.FriendlyName, b.IsCurrentRepositoryHead, b.IsRemote, TryGetTrackingDetails(b)?.AheadBy ?? 0, TryGetTrackingDetails(b)?.BehindBy ?? 0))

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The name 'TryGetTrackingDetails' does not exist in the current context

Check failure on line 83 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The name 'TryGetTrackingDetails' does not exist in the current context
.ToArray();
}
catch (Exception)
{
result = GitOperationResult.GenericError;
}

return (result, branches);
});

return returnValue;
}

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

private static async Task<T?> DoGitOperationAsync<T>(Func<object> payload, bool useSemaphore = false)
{
if (useSemaphore)
await GitOperationSemaphore.WaitAsync();

Check failure on line 105 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The name 'GitOperationSemaphore' does not exist in the current context

Check failure on line 105 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

The name 'GitOperationSemaphore' does not exist in the current context

Check failure on line 105 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

The name 'GitOperationSemaphore' does not exist in the current context

Check failure on line 105 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The name 'GitOperationSemaphore' does not exist in the current context
else
await Task.Yield();

try
{
return (T)payload();
}
finally
{
if (useSemaphore)
GitOperationSemaphore.Release();

Check failure on line 116 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The name 'GitOperationSemaphore' does not exist in the current context

Check failure on line 116 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

The name 'GitOperationSemaphore' does not exist in the current context

Check failure on line 116 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

The name 'GitOperationSemaphore' does not exist in the current context

Check failure on line 116 in src/Files.App/Utils/Git/LibGit2.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The name 'GitOperationSemaphore' does not exist in the current context
}
}

private static IEnumerable<Branch> GetValidBranches(BranchCollection branches)
{
foreach (var branch in branches)
{
try
{
var throwIfInvalid = branch.IsCurrentRepositoryHead;
}
catch (LibGit2SharpException)
{
continue;
}

yield return branch;
}
}
}
Loading