Skip to content

Commit d7ed866

Browse files
authored
Merge pull request #27 from CandyCoded/feature/async-tasks
[feat] Changed git methods to async.
2 parents 724cddf + 9ef22f2 commit d7ed866

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/Git.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if UNITY_EDITOR
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Threading.Tasks;
67
using Debug = UnityEngine.Debug;
78

89
namespace CandyCoded.GitStatus
@@ -17,34 +18,34 @@ public static class Git
1718
public static string GitPath => "/usr/local/bin/git";
1819
#endif
1920

20-
public static Process GenerateProcess(string path, string arguments)
21+
public static Task<Process> GenerateProcess(string path, string arguments)
2122
{
2223

23-
return Process.Start(new ProcessStartInfo
24+
return Task.Run(() => Process.Start(new ProcessStartInfo
2425
{
2526
FileName = path,
2627
Arguments = arguments,
2728
UseShellExecute = false,
2829
RedirectStandardOutput = true,
2930
RedirectStandardError = true,
3031
CreateNoWindow = true
31-
});
32+
}));
3233

3334
}
3435

35-
public static string Branch()
36+
public static async Task<string> Branch()
3637
{
3738

38-
var process = GenerateProcess(GitPath, "rev-parse --abbrev-ref HEAD");
39+
var process = await GenerateProcess(GitPath, "rev-parse --abbrev-ref HEAD");
3940

4041
return process?.StandardOutput.ReadLine();
4142

4243
}
4344

44-
public static string[] Branches()
45+
public static async Task<string[]> Branches()
4546
{
4647

47-
var process = GenerateProcess(GitPath, "for-each-ref --format='%(refname:short)' refs/heads");
48+
var process = await GenerateProcess(GitPath, "for-each-ref --format='%(refname:short)' refs/heads");
4849

4950
var branches = new List<string>();
5051

@@ -66,10 +67,10 @@ public static void CheckoutBranch(string branch)
6667

6768
}
6869

69-
public static string[] ChangedFiles()
70+
public static async Task<string[]> ChangedFiles()
7071
{
7172

72-
var process = GenerateProcess(GitPath, "status --short --untracked-files=no --porcelain");
73+
var process = await GenerateProcess(GitPath, "status --short --untracked-files=no --porcelain");
7374

7475
var changes = new List<string>();
7576

@@ -84,10 +85,10 @@ public static string[] ChangedFiles()
8485

8586
}
8687

87-
public static string[] UntrackedFiles()
88+
public static async Task<string[]> UntrackedFiles()
8889
{
8990

90-
var process = GenerateProcess(GitPath, "ls-files --others --exclude-standard");
91+
var process = await GenerateProcess(GitPath, "ls-files --others --exclude-standard");
9192

9293
var changes = new List<string>();
9394

@@ -102,10 +103,10 @@ public static string[] UntrackedFiles()
102103

103104
}
104105

105-
public static void DiscardChanges(string path)
106+
public static async Task DiscardChanges(string path)
106107
{
107108

108-
var process = GenerateProcess(GitPath, $@"checkout ""{path}""");
109+
var process = await GenerateProcess(GitPath, $@"checkout ""{path}""");
109110
110111
if (process?.StandardError.ReadLine() is string line && line.StartsWith("error: pathspec"))
111112
{

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitMenuItems.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if UNITY_EDITOR
44
using System;
55
using System.IO;
6+
using System.Threading.Tasks;
67
using UnityEditor;
78

89
namespace CandyCoded.GitStatus
@@ -22,10 +23,10 @@ private static string GetSelectedPath()
2223

2324
[MenuItem("Git/Discard Changes", false, PRIORITY)]
2425
[MenuItem("Assets/Discard Changes", false, PRIORITY)]
25-
private static void DiscardChanges()
26+
private static async void DiscardChanges()
2627
{
2728

28-
Git.DiscardChanges(GetSelectedPath());
29+
await Git.DiscardChanges(GetSelectedPath());
2930

3031
}
3132

@@ -40,7 +41,7 @@ private static bool ValidateDiscardChanges()
4041

4142
[MenuItem("Git/Discard All Changes", false, PRIORITY)]
4243
[MenuItem("Assets/Discard All Changes", false, PRIORITY)]
43-
private static void DiscardAllChanges()
44+
private static async void DiscardAllChanges()
4445
{
4546

4647
if (EditorUtility.DisplayDialog(
@@ -50,7 +51,7 @@ private static void DiscardAllChanges()
5051
"Cancel"))
5152
{
5253

53-
Git.DiscardChanges(GetSelectedPath());
54+
await Git.DiscardChanges(GetSelectedPath());
5455

5556
}
5657

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitStatus.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#if UNITY_EDITOR
44
using System;
5+
using System.Threading.Tasks;
56
using UnityEditor;
67

78
namespace CandyCoded.GitStatus
@@ -34,13 +35,17 @@ static GitStatus()
3435
public static void Update()
3536
{
3637

37-
branch = Git.Branch();
38+
Task.Run(UpdateAsync);
3839

39-
branches = Git.Branches();
40+
}
4041

41-
changedFiles = Git.ChangedFiles();
42+
public static async void UpdateAsync()
43+
{
4244

43-
untrackedFiles = Git.UntrackedFiles();
45+
branch = await Git.Branch();
46+
branches = await Git.Branches();
47+
changedFiles = await Git.ChangedFiles();
48+
untrackedFiles = await Git.UntrackedFiles();
4449

4550
lastUpdated = DateTime.Now;
4651

0 commit comments

Comments
 (0)