Skip to content

Commit 5048a05

Browse files
authored
Fix: Fixed crash when opening folders with invalid git configs (#16141)
1 parent 136c809 commit 5048a05

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33

44
using Files.App.Dialogs;
55
using LibGit2Sharp;
6-
using Sentry;
76
using Microsoft.Extensions.Logging;
7+
using Sentry;
88
using System.Net.Http;
99
using System.Net.Http.Json;
10-
using System.Net.Sockets;
1110
using System.Text;
12-
using System.Text.Json;
1311

1412
namespace Files.App.Utils.Git
1513
{
@@ -78,7 +76,7 @@ private set
7876

7977
try
8078
{
81-
if (Repository.IsValid(path))
79+
if (IsRepoValid(path))
8280
return path;
8381
else
8482
{
@@ -99,7 +97,7 @@ private set
9997

10098
public static string GetOriginRepositoryName(string? path)
10199
{
102-
if (string.IsNullOrWhiteSpace(path) || !Repository.IsValid(path))
100+
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
103101
return string.Empty;
104102

105103
using var repository = new Repository(path);
@@ -114,7 +112,7 @@ public static string GetOriginRepositoryName(string? path)
114112

115113
public static async Task<BranchItem[]> GetBranchesNames(string? path)
116114
{
117-
if (string.IsNullOrWhiteSpace(path) || !Repository.IsValid(path))
115+
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
118116
return [];
119117

120118
var (result, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem[])>(() =>
@@ -146,7 +144,7 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
146144

147145
public static async Task<BranchItem?> GetRepositoryHead(string? path)
148146
{
149-
if (string.IsNullOrWhiteSpace(path) || !Repository.IsValid(path))
147+
if (string.IsNullOrWhiteSpace(path) || !IsRepoValid(path))
150148
return null;
151149

152150
var (_, returnValue) = await DoGitOperationAsync<(GitOperationResult, BranchItem?)>(() =>
@@ -181,7 +179,7 @@ public static async Task<bool> Checkout(string? repositoryPath, string? branch)
181179
// Re-enable when Metris feature is available again
182180
// SentrySdk.Metrics.Increment("Triggered git checkout");
183181

184-
if (string.IsNullOrWhiteSpace(repositoryPath) || !Repository.IsValid(repositoryPath))
182+
if (string.IsNullOrWhiteSpace(repositoryPath) || !IsRepoValid(repositoryPath))
185183
return false;
186184

187185
using var repository = new Repository(repositoryPath);
@@ -289,7 +287,7 @@ public static async Task DeleteBranchAsync(string? repositoryPath, string? activ
289287
string.IsNullOrWhiteSpace(activeBranch) ||
290288
string.IsNullOrWhiteSpace(branchToDelete) ||
291289
activeBranch.Equals(branchToDelete, StringComparison.OrdinalIgnoreCase) ||
292-
!Repository.IsValid(repositoryPath))
290+
!IsRepoValid(repositoryPath))
293291
{
294292
return;
295293
}
@@ -322,7 +320,7 @@ await DoGitOperationAsync<GitOperationResult>(() =>
322320

323321
public static bool ValidateBranchNameForRepository(string branchName, string repositoryPath)
324322
{
325-
if (string.IsNullOrEmpty(branchName) || !Repository.IsValid(repositoryPath))
323+
if (string.IsNullOrEmpty(branchName) || !IsRepoValid(repositoryPath))
326324
return false;
327325

328326
var nameValidator = RegexHelpers.GitBranchName();
@@ -649,7 +647,7 @@ public static bool IsRepositoryEx(string path, out string repoRootPath)
649647
if (string.IsNullOrEmpty(repositoryRootPath))
650648
return false;
651649

652-
if (Repository.IsValid(repositoryRootPath))
650+
if (!IsRepoValid(repositoryRootPath))
653651
{
654652
repoRootPath = repositoryRootPath;
655653
return true;
@@ -737,6 +735,11 @@ public static async Task InitializeRepositoryAsync(string? path)
737735
}
738736
}
739737

738+
private static bool IsRepoValid(string path)
739+
{
740+
return SafetyExtensions.IgnoreExceptions(() => Repository.IsValid(path));
741+
}
742+
740743
private static IEnumerable<Branch> GetValidBranches(BranchCollection branches)
741744
{
742745
foreach (var branch in branches)

0 commit comments

Comments
 (0)