From e3a7abe77609b1f85ce4b32c87b23184433005a6 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 1 Mar 2024 13:40:12 +0800 Subject: [PATCH] style: normalize repository's path --- src/App.axaml.cs | 6 ++---- src/ViewModels/Clone.cs | 4 ++-- src/ViewModels/Init.cs | 4 ++-- src/ViewModels/Preference.cs | 9 ++++----- src/ViewModels/Repository.cs | 13 ++++++++++--- src/ViewModels/RepositoryNode.cs | 5 ++++- src/Views/Welcome.axaml.cs | 4 ++-- 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 4d01480a..96276fb9 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -66,17 +66,15 @@ namespace SourceGit { public static void RaiseException(string context, string message) { if (Current is App app && app._notificationReceiver != null) { - var ctx = context.Replace('\\', '/'); var notice = new Models.Notification() { IsError = true, Message = message }; - app._notificationReceiver.OnReceiveNotification(ctx, notice); + app._notificationReceiver.OnReceiveNotification(context, notice); } } public static void SendNotification(string context, string message) { if (Current is App app && app._notificationReceiver != null) { - var ctx = context.Replace('\\', '/'); var notice = new Models.Notification() { IsError = false, Message = message }; - app._notificationReceiver.OnReceiveNotification(ctx, notice); + app._notificationReceiver.OnReceiveNotification(context, notice); } } diff --git a/src/ViewModels/Clone.cs b/src/ViewModels/Clone.cs index afc6cb90..7c06f7fe 100644 --- a/src/ViewModels/Clone.cs +++ b/src/ViewModels/Clone.cs @@ -89,8 +89,8 @@ namespace SourceGit.ViewModels { CallUIThread(() => { var repo = Preference.AddRepository(path, Path.Combine(path, ".git")); var node = new RepositoryNode() { - Id = path, - Name = Path.GetFileName(path), + Id = repo.FullPath, + Name = Path.GetFileName(repo.FullPath), Bookmark = 0, IsRepository = true, }; diff --git a/src/ViewModels/Init.cs b/src/ViewModels/Init.cs index 9381b568..985453a0 100644 --- a/src/ViewModels/Init.cs +++ b/src/ViewModels/Init.cs @@ -25,8 +25,8 @@ namespace SourceGit.ViewModels { CallUIThread(() => { var repo = Preference.AddRepository(_targetPath, gitDir); var node = new RepositoryNode() { - Id = _targetPath, - Name = Path.GetFileName(_targetPath), + Id = repo.FullPath, + Name = Path.GetFileName(repo.FullPath), Bookmark = 0, IsRepository = true, }; diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index e0f9c173..c453e63c 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -209,23 +209,22 @@ namespace SourceGit.ViewModels { } public static Repository FindRepository(string path) { - var dir = new DirectoryInfo(path); foreach (var repo in _instance.Repositories) { - if (repo.FullPath == dir.FullName) return repo; + if (repo.FullPath == path) return repo; } return null; } public static Repository AddRepository(string rootDir, string gitDir) { - var repo = FindRepository(rootDir); + var normalized = rootDir.Replace('\\', '/'); + var repo = FindRepository(normalized); if (repo != null) { repo.GitDir = gitDir; return repo; } - var dir = new DirectoryInfo(rootDir); repo = new Repository() { - FullPath = dir.FullName, + FullPath = normalized, GitDir = gitDir }; diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 69821e2d..6caf4f12 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -12,7 +12,14 @@ namespace SourceGit.ViewModels { public class Repository : ObservableObject, Models.IRepository { public string FullPath { get => _fullpath; - set => SetProperty(ref _fullpath, value); + set { + if (value != null) { + var normalized = value.Replace('\\', '/'); + SetProperty(ref _fullpath, normalized); + } else { + SetProperty(ref _fullpath, null); + } + } } public string GitDir { @@ -1009,8 +1016,8 @@ namespace SourceGit.ViewModels { var gitDir = new Commands.QueryGitDir(root).Result(); var repo = Preference.AddRepository(root, gitDir); var node = new RepositoryNode() { - Id = root, - Name = Path.GetFileName(root), + Id = repo.FullPath, + Name = Path.GetFileName(repo.FullPath), Bookmark = 0, IsRepository = true, }; diff --git a/src/ViewModels/RepositoryNode.cs b/src/ViewModels/RepositoryNode.cs index 6d3979ae..0955f028 100644 --- a/src/ViewModels/RepositoryNode.cs +++ b/src/ViewModels/RepositoryNode.cs @@ -6,7 +6,10 @@ namespace SourceGit.ViewModels { public class RepositoryNode : ObservableObject { public string Id { get => _id; - set => SetProperty(ref _id, value); + set { + var normalized = value.Replace('\\', '/'); + SetProperty(ref _id, normalized); + } } public string Name { diff --git a/src/Views/Welcome.axaml.cs b/src/Views/Welcome.axaml.cs index 23a11348..3dd23bb3 100644 --- a/src/Views/Welcome.axaml.cs +++ b/src/Views/Welcome.axaml.cs @@ -192,8 +192,8 @@ namespace SourceGit.Views { Dispatcher.UIThread.Invoke(() => { var repo = ViewModels.Preference.AddRepository(root, gitDir); var node = new ViewModels.RepositoryNode() { - Id = root, - Name = Path.GetFileName(root), + Id = repo.FullPath, + Name = Path.GetFileName(repo.FullPath), Bookmark = 0, IsRepository = true, };