enhance: show git errors when failed to get the repository root dir of given path (#397)

This commit is contained in:
leo 2024-08-27 15:35:10 +08:00
parent 963916219f
commit b16d267c9b
No known key found for this signature in database
14 changed files with 40 additions and 39 deletions

View file

@ -17,8 +17,9 @@ namespace SourceGit.Commands
public class ReadToEndResult public class ReadToEndResult
{ {
public bool IsSuccess { get; set; } public bool IsSuccess { get; set; } = false;
public string StdOut { get; set; } public string StdOut { get; set; } = "";
public string StdErr { get; set; } = "";
} }
public enum EditorType public enum EditorType
@ -198,18 +199,20 @@ namespace SourceGit.Commands
{ {
proc.Start(); proc.Start();
} }
catch catch (Exception e)
{ {
return new ReadToEndResult() return new ReadToEndResult()
{ {
IsSuccess = false, IsSuccess = false,
StdOut = string.Empty, StdOut = string.Empty,
StdErr = e.Message,
}; };
} }
var rs = new ReadToEndResult() var rs = new ReadToEndResult()
{ {
StdOut = proc.StandardOutput.ReadToEnd(), StdOut = proc.StandardOutput.ReadToEnd(),
StdErr = proc.StandardError.ReadToEnd(),
}; };
proc.WaitForExit(); proc.WaitForExit();

View file

@ -6,15 +6,6 @@
{ {
WorkingDirectory = path; WorkingDirectory = path;
Args = "rev-parse --show-toplevel"; Args = "rev-parse --show-toplevel";
RaiseError = false;
}
public string Result()
{
var rs = ReadToEnd().StdOut;
if (string.IsNullOrEmpty(rs))
return null;
return rs.Trim();
} }
} }
} }

View file

@ -334,7 +334,6 @@
<x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Verwerfen</x:String> <x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Verwerfen</x:String>
<x:String x:Key="Text.Init" xml:space="preserve">Initialisiere Repository</x:String> <x:String x:Key="Text.Init" xml:space="preserve">Initialisiere Repository</x:String>
<x:String x:Key="Text.Init.Path" xml:space="preserve">Pfad:</x:String> <x:String x:Key="Text.Init.Path" xml:space="preserve">Pfad:</x:String>
<x:String x:Key="Text.Init.Tip" xml:space="preserve">Ungültiges Repository erkannt. `git init` auf diesem Pfad ausführen?</x:String>
<x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick wird durchgeführt. Drücke 'Abbrechen' um den originalen HEAD wiederherzustellen.</x:String> <x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick wird durchgeführt. Drücke 'Abbrechen' um den originalen HEAD wiederherzustellen.</x:String>
<x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge request wird durchgeführt. Drücke 'Abbrechen' um den originalen HEAD wiederherzustellen.</x:String> <x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge request wird durchgeführt. Drücke 'Abbrechen' um den originalen HEAD wiederherzustellen.</x:String>
<x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase wird durchgeführt. Drücke 'Abbrechen' um den originalen HEAD wiederherzustellen.</x:String> <x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase wird durchgeführt. Drücke 'Abbrechen' um den originalen HEAD wiederherzustellen.</x:String>

View file

@ -336,7 +336,6 @@
<x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Discard</x:String> <x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Discard</x:String>
<x:String x:Key="Text.Init" xml:space="preserve">Initialize Repository</x:String> <x:String x:Key="Text.Init" xml:space="preserve">Initialize Repository</x:String>
<x:String x:Key="Text.Init.Path" xml:space="preserve">Path:</x:String> <x:String x:Key="Text.Init.Path" xml:space="preserve">Path:</x:String>
<x:String x:Key="Text.Init.Tip" xml:space="preserve">Invalid repository detected. Run `git init` under this path?</x:String>
<x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick in progress. Press 'Abort' to restore original HEAD.</x:String> <x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick in progress. Press 'Abort' to restore original HEAD.</x:String>
<x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge request in progress. Press 'Abort' to restore original HEAD.</x:String> <x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge request in progress. Press 'Abort' to restore original HEAD.</x:String>
<x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase in progress. Press 'Abort' to restore original HEAD.</x:String> <x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase in progress. Press 'Abort' to restore original HEAD.</x:String>

View file

@ -337,7 +337,6 @@
<x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Rejeter</x:String> <x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Rejeter</x:String>
<x:String x:Key="Text.Init" xml:space="preserve">Initialize Repository</x:String> <x:String x:Key="Text.Init" xml:space="preserve">Initialize Repository</x:String>
<x:String x:Key="Text.Init.Path" xml:space="preserve">Path:</x:String> <x:String x:Key="Text.Init.Path" xml:space="preserve">Path:</x:String>
<x:String x:Key="Text.Init.Tip" xml:space="preserve">Invalid repository detected. Run `git init` under this path?</x:String>
<x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick in progress. Press 'Abort' to restore original HEAD.</x:String> <x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick in progress. Press 'Abort' to restore original HEAD.</x:String>
<x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge request in progress. Press 'Abort' to restore original HEAD.</x:String> <x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge request in progress. Press 'Abort' to restore original HEAD.</x:String>
<x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase in progress. Press 'Abort' to restore original HEAD.</x:String> <x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase in progress. Press 'Abort' to restore original HEAD.</x:String>

View file

@ -332,7 +332,6 @@
<x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Descartar</x:String> <x:String x:Key="Text.Hunk.Discard" xml:space="preserve">Descartar</x:String>
<x:String x:Key="Text.Init" xml:space="preserve">Inicializar Repositório</x:String> <x:String x:Key="Text.Init" xml:space="preserve">Inicializar Repositório</x:String>
<x:String x:Key="Text.Init.Path" xml:space="preserve">Caminho:</x:String> <x:String x:Key="Text.Init.Path" xml:space="preserve">Caminho:</x:String>
<x:String x:Key="Text.Init.Tip" xml:space="preserve">Repositório inválido detectado. Executar `git init` neste caminho?</x:String>
<x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick em andamento. Pressione 'Abort' para restaurar o HEAD original.</x:String> <x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">Cherry-Pick em andamento. Pressione 'Abort' para restaurar o HEAD original.</x:String>
<x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge em andamento. Pressione 'Abort' para restaurar o HEAD original.</x:String> <x:String x:Key="Text.InProgress.Merge" xml:space="preserve">Merge em andamento. Pressione 'Abort' para restaurar o HEAD original.</x:String>
<x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase em andamento. Pressione 'Abort' para restaurar o HEAD original.</x:String> <x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">Rebase em andamento. Pressione 'Abort' para restaurar o HEAD original.</x:String>

View file

@ -339,7 +339,6 @@
<x:String x:Key="Text.Hunk.Discard" xml:space="preserve">丢弃</x:String> <x:String x:Key="Text.Hunk.Discard" xml:space="preserve">丢弃</x:String>
<x:String x:Key="Text.Init" xml:space="preserve">初始化新仓库</x:String> <x:String x:Key="Text.Init" xml:space="preserve">初始化新仓库</x:String>
<x:String x:Key="Text.Init.Path" xml:space="preserve">路径 </x:String> <x:String x:Key="Text.Init.Path" xml:space="preserve">路径 </x:String>
<x:String x:Key="Text.Init.Tip" xml:space="preserve">选择目录不是有效的Git仓库。是否需要在此目录执行`git init`操作?</x:String>
<x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">挑选Cherry-Pick操作进行中。点击【终止】回滚到操作前的状态。</x:String> <x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">挑选Cherry-Pick操作进行中。点击【终止】回滚到操作前的状态。</x:String>
<x:String x:Key="Text.InProgress.Merge" xml:space="preserve">合并操作进行中。点击【终止】回滚到操作前的状态。</x:String> <x:String x:Key="Text.InProgress.Merge" xml:space="preserve">合并操作进行中。点击【终止】回滚到操作前的状态。</x:String>
<x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">变基Rebase操作进行中。点击【终止】回滚到操作前的状态。</x:String> <x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">变基Rebase操作进行中。点击【终止】回滚到操作前的状态。</x:String>

View file

@ -339,7 +339,6 @@
<x:String x:Key="Text.Hunk.Discard" xml:space="preserve">捨棄</x:String> <x:String x:Key="Text.Hunk.Discard" xml:space="preserve">捨棄</x:String>
<x:String x:Key="Text.Init" xml:space="preserve">初始化存放庫</x:String> <x:String x:Key="Text.Init" xml:space="preserve">初始化存放庫</x:String>
<x:String x:Key="Text.Init.Path" xml:space="preserve">路徑:</x:String> <x:String x:Key="Text.Init.Path" xml:space="preserve">路徑:</x:String>
<x:String x:Key="Text.Init.Tip" xml:space="preserve">選擇目錄並非有效的 Git 存放庫。是否要在此目錄執行 `git init` 以進行初始化?</x:String>
<x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">揀選 (cherry-pick) 操作進行中。點選 [中止] 復原到操作前的狀態。</x:String> <x:String x:Key="Text.InProgress.CherryPick" xml:space="preserve">揀選 (cherry-pick) 操作進行中。點選 [中止] 復原到操作前的狀態。</x:String>
<x:String x:Key="Text.InProgress.Merge" xml:space="preserve">合併操作進行中。點選 [中止] 復原到操作前的狀態。</x:String> <x:String x:Key="Text.InProgress.Merge" xml:space="preserve">合併操作進行中。點選 [中止] 復原到操作前的狀態。</x:String>
<x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">重定基底 (rebase) 操作進行中。點選 [中止] 復原到操作前的狀態。</x:String> <x:String x:Key="Text.InProgress.Rebase" xml:space="preserve">重定基底 (rebase) 操作進行中。點選 [中止] 復原到操作前的狀態。</x:String>

View file

@ -10,11 +10,18 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _targetPath, value); set => SetProperty(ref _targetPath, value);
} }
public Init(string path, RepositoryNode parent) public string Reason
{
get;
private set;
}
public Init(string path, RepositoryNode parent, string reason)
{ {
_targetPath = path; _targetPath = path;
_parentNode = parent; _parentNode = parent;
Reason = string.IsNullOrEmpty(reason) ? "Invalid repository detected!" : reason;
View = new Views.Init() { DataContext = this }; View = new Views.Init() { DataContext = this };
} }

View file

@ -35,8 +35,8 @@ namespace SourceGit.ViewModels
var pref = Preference.Instance; var pref = Preference.Instance;
if (!string.IsNullOrEmpty(startupRepo)) if (!string.IsNullOrEmpty(startupRepo))
{ {
var root = new Commands.QueryRepositoryRootPath(startupRepo).Result(); var test = new Commands.QueryRepositoryRootPath(startupRepo).ReadToEnd();
if (string.IsNullOrEmpty(root)) if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
{ {
Pages[0].Notifications.Add(new Models.Notification Pages[0].Notifications.Add(new Models.Notification
{ {
@ -46,7 +46,7 @@ namespace SourceGit.ViewModels
return; return;
} }
var normalized = root.Replace("\\", "/"); var normalized = test.StdOut.Trim().Replace("\\", "/");
var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false); var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false);
Welcome.Instance.Refresh(); Welcome.Instance.Refresh();
OpenRepositoryInTab(node, null); OpenRepositoryInTab(node, null);

View file

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

View file

@ -10,16 +10,22 @@
<TextBlock FontSize="18" <TextBlock FontSize="18"
Classes="bold" Classes="bold"
Text="{DynamicResource Text.Init}"/> Text="{DynamicResource Text.Init}"/>
<Grid Margin="0,16,8,0" RowDefinitions="28,28" ColumnDefinitions="120,*"> <Grid Margin="0,16,8,0" ColumnDefinitions="100,*">
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="Auto" MinHeight="32"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" <TextBlock Grid.Row="0" Grid.Column="0"
Text="{DynamicResource Text.Init.Path}" Text="{DynamicResource Text.Init.Path}"
HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" <TextBlock Grid.Row="0" Grid.Column="1"
Text="{Binding TargetPath}"/> Text="{Binding TargetPath}"/>
<TextBlock Grid.Row="1" Grid.Column="1" <TextBlock Grid.Row="1" Grid.Column="1"
Text="{DynamicResource Text.Init.Tip}" Foreground="{DynamicResource Brush.FG2}"
Foreground="{DynamicResource Brush.FG2}"/> Text="{Binding Reason}"
TextWrapping="Wrap"/>
</Grid> </Grid>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View file

@ -279,14 +279,14 @@ namespace SourceGit.Views
return; return;
} }
var root = new Commands.QueryRepositoryRootPath(path).Result(); var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd();
if (string.IsNullOrEmpty(root)) if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
{ {
ViewModels.Welcome.Instance.InitRepository(path, parent); ViewModels.Welcome.Instance.InitRepository(path, parent, test.StdErr);
return; return;
} }
var normalizedPath = root.Replace("\\", "/"); var normalizedPath = test.StdOut.Trim().Replace("\\", "/");
var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true); var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
ViewModels.Welcome.Instance.Refresh(); ViewModels.Welcome.Instance.Refresh();

View file

@ -55,14 +55,14 @@ namespace SourceGit.Views
return; return;
} }
var root = new Commands.QueryRepositoryRootPath(path).Result(); var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd();
if (string.IsNullOrEmpty(root)) if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
{ {
ViewModels.Welcome.Instance.InitRepository(path, parent); ViewModels.Welcome.Instance.InitRepository(path, parent, test.StdErr);
return; return;
} }
var normalizedPath = root.Replace("\\", "/"); var normalizedPath = test.StdOut.Trim().Replace("\\", "/");
var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, false); var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, false);
ViewModels.Welcome.Instance.Refresh(); ViewModels.Welcome.Instance.Refresh();