Skip to content

Commit 370d995

Browse files
committed
code_style: move some code from Views.Histories to ViewModels.Histories
Signed-off-by: leo <[email protected]>
1 parent 5ff3450 commit 370d995

File tree

2 files changed

+118
-90
lines changed

2 files changed

+118
-90
lines changed

src/ViewModels/Histories.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,95 @@ public void CheckoutBranchByCommit(Models.Commit commit)
285285
}
286286
}
287287

288+
public async Task CherryPickAsync(Models.Commit commit)
289+
{
290+
if (_repo.CanCreatePopup())
291+
{
292+
if (commit.Parents.Count <= 1)
293+
{
294+
_repo.ShowPopup(new CherryPick(_repo, [commit]));
295+
}
296+
else
297+
{
298+
var parents = new List<Models.Commit>();
299+
foreach (var sha in commit.Parents)
300+
{
301+
var parent = _commits.Find(x => x.SHA == sha);
302+
if (parent == null)
303+
parent = await new Commands.QuerySingleCommit(_repo.FullPath, sha).GetResultAsync();
304+
305+
if (parent != null)
306+
parents.Add(parent);
307+
}
308+
309+
_repo.ShowPopup(new CherryPick(_repo, commit, parents));
310+
}
311+
}
312+
}
313+
314+
public async Task RewordHeadAsync(Models.Commit head)
315+
{
316+
if (_repo.CanCreatePopup())
317+
{
318+
var message = await new Commands.QueryCommitFullMessage(_repo.FullPath, head.SHA).GetResultAsync();
319+
_repo.ShowPopup(new Reword(_repo, head, message));
320+
}
321+
}
322+
323+
public async Task SquashHeadAsync(Models.Commit head)
324+
{
325+
if (head.Parents.Count == 1)
326+
{
327+
var message = await new Commands.QueryCommitFullMessage(_repo.FullPath, head.SHA).GetResultAsync();
328+
var parent = _commits.Find(x => x.SHA.Equals(head.Parents[0]));
329+
if (parent != null && _repo.CanCreatePopup())
330+
_repo.ShowPopup(new Squash(_repo, parent, message));
331+
}
332+
}
333+
334+
public async Task InteractiveRebaseAsync(Models.Commit commit, Models.InteractiveRebaseAction act)
335+
{
336+
var prefill = new InteractiveRebasePrefill(commit.SHA, act);
337+
var start = act switch
338+
{
339+
Models.InteractiveRebaseAction.Squash or Models.InteractiveRebaseAction.Fixup => $"{commit.SHA}~~",
340+
_ => $"{commit.SHA}~",
341+
};
342+
343+
var on = await new Commands.QuerySingleCommit(_repo.FullPath, start).GetResultAsync();
344+
if (on == null)
345+
App.RaiseException(_repo.FullPath, $"Can not squash current commit into parent!");
346+
else
347+
await App.ShowDialog(new InteractiveRebase(_repo, on, prefill));
348+
}
349+
350+
public async Task CopyCommitFullMessageAsync(Models.Commit commit)
351+
{
352+
var message = await new Commands.QueryCommitFullMessage(_repo.FullPath, commit.SHA).GetResultAsync();
353+
await App.CopyTextAsync(message);
354+
}
355+
356+
public async Task<Models.Commit> CompareWithHeadAsync(Models.Commit commit)
357+
{
358+
var head = _commits.Find(x => x.IsCurrentHead);
359+
if (head == null)
360+
{
361+
_repo.SelectedSearchedCommit = null;
362+
head = await new Commands.QuerySingleCommit(_repo.FullPath, "HEAD").GetResultAsync();
363+
if (head != null)
364+
DetailContext = new RevisionCompare(_repo.FullPath, commit, head);
365+
366+
return null;
367+
}
368+
369+
return head;
370+
}
371+
372+
public void CompareWithWorktree(Models.Commit commit)
373+
{
374+
DetailContext = new RevisionCompare(_repo.FullPath, commit, null);
375+
}
376+
288377
private void NavigateTo(Models.Commit commit)
289378
{
290379
AutoSelectedCommit = commit;

src/Views/Histories.axaml.cs

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
459459

460460
var menu = new ContextMenu();
461461
var tags = new List<Models.Tag>();
462+
var isHead = commit.IsCurrentHead;
462463

463464
if (commit.HasDecorators)
464465
{
@@ -525,32 +526,14 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
525526
{
526527
var target = commit.GetFriendlyName();
527528

528-
if (current.Head != commit.SHA)
529-
{
530-
var reset = new MenuItem();
531-
reset.Header = App.Text("CommitCM.Reset", current.Name, target);
532-
reset.Icon = App.CreateMenuIcon("Icons.Reset");
533-
reset.Click += (_, e) =>
534-
{
535-
if (repo.CanCreatePopup())
536-
repo.ShowPopup(new ViewModels.Reset(repo, current, commit));
537-
e.Handled = true;
538-
};
539-
menu.Items.Add(reset);
540-
}
541-
else
529+
if (isHead)
542530
{
543531
var reword = new MenuItem();
544532
reword.Header = App.Text("CommitCM.Reword");
545533
reword.Icon = App.CreateMenuIcon("Icons.Edit");
546534
reword.Click += async (_, e) =>
547535
{
548-
if (repo.CanCreatePopup())
549-
{
550-
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
551-
repo.ShowPopup(new ViewModels.Reword(repo, commit, message));
552-
}
553-
536+
await vm.RewordHeadAsync(commit);
554537
e.Handled = true;
555538
};
556539
menu.Items.Add(reword);
@@ -561,18 +544,24 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
561544
squash.IsEnabled = commit.Parents.Count == 1;
562545
squash.Click += async (_, e) =>
563546
{
564-
if (commit.Parents.Count == 1)
565-
{
566-
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
567-
var parent = vm.Commits.Find(x => x.SHA.Equals(commit.Parents[0]));
568-
if (parent != null && repo.CanCreatePopup())
569-
repo.ShowPopup(new ViewModels.Squash(repo, parent, message));
570-
}
571-
547+
await vm.SquashHeadAsync(commit);
572548
e.Handled = true;
573549
};
574550
menu.Items.Add(squash);
575551
}
552+
else
553+
{
554+
var reset = new MenuItem();
555+
reset.Header = App.Text("CommitCM.Reset", current.Name, target);
556+
reset.Icon = App.CreateMenuIcon("Icons.Reset");
557+
reset.Click += (_, e) =>
558+
{
559+
if (repo.CanCreatePopup())
560+
repo.ShowPopup(new ViewModels.Reset(repo, current, commit));
561+
e.Handled = true;
562+
};
563+
menu.Items.Add(reset);
564+
}
576565

577566
if (!commit.IsMerged)
578567
{
@@ -607,29 +596,7 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
607596
cherryPick.Icon = App.CreateMenuIcon("Icons.CherryPick");
608597
cherryPick.Click += async (_, e) =>
609598
{
610-
if (repo.CanCreatePopup())
611-
{
612-
if (commit.Parents.Count <= 1)
613-
{
614-
repo.ShowPopup(new ViewModels.CherryPick(repo, [commit]));
615-
}
616-
else
617-
{
618-
var parents = new List<Models.Commit>();
619-
foreach (var sha in commit.Parents)
620-
{
621-
var parent = vm.Commits.Find(x => x.SHA == sha);
622-
if (parent == null)
623-
parent = await new Commands.QuerySingleCommit(repo.FullPath, sha).GetResultAsync();
624-
625-
if (parent != null)
626-
parents.Add(parent);
627-
}
628-
629-
repo.ShowPopup(new ViewModels.CherryPick(repo, commit, parents));
630-
}
631-
}
632-
599+
await vm.CherryPickAsync(commit);
633600
e.Handled = true;
634601
};
635602
menu.Items.Add(cherryPick);
@@ -648,7 +615,7 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
648615
menu.Items.Add(revert);
649616
}
650617

651-
if (current.Head != commit.SHA)
618+
if (!isHead)
652619
{
653620
var checkoutCommit = new MenuItem();
654621
checkoutCommit.Header = App.Text("CommitCM.Checkout");
@@ -679,57 +646,39 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
679646
reword.Header = App.Text("CommitCM.InteractiveRebase.Reword");
680647
reword.Click += async (_, e) =>
681648
{
682-
var prefill = new ViewModels.InteractiveRebasePrefill(commit.SHA, Models.InteractiveRebaseAction.Reword);
683-
var on = await new Commands.QuerySingleCommit(repo.FullPath, $"{commit.SHA}~").GetResultAsync();
684-
await App.ShowDialog(new ViewModels.InteractiveRebase(repo, on, prefill));
649+
await vm.InteractiveRebaseAsync(commit, Models.InteractiveRebaseAction.Reword);
685650
e.Handled = true;
686651
};
687652

688653
var edit = new MenuItem();
689654
edit.Header = App.Text("CommitCM.InteractiveRebase.Edit");
690655
edit.Click += async (_, e) =>
691656
{
692-
var prefill = new ViewModels.InteractiveRebasePrefill(commit.SHA, Models.InteractiveRebaseAction.Edit);
693-
var on = await new Commands.QuerySingleCommit(repo.FullPath, $"{commit.SHA}~").GetResultAsync();
694-
await App.ShowDialog(new ViewModels.InteractiveRebase(repo, on, prefill));
657+
await vm.InteractiveRebaseAsync(commit, Models.InteractiveRebaseAction.Edit);
695658
e.Handled = true;
696659
};
697660

698661
var squash = new MenuItem();
699662
squash.Header = App.Text("CommitCM.InteractiveRebase.Squash");
700663
squash.Click += async (_, e) =>
701664
{
702-
var prefill = new ViewModels.InteractiveRebasePrefill(commit.SHA, Models.InteractiveRebaseAction.Squash);
703-
var on = await new Commands.QuerySingleCommit(repo.FullPath, $"{commit.SHA}~~").GetResultAsync();
704-
if (on != null)
705-
await App.ShowDialog(new ViewModels.InteractiveRebase(repo, on, prefill));
706-
else
707-
App.RaiseException(repo.FullPath, $"Can not squash current commit into parent!");
708-
665+
await vm.InteractiveRebaseAsync(commit, Models.InteractiveRebaseAction.Squash);
709666
e.Handled = true;
710667
};
711668

712669
var fixup = new MenuItem();
713670
fixup.Header = App.Text("CommitCM.InteractiveRebase.Fixup");
714671
fixup.Click += async (_, e) =>
715672
{
716-
var prefill = new ViewModels.InteractiveRebasePrefill(commit.SHA, Models.InteractiveRebaseAction.Fixup);
717-
var on = await new Commands.QuerySingleCommit(repo.FullPath, $"{commit.SHA}~~").GetResultAsync();
718-
if (on != null)
719-
await App.ShowDialog(new ViewModels.InteractiveRebase(repo, on, prefill));
720-
else
721-
App.RaiseException(repo.FullPath, $"Can not fixup current commit into parent!");
722-
673+
await vm.InteractiveRebaseAsync(commit, Models.InteractiveRebaseAction.Fixup);
723674
e.Handled = true;
724675
};
725676

726677
var drop = new MenuItem();
727678
drop.Header = App.Text("CommitCM.InteractiveRebase.Drop");
728679
drop.Click += async (_, e) =>
729680
{
730-
var prefill = new ViewModels.InteractiveRebasePrefill(commit.SHA, Models.InteractiveRebaseAction.Drop);
731-
var on = await new Commands.QuerySingleCommit(repo.FullPath, $"{commit.SHA}~").GetResultAsync();
732-
await App.ShowDialog(new ViewModels.InteractiveRebase(repo, on, prefill));
681+
await vm.InteractiveRebaseAsync(commit, Models.InteractiveRebaseAction.Drop);
733682
e.Handled = true;
734683
};
735684

@@ -763,7 +712,7 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
763712
menu.Items.Add(new MenuItem() { Header = "-" });
764713
}
765714

766-
if (current.Head != commit.SHA)
715+
if (!isHead)
767716
{
768717
if (current.TrackStatus.Ahead.Contains(commit.SHA))
769718
{
@@ -786,18 +735,9 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
786735
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
787736
compareWithHead.Click += async (_, e) =>
788737
{
789-
var head = vm.Commits.Find(x => x.SHA == current.Head);
790-
if (head == null)
791-
{
792-
repo.SelectedSearchedCommit = null;
793-
head = await new Commands.QuerySingleCommit(repo.FullPath, current.Head).GetResultAsync();
794-
if (head != null)
795-
vm.DetailContext = new ViewModels.RevisionCompare(repo.FullPath, commit, head);
796-
}
797-
else
798-
{
738+
var head = await vm.CompareWithHeadAsync(commit);
739+
if (head != null)
799740
CommitListContainer.SelectedItems.Add(head);
800-
}
801741

802742
e.Handled = true;
803743
};
@@ -810,7 +750,7 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
810750
compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare");
811751
compareWithWorktree.Click += (_, e) =>
812752
{
813-
vm.DetailContext = new ViewModels.RevisionCompare(repo.FullPath, commit, null);
753+
vm.CompareWithWorktree(commit);
814754
e.Handled = true;
815755
};
816756
menu.Items.Add(compareWithWorktree);
@@ -920,8 +860,7 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
920860
copyMessage.Icon = App.CreateMenuIcon("Icons.Info");
921861
copyMessage.Click += async (_, e) =>
922862
{
923-
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
924-
await App.CopyTextAsync(message);
863+
await vm.CopyCommitFullMessageAsync(commit);
925864
e.Handled = true;
926865
};
927866

0 commit comments

Comments
 (0)