-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathQueryLocalChanges.cs
More file actions
70 lines (62 loc) · 2.76 KB
/
QueryLocalChanges.cs
File metadata and controls
70 lines (62 loc) · 2.76 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
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
public partial class QueryLocalChanges : Command
{
[GeneratedRegex(@"^(\s?[\w\?]{1,4})\s+(.+)$")]
private static partial Regex REG_FORMAT();
private static readonly string[] UNTRACKED = ["no", "all"];
public QueryLocalChanges(string repo, bool includeUntracked = true)
{
WorkingDirectory = repo;
Context = repo;
Args = $"--no-optional-locks status -u{UNTRACKED[includeUntracked ? 1 : 0]} --ignore-submodules=dirty --porcelain";
}
public async Task<List<Models.Change>> GetResultAsync()
{
var outs = new List<Models.Change>();
var rs = await ReadToEndAsync().ConfigureAwait(false);
if (!rs.IsSuccess)
{
App.RaiseException(Context, rs.StdErr);
return outs;
}
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var match = REG_FORMAT().Match(line);
if (!match.Success)
continue;
var change = new Models.Change() { Path = match.Groups[2].Value };
var status = match.Groups[1].Value;
change.ConflictReason = status switch
{
"DD" => Models.ConflictReason.BothDeleted,
"AU" => Models.ConflictReason.AddedByUs,
"UD" => Models.ConflictReason.DeletedByThem,
"UA" => Models.ConflictReason.AddedByThem,
"DU" => Models.ConflictReason.DeletedByUs,
"AA" => Models.ConflictReason.BothAdded,
"UU" => Models.ConflictReason.BothModified,
_ => Models.ConflictReason.None
};
if (change.ConflictReason != Models.ConflictReason.None)
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
else if (status == "??")
change.Set(Models.ChangeState.None, Models.ChangeState.Untracked);
else
{
var indexStatus = Models.Change.ChangeStateFromCode(status[0]);
var worktreeStatus = status.Length > 1 ? Models.Change.ChangeStateFromCode(status[1]) : Models.ChangeState.None;
change.Set(indexStatus, worktreeStatus);
}
if (change.Index != Models.ChangeState.None || change.WorkTree != Models.ChangeState.None)
outs.Add(change);
}
return outs;
}
}
}