Skip to content

Code Quality: Reduce git folders loading time #12980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2023
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
6 changes: 1 addition & 5 deletions src/Files.App/Data/Models/CurrentInstanceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ public string GitBranchName
get
{
if (IsGitRepository)
{
using var repository = new Repository(gitRepositoryPath);
return repository.Branches.FirstOrDefault(branch =>
branch.IsCurrentRepositoryHead)?.FriendlyName ?? string.Empty;
}
return GitHelpers.GetRepositoryHeadName(gitRepositoryPath);

return string.Empty;
}
Expand Down
14 changes: 13 additions & 1 deletion src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal static class GitHelpers

private const int END_OF_ORIGIN_PREFIX = 7;

private const int MAX_NUMBER_OF_BRANCHES = 15;

private static readonly ILogger _logger = Ioc.Default.GetRequiredService<ILogger<App>>();

private static readonly IDialogService _dialogService = Ioc.Default.GetRequiredService<IDialogService>();
Expand Down Expand Up @@ -112,12 +114,22 @@ public static BranchItem[] GetBranchesNames(string? path)
return repository.Branches
.Where(b => !b.IsRemote || b.RemoteName == "origin")
.OrderByDescending(b => b.IsCurrentRepositoryHead)
.ThenBy(b => b.IsRemote)
.ThenByDescending(b => b.Tip?.Committer.When)
.Take(MAX_NUMBER_OF_BRANCHES)
.Select(b => new BranchItem(b.FriendlyName, b.IsRemote, b.TrackingDetails.AheadBy, b.TrackingDetails.BehindBy))
.ToArray();
}

public static string GetRepositoryHeadName(string? path)
{
if (string.IsNullOrWhiteSpace(path) || !Repository.IsValid(path))
return string.Empty;

using var repository = new Repository(path);
return repository.Branches
.FirstOrDefault(b => b.IsCurrentRepositoryHead)?.FriendlyName ?? string.Empty;
}

public static async Task<bool> Checkout(string? repositoryPath, string? branch)
{
Analytics.TrackEvent("Triggered git checkout");
Expand Down
6 changes: 3 additions & 3 deletions src/Files.App/ViewModels/Previews/FolderPreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ private async Task LoadPreviewAndDetailsAsync()
!string.IsNullOrEmpty(repoPath))
{
var gitDirectory = GitHelpers.GetGitRepositoryPath(Folder.Path, Path.GetPathRoot(Folder.Path));
var branches = GitHelpers.GetBranchesNames(gitDirectory);
var headName = GitHelpers.GetRepositoryHeadName(gitDirectory);
var repositoryName = GitHelpers.GetOriginRepositoryName(gitDirectory);

if(!string.IsNullOrEmpty(gitDirectory))
Item.FileDetails.Add(GetFileProperty("GitOriginRepositoryName", repositoryName));

if (branches.Length > 0)
Item.FileDetails.Add(GetFileProperty("GitCurrentBranch", branches.First().Name));
if (!string.IsNullOrWhiteSpace(headName))
Item.FileDetails.Add(GetFileProperty("GitCurrentBranch", headName));
}
}

Expand Down