forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareRevisions.cs
More file actions
91 lines (78 loc) · 3.12 KB
/
Copy pathCompareRevisions.cs
File metadata and controls
91 lines (78 loc) · 3.12 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
public partial class CompareRevisions : Command
{
[GeneratedRegex(@"^([MAD])\s+(.+)$")]
private static partial Regex REG_FORMAT();
[GeneratedRegex(@"^([CR])[0-9]{0,4}\s+(.+)$")]
private static partial Regex REG_RENAME_FORMAT();
public CompareRevisions(string repo, string start, string end)
{
WorkingDirectory = repo;
Context = repo;
var based = string.IsNullOrEmpty(start) ? "-R" : start;
Args = $"diff --name-status {based} {end}";
}
public CompareRevisions(string repo, string start, string end, string path)
{
WorkingDirectory = repo;
Context = repo;
var based = string.IsNullOrEmpty(start) ? "-R" : start;
Args = $"diff --name-status {based} {end} -- {path.Quoted()}";
}
public async Task<List<Models.Change>> ReadAsync()
{
var changes = new List<Models.Change>();
try
{
using var proc = new Process();
proc.StartInfo = CreateGitStartInfo(true);
proc.Start();
while (await proc.StandardOutput.ReadLineAsync().ConfigureAwait(false) is { } line)
{
var match = REG_FORMAT().Match(line);
if (!match.Success)
{
match = REG_RENAME_FORMAT().Match(line);
if (match.Success)
{
var type = match.Groups[1].Value;
var renamed = new Models.Change() { Path = match.Groups[2].Value };
renamed.Set(type == "R" ? Models.ChangeState.Renamed : Models.ChangeState.Copied);
changes.Add(renamed);
}
continue;
}
var change = new Models.Change() { Path = match.Groups[2].Value };
var status = match.Groups[1].Value;
switch (status[0])
{
case 'M':
change.Set(Models.ChangeState.Modified);
changes.Add(change);
break;
case 'A':
change.Set(Models.ChangeState.Added);
changes.Add(change);
break;
case 'D':
change.Set(Models.ChangeState.Deleted);
changes.Add(change);
break;
}
}
await proc.WaitForExitAsync().ConfigureAwait(false);
changes.Sort((l, r) => Models.NumericSort.Compare(l.Path, r.Path));
}
catch
{
//ignore changes;
}
return changes;
}
}
}