From 5f4c1bb984c1f2a76d241a0536c30de03b557b27 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 13 Jan 2025 17:37:23 +0800 Subject: [PATCH] enhance: do not show `Initialize Repository` popup for bare repositories (#891) --- src/ViewModels/Welcome.cs | 31 ++++++++++++++++++++++++++ src/Views/Welcome.axaml.cs | 36 ++----------------------------- src/Views/WelcomeToolbar.axaml.cs | 27 +---------------------- 3 files changed, 34 insertions(+), 60 deletions(-) diff --git a/src/ViewModels/Welcome.cs b/src/ViewModels/Welcome.cs index e6d64469..5c9c9380 100644 --- a/src/ViewModels/Welcome.cs +++ b/src/ViewModels/Welcome.cs @@ -83,6 +83,37 @@ namespace SourceGit.ViewModels } } + public void OpenOrInitRepository(string path, RepositoryNode parent, bool bMoveExistedNode) + { + if (!Directory.Exists(path)) + { + if (File.Exists(path)) + path = Path.GetDirectoryName(path); + else + return; + } + + var isBare = new Commands.IsBareRepository(path).Result(); + if (isBare) + { + App.RaiseException(string.Empty, $"'{path}' is a bare repository, which is not supported by SourceGit!"); + return; + } + + var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd(); + if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut)) + { + InitRepository(path, parent, test.StdErr); + return; + } + + var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), parent, bMoveExistedNode); + Refresh(); + + var launcher = App.GetLauncer(); + launcher?.OpenRepositoryInTab(node, launcher.ActivePage); + } + public void InitRepository(string path, RepositoryNode parent, string reason) { if (!Preferences.Instance.IsGitConfigured()) diff --git a/src/Views/Welcome.axaml.cs b/src/Views/Welcome.axaml.cs index ffb8332b..3ab11782 100644 --- a/src/Views/Welcome.axaml.cs +++ b/src/Views/Welcome.axaml.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using Avalonia; using Avalonia.Controls; @@ -197,7 +196,7 @@ namespace SourceGit.Views { foreach (var item in items) { - OpenOrInitRepository(item.Path.LocalPath); + ViewModels.Welcome.Instance.OpenOrInitRepository(item.Path.LocalPath, null, true); break; } } @@ -261,7 +260,7 @@ namespace SourceGit.Views { foreach (var item in items) { - OpenOrInitRepository(item.Path.LocalPath, to); + ViewModels.Welcome.Instance.OpenOrInitRepository(item.Path.LocalPath, to, true); break; } } @@ -290,37 +289,6 @@ namespace SourceGit.Views } } - private void OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null) - { - if (!Directory.Exists(path)) - { - if (File.Exists(path)) - path = Path.GetDirectoryName(path); - else - return; - } - - var isBare = new Commands.IsBareRepository(path).Result(); - if (isBare) - { - App.RaiseException(string.Empty, $"'{path}' is a bare repository, which is not supported by SourceGit!"); - return; - } - - var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd(); - if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut)) - { - ViewModels.Welcome.Instance.InitRepository(path, parent, test.StdErr); - return; - } - - var node = ViewModels.Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), parent, true); - ViewModels.Welcome.Instance.Refresh(); - - var launcher = this.FindAncestorOfType()?.DataContext as ViewModels.Launcher; - launcher?.OpenRepositoryInTab(node, launcher.ActivePage); - } - private bool _pressedTreeNode = false; private Point _pressedTreeNodePosition = new Point(); private bool _startDragTreeNode = false; diff --git a/src/Views/WelcomeToolbar.axaml.cs b/src/Views/WelcomeToolbar.axaml.cs index f1ff14f3..9e170c1a 100644 --- a/src/Views/WelcomeToolbar.axaml.cs +++ b/src/Views/WelcomeToolbar.axaml.cs @@ -4,7 +4,6 @@ using System.IO; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; -using Avalonia.VisualTree; namespace SourceGit.Views { @@ -36,7 +35,7 @@ namespace SourceGit.Views { var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options); if (selected.Count == 1) - OpenOrInitRepository(selected[0].Path.LocalPath); + ViewModels.Welcome.Instance.OpenOrInitRepository(selected[0].Path.LocalPath, null, false); } catch (Exception exception) { @@ -45,30 +44,6 @@ namespace SourceGit.Views e.Handled = true; } - - private void OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null) - { - if (!Directory.Exists(path)) - { - if (File.Exists(path)) - path = Path.GetDirectoryName(path); - else - return; - } - - var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd(); - if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut)) - { - ViewModels.Welcome.Instance.InitRepository(path, parent, test.StdErr); - return; - } - - var node = ViewModels.Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), parent, false); - ViewModels.Welcome.Instance.Refresh(); - - var launcher = this.FindAncestorOfType()?.DataContext as ViewModels.Launcher; - launcher?.OpenRepositoryInTab(node, launcher.ActivePage); - } } }