Skip to content

Commit 3ec92cc

Browse files
committed
Async event handlers as needed
1 parent d35fa72 commit 3ec92cc

19 files changed

+77
-311
lines changed

src/Commands/Command.cs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,77 +33,6 @@ public enum EditorType
3333
public bool RaiseError { get; set; } = true;
3434
public Models.ICommandLog Log { get; set; } = null;
3535

36-
public bool Exec()
37-
{
38-
Log?.AppendLine($"$ git {Args}\n");
39-
40-
var start = CreateGitStartInfo();
41-
var errs = new List<string>();
42-
var proc = new Process() { StartInfo = start };
43-
44-
proc.OutputDataReceived += (_, e) => HandleOutput(e.Data, errs);
45-
proc.ErrorDataReceived += (_, e) => HandleOutput(e.Data, errs);
46-
47-
var dummy = null as Process;
48-
var dummyProcLock = new object();
49-
try
50-
{
51-
proc.Start();
52-
53-
// It not safe, please only use `CancellationToken` in readonly commands.
54-
if (CancellationToken.CanBeCanceled)
55-
{
56-
dummy = proc;
57-
CancellationToken.Register(() =>
58-
{
59-
lock (dummyProcLock)
60-
{
61-
if (dummy is { HasExited: false })
62-
dummy.Kill();
63-
}
64-
});
65-
}
66-
}
67-
catch (Exception e)
68-
{
69-
if (RaiseError)
70-
App.RaiseException(Context, e.Message);
71-
72-
Log?.AppendLine(string.Empty);
73-
return false;
74-
}
75-
76-
proc.BeginOutputReadLine();
77-
proc.BeginErrorReadLine();
78-
proc.WaitForExit();
79-
80-
if (dummy != null)
81-
{
82-
lock (dummyProcLock)
83-
{
84-
dummy = null;
85-
}
86-
}
87-
88-
int exitCode = proc.ExitCode;
89-
proc.Close();
90-
Log?.AppendLine(string.Empty);
91-
92-
if (!CancellationToken.IsCancellationRequested && exitCode != 0)
93-
{
94-
if (RaiseError)
95-
{
96-
var errMsg = string.Join("\n", errs).Trim();
97-
if (!string.IsNullOrEmpty(errMsg))
98-
App.RaiseException(Context, errMsg);
99-
}
100-
101-
return false;
102-
}
103-
104-
return true;
105-
}
106-
10736
public ReadToEndResult ReadToEnd()
10837
{
10938
var start = CreateGitStartInfo();

src/Commands/Config.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ public string Get(string key)
5252
return ReadToEnd().StdOut.Trim();
5353
}
5454

55-
public bool Set(string key, string value, bool allowEmpty = false)
56-
{
57-
var scope = _isLocal ? "--local" : "--global";
58-
59-
if (!allowEmpty && string.IsNullOrWhiteSpace(value))
60-
Args = $"config {scope} --unset {key}";
61-
else
62-
Args = $"config {scope} {key} \"{value}\"";
63-
64-
return Exec();
65-
}
66-
6755
public async Task<Dictionary<string, string>> ListAllAsync()
6856
{
6957
Args = "config -l";

src/Commands/Discard.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,6 @@ namespace SourceGit.Commands
77
{
88
public static class Discard
99
{
10-
/// <summary>
11-
/// Discard selected changes (only unstaged).
12-
/// </summary>
13-
/// <param name="repo"></param>
14-
/// <param name="changes"></param>
15-
/// <param name="log"></param>
16-
public static void Changes(string repo, List<Models.Change> changes, Models.ICommandLog log)
17-
{
18-
var restores = new List<string>();
19-
20-
try
21-
{
22-
foreach (var c in changes)
23-
{
24-
if (c.WorkTree == Models.ChangeState.Untracked || c.WorkTree == Models.ChangeState.Added)
25-
{
26-
var fullPath = Path.Combine(repo, c.Path);
27-
if (Directory.Exists(fullPath))
28-
Directory.Delete(fullPath, true);
29-
else
30-
File.Delete(fullPath);
31-
}
32-
else
33-
{
34-
restores.Add(c.Path);
35-
}
36-
}
37-
}
38-
catch (Exception e)
39-
{
40-
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
41-
}
42-
43-
if (restores.Count > 0)
44-
{
45-
var pathSpecFile = Path.GetTempFileName();
46-
File.WriteAllLines(pathSpecFile, restores);
47-
new Restore(repo, pathSpecFile, false) { Log = log }.Exec();
48-
File.Delete(pathSpecFile);
49-
}
50-
}
51-
5210
/// <summary>
5311
/// Discard all local changes (unstaged & staged)
5412
/// </summary>

src/Commands/IsCommitSHA.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ public IsCommitSHA(string repo, string hash)
1010
Args = $"cat-file -t {hash}";
1111
}
1212

13-
public bool Result()
14-
{
15-
var rs = ReadToEnd();
16-
return rs.IsSuccess && rs.StdOut.Trim().Equals("commit");
17-
}
18-
1913
public async Task<bool> ResultAsync()
2014
{
2115
var rs = await ReadToEndAsync();

src/Commands/LFS.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public bool IsEnabled()
3737
return content.Contains("git lfs pre-push");
3838
}
3939

40-
public bool Install(Models.ICommandLog log)
41-
{
42-
return new SubCmd(_repo, "lfs install --local", log).Exec();
43-
}
44-
4540
public async Task<bool> InstallAsync(Models.ICommandLog log)
4641
{
4742
return await new SubCmd(_repo, "lfs install --local", log).ExecAsync();

src/Commands/QueryStagedFileBlobGuid.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ public QueryStagedFileBlobGuid(string repo, string file)
1515
Args = $"ls-files -s -- \"{file}\"";
1616
}
1717

18-
public string Result()
19-
{
20-
var rs = ReadToEnd();
21-
var match = REG_FORMAT().Match(rs.StdOut.Trim());
22-
if (match.Success)
23-
{
24-
return match.Groups[1].Value;
25-
}
26-
27-
return string.Empty;
28-
}
29-
3018
public async Task<string> ResultAsync()
3119
{
3220
var rs = await ReadToEndAsync();

src/Commands/Stash.cs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,6 @@ public Stash(string repo)
1212
Context = repo;
1313
}
1414

15-
public bool Push(string message, List<Models.Change> changes, bool keepIndex)
16-
{
17-
var builder = new StringBuilder();
18-
builder.Append("stash push --include-untracked ");
19-
if (keepIndex)
20-
builder.Append("--keep-index ");
21-
builder.Append("-m \"");
22-
builder.Append(message);
23-
builder.Append("\" -- ");
24-
25-
foreach (var c in changes)
26-
builder.Append($"\"{c.Path}\" ");
27-
28-
Args = builder.ToString();
29-
return Exec();
30-
}
31-
32-
public bool Push(string message, string pathspecFromFile, bool keepIndex)
33-
{
34-
var builder = new StringBuilder();
35-
builder.Append("stash push --include-untracked --pathspec-from-file=\"");
36-
builder.Append(pathspecFromFile);
37-
builder.Append("\" ");
38-
if (keepIndex)
39-
builder.Append("--keep-index ");
40-
builder.Append("-m \"");
41-
builder.Append(message);
42-
builder.Append("\"");
43-
44-
Args = builder.ToString();
45-
return Exec();
46-
}
47-
4815
public async Task<bool> PushAsync(string message, bool includeUntracked = true, bool keepIndex = false)
4916
{
5017
var builder = new StringBuilder();

src/Commands/Worktree.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -57,62 +57,6 @@ public Worktree(string repo)
5757
return worktrees;
5858
}
5959

60-
public bool Lock(string fullpath)
61-
{
62-
Args = $"worktree lock \"{fullpath}\"";
63-
return Exec();
64-
}
65-
66-
public bool Unlock(string fullpath)
67-
{
68-
Args = $"worktree unlock \"{fullpath}\"";
69-
return Exec();
70-
}
71-
72-
public async Task<List<Models.Worktree>> ListAsync()
73-
{
74-
Args = "worktree list --porcelain";
75-
76-
var rs = await ReadToEndAsync();
77-
var worktrees = new List<Models.Worktree>();
78-
var last = null as Models.Worktree;
79-
if (rs.IsSuccess)
80-
{
81-
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
82-
foreach (var line in lines)
83-
{
84-
if (line.StartsWith("worktree ", StringComparison.Ordinal))
85-
{
86-
last = new Models.Worktree() { FullPath = line.Substring(9).Trim() };
87-
last.RelativePath = Path.GetRelativePath(WorkingDirectory, last.FullPath);
88-
worktrees.Add(last);
89-
}
90-
else if (line.StartsWith("bare", StringComparison.Ordinal))
91-
{
92-
last!.IsBare = true;
93-
}
94-
else if (line.StartsWith("HEAD ", StringComparison.Ordinal))
95-
{
96-
last!.Head = line.Substring(5).Trim();
97-
}
98-
else if (line.StartsWith("branch ", StringComparison.Ordinal))
99-
{
100-
last!.Branch = line.Substring(7).Trim();
101-
}
102-
else if (line.StartsWith("detached", StringComparison.Ordinal))
103-
{
104-
last!.IsDetached = true;
105-
}
106-
else if (line.StartsWith("locked", StringComparison.Ordinal))
107-
{
108-
last!.IsLocked = true;
109-
}
110-
}
111-
}
112-
113-
return worktrees;
114-
}
115-
11660
public async Task<bool> AddAsync(string fullpath, string name, bool createNew, string tracking)
11761
{
11862
Args = "worktree add ";

src/ViewModels/AssumeUnchangedManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public AssumeUnchangedManager(Repository repo)
2121
});
2222
}
2323

24-
public void Remove(string file)
24+
public async Task RemoveAsync(string file)
2525
{
2626
if (!string.IsNullOrEmpty(file))
2727
{
2828
var log = _repo.CreateLog("Remove Assume Unchanged File");
29-
new Commands.AssumeUnchanged(_repo.FullPath, file, false).Use(log).Exec();
29+
await new Commands.AssumeUnchanged(_repo.FullPath, file, false).Use(log).ExecAsync();
3030
log.Complete();
3131
Files.Remove(file);
3232
}

src/ViewModels/CommitDetail.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ private void Refresh()
721721
Task.Run(async () =>
722722
{
723723
var message = await new Commands.QueryCommitFullMessage(_repo.FullPath, _commit.SHA).ResultAsync();
724-
var inlines = ParseInlinesInMessage(message);
724+
var inlines = await ParseInlinesInMessageAsync(message);
725725

726726
if (!token.IsCancellationRequested)
727727
await Dispatcher.UIThread.InvokeAsync(() => FullMessage = new Models.CommitFullMessage { Message = message, Inlines = inlines });
@@ -776,7 +776,7 @@ private void Refresh()
776776
}, token);
777777
}
778778

779-
private Models.InlineElementCollector ParseInlinesInMessage(string message)
779+
private async Task<Models.InlineElementCollector> ParseInlinesInMessageAsync(string message)
780780
{
781781
var inlines = new Models.InlineElementCollector();
782782
if (_repo.Settings.IssueTrackerRules is { Count: > 0 } rules)
@@ -815,7 +815,7 @@ private Models.InlineElementCollector ParseInlinesInMessage(string message)
815815
continue;
816816

817817
var sha = match.Groups[1].Value;
818-
var isCommitSHA = new Commands.IsCommitSHA(_repo.FullPath, sha).Result();
818+
var isCommitSHA = await new Commands.IsCommitSHA(_repo.FullPath, sha).ResultAsync();
819819
if (isCommitSHA)
820820
inlines.Add(new Models.InlineElement(Models.InlineElementType.CommitSHA, start, len, sha));
821821
}

0 commit comments

Comments
 (0)