Skip to content

Commit ce23e8c

Browse files
committed
refactor: use async methods to commit changes
Signed-off-by: leo <[email protected]>
1 parent 3cce6c5 commit ce23e8c

12 files changed

+138
-146
lines changed

src/App.axaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,12 @@ public static void ShowWindow(object data)
158158
}
159159
}
160160

161-
public static async Task<bool> AskConfirmAsync(string message, Action onSure)
161+
public static async Task<bool> AskConfirmAsync(string message)
162162
{
163163
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
164164
{
165165
var confirm = new Views.Confirm();
166166
confirm.Message.Text = message;
167-
confirm.OnSure = onSure;
168167
return await confirm.ShowDialog<bool>(owner);
169168
}
170169

src/Models/Commit.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public enum CommitSearchMethod
1616
ByContent,
1717
}
1818

19+
public enum CommitCheckPassed
20+
{
21+
None = 0,
22+
DetachedHead,
23+
Filter,
24+
FileCount,
25+
}
26+
1927
public class Commit
2028
{
2129
// As retrieved by: git mktree </dev/null

src/ViewModels/Checkout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override async Task<bool> Sure()
4747
if (refs.Count == 0)
4848
{
4949
var msg = App.Text("Checkout.WarnLostCommits");
50-
var shouldContinue = await App.AskConfirmAsync(msg, null);
50+
var shouldContinue = await App.AskConfirmAsync(msg);
5151
if (!shouldContinue)
5252
{
5353
_repo.SetWatcherEnabled(true);

src/ViewModels/CheckoutAndFastForward.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override async Task<bool> Sure()
5252
if (refs.Count == 0)
5353
{
5454
var msg = App.Text("Checkout.WarnLostCommits");
55-
var shouldContinue = await App.AskConfirmAsync(msg, null);
55+
var shouldContinue = await App.AskConfirmAsync(msg);
5656
if (!shouldContinue)
5757
{
5858
_repo.SetWatcherEnabled(true);

src/ViewModels/CheckoutCommit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override async Task<bool> Sure()
4747
if (refs.Count == 0)
4848
{
4949
var msg = App.Text("Checkout.WarnLostCommits");
50-
var shouldContinue = await App.AskConfirmAsync(msg, null);
50+
var shouldContinue = await App.AskConfirmAsync(msg);
5151
if (!shouldContinue)
5252
{
5353
_repo.SetWatcherEnabled(true);
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System.Threading.Tasks;
22

33
namespace SourceGit.ViewModels
44
{
@@ -16,23 +16,25 @@ public string Message
1616
private set;
1717
}
1818

19-
public ConfirmEmptyCommit(bool hasLocalChanges, Action<bool> onSure)
19+
public ConfirmEmptyCommit(WorkingCopy wc, bool autoPush, int unstagedCount)
2020
{
21-
HasLocalChanges = hasLocalChanges;
22-
Message = App.Text(hasLocalChanges ? "ConfirmEmptyCommit.WithLocalChanges" : "ConfirmEmptyCommit.NoLocalChanges");
23-
_onSure = onSure;
21+
_wc = wc;
22+
_autoPush = autoPush;
23+
HasLocalChanges = unstagedCount > 0;
24+
Message = App.Text(HasLocalChanges ? "ConfirmEmptyCommit.WithLocalChanges" : "ConfirmEmptyCommit.NoLocalChanges");
2425
}
2526

26-
public void StageAllThenCommit()
27+
public async Task StageAllThenCommitAsync()
2728
{
28-
_onSure?.Invoke(true);
29+
await _wc.CommitAsync(true, _autoPush, Models.CommitCheckPassed.FileCount);
2930
}
3031

31-
public void Continue()
32+
public async Task ContinueAsync()
3233
{
33-
_onSure?.Invoke(false);
34+
await _wc.CommitAsync(false, _autoPush, Models.CommitCheckPassed.FileCount);
3435
}
3536

36-
private Action<bool> _onSure;
37+
private readonly WorkingCopy _wc;
38+
private readonly bool _autoPush;
3739
}
3840
}

src/ViewModels/CreateBranch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public override async Task<bool> Sure()
131131
if (refs.Count == 0)
132132
{
133133
var msg = App.Text("Checkout.WarnLostCommits");
134-
var shouldContinue = await App.AskConfirmAsync(msg, null);
134+
var shouldContinue = await App.AskConfirmAsync(msg);
135135
if (!shouldContinue)
136136
{
137137
_repo.SetWatcherEnabled(true);

src/ViewModels/WorkingCopy.cs

Lines changed: 77 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -606,19 +606,86 @@ public void ApplyCommitMessageTemplate(Models.CommitTemplate tmpl)
606606
CommitMessage = tmpl.Apply(_repo.CurrentBranch, _staged);
607607
}
608608

609-
public void Commit()
609+
public async Task CommitAsync(bool autoStage, bool autoPush, Models.CommitCheckPassed checkPassed = Models.CommitCheckPassed.None)
610610
{
611-
DoCommit(false, false);
612-
}
611+
if (string.IsNullOrWhiteSpace(_commitMessage))
612+
return;
613613

614-
public void CommitWithAutoStage()
615-
{
616-
DoCommit(true, false);
617-
}
614+
if (!_repo.CanCreatePopup())
615+
{
616+
App.RaiseException(_repo.FullPath, "Repository has an unfinished job! Please wait!");
617+
return;
618+
}
618619

619-
public void CommitWithPush()
620-
{
621-
DoCommit(false, true);
620+
if (autoStage && HasUnsolvedConflicts)
621+
{
622+
App.RaiseException(_repo.FullPath, "Repository has unsolved conflict(s). Auto-stage and commit is disabled!");
623+
return;
624+
}
625+
626+
if (_repo.CurrentBranch is { IsDetachedHead: true } && checkPassed < Models.CommitCheckPassed.DetachedHead)
627+
{
628+
var msg = App.Text("WorkingCopy.ConfirmCommitWithDetachedHead");
629+
var sure = await App.AskConfirmAsync(msg);
630+
if (sure)
631+
await CommitAsync(autoStage, autoPush, Models.CommitCheckPassed.DetachedHead);
632+
return;
633+
}
634+
635+
if (!string.IsNullOrEmpty(_filter) && _staged.Count > _visibleStaged.Count && checkPassed < Models.CommitCheckPassed.Filter)
636+
{
637+
var msg = App.Text("WorkingCopy.ConfirmCommitWithFilter", _staged.Count, _visibleStaged.Count, _staged.Count - _visibleStaged.Count);
638+
var sure = await App.AskConfirmAsync(msg);
639+
if (sure)
640+
await CommitAsync(autoStage, autoPush, Models.CommitCheckPassed.Filter);
641+
return;
642+
}
643+
644+
if (checkPassed < Models.CommitCheckPassed.FileCount && !_useAmend)
645+
{
646+
if ((!autoStage && _staged.Count == 0) || (autoStage && _cached.Count == 0))
647+
{
648+
await App.ShowDialog(new ConfirmEmptyCommit(this, autoPush, _cached.Count));
649+
return;
650+
}
651+
}
652+
653+
IsCommitting = true;
654+
_repo.Settings.PushCommitMessage(_commitMessage);
655+
_repo.SetWatcherEnabled(false);
656+
657+
var signOff = _repo.Settings.EnableSignOffForCommit;
658+
var log = _repo.CreateLog("Commit");
659+
var succ = true;
660+
if (autoStage && _unstaged.Count > 0)
661+
succ = await new Commands.Add(_repo.FullPath, _repo.IncludeUntracked).Use(log).ExecAsync().ConfigureAwait(false);
662+
663+
if (succ)
664+
succ = await new Commands.Commit(_repo.FullPath, _commitMessage, signOff, _useAmend, _resetAuthor).Use(log).RunAsync().ConfigureAwait(false);
665+
666+
log.Complete();
667+
668+
if (succ)
669+
{
670+
CommitMessage = string.Empty;
671+
UseAmend = false;
672+
if (autoPush && _repo.Remotes.Count > 0)
673+
{
674+
Models.Branch pushBranch = null;
675+
if (_repo.CurrentBranch == null)
676+
{
677+
var currentBranchName = await new Commands.QueryCurrentBranch(_repo.FullPath).GetResultAsync();
678+
pushBranch = new Models.Branch() { Name = currentBranchName };
679+
}
680+
681+
if (_repo.CanCreatePopup())
682+
_repo.ShowAndStartPopup(new Push(_repo, pushBranch));
683+
}
684+
}
685+
686+
_repo.MarkBranchesDirtyManually();
687+
_repo.SetWatcherEnabled(true);
688+
IsCommitting = false;
622689
}
623690

624691
private List<Models.Change> GetVisibleChanges(List<Models.Change> changes)
@@ -745,102 +812,6 @@ private void SetDetail(Models.Change change, bool isUnstaged)
745812
DetailContext = new DiffContext(_repo.FullPath, new Models.DiffOption(change, isUnstaged), _detailContext as DiffContext);
746813
}
747814

748-
private void DoCommit(bool autoStage, bool autoPush, CommitCheckPassed checkPassed = CommitCheckPassed.None)
749-
{
750-
if (string.IsNullOrWhiteSpace(_commitMessage))
751-
return;
752-
753-
if (!_repo.CanCreatePopup())
754-
{
755-
App.RaiseException(_repo.FullPath, "Repository has an unfinished job! Please wait!");
756-
return;
757-
}
758-
759-
if (autoStage && HasUnsolvedConflicts)
760-
{
761-
App.RaiseException(_repo.FullPath, "Repository has unsolved conflict(s). Auto-stage and commit is disabled!");
762-
return;
763-
}
764-
765-
if (_repo.CurrentBranch is { IsDetachedHead: true } && checkPassed < CommitCheckPassed.DetachedHead)
766-
{
767-
var msg = App.Text("WorkingCopy.ConfirmCommitWithDetachedHead");
768-
_ = App.AskConfirmAsync(msg, () => DoCommit(autoStage, autoPush, CommitCheckPassed.DetachedHead));
769-
return;
770-
}
771-
772-
if (!string.IsNullOrEmpty(_filter) && _staged.Count > _visibleStaged.Count && checkPassed < CommitCheckPassed.Filter)
773-
{
774-
var msg = App.Text("WorkingCopy.ConfirmCommitWithFilter", _staged.Count, _visibleStaged.Count, _staged.Count - _visibleStaged.Count);
775-
_ = App.AskConfirmAsync(msg, () => DoCommit(autoStage, autoPush, CommitCheckPassed.Filter));
776-
return;
777-
}
778-
779-
if (checkPassed < CommitCheckPassed.FileCount && !_useAmend)
780-
{
781-
if ((!autoStage && _staged.Count == 0) || (autoStage && _cached.Count == 0))
782-
{
783-
_ = App.ShowDialog(new ConfirmEmptyCommit(_cached.Count > 0, stageAll => DoCommit(stageAll, autoPush, CommitCheckPassed.FileCount)));
784-
return;
785-
}
786-
}
787-
788-
IsCommitting = true;
789-
_repo.Settings.PushCommitMessage(_commitMessage);
790-
_repo.SetWatcherEnabled(false);
791-
792-
var signOff = _repo.Settings.EnableSignOffForCommit;
793-
var log = _repo.CreateLog("Commit");
794-
Task.Run(async () =>
795-
{
796-
var succ = true;
797-
if (autoStage && _unstaged.Count > 0)
798-
succ = await new Commands.Add(_repo.FullPath, _repo.IncludeUntracked).Use(log).ExecAsync().ConfigureAwait(false);
799-
800-
if (succ)
801-
succ = await new Commands.Commit(_repo.FullPath, _commitMessage, signOff, _useAmend, _resetAuthor).Use(log).RunAsync().ConfigureAwait(false);
802-
803-
log.Complete();
804-
805-
Dispatcher.UIThread.Post(() =>
806-
{
807-
if (succ)
808-
{
809-
CommitMessage = string.Empty;
810-
UseAmend = false;
811-
if (autoPush && _repo.Remotes.Count > 0)
812-
PushAfterCommit();
813-
}
814-
815-
_repo.MarkBranchesDirtyManually();
816-
_repo.SetWatcherEnabled(true);
817-
IsCommitting = false;
818-
});
819-
});
820-
}
821-
822-
private void PushAfterCommit()
823-
{
824-
if (_repo.CurrentBranch == null)
825-
{
826-
Task.Run(async () =>
827-
{
828-
var currentBranchName = await new Commands.QueryCurrentBranch(_repo.FullPath).GetResultAsync();
829-
var tmp = new Models.Branch() { Name = currentBranchName };
830-
831-
Dispatcher.UIThread.Post(() =>
832-
{
833-
if (_repo.CanCreatePopup())
834-
_repo.ShowAndStartPopup(new Push(_repo, tmp));
835-
});
836-
});
837-
}
838-
else if (_repo.CanCreatePopup())
839-
{
840-
_repo.ShowAndStartPopup(new Push(_repo, null));
841-
}
842-
}
843-
844815
private bool IsChanged(List<Models.Change> old, List<Models.Change> cur)
845816
{
846817
if (old.Count != cur.Count)
@@ -857,14 +828,6 @@ private bool IsChanged(List<Models.Change> old, List<Models.Change> cur)
857828
return false;
858829
}
859830

860-
private enum CommitCheckPassed
861-
{
862-
None = 0,
863-
DetachedHead,
864-
Filter,
865-
FileCount,
866-
}
867-
868831
private Repository _repo = null;
869832
private bool _isLoadingData = false;
870833
private bool _isStaging = false;

src/Views/Confirm.axaml.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
using System;
21
using Avalonia.Interactivity;
32

43
namespace SourceGit.Views
54
{
65
public partial class Confirm : ChromelessWindow
76
{
8-
public Action OnSure
9-
{
10-
get;
11-
set;
12-
}
13-
147
public Confirm()
158
{
169
InitializeComponent();
1710
}
1811

1912
private void Sure(object _1, RoutedEventArgs _2)
2013
{
21-
OnSure?.Invoke();
2214
Close(true);
2315
}
2416

src/Views/ConfirmEmptyCommit.axaml.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ public ConfirmEmptyCommit()
99
InitializeComponent();
1010
}
1111

12-
private void StageAllThenCommit(object _1, RoutedEventArgs _2)
12+
private async void StageAllThenCommit(object _1, RoutedEventArgs _2)
1313
{
14-
(DataContext as ViewModels.ConfirmEmptyCommit)?.StageAllThenCommit();
14+
if (DataContext is ViewModels.ConfirmEmptyCommit vm)
15+
await vm.StageAllThenCommitAsync();
16+
1517
Close();
1618
}
1719

18-
private void Continue(object _1, RoutedEventArgs _2)
20+
private async void Continue(object _1, RoutedEventArgs _2)
1921
{
20-
(DataContext as ViewModels.ConfirmEmptyCommit)?.Continue();
22+
if (DataContext is ViewModels.ConfirmEmptyCommit vm)
23+
await vm.ContinueAsync();
24+
2125
Close();
2226
}
2327

0 commit comments

Comments
 (0)