fix: avoid duplicated nodes being added into the repository tree

This commit is contained in:
leo 2024-05-07 15:49:57 +08:00
parent 207e82b391
commit eeb6abb560
4 changed files with 27 additions and 28 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
@ -114,15 +113,7 @@ namespace SourceGit.ViewModels
CallUIThread(() =>
{
var repo = Preference.AddRepository(path, Path.Combine(path, ".git"));
var node = new RepositoryNode()
{
Id = repo.FullPath,
Name = Path.GetFileName(repo.FullPath),
Bookmark = 0,
IsRepository = true,
};
Preference.AddNode(node);
var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null);
_launcher.OpenRepositoryInTab(node, _page);
});

View file

@ -28,18 +28,10 @@ namespace SourceGit.ViewModels
return false;
var gitDir = Path.GetFullPath(Path.Combine(_targetPath, ".git"));
CallUIThread(() =>
{
var repo = Preference.AddRepository(_targetPath, gitDir);
var node = new RepositoryNode()
{
Id = repo.FullPath,
Name = Path.GetFileName(repo.FullPath),
Bookmark = 0,
IsRepository = true,
};
Preference.AddNode(node);
Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null);
});
return true;

View file

@ -346,6 +346,29 @@ namespace SourceGit.ViewModels
return FindNodeRecursive(id, _instance.RepositoryNodes);
}
public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent)
{
var node = FindNodeRecursive(repo, _instance.RepositoryNodes);
if (node == null)
{
node = new RepositoryNode()
{
Id = Guid.NewGuid().ToString(),
Name = Path.GetFileName(repo),
Bookmark = 0,
IsRepository = true,
};
AddNode(node, parent);
}
else
{
MoveNode(node, parent);
}
return node;
}
public static void MoveNode(RepositoryNode node, RepositoryNode to = null)
{
if (to == null && _instance._repositoryNodes.Contains(node))

View file

@ -257,14 +257,7 @@ namespace SourceGit.Views
Dispatcher.UIThread.Invoke(() =>
{
var repo = ViewModels.Preference.AddRepository(root, gitDir);
var node = new ViewModels.RepositoryNode()
{
Id = repo.FullPath,
Name = Path.GetFileName(repo.FullPath),
Bookmark = 0,
IsRepository = true,
};
ViewModels.Preference.AddNode(node, parent);
var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, parent);
launcher.OpenRepositoryInTab(node, page);
});
});