Skip to content

Commit fc85dd3

Browse files
committedMar 24, 2025
enhance: improve Repository.Open() performance (#1121)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 0a877c6 commit fc85dd3

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed
 

‎src/ViewModels/Launcher.cs

+36-11
Original file line numberDiff line numberDiff line change
@@ -275,22 +275,16 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
275275

276276
if (!Path.Exists(node.Id))
277277
{
278-
var ctx = page == null ? ActivePage.Node.Id : page.Node.Id;
279-
App.RaiseException(ctx, "Repository does NOT exists any more. Please remove it.");
278+
App.RaiseException(node.Id, "Repository does NOT exists any more. Please remove it.");
280279
return;
281280
}
282281

283282
var isBare = new Commands.IsBareRepository(node.Id).Result();
284-
var gitDir = node.Id;
285-
if (!isBare)
283+
var gitDir = isBare ? node.Id : GetRepositoryGitDir(node.Id);
284+
if (string.IsNullOrEmpty(gitDir))
286285
{
287-
gitDir = new Commands.QueryGitDir(node.Id).Result();
288-
if (string.IsNullOrEmpty(gitDir))
289-
{
290-
var ctx = page == null ? ActivePage.Node.Id : page.Node.Id;
291-
App.RaiseException(ctx, "Given path is not a valid git repository!");
292-
return;
293-
}
286+
App.RaiseException(node.Id, "Given path is not a valid git repository!");
287+
return;
294288
}
295289

296290
var repo = new Repository(isBare, node.Id, gitDir);
@@ -469,6 +463,37 @@ public ContextMenu CreateContextForPageTab(LauncherPage page)
469463
return menu;
470464
}
471465

466+
private string GetRepositoryGitDir(string repo)
467+
{
468+
var fullpath = Path.Combine(repo, ".git");
469+
if (Directory.Exists(fullpath))
470+
{
471+
if (Directory.Exists(Path.Combine(fullpath, "refs")) &&
472+
Directory.Exists(Path.Combine(fullpath, "objects")) &&
473+
File.Exists(Path.Combine(fullpath, "HEAD")))
474+
return fullpath;
475+
476+
return null;
477+
}
478+
479+
if (File.Exists(fullpath))
480+
{
481+
var redirect = File.ReadAllText(fullpath).Trim();
482+
if (redirect.StartsWith("gitdir: ", StringComparison.Ordinal))
483+
redirect = redirect.Substring(8);
484+
485+
if (!Path.IsPathRooted(redirect))
486+
redirect = Path.GetFullPath(Path.Combine(repo, redirect));
487+
488+
if (Directory.Exists(redirect))
489+
return redirect;
490+
491+
return null;
492+
}
493+
494+
return new Commands.QueryGitDir(repo).Result();
495+
}
496+
472497
private void SwitchWorkspace(Workspace to)
473498
{
474499
foreach (var one in Pages)

0 commit comments

Comments
 (0)