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

View file

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

View file

@ -346,6 +346,29 @@ namespace SourceGit.ViewModels
return FindNodeRecursive(id, _instance.RepositoryNodes); 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) public static void MoveNode(RepositoryNode node, RepositoryNode to = null)
{ {
if (to == null && _instance._repositoryNodes.Contains(node)) if (to == null && _instance._repositoryNodes.Contains(node))

View file

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