Skip to content

Commit c2187ed

Browse files
committed
fix: running git command in UIThread via context menu entry blocks whole app (#1384)
Signed-off-by: leo <[email protected]>
1 parent d85f82e commit c2187ed

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/ViewModels/CommitDetail.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public List<Models.CommitLink> WebLinks
5656
{
5757
get;
5858
private set;
59-
} = [];
59+
}
6060

6161
public List<string> Children
6262
{
@@ -426,12 +426,12 @@ public ContextMenu CreateRevisionFileContextMenu(Models.Object file)
426426
var openWith = new MenuItem();
427427
openWith.Header = App.Text("OpenWith");
428428
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
429-
openWith.Click += (_, ev) =>
429+
openWith.Click += async (_, ev) =>
430430
{
431431
var fileName = Path.GetFileNameWithoutExtension(fullPath) ?? "";
432432
var fileExt = Path.GetExtension(fullPath) ?? "";
433433
var tmpFile = Path.Combine(Path.GetTempPath(), $"{fileName}~{_commit.SHA.Substring(0, 10)}{fileExt}");
434-
Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, tmpFile);
434+
await Task.Run(() => Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, tmpFile));
435435
Native.OS.OpenWithDefaultEditor(tmpFile);
436436
ev.Handled = true;
437437
};
@@ -453,9 +453,9 @@ public ContextMenu CreateRevisionFileContextMenu(Models.Object file)
453453
if (selected.Count == 1)
454454
{
455455
var folder = selected[0];
456-
var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder?.Path.ToString();
457-
var saveTo = Path.Combine(folderPath, Path.GetFileName(file.Path));
458-
Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, saveTo);
456+
var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder.Path.ToString();
457+
var saveTo = Path.Combine(folderPath, Path.GetFileName(file.Path)!);
458+
await Task.Run(() => Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, saveTo));
459459
}
460460
}
461461
catch (Exception e)

src/ViewModels/FileHistories.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public FileHistoriesSingleRevision(Repository repo, string file, Models.Commit r
4747
RefreshViewContent();
4848
}
4949

50-
public void ResetToSelectedRevision()
50+
public Task<bool> ResetToSelectedRevision()
5151
{
52-
new Commands.Checkout(_repo.FullPath).FileWithRevision(_file, $"{_revision.SHA}");
52+
return Task.Run(() => new Commands.Checkout(_repo.FullPath).FileWithRevision(_file, $"{_revision.SHA}"));
5353
}
5454

5555
private void RefreshViewContent()
@@ -84,7 +84,7 @@ private void SetViewContentAsRevisionFile()
8484
var stream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
8585
var fileSize = stream.Length;
8686
var bitmap = fileSize > 0 ? new Bitmap(stream) : null;
87-
var imageType = Path.GetExtension(_file).TrimStart('.').ToUpper(CultureInfo.CurrentCulture);
87+
var imageType = Path.GetExtension(_file)!.TrimStart('.').ToUpper(CultureInfo.CurrentCulture);
8888
var image = new Models.RevisionImageFile() { Image = bitmap, FileSize = fileSize, ImageType = imageType };
8989
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, image));
9090
}
@@ -103,7 +103,7 @@ private void SetViewContentAsRevisionFile()
103103
var matchLFS = REG_LFS_FORMAT().Match(content);
104104
if (matchLFS.Success)
105105
{
106-
var lfs = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };
106+
var lfs = new Models.RevisionLFSObject() { Object = new() };
107107
lfs.Object.Oid = matchLFS.Groups[1].Value;
108108
lfs.Object.Size = long.Parse(matchLFS.Groups[2].Value);
109109
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, lfs));
@@ -156,15 +156,12 @@ private void SetViewContentAsDiff()
156156
[GeneratedRegex(@"^version https://git-lfs.github.com/spec/v\d+\r?\noid sha256:([0-9a-f]+)\r?\nsize (\d+)[\r\n]*$")]
157157
private static partial Regex REG_LFS_FORMAT();
158158

159-
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
160-
{
161-
".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"
162-
};
159+
private static readonly HashSet<string> IMG_EXTS = [".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"];
163160

164161
private Repository _repo = null;
165162
private string _file = null;
166163
private Models.Commit _revision = null;
167-
private bool _isDiffMode = true;
164+
private bool _isDiffMode = false;
168165
private object _viewContent = null;
169166
}
170167

@@ -265,7 +262,6 @@ public object ViewContent
265262
public FileHistories(Repository repo, string file, string commit = null)
266263
{
267264
_repo = repo;
268-
_file = file;
269265

270266
Task.Run(() =>
271267
{
@@ -288,10 +284,10 @@ public FileHistories(Repository repo, string file, string commit = null)
288284
switch (SelectedCommits.Count)
289285
{
290286
case 1:
291-
ViewContent = new FileHistoriesSingleRevision(_repo, _file, SelectedCommits[0], _prevIsDiffMode);
287+
ViewContent = new FileHistoriesSingleRevision(_repo, file, SelectedCommits[0], _prevIsDiffMode);
292288
break;
293289
case 2:
294-
ViewContent = new FileHistoriesCompareRevisions(_repo, _file, SelectedCommits[0], SelectedCommits[1]);
290+
ViewContent = new FileHistoriesCompareRevisions(_repo, file, SelectedCommits[0], SelectedCommits[1]);
295291
break;
296292
default:
297293
ViewContent = SelectedCommits.Count;
@@ -317,11 +313,10 @@ public string GetCommitFullMessage(Models.Commit commit)
317313
}
318314

319315
private readonly Repository _repo = null;
320-
private readonly string _file = null;
321316
private bool _isLoading = true;
322317
private bool _prevIsDiffMode = true;
323318
private List<Models.Commit> _commits = null;
324-
private Dictionary<string, string> _fullCommitMessages = new Dictionary<string, string>();
319+
private Dictionary<string, string> _fullCommitMessages = new();
325320
private object _viewContent = null;
326321
}
327322
}

src/ViewModels/StashesPage.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,28 @@ public ContextMenu MakeContextMenuForChange(Models.Change change)
234234
var resetToThisRevision = new MenuItem();
235235
resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision");
236236
resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout");
237-
resetToThisRevision.Click += (_, ev) =>
237+
resetToThisRevision.Click += async (_, ev) =>
238238
{
239239
var log = _repo.CreateLog($"Reset File to '{_selectedStash.SHA}'");
240-
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.Path, $"{_selectedStash.SHA}");
240+
241+
await Task.Run(() =>
242+
{
243+
if (_untracked.Contains(change))
244+
{
245+
Commands.SaveRevisionFile.Run(_repo.FullPath, _selectedStash.Parents[2], change.Path, fullPath);
246+
}
247+
else if (change.Index == Models.ChangeState.Added)
248+
{
249+
Commands.SaveRevisionFile.Run(_repo.FullPath, _selectedStash.SHA, change.Path, fullPath);
250+
}
251+
else
252+
{
253+
new Commands.Checkout(_repo.FullPath)
254+
.Use(log)
255+
.FileWithRevision(change.Path, $"{_selectedStash.SHA}");
256+
}
257+
});
258+
241259
log.Complete();
242260
ev.Handled = true;
243261
};

src/Views/FileHistories.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ private void OnPressCommitSHA(object sender, PointerPressedEventArgs e)
2525
e.Handled = true;
2626
}
2727

28-
private void OnResetToSelectedRevision(object sender, RoutedEventArgs e)
28+
private async void OnResetToSelectedRevision(object sender, RoutedEventArgs e)
2929
{
3030
if (sender is Button { DataContext: ViewModels.FileHistoriesSingleRevision single })
3131
{
32-
single.ResetToSelectedRevision();
32+
await single.ResetToSelectedRevision();
3333
NotifyDonePanel.IsVisible = true;
3434
}
3535

0 commit comments

Comments
 (0)