fix: avoid crash when closing a repo which is deleted in file system (#1878)

related to #415
This commit is contained in:
Gadfly
2025-10-27 11:34:10 +08:00
committed by GitHub
parent cf63253659
commit ac660aafa5
7 changed files with 141 additions and 49 deletions

View File

@@ -565,15 +565,22 @@ namespace SourceGit.ViewModels
SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect
Logs.Clear();
if (!_isWorktree)
if (!_isWorktree && Directory.Exists(_gitCommonDir))
{
if (_workingCopy.InProgressContext != null && !string.IsNullOrEmpty(_workingCopy.CommitMessage))
File.WriteAllText(Path.Combine(GitDir, "MERGE_MSG"), _workingCopy.CommitMessage);
else
_settings.LastCommitMessage = _workingCopy.CommitMessage;
try
{
if (_workingCopy.InProgressContext != null && !string.IsNullOrEmpty(_workingCopy.CommitMessage))
File.WriteAllText(Path.Combine(GitDir, "MERGE_MSG"), _workingCopy.CommitMessage);
else
_settings.LastCommitMessage = _workingCopy.CommitMessage;
using var stream = File.Create(Path.Combine(_gitCommonDir, "sourcegit.settings"));
JsonSerializer.Serialize(stream, _settings, JsonCodeGen.Default.RepositorySettings);
using var stream = File.Create(Path.Combine(_gitCommonDir, "sourcegit.settings"));
JsonSerializer.Serialize(stream, _settings, JsonCodeGen.Default.RepositorySettings);
}
catch (Exception)
{
// Ignore
}
}
if (_cancellationRefreshBranches is { IsCancellationRequested: false })

View File

@@ -37,11 +37,18 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
try
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(selected, saveTo);
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(selected, saveTo);
}
}
catch (Exception exception)
{
App.RaiseException(repo, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;

View File

@@ -56,11 +56,18 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
try
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(changes, saveTo);
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(changes, saveTo);
}
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
@@ -117,11 +124,18 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
try
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(changes, saveTo);
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(changes, saveTo);
}
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
@@ -259,11 +273,18 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
try
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync([change], saveTo);
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync([change], saveTo);
}
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;

View File

@@ -51,11 +51,19 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await StorageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await compare.SaveAsPatch(storageFile.Path.LocalPath);
try
{
var storageFile = await StorageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await compare.SaveAsPatch(storageFile.Path.LocalPath);
NotifyDonePanel.IsVisible = true;
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
}
NotifyDonePanel.IsVisible = true;
e.Handled = true;
}
}

View File

@@ -35,11 +35,18 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
try
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(selected, saveTo);
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var saveTo = storageFile.Path.LocalPath;
await vm.SaveChangesAsPatchAsync(selected, saveTo);
}
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
@@ -166,9 +173,16 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storage.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesAsPatchAsync(null, storageFile.Path.LocalPath);
try
{
var storageFile = await storage.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesAsPatchAsync(null, storageFile.Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
}

View File

@@ -83,9 +83,16 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveStashAsPatchAsync(stash, storageFile.Path.LocalPath);
try
{
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveStashAsPatchAsync(stash, storageFile.Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
}
ev.Handled = true;
};

View File

@@ -391,9 +391,16 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
try
{
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
};
@@ -788,9 +795,16 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
try
{
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
};
@@ -975,9 +989,16 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
try
{
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
};
@@ -1183,9 +1204,16 @@ namespace SourceGit.Views
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
try
{
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true;
};