-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathQueryRepositoryStatus.cs
More file actions
59 lines (49 loc) · 2.02 KB
/
QueryRepositoryStatus.cs
File metadata and controls
59 lines (49 loc) · 2.02 KB
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
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
public partial class QueryRepositoryStatus : Command
{
[GeneratedRegex(@"\+(\d+) \-(\d+)")]
private static partial Regex REG_BRANCH_AB();
public QueryRepositoryStatus(string repo)
{
WorkingDirectory = repo;
RaiseError = false;
}
public async Task<Models.RepositoryStatus> GetResultAsync()
{
Args = "status --porcelain=v2 -b";
var rs = await ReadToEndAsync().ConfigureAwait(false);
if (!rs.IsSuccess)
return null;
var status = new Models.RepositoryStatus();
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
var count = lines.Length;
if (count < 2)
return null;
var sha1 = lines[0].Substring(13).Trim(); // Remove "# branch.oid " prefix
var head = lines[1].Substring(14).Trim(); // Remove "# branch.head " prefix
if (head.Equals("(detached)", StringComparison.Ordinal))
status.CurrentBranch = sha1.Length > 10 ? $"({sha1.Substring(0, 10)})" : "-";
else
status.CurrentBranch = head;
if (count == 4 && lines[3].StartsWith("# branch.ab ", StringComparison.Ordinal))
ParseTrackStatus(status, lines[3].Substring(12).Trim());
status.LocalChanges = await new CountLocalChanges(WorkingDirectory, true) { RaiseError = false }
.GetResultAsync()
.ConfigureAwait(false);
return status;
}
private void ParseTrackStatus(Models.RepositoryStatus status, string input)
{
var match = REG_BRANCH_AB().Match(input);
if (match.Success)
{
status.Ahead = int.Parse(match.Groups[1].Value);
status.Behind = int.Parse(match.Groups[2].Value);
}
}
}
}