Skip to content

Commit ac660aa

Browse files
authored
fix: avoid crash when closing a repo which is deleted in file system (#1878)
related to #415
1 parent cf63253 commit ac660aa

File tree

7 files changed

+141
-49
lines changed

7 files changed

+141
-49
lines changed

src/ViewModels/Repository.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,15 +565,22 @@ public void Close()
565565
SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect
566566
Logs.Clear();
567567

568-
if (!_isWorktree)
568+
if (!_isWorktree && Directory.Exists(_gitCommonDir))
569569
{
570-
if (_workingCopy.InProgressContext != null && !string.IsNullOrEmpty(_workingCopy.CommitMessage))
571-
File.WriteAllText(Path.Combine(GitDir, "MERGE_MSG"), _workingCopy.CommitMessage);
572-
else
573-
_settings.LastCommitMessage = _workingCopy.CommitMessage;
570+
try
571+
{
572+
if (_workingCopy.InProgressContext != null && !string.IsNullOrEmpty(_workingCopy.CommitMessage))
573+
File.WriteAllText(Path.Combine(GitDir, "MERGE_MSG"), _workingCopy.CommitMessage);
574+
else
575+
_settings.LastCommitMessage = _workingCopy.CommitMessage;
574576

575-
using var stream = File.Create(Path.Combine(_gitCommonDir, "sourcegit.settings"));
576-
JsonSerializer.Serialize(stream, _settings, JsonCodeGen.Default.RepositorySettings);
577+
using var stream = File.Create(Path.Combine(_gitCommonDir, "sourcegit.settings"));
578+
JsonSerializer.Serialize(stream, _settings, JsonCodeGen.Default.RepositorySettings);
579+
}
580+
catch (Exception)
581+
{
582+
// Ignore
583+
}
577584
}
578585

579586
if (_cancellationRefreshBranches is { IsCancellationRequested: false })

src/Views/BranchCompare.axaml.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
3737
options.DefaultExtension = ".patch";
3838
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
3939

40-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
41-
if (storageFile != null)
40+
try
4241
{
43-
var saveTo = storageFile.Path.LocalPath;
44-
await vm.SaveChangesAsPatchAsync(selected, saveTo);
42+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
43+
if (storageFile != null)
44+
{
45+
var saveTo = storageFile.Path.LocalPath;
46+
await vm.SaveChangesAsPatchAsync(selected, saveTo);
47+
}
48+
}
49+
catch (Exception exception)
50+
{
51+
App.RaiseException(repo, $"Failed to save as patch: {exception.Message}");
4552
}
4653

4754
e.Handled = true;

src/Views/CommitDetail.axaml.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,18 @@ public ContextMenu CreateChangeContextMenuByFolder(ViewModels.ChangeTreeNode nod
5656
options.DefaultExtension = ".patch";
5757
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
5858

59-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
60-
if (storageFile != null)
59+
try
6160
{
62-
var saveTo = storageFile.Path.LocalPath;
63-
await vm.SaveChangesAsPatchAsync(changes, saveTo);
61+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
62+
if (storageFile != null)
63+
{
64+
var saveTo = storageFile.Path.LocalPath;
65+
await vm.SaveChangesAsPatchAsync(changes, saveTo);
66+
}
67+
}
68+
catch (Exception exception)
69+
{
70+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
6471
}
6572

6673
e.Handled = true;
@@ -117,11 +124,18 @@ public ContextMenu CreateMultipleChangesContextMenu(List<Models.Change> changes)
117124
options.DefaultExtension = ".patch";
118125
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
119126

120-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
121-
if (storageFile != null)
127+
try
128+
{
129+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
130+
if (storageFile != null)
131+
{
132+
var saveTo = storageFile.Path.LocalPath;
133+
await vm.SaveChangesAsPatchAsync(changes, saveTo);
134+
}
135+
}
136+
catch (Exception exception)
122137
{
123-
var saveTo = storageFile.Path.LocalPath;
124-
await vm.SaveChangesAsPatchAsync(changes, saveTo);
138+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
125139
}
126140

127141
e.Handled = true;
@@ -259,11 +273,18 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
259273
options.DefaultExtension = ".patch";
260274
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
261275

262-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
263-
if (storageFile != null)
276+
try
277+
{
278+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
279+
if (storageFile != null)
280+
{
281+
var saveTo = storageFile.Path.LocalPath;
282+
await vm.SaveChangesAsPatchAsync([change], saveTo);
283+
}
284+
}
285+
catch (Exception exception)
264286
{
265-
var saveTo = storageFile.Path.LocalPath;
266-
await vm.SaveChangesAsPatchAsync([change], saveTo);
287+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
267288
}
268289

269290
e.Handled = true;

src/Views/FileHistories.axaml.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ private async void OnSaveAsPatch(object sender, RoutedEventArgs e)
5151
options.DefaultExtension = ".patch";
5252
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
5353

54-
var storageFile = await StorageProvider.SaveFilePickerAsync(options);
55-
if (storageFile != null)
56-
await compare.SaveAsPatch(storageFile.Path.LocalPath);
54+
try
55+
{
56+
var storageFile = await StorageProvider.SaveFilePickerAsync(options);
57+
if (storageFile != null)
58+
await compare.SaveAsPatch(storageFile.Path.LocalPath);
59+
60+
NotifyDonePanel.IsVisible = true;
61+
}
62+
catch (Exception exception)
63+
{
64+
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
65+
}
5766

58-
NotifyDonePanel.IsVisible = true;
5967
e.Handled = true;
6068
}
6169
}

src/Views/RevisionCompare.axaml.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
3535
options.DefaultExtension = ".patch";
3636
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
3737

38-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
39-
if (storageFile != null)
38+
try
4039
{
41-
var saveTo = storageFile.Path.LocalPath;
42-
await vm.SaveChangesAsPatchAsync(selected, saveTo);
40+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
41+
if (storageFile != null)
42+
{
43+
var saveTo = storageFile.Path.LocalPath;
44+
await vm.SaveChangesAsPatchAsync(selected, saveTo);
45+
}
46+
}
47+
catch (Exception exception)
48+
{
49+
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
4350
}
4451

4552
e.Handled = true;
@@ -166,9 +173,16 @@ private async void OnSaveAsPatch(object sender, RoutedEventArgs e)
166173
options.DefaultExtension = ".patch";
167174
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
168175

169-
var storageFile = await storage.SaveFilePickerAsync(options);
170-
if (storageFile != null)
171-
await vm.SaveChangesAsPatchAsync(null, storageFile.Path.LocalPath);
176+
try
177+
{
178+
var storageFile = await storage.SaveFilePickerAsync(options);
179+
if (storageFile != null)
180+
await vm.SaveChangesAsPatchAsync(null, storageFile.Path.LocalPath);
181+
}
182+
catch (Exception exception)
183+
{
184+
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
185+
}
172186

173187
e.Handled = true;
174188
}

src/Views/StashesPage.axaml.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,16 @@ private void OnStashContextRequested(object sender, ContextRequestedEventArgs e)
8383
options.DefaultExtension = ".patch";
8484
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
8585

86-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
87-
if (storageFile != null)
88-
await vm.SaveStashAsPatchAsync(stash, storageFile.Path.LocalPath);
86+
try
87+
{
88+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
89+
if (storageFile != null)
90+
await vm.SaveStashAsPatchAsync(stash, storageFile.Path.LocalPath);
91+
}
92+
catch (Exception exception)
93+
{
94+
App.RaiseException(string.Empty, $"Failed to save as patch: {exception.Message}");
95+
}
8996

9097
ev.Handled = true;
9198
};

src/Views/WorkingCopy.axaml.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,16 @@ private ContextMenu CreateContextMenuForUnstagedChanges(ViewModels.WorkingCopy v
391391
options.DefaultExtension = ".patch";
392392
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
393393

394-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
395-
if (storageFile != null)
396-
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
394+
try
395+
{
396+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
397+
if (storageFile != null)
398+
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
399+
}
400+
catch (Exception exception)
401+
{
402+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
403+
}
397404

398405
e.Handled = true;
399406
};
@@ -788,9 +795,16 @@ private ContextMenu CreateContextMenuForUnstagedChanges(ViewModels.WorkingCopy v
788795
options.DefaultExtension = ".patch";
789796
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
790797

791-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
792-
if (storageFile != null)
793-
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
798+
try
799+
{
800+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
801+
if (storageFile != null)
802+
await vm.SaveChangesToPatchAsync(selectedUnstaged, true, storageFile.Path.LocalPath);
803+
}
804+
catch (Exception exception)
805+
{
806+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
807+
}
794808

795809
e.Handled = true;
796810
};
@@ -975,9 +989,16 @@ public ContextMenu CreateContextMenuForStagedChanges(ViewModels.WorkingCopy vm,
975989
options.DefaultExtension = ".patch";
976990
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
977991

978-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
979-
if (storageFile != null)
980-
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
992+
try
993+
{
994+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
995+
if (storageFile != null)
996+
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
997+
}
998+
catch (Exception exception)
999+
{
1000+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
1001+
}
9811002

9821003
e.Handled = true;
9831004
};
@@ -1183,9 +1204,16 @@ public ContextMenu CreateContextMenuForStagedChanges(ViewModels.WorkingCopy vm,
11831204
options.DefaultExtension = ".patch";
11841205
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
11851206

1186-
var storageFile = await storageProvider.SaveFilePickerAsync(options);
1187-
if (storageFile != null)
1188-
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
1207+
try
1208+
{
1209+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
1210+
if (storageFile != null)
1211+
await vm.SaveChangesToPatchAsync(selectedStaged, false, storageFile.Path.LocalPath);
1212+
}
1213+
catch (Exception exception)
1214+
{
1215+
App.RaiseException(repo.FullPath, $"Failed to save as patch: {exception.Message}");
1216+
}
11891217

11901218
e.Handled = true;
11911219
};

0 commit comments

Comments
 (0)