refactor: rewrite Preference.FindOrAddNodeByRepositoryPath

* prevent moving node while opening repository directly from commandline
* supports to set parent node while dropping folder to initialize repository
This commit is contained in:
leo 2024-05-20 17:36:43 +08:00
parent 8d726656dc
commit faf2c39056
6 changed files with 15 additions and 12 deletions

View file

@ -113,7 +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 = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null); var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null, true);
_launcher.OpenRepositoryInTab(node, _page); _launcher.OpenRepositoryInTab(node, _page);
}); });

View file

@ -11,9 +11,11 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _targetPath, value); set => SetProperty(ref _targetPath, value);
} }
public Init(string path) public Init(string path, RepositoryNode parent)
{ {
TargetPath = path; _targetPath = path;
_parentNode = parent;
View = new Views.Init() { DataContext = this }; View = new Views.Init() { DataContext = this };
} }
@ -31,13 +33,14 @@ namespace SourceGit.ViewModels
CallUIThread(() => CallUIThread(() =>
{ {
var repo = Preference.AddRepository(_targetPath, gitDir); var repo = Preference.AddRepository(_targetPath, gitDir);
Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null); Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, _parentNode, true);
}); });
return true; return true;
}); });
} }
private string _targetPath; private string _targetPath = string.Empty;
private RepositoryNode _parentNode = null;
} }
} }

View file

@ -49,7 +49,7 @@ namespace SourceGit.ViewModels
var gitDir = new Commands.QueryGitDir(root).Result(); var gitDir = new Commands.QueryGitDir(root).Result();
var repo = Preference.AddRepository(root, gitDir); var repo = Preference.AddRepository(root, gitDir);
var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null); var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null, false);
OpenRepositoryInTab(node, null); OpenRepositoryInTab(node, null);
} }
else if (Preference.Instance.RestoreTabs) else if (Preference.Instance.RestoreTabs)

View file

@ -363,7 +363,7 @@ namespace SourceGit.ViewModels
return FindNodeRecursive(id, _instance.RepositoryNodes); return FindNodeRecursive(id, _instance.RepositoryNodes);
} }
public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent) public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent, bool shouldMoveNode)
{ {
var node = FindNodeRecursive(repo, _instance.RepositoryNodes); var node = FindNodeRecursive(repo, _instance.RepositoryNodes);
if (node == null) if (node == null)
@ -378,7 +378,7 @@ namespace SourceGit.ViewModels
AddNode(node, parent); AddNode(node, parent);
} }
else else if (shouldMoveNode)
{ {
MoveNode(node, parent); MoveNode(node, parent);
} }

View file

@ -26,7 +26,7 @@ namespace SourceGit.ViewModels
} }
} }
public void InitRepository(string path) public void InitRepository(string path, RepositoryNode parent)
{ {
if (!Preference.Instance.IsGitConfigured) if (!Preference.Instance.IsGitConfigured)
{ {
@ -36,7 +36,7 @@ namespace SourceGit.ViewModels
if (PopupHost.CanCreatePopup()) if (PopupHost.CanCreatePopup())
{ {
PopupHost.ShowPopup(new Init(path)); PopupHost.ShowPopup(new Init(path, parent));
} }
} }

View file

@ -248,7 +248,7 @@ namespace SourceGit.Views
{ {
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
(DataContext as ViewModels.Welcome).InitRepository(path); (DataContext as ViewModels.Welcome).InitRepository(path, parent);
}); });
return; return;
} }
@ -257,7 +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 = ViewModels.Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, parent); var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, parent, true);
launcher.OpenRepositoryInTab(node, page); launcher.OpenRepositoryInTab(node, page);
}); });
}); });