Skip to content

Commit 7e88bef

Browse files
committed
handle non-existent working directory
1 parent 541e482 commit 7e88bef

1 file changed

Lines changed: 60 additions & 31 deletions

File tree

GitContentSearch.UI/ViewModels/MainWindowViewModel.cs

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,70 @@ private async Task HandleFilePathLostFocusAsync()
3838
if (string.IsNullOrWhiteSpace(FilePath) || string.IsNullOrWhiteSpace(WorkingDirectory))
3939
return;
4040

41-
var processWrapper = new ProcessWrapper();
42-
var absolutePath = Path.IsPathRooted(FilePath)
43-
? FilePath
44-
: Path.GetFullPath(Path.Combine(WorkingDirectory, FilePath));
45-
46-
var result = await Task.Run(() =>
47-
processWrapper.Start("rev-parse --show-toplevel", Path.GetDirectoryName(absolutePath), null));
48-
49-
if (result.ExitCode == 0)
41+
try
5042
{
51-
var gitRoot = result.StandardOutput.Trim().Replace('/', '\\'); // Normalize to Windows path
43+
var processWrapper = new ProcessWrapper();
44+
var absolutePath = Path.IsPathRooted(FilePath)
45+
? FilePath
46+
: Path.GetFullPath(Path.Combine(WorkingDirectory, FilePath));
47+
48+
var directoryPath = Path.GetDirectoryName(absolutePath);
49+
if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath))
50+
{
51+
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
52+
{
53+
LogOutput.Add($"Error: Directory '{directoryPath}' does not exist or is invalid.");
54+
FilePath = string.Empty;
55+
WorkingDirectory = string.Empty;
56+
});
57+
return;
58+
}
59+
60+
var result = await Task.Run(() =>
61+
processWrapper.Start("rev-parse --show-toplevel", directoryPath, null));
5262

53-
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
63+
if (result.ExitCode == 0)
5464
{
55-
LogOutput.Add($"Git Root: {gitRoot}");
56-
LogOutput.Add($"File Path: {absolutePath}");
57-
58-
WorkingDirectory = gitRoot;
65+
var gitRoot = result.StandardOutput.Trim().Replace('/', '\\'); // Normalize to Windows path
5966

60-
// Convert paths to the same format and case for comparison
61-
if (absolutePath.StartsWith(gitRoot, StringComparison.OrdinalIgnoreCase))
67+
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
6268
{
63-
var relativePath = absolutePath[gitRoot.Length..].TrimStart('\\', '/');
64-
FilePath = relativePath.Replace('\\', '/');
65-
LogOutput.Add($"Relative Path: {FilePath}");
66-
}
67-
else
69+
LogOutput.Add($"Git Root: {gitRoot}");
70+
LogOutput.Add($"File Path: {absolutePath}");
71+
72+
WorkingDirectory = gitRoot;
73+
74+
// Convert paths to the same format and case for comparison
75+
if (absolutePath.StartsWith(gitRoot, StringComparison.OrdinalIgnoreCase))
76+
{
77+
var relativePath = absolutePath[gitRoot.Length..].TrimStart('\\', '/');
78+
FilePath = relativePath.Replace('\\', '/');
79+
LogOutput.Add($"Relative Path: {FilePath}");
80+
}
81+
else
82+
{
83+
LogOutput.Add("Warning: File path does not start with git root path.");
84+
FilePath = string.Empty;
85+
}
86+
});
87+
}
88+
else
89+
{
90+
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
6891
{
69-
LogOutput.Add("Warning: File path does not start with git root path.");
92+
LogOutput.Add("Warning: Selected file is not in a Git repository. Please select a file within a Git repository.");
7093
FilePath = string.Empty;
71-
}
72-
});
94+
WorkingDirectory = string.Empty;
95+
});
96+
}
7397
}
74-
else
98+
catch (Exception ex)
7599
{
76-
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
100+
LogOutput.Add($"Error: {ex.Message}");
101+
if (ex.InnerException != null)
77102
{
78-
LogOutput.Add("Warning: Selected file is not in a Git repository. Please select a file within a Git repository.");
79-
FilePath = string.Empty;
80-
WorkingDirectory = string.Empty;
81-
});
103+
LogOutput.Add($"Inner Error: {ex.InnerException.Message}");
104+
}
82105
}
83106
}
84107

@@ -286,6 +309,12 @@ private async Task StartSearchAsync()
286309
LogOutput.Clear();
287310
try
288311
{
312+
if (!Directory.Exists(WorkingDirectory))
313+
{
314+
LogOutput.Add($"Error: Working directory '{WorkingDirectory}' does not exist or is invalid.");
315+
return;
316+
}
317+
289318
var processWrapper = new ProcessWrapper();
290319
string logAndTempFileDirectory = LogDirectory;
291320
if (string.IsNullOrEmpty(logAndTempFileDirectory))

0 commit comments

Comments
 (0)