Skip to content

Commit 206634b

Browse files
committed
Move I/O operations out of constructors
1 parent ec17ea9 commit 206634b

40 files changed

+323
-73
lines changed

src/Commands/Command.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public enum EditorType
3232
public bool RaiseError { get; set; } = true;
3333
public Models.ICommandLog Log { get; set; } = null;
3434

35-
public bool Exec()
35+
public virtual bool Exec()
3636
{
3737
Log?.AppendLine($"$ git {Args}\n");
3838

src/Commands/Fetch.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public Fetch(string repo, string remote, bool noTags, bool force)
66
{
77
WorkingDirectory = repo;
88
Context = repo;
9-
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
9+
_remote = remote;
1010
Args = "fetch --progress --verbose ";
1111

1212
if (noTags)
@@ -24,8 +24,16 @@ public Fetch(string repo, Models.Branch local, Models.Branch remote)
2424
{
2525
WorkingDirectory = repo;
2626
Context = repo;
27-
SSHKey = new Config(repo).Get($"remote.{remote.Remote}.sshkey");
27+
_remote = remote.Remote;
2828
Args = $"fetch --progress --verbose {remote.Remote} {remote.Name}:{local.Name}";
2929
}
30+
31+
public override bool Exec()
32+
{
33+
SSHKey = new Config(Context).Get($"remote.{_remote}.sshkey");
34+
return base.Exec();
35+
}
36+
37+
private readonly string _remote;
3038
}
3139
}

src/Commands/Pull.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ public Pull(string repo, string remote, string branch, bool useRebase)
66
{
77
WorkingDirectory = repo;
88
Context = repo;
9-
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
9+
_remote = remote;
1010
Args = "pull --verbose --progress ";
1111

1212
if (useRebase)
1313
Args += "--rebase=true ";
1414

1515
Args += $"{remote} {branch}";
1616
}
17+
18+
public override bool Exec()
19+
{
20+
SSHKey = new Config(Context).Get($"remote.{_remote}.sshkey");
21+
return base.Exec();
22+
}
23+
24+
private readonly string _remote;
1725
}
1826
}

src/Commands/Push.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public Push(string repo, string local, string remote, string remoteBranch, bool
66
{
77
WorkingDirectory = repo;
88
Context = repo;
9-
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
9+
_remote = remote;
1010
Args = "push --progress --verbose ";
1111

1212
if (withTags)
@@ -25,13 +25,21 @@ public Push(string repo, string remote, string refname, bool isDelete)
2525
{
2626
WorkingDirectory = repo;
2727
Context = repo;
28-
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
28+
_remote = remote;
2929
Args = "push ";
3030

3131
if (isDelete)
3232
Args += "--delete ";
3333

3434
Args += $"{remote} {refname}";
3535
}
36+
37+
public override bool Exec()
38+
{
39+
SSHKey = new Config(Context).Get($"remote.{_remote}.sshkey");
40+
return base.Exec();
41+
}
42+
43+
private readonly string _remote;
3644
}
3745
}

src/ViewModels/AIAssistant.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public AIAssistant(Repository repo, Models.OpenAIService service, List<Models.Ch
3030
_changes = changes;
3131
_onApply = onApply;
3232
_cancel = new CancellationTokenSource();
33-
34-
Gen();
3533
}
3634

3735
public void Regen()
@@ -52,7 +50,7 @@ public void Cancel()
5250
_cancel?.Cancel();
5351
}
5452

55-
private void Gen()
53+
public void Gen()
5654
{
5755
Text = string.Empty;
5856
IsGenerating = true;

src/ViewModels/AssumeUnchangedManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public AssumeUnchangedManager(Repository repo)
1313
{
1414
_repo = repo;
1515
Files = new AvaloniaList<string>();
16+
}
1617

18+
public void Load()
19+
{
1720
Task.Run(() =>
1821
{
1922
var collect = new Commands.QueryAssumeUnchangedFiles(_repo.FullPath).Result();

src/ViewModels/BranchCompare.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ public BranchCompare(string repo, Models.Branch baseBranch, Models.Branch toBran
8080
_repo = repo;
8181
_based = baseBranch;
8282
_to = toBranch;
83-
84-
Refresh();
8583
}
8684

8785
public void NavigateTo(string commitSHA)
@@ -176,7 +174,7 @@ public ContextMenu CreateChangeContextMenu()
176174
return menu;
177175
}
178176

179-
private void Refresh()
177+
public void Refresh()
180178
{
181179
Task.Run(() =>
182180
{

src/ViewModels/Clone.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public Clone(string pageId)
6767
_parentFolder = activeWorkspace?.DefaultCloneDir;
6868
if (string.IsNullOrEmpty(ParentFolder))
6969
_parentFolder = Preferences.Instance.GitDefaultCloneDir;
70+
}
7071

72+
public void Load()
73+
{
7174
Task.Run(async () =>
7275
{
7376
try

src/ViewModels/Conflict.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public ConflictSourceBranch(string name, string head, Models.Commit revision)
1515
Revision = revision;
1616
}
1717

18-
public ConflictSourceBranch(Repository repo, Models.Branch branch)
18+
public ConflictSourceBranch(Models.Branch branch, Models.Commit revision)
1919
{
2020
Name = branch.Name;
2121
Head = branch.Head;
22-
Revision = new Commands.QuerySingleCommit(repo.FullPath, branch.Head).Result() ?? new Models.Commit() { SHA = branch.Head };
22+
Revision = revision ?? new Models.Commit() { SHA = branch.Head };
2323
}
2424
}
2525

@@ -61,25 +61,31 @@ public bool CanUseExternalMergeTool
6161

6262
public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
6363
{
64+
_repo = repo;
6465
_wc = wc;
6566
_change = change;
67+
}
6668

67-
var isSubmodule = repo.Submodules.Find(x => x.Path.Equals(change.Path, StringComparison.Ordinal)) != null;
69+
public void Load()
70+
{
71+
var isSubmodule = _repo.Submodules.Find(x => x.Path.Equals(_change.Path, StringComparison.Ordinal)) != null;
6872
if (!isSubmodule && (_change.ConflictReason == Models.ConflictReason.BothAdded || _change.ConflictReason == Models.ConflictReason.BothModified))
6973
{
7074
CanUseExternalMergeTool = true;
71-
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).Result();
75+
IsResolved = new Commands.IsConflictResolved(_repo.FullPath, _change).Result();
7276
}
7377

74-
var context = wc.InProgressContext;
78+
var context = _wc.InProgressContext;
79+
var revision = new Commands.QuerySingleCommit(_repo.FullPath, _repo.CurrentBranch.Head).Result();
80+
var mine = new ConflictSourceBranch(_repo.CurrentBranch, revision);
7581
if (context is CherryPickInProgress cherryPick)
7682
{
7783
Theirs = cherryPick.Head;
78-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
84+
Mine = mine;
7985
}
8086
else if (context is RebaseInProgress rebase)
8187
{
82-
var b = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
88+
var b = _repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
8389
if (b != null)
8490
Theirs = new ConflictSourceBranch(b.Name, b.Head, rebase.StoppedAt);
8591
else
@@ -90,17 +96,17 @@ public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
9096
else if (context is RevertInProgress revert)
9197
{
9298
Theirs = revert.Head;
93-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
99+
Mine = mine;
94100
}
95101
else if (context is MergeInProgress merge)
96102
{
97103
Theirs = merge.Source;
98-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
104+
Mine = mine;
99105
}
100106
else
101107
{
102108
Theirs = "Stash or Patch";
103-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
109+
Mine = mine;
104110
}
105111
}
106112

@@ -119,6 +125,7 @@ public void OpenExternalMergeTool()
119125
_wc.UseExternalMergeTool(_change);
120126
}
121127

128+
private readonly Repository _repo;
122129
private WorkingCopy _wc = null;
123130
private Models.Change _change = null;
124131
}

src/ViewModels/CreateTag.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public CreateTag(Repository repo, Models.Branch branch)
5151
_basedOn = branch.Head;
5252

5353
BasedOn = branch;
54-
SignTag = new Commands.Config(repo.FullPath).Get("tag.gpgsign").Equals("true", StringComparison.OrdinalIgnoreCase);
5554
}
5655

5756
public CreateTag(Repository repo, Models.Commit commit)
@@ -60,7 +59,11 @@ public CreateTag(Repository repo, Models.Commit commit)
6059
_basedOn = commit.SHA;
6160

6261
BasedOn = commit;
63-
SignTag = new Commands.Config(repo.FullPath).Get("tag.gpgsign").Equals("true", StringComparison.OrdinalIgnoreCase);
62+
}
63+
64+
public void Load()
65+
{
66+
SignTag = new Commands.Config(_repo.FullPath).Get("tag.gpgsign").Equals("true", StringComparison.OrdinalIgnoreCase);
6467
}
6568

6669
public static ValidationResult ValidateTagName(string name, ValidationContext ctx)

0 commit comments

Comments
 (0)