Skip to content

Commit 4076582

Browse files
committed
code_review: PR #1492
- Remove all synchronous method in commands - `Command.ReadToEndAsync` now is protected method - Rename `ResultAsync` to `GetResultAsync` - Call `ConfigureAwait(false)` when there's no context Signed-off-by: leo <[email protected]>
1 parent 463e304 commit 4076582

File tree

166 files changed

+1624
-2162
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+1624
-2162
lines changed

src/App.Commands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ public static bool IsCheckForUpdateCommandVisible
4343
public static readonly Command OpenAboutCommand = new Command(_ => ShowWindow(new Views.About(), false));
4444
public static readonly Command CheckForUpdateCommand = new Command(_ => (Current as App)?.Check4Update(true));
4545
public static readonly Command QuitCommand = new Command(_ => Quit(0));
46-
public static readonly Command CopyTextBlockCommand = new Command(p =>
46+
public static readonly Command CopyTextBlockCommand = new Command(async p =>
4747
{
4848
var textBlock = p as TextBlock;
4949
if (textBlock == null)
5050
return;
5151

5252
if (textBlock.Inlines is { Count: > 0 } inlines)
53-
CopyText(inlines.Text);
53+
await CopyTextAsync(inlines.Text);
5454
else if (!string.IsNullOrEmpty(textBlock.Text))
55-
CopyText(textBlock.Text);
55+
await CopyTextAsync(textBlock.Text);
5656
});
5757
}
5858
}

src/App.axaml.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ public static void ShowWindow(object data, bool showAsDialog)
146146
}
147147
}
148148

149+
public static async Task<bool> AskConfirmAsync(string message, Action onSure)
150+
{
151+
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
152+
{
153+
var confirm = new Views.Confirm();
154+
confirm.Message.Text = message;
155+
return await confirm.ShowDialog<bool>(owner);
156+
}
157+
158+
return false;
159+
}
160+
149161
public static void RaiseException(string context, string message)
150162
{
151163
if (Current is App { _launcher: not null } app)
@@ -281,7 +293,7 @@ public static void SetFonts(string defaultFont, string monospaceFont, bool onlyU
281293
}
282294
}
283295

284-
public static async void CopyText(string data)
296+
public static async Task CopyTextAsync(string data)
285297
{
286298
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow.Clipboard: { } clipboard })
287299
await clipboard.SetTextAsync(data ?? "");
@@ -571,7 +583,7 @@ private void TryOpenRepository(string repo)
571583
{
572584
if (!string.IsNullOrEmpty(repo) && Directory.Exists(repo))
573585
{
574-
var test = new Commands.QueryRepositoryRootPath(repo).ReadToEnd();
586+
var test = new Commands.QueryRepositoryRootPath(repo).GetResultAsync().Result;
575587
if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut))
576588
{
577589
Dispatcher.UIThread.Invoke(() =>

src/Commands/Blame.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public Blame(string repo, string file, string revision)
2020
_result.File = file;
2121
}
2222

23-
public async Task<Models.BlameData> ResultAsync()
23+
public async Task<Models.BlameData> ReadAsync()
2424
{
25-
var rs = await ReadToEndAsync();
25+
var rs = await ReadToEndAsync().ConfigureAwait(false);
2626
if (!rs.IsSuccess)
2727
return _result;
2828

src/Commands/Branch.cs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ namespace SourceGit.Commands
55
{
66
public static class Branch
77
{
8-
public static string ShowCurrent(string repo)
9-
{
10-
var cmd = new Command();
11-
cmd.WorkingDirectory = repo;
12-
cmd.Context = repo;
13-
cmd.Args = "branch --show-current";
14-
return cmd.ReadToEnd().StdOut.Trim();
15-
}
16-
17-
public static async Task<string> ShowCurrentAsync(string repo)
18-
{
19-
var cmd = new Command();
20-
cmd.WorkingDirectory = repo;
21-
cmd.Context = repo;
22-
cmd.Args = "branch --show-current";
23-
return (await cmd.ReadToEndAsync()).StdOut.Trim();
24-
}
25-
268
public static async Task<bool> CreateAsync(string repo, string name, string basedOn, bool force, Models.ICommandLog log)
279
{
2810
var builder = new StringBuilder();
@@ -38,7 +20,7 @@ public static async Task<bool> CreateAsync(string repo, string name, string base
3820
cmd.Context = repo;
3921
cmd.Args = builder.ToString();
4022
cmd.Log = log;
41-
return await cmd.ExecAsync();
23+
return await cmd.ExecAsync().ConfigureAwait(false);
4224
}
4325

4426
public static async Task<bool> RenameAsync(string repo, string name, string to, Models.ICommandLog log)
@@ -48,7 +30,7 @@ public static async Task<bool> RenameAsync(string repo, string name, string to,
4830
cmd.Context = repo;
4931
cmd.Args = $"branch -M {name} {to}";
5032
cmd.Log = log;
51-
return await cmd.ExecAsync();
33+
return await cmd.ExecAsync().ConfigureAwait(false);
5234
}
5335

5436
public static async Task<bool> SetUpstreamAsync(string repo, string name, string upstream, Models.ICommandLog log)
@@ -63,7 +45,7 @@ public static async Task<bool> SetUpstreamAsync(string repo, string name, string
6345
else
6446
cmd.Args = $"branch {name} -u {upstream}";
6547

66-
return await cmd.ExecAsync();
48+
return await cmd.ExecAsync().ConfigureAwait(false);
6749
}
6850

6951
public static async Task<bool> DeleteLocalAsync(string repo, string name, Models.ICommandLog log)
@@ -73,21 +55,21 @@ public static async Task<bool> DeleteLocalAsync(string repo, string name, Models
7355
cmd.Context = repo;
7456
cmd.Args = $"branch -D {name}";
7557
cmd.Log = log;
76-
return await cmd.ExecAsync();
58+
return await cmd.ExecAsync().ConfigureAwait(false);
7759
}
7860

7961
public static async Task<bool> DeleteRemoteAsync(string repo, string remote, string name, Models.ICommandLog log)
8062
{
81-
bool exists = await new Remote(repo).HasBranchAsync(remote, name);
63+
bool exists = await new Remote(repo).HasBranchAsync(remote, name).ConfigureAwait(false);
8264
if (exists)
83-
return await new Push(repo, remote, $"refs/heads/{name}", true) { Log = log }.ExecAsync();
65+
return await new Push(repo, remote, $"refs/heads/{name}", true) { Log = log }.ExecAsync().ConfigureAwait(false);
8466

8567
var cmd = new Command();
8668
cmd.WorkingDirectory = repo;
8769
cmd.Context = repo;
8870
cmd.Args = $"branch -D -r {remote}/{name}";
8971
cmd.Log = log;
90-
return await cmd.ExecAsync();
72+
return await cmd.ExecAsync().ConfigureAwait(false);
9173
}
9274
}
9375
}

src/Commands/Checkout.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task<bool> BranchAsync(string branch, bool force)
2121
builder.Append(branch);
2222

2323
Args = builder.ToString();
24-
return await ExecAsync();
24+
return await ExecAsync().ConfigureAwait(false);
2525
}
2626

2727
public async Task<bool> BranchAsync(string branch, string basedOn, bool force, bool allowOverwrite)
@@ -36,14 +36,14 @@ public async Task<bool> BranchAsync(string branch, string basedOn, bool force, b
3636
builder.Append(basedOn);
3737

3838
Args = builder.ToString();
39-
return await ExecAsync();
39+
return await ExecAsync().ConfigureAwait(false);
4040
}
4141

4242
public async Task<bool> CommitAsync(string commitId, bool force)
4343
{
4444
var option = force ? "--force" : string.Empty;
4545
Args = $"checkout {option} --detach --progress {commitId}";
46-
return await ExecAsync();
46+
return await ExecAsync().ConfigureAwait(false);
4747
}
4848

4949
public async Task<bool> UseTheirsAsync(List<string> files)
@@ -57,7 +57,7 @@ public async Task<bool> UseTheirsAsync(List<string> files)
5757
builder.Append("\"");
5858
}
5959
Args = builder.ToString();
60-
return await ExecAsync();
60+
return await ExecAsync().ConfigureAwait(false);
6161
}
6262

6363
public async Task<bool> UseMineAsync(List<string> files)
@@ -72,13 +72,13 @@ public async Task<bool> UseMineAsync(List<string> files)
7272
}
7373

7474
Args = builder.ToString();
75-
return await ExecAsync();
75+
return await ExecAsync().ConfigureAwait(false);
7676
}
7777

7878
public async Task<bool> FileWithRevisionAsync(string file, string revision)
7979
{
8080
Args = $"checkout --no-overlay {revision} -- \"{file}\"";
81-
return await ExecAsync();
81+
return await ExecAsync().ConfigureAwait(false);
8282
}
8383
}
8484
}

src/Commands/Command.cs

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ namespace SourceGit.Commands
1010
{
1111
public partial class Command
1212
{
13-
public class ReadToEndResult
13+
public class Result
1414
{
1515
public bool IsSuccess { get; set; } = false;
16-
public string StdOut { get; set; } = "";
17-
public string StdErr { get; set; } = "";
16+
public string StdOut { get; set; } = string.Empty;
17+
public string StdErr { get; set; } = string.Empty;
18+
19+
public static Result Failed(string reason) => new Result() { StdErr = reason };
1820
}
1921

2022
public enum EditorType
@@ -25,46 +27,16 @@ public enum EditorType
2527
}
2628

2729
public string Context { get; set; } = string.Empty;
28-
public CancellationToken CancellationToken { get; set; } = CancellationToken.None;
2930
public string WorkingDirectory { get; set; } = null;
30-
public EditorType Editor { get; set; } = EditorType.CoreEditor; // Only used in Exec() mode
31+
public EditorType Editor { get; set; } = EditorType.CoreEditor;
3132
public string SSHKey { get; set; } = string.Empty;
3233
public string Args { get; set; } = string.Empty;
34+
35+
// Only used in `ExecAsync` mode.
36+
public CancellationToken CancellationToken { get; set; } = CancellationToken.None;
3337
public bool RaiseError { get; set; } = true;
3438
public Models.ICommandLog Log { get; set; } = null;
3539

36-
public ReadToEndResult ReadToEnd()
37-
{
38-
var start = CreateGitStartInfo();
39-
var proc = new Process() { StartInfo = start };
40-
41-
try
42-
{
43-
proc.Start();
44-
}
45-
catch (Exception e)
46-
{
47-
return new ReadToEndResult()
48-
{
49-
IsSuccess = false,
50-
StdOut = string.Empty,
51-
StdErr = e.Message,
52-
};
53-
}
54-
55-
var rs = new ReadToEndResult()
56-
{
57-
StdOut = proc.StandardOutput.ReadToEnd(),
58-
StdErr = proc.StandardError.ReadToEnd(),
59-
};
60-
61-
proc.WaitForExit();
62-
rs.IsSuccess = proc.ExitCode == 0;
63-
proc.Close();
64-
65-
return rs;
66-
}
67-
6840
public async Task<bool> ExecAsync()
6941
{
7042
Log?.AppendLine($"$ git {Args}\n");
@@ -107,7 +79,15 @@ public async Task<bool> ExecAsync()
10779

10880
proc.BeginOutputReadLine();
10981
proc.BeginErrorReadLine();
110-
await proc.WaitForExitAsync(CancellationToken);
82+
83+
try
84+
{
85+
await proc.WaitForExitAsync(CancellationToken).ConfigureAwait(false);
86+
}
87+
catch (Exception e)
88+
{
89+
HandleOutput(e.Message, errs);
90+
}
11191

11292
if (dummy != null)
11393
{
@@ -136,7 +116,7 @@ public async Task<bool> ExecAsync()
136116
return true;
137117
}
138118

139-
public async Task<ReadToEndResult> ReadToEndAsync()
119+
protected async Task<Result> ReadToEndAsync()
140120
{
141121
var start = CreateGitStartInfo();
142122
var proc = new Process() { StartInfo = start };
@@ -147,24 +127,16 @@ public async Task<ReadToEndResult> ReadToEndAsync()
147127
}
148128
catch (Exception e)
149129
{
150-
return new ReadToEndResult()
151-
{
152-
IsSuccess = false,
153-
StdOut = string.Empty,
154-
StdErr = e.Message,
155-
};
130+
return Result.Failed(e.Message);
156131
}
157132

158-
var rs = new ReadToEndResult()
159-
{
160-
StdOut = await proc.StandardOutput.ReadToEndAsync(CancellationToken),
161-
StdErr = await proc.StandardError.ReadToEndAsync(CancellationToken),
162-
};
133+
var rs = new Result() { IsSuccess = true };
134+
rs.StdOut = await proc.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
135+
rs.StdErr = await proc.StandardError.ReadToEndAsync().ConfigureAwait(false);
136+
await proc.WaitForExitAsync().ConfigureAwait(false);
163137

164-
await proc.WaitForExitAsync(CancellationToken);
165138
rs.IsSuccess = proc.ExitCode == 0;
166139
proc.Close();
167-
168140
return rs;
169141
}
170142

src/Commands/Commit.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Commit : Command
88
public Commit(string repo, string message, bool signOff, bool amend, bool resetAuthor)
99
{
1010
_tmpFile = Path.GetTempFileName();
11-
File.WriteAllText(_tmpFile, message);
11+
_message = message;
1212

1313
WorkingDirectory = repo;
1414
Context = repo;
@@ -21,20 +21,20 @@ public Commit(string repo, string message, bool signOff, bool amend, bool resetA
2121

2222
public async Task<bool> RunAsync()
2323
{
24-
var succ = await ExecAsync();
25-
2624
try
2725
{
26+
await File.WriteAllTextAsync(_tmpFile, _message).ConfigureAwait(false);
27+
var succ = await ExecAsync().ConfigureAwait(false);
2828
File.Delete(_tmpFile);
29+
return succ;
2930
}
3031
catch
3132
{
32-
// Ignore
33+
return false;
3334
}
34-
35-
return succ;
3635
}
3736

38-
private readonly string _tmpFile;
37+
private readonly string _tmpFile = string.Empty;
38+
private readonly string _message = string.Empty;
3939
}
4040
}

0 commit comments

Comments
 (0)