Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Files.App/Data/Models/DirectoryPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async Task LoadBranches()
if (string.IsNullOrEmpty(_gitRepositoryPath))
return;

var branches = await GitHelpers.GetBranchesNames(_gitRepositoryPath);
var branches = await GitHelpers.GetBranchNames(_gitRepositoryPath);

_localBranches.Clear();
_remoteBranches.Clear();
Expand Down
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 @@ -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

// Property already moved into abstraction
Expand Down Expand Up @@ -88,38 +91,6 @@ private set
// Event handler already moved into abstraction
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
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
34 changes: 33 additions & 1 deletion src/Files.App/Utils/Git/LibGit2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LibGit2Sharp;
using LibGit2Sharp;
using Microsoft.Extensions.Logging;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -95,6 +95,38 @@ public string GetOriginRepositoryName(string? path)
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[])>(() =>
{
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;
}

private static bool IsRepoValid(string path)
{
return SafetyExtensions.IgnoreExceptions(() => Repository.IsValid(path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public AddBranchDialogViewModel(string repositoryPath, string activeBranch)

public async Task LoadBranches()
{
Branches = (await GitHelpers.GetBranchesNames(_repositoryPath)).Select(b => b.Name).ToArray();
Branches = (await GitHelpers.GetBranchNames(_repositoryPath)).Select(b => b.Name).ToArray();
OnPropertyChanged(nameof(Branches));
}
}
Expand Down
Loading