From 78bb68b82e7b1d3b268fa4e557abbc3f098b57a2 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 4 Aug 2021 19:11:22 +0800 Subject: [PATCH] fix: fix crash reported by https://gitee.com/sourcegit/sourcegit/issues/I43UGJ --- src/Commands/QueryGitDir.cs | 2 +- src/Views/Popups/Clone.xaml.cs | 17 ++++++++++++----- src/Views/Popups/Init.xaml.cs | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Commands/QueryGitDir.cs b/src/Commands/QueryGitDir.cs index dd421a21..af45273d 100644 --- a/src/Commands/QueryGitDir.cs +++ b/src/Commands/QueryGitDir.cs @@ -17,7 +17,7 @@ namespace SourceGit.Commands { rs = rs.Trim(); if (Path.IsPathRooted(rs)) return rs; - return Path.Combine(Cwd, rs); + return Path.GetFullPath(Path.Combine(Cwd, rs)); } } } diff --git a/src/Views/Popups/Clone.xaml.cs b/src/Views/Popups/Clone.xaml.cs index 6bd9d1d5..a19fb3cb 100644 --- a/src/Views/Popups/Clone.xaml.cs +++ b/src/Views/Popups/Clone.xaml.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Threading.Tasks; using System.Windows.Controls; @@ -40,14 +41,20 @@ namespace SourceGit.Views.Popups { var path = Folder; if (!string.IsNullOrEmpty(LocalName)) { - path += $"/{LocalName}"; + path = Path.GetFullPath(Path.Combine(path, LocalName)); } else { - var idx = Uri.LastIndexOfAny(new char[] { '\\', '/' }); - var name = Uri.Substring(idx + 1); - path += $"/{name.Replace(".git", "")}"; + var name = Path.GetFileName(Uri); + if (name.EndsWith(".git")) name = name.Substring(0, name.Length - 4); + path = Path.GetFullPath(Path.Combine(path, name)); } - var repo = Models.Preference.Instance.AddRepository(path, path + "/.git", ""); + if (!Directory.Exists(path)) { + Models.Exception.Raise($"Folder {path} not found!"); + return false; + } + + var gitDir = new Commands.QueryGitDir(path).Result(); + var repo = Models.Preference.Instance.AddRepository(path, gitDir, ""); if (repo != null) Dispatcher.Invoke(() => Models.Watcher.Open(repo)); return true; }); diff --git a/src/Views/Popups/Init.xaml.cs b/src/Views/Popups/Init.xaml.cs index 53deb24d..d89e5d4d 100644 --- a/src/Views/Popups/Init.xaml.cs +++ b/src/Views/Popups/Init.xaml.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Threading.Tasks; namespace SourceGit.Views.Popups { @@ -22,7 +23,8 @@ namespace SourceGit.Views.Popups { var succ = new Commands.Init(WorkDir).Exec(); if (!succ) return false; - var repo = Models.Preference.Instance.AddRepository(WorkDir, WorkDir + "\\.git", ""); + var gitDir = Path.GetFullPath(Path.Combine(WorkDir, ".git")); + var repo = Models.Preference.Instance.AddRepository(WorkDir, gitDir, ""); Dispatcher.Invoke(() => Models.Watcher.Open(repo)); return true; });