enhance: improve Repository.Open() performance (#1121)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-03-24 19:28:16 +08:00
parent 0a877c6730
commit fc85dd3269
No known key found for this signature in database

View file

@ -275,22 +275,16 @@ namespace SourceGit.ViewModels
if (!Path.Exists(node.Id)) if (!Path.Exists(node.Id))
{ {
var ctx = page == null ? ActivePage.Node.Id : page.Node.Id; App.RaiseException(node.Id, "Repository does NOT exists any more. Please remove it.");
App.RaiseException(ctx, "Repository does NOT exists any more. Please remove it.");
return; return;
} }
var isBare = new Commands.IsBareRepository(node.Id).Result(); var isBare = new Commands.IsBareRepository(node.Id).Result();
var gitDir = node.Id; var gitDir = isBare ? node.Id : GetRepositoryGitDir(node.Id);
if (!isBare) if (string.IsNullOrEmpty(gitDir))
{ {
gitDir = new Commands.QueryGitDir(node.Id).Result(); App.RaiseException(node.Id, "Given path is not a valid git repository!");
if (string.IsNullOrEmpty(gitDir)) return;
{
var ctx = page == null ? ActivePage.Node.Id : page.Node.Id;
App.RaiseException(ctx, "Given path is not a valid git repository!");
return;
}
} }
var repo = new Repository(isBare, node.Id, gitDir); var repo = new Repository(isBare, node.Id, gitDir);
@ -469,6 +463,37 @@ namespace SourceGit.ViewModels
return menu; return menu;
} }
private string GetRepositoryGitDir(string repo)
{
var fullpath = Path.Combine(repo, ".git");
if (Directory.Exists(fullpath))
{
if (Directory.Exists(Path.Combine(fullpath, "refs")) &&
Directory.Exists(Path.Combine(fullpath, "objects")) &&
File.Exists(Path.Combine(fullpath, "HEAD")))
return fullpath;
return null;
}
if (File.Exists(fullpath))
{
var redirect = File.ReadAllText(fullpath).Trim();
if (redirect.StartsWith("gitdir: ", StringComparison.Ordinal))
redirect = redirect.Substring(8);
if (!Path.IsPathRooted(redirect))
redirect = Path.GetFullPath(Path.Combine(repo, redirect));
if (Directory.Exists(redirect))
return redirect;
return null;
}
return new Commands.QueryGitDir(repo).Result();
}
private void SwitchWorkspace(Workspace to) private void SwitchWorkspace(Workspace to)
{ {
foreach (var one in Pages) foreach (var one in Pages)