style<Repository>: normalize repository's path

This commit is contained in:
leo 2024-03-01 13:40:12 +08:00
parent 1c005983c7
commit e3a7abe776
7 changed files with 26 additions and 19 deletions

View file

@ -66,17 +66,15 @@ namespace SourceGit {
public static void RaiseException(string context, string message) { public static void RaiseException(string context, string message) {
if (Current is App app && app._notificationReceiver != null) { if (Current is App app && app._notificationReceiver != null) {
var ctx = context.Replace('\\', '/');
var notice = new Models.Notification() { IsError = true, Message = message }; 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) { public static void SendNotification(string context, string message) {
if (Current is App app && app._notificationReceiver != null) { if (Current is App app && app._notificationReceiver != null) {
var ctx = context.Replace('\\', '/');
var notice = new Models.Notification() { IsError = false, Message = message }; var notice = new Models.Notification() { IsError = false, Message = message };
app._notificationReceiver.OnReceiveNotification(ctx, notice); app._notificationReceiver.OnReceiveNotification(context, notice);
} }
} }

View file

@ -89,8 +89,8 @@ 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 = new RepositoryNode() {
Id = path, Id = repo.FullPath,
Name = Path.GetFileName(path), Name = Path.GetFileName(repo.FullPath),
Bookmark = 0, Bookmark = 0,
IsRepository = true, IsRepository = true,
}; };

View file

@ -25,8 +25,8 @@ namespace SourceGit.ViewModels {
CallUIThread(() => { CallUIThread(() => {
var repo = Preference.AddRepository(_targetPath, gitDir); var repo = Preference.AddRepository(_targetPath, gitDir);
var node = new RepositoryNode() { var node = new RepositoryNode() {
Id = _targetPath, Id = repo.FullPath,
Name = Path.GetFileName(_targetPath), Name = Path.GetFileName(repo.FullPath),
Bookmark = 0, Bookmark = 0,
IsRepository = true, IsRepository = true,
}; };

View file

@ -209,23 +209,22 @@ namespace SourceGit.ViewModels {
} }
public static Repository FindRepository(string path) { public static Repository FindRepository(string path) {
var dir = new DirectoryInfo(path);
foreach (var repo in _instance.Repositories) { foreach (var repo in _instance.Repositories) {
if (repo.FullPath == dir.FullName) return repo; if (repo.FullPath == path) return repo;
} }
return null; return null;
} }
public static Repository AddRepository(string rootDir, string gitDir) { public static Repository AddRepository(string rootDir, string gitDir) {
var repo = FindRepository(rootDir); var normalized = rootDir.Replace('\\', '/');
var repo = FindRepository(normalized);
if (repo != null) { if (repo != null) {
repo.GitDir = gitDir; repo.GitDir = gitDir;
return repo; return repo;
} }
var dir = new DirectoryInfo(rootDir);
repo = new Repository() { repo = new Repository() {
FullPath = dir.FullName, FullPath = normalized,
GitDir = gitDir GitDir = gitDir
}; };

View file

@ -12,7 +12,14 @@ namespace SourceGit.ViewModels {
public class Repository : ObservableObject, Models.IRepository { public class Repository : ObservableObject, Models.IRepository {
public string FullPath { public string FullPath {
get => _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 { public string GitDir {
@ -1009,8 +1016,8 @@ 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 = new RepositoryNode() { var node = new RepositoryNode() {
Id = root, Id = repo.FullPath,
Name = Path.GetFileName(root), Name = Path.GetFileName(repo.FullPath),
Bookmark = 0, Bookmark = 0,
IsRepository = true, IsRepository = true,
}; };

View file

@ -6,7 +6,10 @@ namespace SourceGit.ViewModels {
public class RepositoryNode : ObservableObject { public class RepositoryNode : ObservableObject {
public string Id { public string Id {
get => _id; get => _id;
set => SetProperty(ref _id, value); set {
var normalized = value.Replace('\\', '/');
SetProperty(ref _id, normalized);
}
} }
public string Name { public string Name {

View file

@ -192,8 +192,8 @@ 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 = new ViewModels.RepositoryNode() {
Id = root, Id = repo.FullPath,
Name = Path.GetFileName(root), Name = Path.GetFileName(repo.FullPath),
Bookmark = 0, Bookmark = 0,
IsRepository = true, IsRepository = true,
}; };