forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitUICommands.cs
1991 lines (1663 loc) · 71.6 KB
/
GitUICommands.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GitCommands;
using GitCommands.Git;
using GitCommands.Git.Commands;
using GitCommands.Settings;
using GitExtUtils;
using GitUI.CommandsDialogs;
using GitUI.CommandsDialogs.RepoHosting;
using GitUI.CommandsDialogs.SettingsDialog;
using GitUI.HelperDialogs;
using GitUIPluginInterfaces;
using GitUIPluginInterfaces.RepositoryHosts;
using JetBrains.Annotations;
using static GitUI.CommandsDialogs.FormBrowse;
namespace GitUI
{
/// <summary>Contains methods to invoke GitEx forms, dialogs, etc.</summary>
public sealed class GitUICommands : IGitUICommands
{
private const string BlameHistoryCommand = "blamehistory";
private const string FileHistoryCommand = "filehistory";
private const string FilterByRevisionArg = "--filter-by-revision";
private const string PathFilterArg = "--pathFilter";
private readonly ICommitTemplateManager _commitTemplateManager;
private readonly IFullPathResolver _fullPathResolver;
private readonly IFindFilePredicateProvider _findFilePredicateProvider;
public GitModule Module { get; private set; }
public ILockableNotifier RepoChangedNotifier { get; }
public IBrowseRepo? BrowseRepo { get; set; }
public GitUICommands(GitModule module)
{
Module = module ?? throw new ArgumentNullException(nameof(module));
_commitTemplateManager = new CommitTemplateManager(() => module);
RepoChangedNotifier = new ActionNotifier(
() => InvokeEvent(null, PostRepositoryChanged));
_fullPathResolver = new FullPathResolver(() => Module.WorkingDir);
_findFilePredicateProvider = new FindFilePredicateProvider();
}
public GitUICommands(string? workingDir)
: this(new GitModule(workingDir))
{
}
public IGitModule GitModule => Module;
#region Events
public event EventHandler<GitUIEventArgs>? PreCheckoutRevision;
public event EventHandler<GitUIPostActionEventArgs>? PostCheckoutRevision;
public event EventHandler<GitUIEventArgs>? PreCheckoutBranch;
public event EventHandler<GitUIPostActionEventArgs>? PostCheckoutBranch;
public event EventHandler<GitUIEventArgs>? PreCommit;
public event EventHandler<GitUIPostActionEventArgs>? PostCommit;
public event EventHandler<GitUIPostActionEventArgs>? PostEditGitIgnore;
public event EventHandler<GitUIPostActionEventArgs>? PostSettings;
public event EventHandler<GitUIPostActionEventArgs>? PostUpdateSubmodules;
public event EventHandler<GitUIEventArgs>? PostBrowseInitialize;
/// <summary>
/// listeners for changes being made to repository
/// </summary>
public event EventHandler<GitUIEventArgs>? PostRepositoryChanged;
public event EventHandler<GitUIEventArgs>? PostRegisterPlugin;
#endregion
private bool RequiresValidWorkingDir(object? owner)
{
if (!Module.IsValidGitWorkingDir())
{
MessageBoxes.NotValidGitDirectory(owner as IWin32Window);
return false;
}
return true;
}
public void StartBatchFileProcessDialog(string batchFile)
{
var tempFile = Path.Combine(Path.GetTempPath(), $"GitExtensions-{Guid.NewGuid():N}.cmd");
try
{
using (StreamWriter writer = new(tempFile))
{
writer.WriteLine("@prompt $G");
writer.Write(batchFile);
}
FormProcess.ShowDialog(null, arguments: $"/C \"{tempFile}\"", Module.WorkingDir, input: null, useDialogSettings: true, process: "cmd.exe");
}
finally
{
File.Delete(tempFile);
}
}
public bool StartCommandLineProcessDialog(IWin32Window? owner, IGitCommand command)
{
bool success = command.AccessesRemote
? FormRemoteProcess.ShowDialog(owner, this, command.Arguments)
: FormProcess.ShowDialog(owner, arguments: command.Arguments, Module.WorkingDir, input: null, useDialogSettings: true);
if (success && command.ChangesRepoState)
{
RepoChangedNotifier.Notify();
}
return success;
}
public void StartCommandLineProcessDialog(IWin32Window? owner, string? command, ArgumentString arguments)
{
FormProcess.ShowDialog(owner, arguments, Module.WorkingDir, input: null, useDialogSettings: true, process: command);
}
public void StartGitCommandProcessDialog(IWin32Window? owner, ArgumentString arguments)
{
FormProcess.ShowDialog(owner, arguments, Module.WorkingDir, input: null, useDialogSettings: true);
}
public bool StartDeleteBranchDialog(IWin32Window? owner, string branch)
{
return StartDeleteBranchDialog(owner, new[] { branch });
}
public bool StartDeleteBranchDialog(IWin32Window? owner, IEnumerable<string> branches)
{
return DoActionOnRepo(owner, action: () =>
{
using FormDeleteBranch form = new(this, branches);
form.ShowDialog(owner);
return true;
}, changesRepo: false);
}
public bool StartDeleteRemoteBranchDialog(IWin32Window? owner, string remoteBranch)
{
return DoActionOnRepo(owner, action: () =>
{
using FormDeleteRemoteBranch form = new(this, remoteBranch);
form.ShowDialog(owner);
return true;
}, changesRepo: false);
}
public bool StartCheckoutRevisionDialog(IWin32Window? owner, string? revision = null)
{
return DoActionOnRepo(owner, action: () =>
{
using FormCheckoutRevision form = new(this);
form.SetRevision(revision);
return form.ShowDialog(owner) == DialogResult.OK;
}, preEvent: PreCheckoutRevision, postEvent: PostCheckoutRevision);
}
public bool StartResetCurrentBranchDialog(IWin32Window? owner, string branch)
{
var objectId = Module.RevParse(branch);
if (objectId is null)
{
MessageBox.Show($"Branch \"{branch}\" could not be resolved.", TranslatedStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
using var form = FormResetCurrentBranch.Create(this, Module.GetRevision(objectId));
return form.ShowDialog(owner) == DialogResult.OK;
}
public bool StashSave(IWin32Window? owner, bool includeUntrackedFiles, bool keepIndex = false, string message = "", IReadOnlyList<string>? selectedFiles = null)
{
bool Action()
{
var arguments = GitCommandHelpers.StashSaveCmd(includeUntrackedFiles, keepIndex, message, selectedFiles);
FormProcess.ShowDialog(owner, arguments, Module.WorkingDir, input: null, useDialogSettings: true);
// git-stash may have changed commits also if aborted, the grid must be refreshed
return true;
}
return DoActionOnRepo(owner, Action);
}
public bool StashStaged(IWin32Window? owner)
{
bool Action()
{
FormProcess.ShowDialog(owner, arguments: "stash --staged", Module.WorkingDir, input: null, useDialogSettings: true);
// git-stash may have changed commits also if aborted, the grid must be refreshed
return true;
}
return DoActionOnRepo(owner, Action);
}
public bool StashPop(IWin32Window? owner)
{
bool Action()
{
FormProcess.ShowDialog(owner, arguments: "stash pop", Module.WorkingDir, input: null, useDialogSettings: true);
MergeConflictHandler.HandleMergeConflicts(this, owner, false, false);
// git-stash may have changed commits also if aborted, the grid must be refreshed
return true;
}
return DoActionOnRepo(owner, Action);
}
public bool StashDrop(IWin32Window? owner, string stashName)
{
bool Action()
{
FormProcess.ShowDialog(owner, arguments: $"stash drop {stashName.Quote()}", Module.WorkingDir, input: null, useDialogSettings: true);
// git-stash may have changed commits also if aborted, the grid must be refreshed
return true;
}
return DoActionOnRepo(owner, Action);
}
public bool StashApply(IWin32Window? owner, string stashName)
{
bool Action()
{
FormProcess.ShowDialog(owner, arguments: $"stash apply {stashName.Quote()}", Module.WorkingDir, input: null, useDialogSettings: true);
MergeConflictHandler.HandleMergeConflicts(this, owner, false, false);
// git-stash may have changed commits also if aborted, the grid must be refreshed
return true;
}
return DoActionOnRepo(owner, Action);
}
public void ShowModelessForm(IWin32Window? owner, bool requiresValidWorkingDir,
EventHandler<GitUIEventArgs>? preEvent, EventHandler<GitUIPostActionEventArgs>? postEvent, Func<Form> provideForm)
{
if (requiresValidWorkingDir && !RequiresValidWorkingDir(owner))
{
return;
}
if (!InvokeEvent(owner, preEvent))
{
return;
}
Form form = provideForm();
void FormClosed(object sender, FormClosedEventArgs e)
{
form.FormClosed -= FormClosed;
InvokePostEvent(owner, true, postEvent);
}
form.FormClosed += FormClosed;
form.ShowInTaskbar = true;
if (Application.OpenForms.Count > 0)
{
form.Show();
}
else
{
form.ShowDialog();
}
}
/// <param name="requiresValidWorkingDir">If action requires valid working directory.</param>
/// <param name="owner">Owner window.</param>
/// <param name="changesRepo">if successfully done action changes repo state.</param>
/// <param name="preEvent">Event invoked before performing action.</param>
/// <param name="postEvent">Event invoked after performing action.</param>
/// <param name="action">Action to do. Return true to indicate that the action was successfully done.</param>
/// <returns>true if action was successfully done, false otherwise.</returns>
private bool DoActionOnRepo(
IWin32Window? owner,
[InstantHandle] Func<bool> action,
bool requiresValidWorkingDir = true,
bool changesRepo = true,
EventHandler<GitUIEventArgs>? preEvent = null,
EventHandler<GitUIPostActionEventArgs>? postEvent = null)
{
bool actionDone = false;
RepoChangedNotifier.Lock();
try
{
if (requiresValidWorkingDir && !RequiresValidWorkingDir(owner))
{
return false;
}
if (!InvokeEvent(owner, preEvent))
{
return false;
}
try
{
actionDone = action();
}
finally
{
InvokePostEvent(owner, actionDone, postEvent);
}
}
finally
{
// The action may not have required a valid working directory to run, but if there isn't one,
// we shouldn't send a "repo changed" notify.
bool requestNotify = actionDone && changesRepo && Module.IsValidGitWorkingDir();
RepoChangedNotifier.UnLock(requestNotify);
}
return actionDone;
}
public bool DoActionOnRepo(Func<bool> action)
{
return DoActionOnRepo(owner: null, action, requiresValidWorkingDir: false);
}
#region Checkout
public bool StartCheckoutBranch(IWin32Window? owner, string branch = "", bool remote = false, IReadOnlyList<ObjectId>? containRevisions = null)
{
return DoActionOnRepo(owner, action: () =>
{
using FormCheckoutBranch form = new(this, branch, remote, containRevisions);
return form.DoDefaultActionOrShow(owner) != DialogResult.Cancel;
}, preEvent: PreCheckoutBranch, postEvent: PostCheckoutBranch);
}
public bool StartCheckoutBranch(IWin32Window? owner, IReadOnlyList<ObjectId>? containRevisions)
{
return StartCheckoutBranch(owner, "", false, containRevisions);
}
public bool StartCheckoutRemoteBranch(IWin32Window? owner, string branch)
{
return StartCheckoutBranch(owner, branch, true);
}
#endregion
/// <summary>
/// Launches a new GE instance.
/// </summary>
/// <param name="arguments">The command line arguments.</param>
/// <param name="workingDir">The working directory for the new process.</param>
/// <returns>The <see cref="IProcess"/> object for controlling the launched instance.</returns>
public static IProcess Launch(string arguments, string workingDir = "")
=> new Executable(Application.ExecutablePath, workingDir).Start(arguments);
/// <summary>
/// Launch FormBrowse in a new GE instance.
/// </summary>
/// <param name="workingDir">The working directory for the new process.</param>
/// <param name="selectedId">The optional commit to be selected.</param>
/// <param name="firstId">The first commit to be selected, the first commit in a diff.</param>
public static void LaunchBrowse(string workingDir = "", ObjectId? selectedId = null, ObjectId? firstId = null)
{
if (!Directory.Exists(workingDir))
{
MessageBoxes.GitExtensionsDirectoryDoesNotExist(owner: null, workingDir);
return;
}
StringBuilder arguments = new("browse");
if (selectedId is null)
{
selectedId = firstId;
firstId = null;
}
if (selectedId is not null)
{
arguments.Append(" -commit=").Append(selectedId);
if (firstId is not null)
{
arguments.Append(',').Append(firstId);
}
}
Launch(arguments.ToString(), workingDir);
}
public bool StartCompareRevisionsDialog(IWin32Window? owner = null)
{
bool Action()
{
using FormLog form = new(this);
return form.ShowDialog(owner) == DialogResult.OK;
}
return DoActionOnRepo(owner, Action);
}
public bool StartAddFilesDialog(IWin32Window? owner, string? addFiles = null)
{
return DoActionOnRepo(owner, action: () =>
{
using FormAddFiles form = new(this, addFiles);
form.ShowDialog(owner);
return true;
});
}
public bool StartCreateBranchDialog(IWin32Window? owner, string? branch)
{
var objectId = Module.RevParse(branch);
if (objectId is null)
{
MessageBox.Show($"Branch \"{branch}\" could not be resolved.", TranslatedStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return StartCreateBranchDialog(owner, objectId);
}
public bool StartCreateBranchDialog(IWin32Window? owner = null, ObjectId? objectId = null, string? newBranchNamePrefix = null)
{
bool Action()
{
using FormCreateBranch form = new(this, objectId, newBranchNamePrefix);
return form.ShowDialog(owner) == DialogResult.OK;
}
return DoActionOnRepo(owner, Action);
}
public bool StartCloneDialog(IWin32Window? owner, string? url = null, bool openedFromProtocolHandler = false, EventHandler<GitModuleEventArgs>? gitModuleChanged = null)
{
bool Action()
{
using FormClone form = new(this, url, openedFromProtocolHandler, gitModuleChanged);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, requiresValidWorkingDir: false, changesRepo: false);
}
public bool StartCloneDialog(IWin32Window? owner, string url, EventHandler<GitModuleEventArgs> gitModuleChanged)
{
return StartCloneDialog(owner, url, false, gitModuleChanged);
}
public bool StartCleanupRepositoryDialog(IWin32Window? owner = null, string? path = null)
{
using FormCleanupRepository form = new(this);
form.SetPathArgument(path);
form.ShowDialog(owner);
return true;
}
public bool StartSquashCommitDialog(IWin32Window? owner, GitRevision revision)
{
bool Action()
{
using FormCommit form = new(this, CommitKind.Squash, revision);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(Action);
}
public bool StartFixupCommitDialog(IWin32Window? owner, GitRevision revision)
{
bool Action()
{
using FormCommit form = new(this, CommitKind.Fixup, revision);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(Action);
}
public bool StartCommitDialog(IWin32Window? owner, string? commitMessage = null, bool showOnlyWhenChanges = false)
{
if (Module.IsBareRepository())
{
return false;
}
bool Action()
{
// Commit dialog can be opened on its own without the main form
// If it is opened by itself, we need to ensure plugins are loaded because some of them
// may have hooks into the commit flow
bool werePluginsRegistered = PluginRegistry.PluginsRegistered;
try
{
// Load plugins synchronously
// if the commit dialog is opened from the main form, all plugins are already loaded and we return instantly,
// if the dialog is loaded on its own, plugins need to be loaded before we load the form
if (!werePluginsRegistered)
{
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
PluginRegistry.Initialize();
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
PluginRegistry.Register(this);
});
}
using FormCommit form = new(this, commitMessage: commitMessage);
if (showOnlyWhenChanges)
{
form.ShowDialogWhenChanges(owner);
}
else
{
form.ShowDialog(owner);
}
}
finally
{
if (!werePluginsRegistered)
{
PluginRegistry.Unregister(this);
}
}
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false, preEvent: PreCommit, postEvent: PostCommit);
}
public bool StartInitializeDialog(IWin32Window? owner = null, string? dir = null, EventHandler<GitModuleEventArgs>? gitModuleChanged = null)
{
bool Action()
{
dir ??= Module.IsValidGitWorkingDir() ? Module.WorkingDir : string.Empty;
using FormInit frm = new(dir, gitModuleChanged);
frm.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, requiresValidWorkingDir: false, changesRepo: false);
}
public bool StartPullDialogAndPullImmediately(IWin32Window? owner = null, string? remoteBranch = null, string? remote = null, AppSettings.PullAction pullAction = AppSettings.PullAction.None)
{
return StartPullDialogAndPullImmediately(out _, owner, remoteBranch, remote, pullAction);
}
/// <param name="pullCompleted">true if pull completed with no errors.</param>
/// <returns>if revision grid should be refreshed.</returns>
public bool StartPullDialogAndPullImmediately(out bool pullCompleted, IWin32Window? owner = null, string? remoteBranch = null, string? remote = null, AppSettings.PullAction pullAction = AppSettings.PullAction.None)
{
return StartPullDialogInternal(owner, pullOnShow: true, out pullCompleted, remoteBranch, remote, pullAction);
}
public bool StartPullDialog(IWin32Window? owner = null, string? remoteBranch = null, string? remote = null, AppSettings.PullAction pullAction = AppSettings.PullAction.None)
{
return StartPullDialogInternal(owner, pullOnShow: false, out _, remoteBranch, remote, pullAction);
}
private bool StartPullDialogInternal(IWin32Window? owner, bool pullOnShow, out bool pullCompleted, string? remoteBranch, string? remote, AppSettings.PullAction pullAction)
{
var pulled = false;
bool Action()
{
using FormPull formPull = new(this, remoteBranch, remote, pullAction);
var dlgResult = pullOnShow
? formPull.PullAndShowDialogWhenFailed(owner, remote, pullAction)
: formPull.ShowDialog(owner);
if (dlgResult == DialogResult.OK)
{
pulled = !formPull.ErrorOccurred;
}
return dlgResult == DialogResult.OK;
}
bool done = DoActionOnRepo(owner, Action);
pullCompleted = pulled;
return done;
}
public bool StartViewPatchDialog(IWin32Window? owner, string? patchFile = null)
{
bool Action()
{
using FormViewPatch viewPatch = new(this);
if (!string.IsNullOrEmpty(patchFile))
{
viewPatch.LoadPatch(patchFile);
}
viewPatch.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, requiresValidWorkingDir: false, changesRepo: false);
}
public bool StartFormCommitDiff(ObjectId objectId)
{
bool Action()
{
using FormCommitDiff viewPatch = new(this, objectId);
viewPatch.ShowDialog(null);
return true;
}
return DoActionOnRepo(null, Action, requiresValidWorkingDir: false, changesRepo: false);
}
public bool StartViewPatchDialog(string patchFile)
{
return StartViewPatchDialog(null, patchFile);
}
public bool StartSparseWorkingCopyDialog(IWin32Window? owner)
{
bool Action()
{
using FormSparseWorkingCopy form = new(this);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false);
}
public void AddCommitTemplate(string key, Func<string> addingText, Image? icon)
{
_commitTemplateManager.Register(key, addingText, icon);
}
public void RemoveCommitTemplate(string key)
{
_commitTemplateManager.Unregister(key);
}
public bool StartFormatPatchDialog(IWin32Window? owner = null)
{
bool Action()
{
using FormFormatPatch form = new(this);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false);
}
public bool StartStashDialog(IWin32Window? owner = null, bool manageStashes = true)
{
bool Action()
{
using FormStash form = new(this) { ManageStashes = manageStashes };
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false);
}
public bool StartResetChangesDialog(IWin32Window? owner = null)
{
var workTreeFiles = Module.GetWorkTreeFiles();
return StartResetChangesDialog(owner, workTreeFiles, false);
}
public bool StartResetChangesDialog(IWin32Window? owner, IReadOnlyCollection<GitItemStatus> workTreeFiles, bool onlyWorkTree)
{
// Show a form asking the user if they want to reset the changes.
FormResetChanges.ActionEnum resetAction = FormResetChanges.ShowResetDialog(owner, workTreeFiles.Any(item => !item.IsNew), workTreeFiles.Any(item => item.IsNew));
if (resetAction == FormResetChanges.ActionEnum.Cancel)
{
return false;
}
bool Action()
{
if (onlyWorkTree)
{
GitArgumentBuilder args = new("checkout")
{
"--",
"."
};
Module.GitExecutable.GetOutput(args);
}
else
{
// Reset all changes.
Module.Reset(ResetMode.Hard);
}
if (resetAction == FormResetChanges.ActionEnum.ResetAndDelete)
{
Module.Clean(CleanMode.OnlyNonIgnored, directories: true);
}
return true;
}
return DoActionOnRepo(owner, Action);
}
private bool StartResetChangesDialog(string fileName)
{
// Show a form asking the user if they want to reset the changes.
FormResetChanges.ActionEnum resetAction = FormResetChanges.ShowResetDialog(null, true, false);
if (resetAction == FormResetChanges.ActionEnum.Cancel)
{
return false;
}
using (WaitCursorScope.Enter())
{
// Reset all changes.
Module.ResetFile(fileName);
// Also delete new files, if requested.
if (resetAction == FormResetChanges.ActionEnum.ResetAndDelete)
{
string? errorCaption = null;
string? errorMessage = null;
string? path = _fullPathResolver.Resolve(fileName);
if (File.Exists(path))
{
try
{
File.Delete(path);
}
catch (Exception ex)
{
errorCaption = TranslatedStrings.ErrorCaptionFailedDeleteFile;
errorMessage = ex.Message;
}
}
else
{
errorCaption = TranslatedStrings.ErrorCaptionFailedDeleteFolder;
path.TryDeleteDirectory(out errorMessage);
}
if (errorMessage is not null)
{
MessageBox.Show(null, errorMessage, errorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
return true;
}
public bool StartRevertCommitDialog(IWin32Window? owner, GitRevision revision)
{
bool Action()
{
using FormRevertCommit form = new(this, revision);
return form.ShowDialog(owner) == DialogResult.OK;
}
return DoActionOnRepo(owner, Action);
}
public bool StartResolveConflictsDialog(IWin32Window? owner = null, bool offerCommit = true)
{
bool Action()
{
using FormResolveConflicts form = new(this, offerCommit);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action);
}
public bool StartCherryPickDialog(IWin32Window? owner = null, GitRevision? revision = null)
{
bool Action()
{
using FormCherryPick form = new(this, revision);
return form.ShowDialog(owner) == DialogResult.OK;
}
return DoActionOnRepo(owner, Action);
}
public bool StartCherryPickDialog(IWin32Window? owner, IEnumerable<GitRevision> revisions)
{
if (revisions is null)
{
throw new ArgumentNullException(nameof(revisions));
}
bool Action()
{
FormCherryPick? prevForm = null;
try
{
bool repoChanged = false;
// ReSharper disable once PossibleMultipleEnumeration
foreach (var r in revisions)
{
FormCherryPick frm = new(this, r);
if (prevForm is not null)
{
frm.CopyOptions(prevForm);
prevForm.Dispose();
}
prevForm = frm;
if (frm.ShowDialog(owner) == DialogResult.OK)
{
repoChanged = true;
}
else
{
return repoChanged;
}
}
return repoChanged;
}
finally
{
prevForm?.Dispose();
}
}
return DoActionOnRepo(owner, Action);
}
/// <summary>Start Merge dialog, using the specified branch.</summary>
/// <param name="owner">Owner of the dialog.</param>
/// <param name="branch">Branch to merge into the current branch.</param>
public bool StartMergeBranchDialog(IWin32Window? owner, string? branch)
{
bool Action()
{
using FormMergeBranch form = new(this, branch);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false);
}
public bool StartCreateTagDialog(IWin32Window? owner = null, GitRevision? revision = null)
{
bool Action()
{
using FormCreateTag form = new(this, revision?.ObjectId);
return form.ShowDialog(owner) == DialogResult.OK;
}
return DoActionOnRepo(owner, Action);
}
public bool StartDeleteTagDialog(IWin32Window? owner, string? tag)
{
bool Action()
{
using FormDeleteTag form = new(this, tag);
return form.ShowDialog(owner) == DialogResult.OK;
}
return DoActionOnRepo(owner, Action);
}
public bool StartEditGitIgnoreDialog(IWin32Window? owner, bool localExcludes)
{
bool Action()
{
using FormGitIgnore form = new(this, localExcludes);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false, postEvent: PostEditGitIgnore);
}
public bool StartAddToGitIgnoreDialog(IWin32Window? owner, bool localExclude, params string[] filePattern)
{
bool Action()
{
using FormAddToGitIgnore frm = new(this, localExclude, filePattern);
frm.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false, postEvent: PostEditGitIgnore);
}
public bool StartSettingsDialog(IWin32Window? owner = null, SettingsPageReference? initialPage = null)
{
bool Action()
{
return FormSettings.ShowSettingsDialog(this, owner, initialPage)
is DialogResult.OK;
}
return DoActionOnRepo(owner, Action, requiresValidWorkingDir: false, postEvent: PostSettings);
}
public bool StartSettingsDialog(IGitPlugin gitPlugin)
{
// TODO: how to pass the main dialog as owner of the SettingsDialog (first parameter):
return StartSettingsDialog(null, new SettingsPageReferenceByPlugin(gitPlugin));
}
public bool StartSettingsDialog(Type pageType)
{
return StartSettingsDialog(null, new SettingsPageReferenceByType(pageType));
}
/// <summary>
/// Open the archive dialog.
/// </summary>
/// <param name="revision">Revision to create an archive from.</param>
/// <param name="revision2">Revision for differential archive.</param>
/// <param name="path">Files path for archive.</param>
public bool StartArchiveDialog(IWin32Window? owner = null, GitRevision? revision = null, GitRevision? revision2 = null, string? path = null)
{
return DoActionOnRepo(owner, action: () =>
{
using FormArchive form = new(this)
{
SelectedRevision = revision,
};
form.SetDiffSelectedRevision(revision2);
form.SetPathArgument(path);
form.ShowDialog(owner);
return true;
}, changesRepo: false);
}
public bool StartMailMapDialog(IWin32Window? owner = null)
{
bool Action()
{
using FormMailMap form = new(this);
form.ShowDialog(owner);
return true;
}
return DoActionOnRepo(owner, Action, changesRepo: false);
}
public bool StartVerifyDatabaseDialog(IWin32Window? owner = null)
{
bool Action()
{
using FormVerify form = new(this);
form.ShowDialog(owner);
return true;
}
// TODO: move Notify to FormVerify and friends
return DoActionOnRepo(owner, Action);
}