Skip to content

Commit 6cf1b20

Browse files
committed
refactor: context menu for commit change and revision file
Signed-off-by: leo <[email protected]>
1 parent 321ccf9 commit 6cf1b20

File tree

1 file changed

+62
-48
lines changed

1 file changed

+62
-48
lines changed

src/ViewModels/CommitDetail.cs

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
291291
ev.Handled = true;
292292
};
293293

294-
var fullPath = Path.Combine(_repo.FullPath, change.Path);
294+
var fullPath = Native.OS.GetAbsPath(_repo.FullPath, change.Path);
295295
var explore = new MenuItem();
296296
explore.Header = App.Text("RevealFile");
297297
explore.Icon = App.CreateMenuIcon("Icons.Explore");
@@ -362,35 +362,27 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
362362
var resetToThisRevision = new MenuItem();
363363
resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision");
364364
resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout");
365-
resetToThisRevision.Click += (_, ev) =>
365+
resetToThisRevision.Click += async (_, ev) =>
366366
{
367-
var log = _repo.CreateLog($"Reset File to '{_commit.SHA}'");
368-
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.Path, $"{_commit.SHA}");
369-
log.Complete();
367+
await ResetToThisRevision(change.Path);
370368
ev.Handled = true;
371369
};
372370

373371
var resetToFirstParent = new MenuItem();
374372
resetToFirstParent.Header = App.Text("ChangeCM.CheckoutFirstParentRevision");
375373
resetToFirstParent.Icon = App.CreateMenuIcon("Icons.File.Checkout");
376374
resetToFirstParent.IsEnabled = _commit.Parents.Count > 0;
377-
resetToFirstParent.Click += (_, ev) =>
375+
resetToFirstParent.Click += async (_, ev) =>
378376
{
379-
var log = _repo.CreateLog($"Reset File to '{_commit.SHA}~1'");
380-
if (change.Index == Models.ChangeState.Renamed)
381-
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.OriginalPath, $"{_commit.SHA}~1");
382-
383-
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.Path, $"{_commit.SHA}~1");
384-
log.Complete();
377+
await ResetToParentRevision(change);
385378
ev.Handled = true;
386379
};
387380

388381
menu.Items.Add(resetToThisRevision);
389382
menu.Items.Add(resetToFirstParent);
390383
menu.Items.Add(new MenuItem { Header = "-" });
391384

392-
if (File.Exists(Path.Combine(fullPath)))
393-
TryToAddContextMenuItemsForGitLFS(menu, change.Path);
385+
TryToAddContextMenuItemsForGitLFS(menu, fullPath, change.Path);
394386
}
395387

396388
var copyPath = new MenuItem();
@@ -407,7 +399,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
407399
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
408400
copyFullPath.Click += (_, e) =>
409401
{
410-
App.CopyText(Native.OS.GetAbsPath(_repo.FullPath, change.Path));
402+
App.CopyText(fullPath);
411403
e.Handled = true;
412404
};
413405

@@ -419,7 +411,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
419411
public ContextMenu CreateRevisionFileContextMenu(Models.Object file)
420412
{
421413
var menu = new ContextMenu();
422-
var fullPath = Path.Combine(_repo.FullPath, file.Path);
414+
var fullPath = Native.OS.GetAbsPath(_repo.FullPath, file.Path);
423415
var explore = new MenuItem();
424416
explore.Header = App.Text("RevealFile");
425417
explore.Icon = App.CreateMenuIcon("Icons.Explore");
@@ -499,38 +491,34 @@ public ContextMenu CreateRevisionFileContextMenu(Models.Object file)
499491
menu.Items.Add(blame);
500492
menu.Items.Add(new MenuItem() { Header = "-" });
501493

502-
var resetToThisRevision = new MenuItem();
503-
resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision");
504-
resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout");
505-
resetToThisRevision.IsEnabled = File.Exists(fullPath);
506-
resetToThisRevision.Click += (_, ev) =>
494+
if (!_repo.IsBare)
507495
{
508-
var log = _repo.CreateLog($"Reset File to '{_commit.SHA}'");
509-
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(file.Path, $"{_commit.SHA}");
510-
log.Complete();
511-
ev.Handled = true;
512-
};
496+
var resetToThisRevision = new MenuItem();
497+
resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision");
498+
resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout");
499+
resetToThisRevision.Click += async (_, ev) =>
500+
{
501+
await ResetToThisRevision(file.Path);
502+
ev.Handled = true;
503+
};
513504

514-
var resetToFirstParent = new MenuItem();
515-
resetToFirstParent.Header = App.Text("ChangeCM.CheckoutFirstParentRevision");
516-
resetToFirstParent.Icon = App.CreateMenuIcon("Icons.File.Checkout");
517-
var fileInChanges = _changes.Find(x => x.Path == file.Path);
518-
var fileIndex = fileInChanges?.Index;
519-
resetToFirstParent.IsEnabled = _commit.Parents.Count > 0 && fileIndex != Models.ChangeState.Renamed;
520-
resetToFirstParent.Click += (_, ev) =>
521-
{
522-
var log = _repo.CreateLog($"Reset File to '{_commit.SHA}~1'");
523-
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(file.Path, $"{_commit.SHA}~1");
524-
log.Complete();
525-
ev.Handled = true;
526-
};
505+
var change = _changes.Find(x => x.Path == file.Path) ?? new Models.Change() { Index = Models.ChangeState.None, Path = file.Path };
506+
var resetToFirstParent = new MenuItem();
507+
resetToFirstParent.Header = App.Text("ChangeCM.CheckoutFirstParentRevision");
508+
resetToFirstParent.Icon = App.CreateMenuIcon("Icons.File.Checkout");
509+
resetToFirstParent.IsEnabled = _commit.Parents.Count > 0;
510+
resetToFirstParent.Click += async (_, ev) =>
511+
{
512+
await ResetToParentRevision(change);
513+
ev.Handled = true;
514+
};
527515

528-
menu.Items.Add(resetToThisRevision);
529-
menu.Items.Add(resetToFirstParent);
530-
menu.Items.Add(new MenuItem() { Header = "-" });
516+
menu.Items.Add(resetToThisRevision);
517+
menu.Items.Add(resetToFirstParent);
518+
menu.Items.Add(new MenuItem() { Header = "-" });
531519

532-
if (File.Exists(Path.Combine(fullPath)))
533-
TryToAddContextMenuItemsForGitLFS(menu, file.Path);
520+
TryToAddContextMenuItemsForGitLFS(menu, fullPath, file.Path);
521+
}
534522

535523
var copyPath = new MenuItem();
536524
copyPath.Header = App.Text("CopyPath");
@@ -546,7 +534,7 @@ public ContextMenu CreateRevisionFileContextMenu(Models.Object file)
546534
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
547535
copyFullPath.Click += (_, e) =>
548536
{
549-
App.CopyText(Native.OS.GetAbsPath(_repo.FullPath, file.Path));
537+
App.CopyText(fullPath);
550538
e.Handled = true;
551539
};
552540

@@ -725,8 +713,11 @@ private void RefreshVisibleChanges()
725713
}
726714
}
727715

728-
private void TryToAddContextMenuItemsForGitLFS(ContextMenu menu, string path)
716+
private void TryToAddContextMenuItemsForGitLFS(ContextMenu menu, string fullPath, string path)
729717
{
718+
if (_repo.Remotes.Count == 0 || !File.Exists(fullPath))
719+
return;
720+
730721
var lfsEnabled = new Commands.LFS(_repo.FullPath).IsEnabled();
731722
if (!lfsEnabled)
732723
return;
@@ -738,7 +729,6 @@ private void TryToAddContextMenuItemsForGitLFS(ContextMenu menu, string path)
738729
var lfsLock = new MenuItem();
739730
lfsLock.Header = App.Text("GitLFS.Locks.Lock");
740731
lfsLock.Icon = App.CreateMenuIcon("Icons.Lock");
741-
lfsLock.IsEnabled = _repo.Remotes.Count > 0;
742732
if (_repo.Remotes.Count == 1)
743733
{
744734
lfsLock.Click += async (_, e) =>
@@ -777,7 +767,6 @@ private void TryToAddContextMenuItemsForGitLFS(ContextMenu menu, string path)
777767
var lfsUnlock = new MenuItem();
778768
lfsUnlock.Header = App.Text("GitLFS.Locks.Unlock");
779769
lfsUnlock.Icon = App.CreateMenuIcon("Icons.Unlock");
780-
lfsUnlock.IsEnabled = _repo.Remotes.Count > 0;
781770
if (_repo.Remotes.Count == 1)
782771
{
783772
lfsUnlock.Click += async (_, e) =>
@@ -867,6 +856,31 @@ private void CalcRevisionFileSearchSuggestion()
867856
RevisionFileSearchSuggestion = suggestion;
868857
}
869858

859+
private Task ResetToThisRevision(string path)
860+
{
861+
var log = _repo.CreateLog($"Reset File to '{_commit.SHA}'");
862+
863+
return Task.Run(() =>
864+
{
865+
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(path, $"{_commit.SHA}");
866+
log.Complete();
867+
});
868+
}
869+
870+
private Task ResetToParentRevision(Models.Change change)
871+
{
872+
var log = _repo.CreateLog($"Reset File to '{_commit.SHA}~1'");
873+
874+
return Task.Run(() =>
875+
{
876+
if (change.Index == Models.ChangeState.Renamed)
877+
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.OriginalPath, $"{_commit.SHA}~1");
878+
879+
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.Path, $"{_commit.SHA}~1");
880+
log.Complete();
881+
});
882+
}
883+
870884
[GeneratedRegex(@"\b(https?://|ftp://)[\w\d\._/\-~%@()+:?&=#!]*[\w\d/]")]
871885
private static partial Regex REG_URL_FORMAT();
872886

0 commit comments

Comments
 (0)