Skip to content

Commit 0f25af8

Browse files
Code Quality: Reduce git folders loading time (#12980)
1 parent b26e831 commit 0f25af8

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/Files.App/Data/Models/CurrentInstanceViewModel.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ public string GitBranchName
179179
get
180180
{
181181
if (IsGitRepository)
182-
{
183-
using var repository = new Repository(gitRepositoryPath);
184-
return repository.Branches.FirstOrDefault(branch =>
185-
branch.IsCurrentRepositoryHead)?.FriendlyName ?? string.Empty;
186-
}
182+
return GitHelpers.GetRepositoryHeadName(gitRepositoryPath);
187183

188184
return string.Empty;
189185
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal static class GitHelpers
2525

2626
private const int END_OF_ORIGIN_PREFIX = 7;
2727

28+
private const int MAX_NUMBER_OF_BRANCHES = 15;
29+
2830
private static readonly ILogger _logger = Ioc.Default.GetRequiredService<ILogger<App>>();
2931

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

123+
public static string GetRepositoryHeadName(string? path)
124+
{
125+
if (string.IsNullOrWhiteSpace(path) || !Repository.IsValid(path))
126+
return string.Empty;
127+
128+
using var repository = new Repository(path);
129+
return repository.Branches
130+
.FirstOrDefault(b => b.IsCurrentRepositoryHead)?.FriendlyName ?? string.Empty;
131+
}
132+
121133
public static async Task<bool> Checkout(string? repositoryPath, string? branch)
122134
{
123135
Analytics.TrackEvent("Triggered git checkout");

src/Files.App/ViewModels/Previews/FolderPreviewViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ private async Task LoadPreviewAndDetailsAsync()
5050
!string.IsNullOrEmpty(repoPath))
5151
{
5252
var gitDirectory = GitHelpers.GetGitRepositoryPath(Folder.Path, Path.GetPathRoot(Folder.Path));
53-
var branches = GitHelpers.GetBranchesNames(gitDirectory);
53+
var headName = GitHelpers.GetRepositoryHeadName(gitDirectory);
5454
var repositoryName = GitHelpers.GetOriginRepositoryName(gitDirectory);
5555

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

59-
if (branches.Length > 0)
60-
Item.FileDetails.Add(GetFileProperty("GitCurrentBranch", branches.First().Name));
59+
if (!string.IsNullOrWhiteSpace(headName))
60+
Item.FileDetails.Add(GetFileProperty("GitCurrentBranch", headName));
6161
}
6262
}
6363

0 commit comments

Comments
 (0)