forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffConflictStages.cs
More file actions
199 lines (174 loc) · 6.91 KB
/
DiffConflictStages.cs
File metadata and controls
199 lines (174 loc) · 6.91 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
/// <summary>
/// Computes a diff between the conflict stages :2: (ours) and :3: (theirs)
/// </summary>
public partial class DiffConflictStages : Command
{
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
private static partial Regex REG_INDICATOR();
public DiffConflictStages(string repo, string file, int unified = 9999)
{
WorkingDirectory = repo;
Context = repo;
_result.TextDiff = new Models.TextDiff();
// Diff between stage :2: (ours) and :3: (theirs)
var builder = new StringBuilder();
builder.Append("diff --no-color --no-ext-diff --patch ");
if (Models.DiffOption.IgnoreCRAtEOL)
builder.Append("--ignore-cr-at-eol ");
builder.Append("--unified=").Append(unified).Append(' ');
builder.Append($":2:{file.Quoted()} :3:{file.Quoted()}");
Args = builder.ToString();
}
public async Task<Models.DiffResult> ReadAsync()
{
try
{
using var proc = new Process();
proc.StartInfo = CreateGitStartInfo(true);
proc.Start();
var text = await proc.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
var start = 0;
var end = text.IndexOf('\n', start);
while (end > 0)
{
var line = text[start..end];
ParseLine(line);
start = end + 1;
end = text.IndexOf('\n', start);
}
if (start < text.Length)
ParseLine(text[start..]);
await proc.WaitForExitAsync().ConfigureAwait(false);
}
catch
{
// Ignore exceptions.
}
if (_result.IsBinary || _result.TextDiff.Lines.Count == 0)
{
_result.TextDiff = null;
}
else
{
ProcessInlineHighlights();
_result.TextDiff.MaxLineNumber = Math.Max(_newLine, _oldLine);
}
return _result;
}
private void ParseLine(string line)
{
if (_result.IsBinary)
return;
if (_result.TextDiff.Lines.Count == 0)
{
if (line.StartsWith("Binary", StringComparison.Ordinal))
{
_result.IsBinary = true;
return;
}
var match = REG_INDICATOR().Match(line);
if (!match.Success)
return;
_oldLine = int.Parse(match.Groups[1].Value);
_newLine = int.Parse(match.Groups[2].Value);
_last = new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0);
_result.TextDiff.Lines.Add(_last);
}
else
{
if (line.Length == 0)
{
ProcessInlineHighlights();
_last = new Models.TextDiffLine(Models.TextDiffLineType.Normal, "", _oldLine, _newLine);
_result.TextDiff.Lines.Add(_last);
_oldLine++;
_newLine++;
return;
}
var ch = line[0];
if (ch == '-')
{
_last = new Models.TextDiffLine(Models.TextDiffLineType.Deleted, line.Substring(1), _oldLine, 0);
_deleted.Add(_last);
_oldLine++;
}
else if (ch == '+')
{
_last = new Models.TextDiffLine(Models.TextDiffLineType.Added, line.Substring(1), 0, _newLine);
_added.Add(_last);
_newLine++;
}
else if (ch != '\\')
{
ProcessInlineHighlights();
var match = REG_INDICATOR().Match(line);
if (match.Success)
{
_oldLine = int.Parse(match.Groups[1].Value);
_newLine = int.Parse(match.Groups[2].Value);
_last = new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0);
_result.TextDiff.Lines.Add(_last);
}
else
{
_last = new Models.TextDiffLine(Models.TextDiffLineType.Normal, line.Substring(1), _oldLine, _newLine);
_result.TextDiff.Lines.Add(_last);
_oldLine++;
_newLine++;
}
}
else if (line.Equals("\\ No newline at end of file", StringComparison.Ordinal))
{
_last.NoNewLineEndOfFile = true;
}
}
}
private void ProcessInlineHighlights()
{
if (_deleted.Count > 0)
{
if (_added.Count == _deleted.Count)
{
for (int i = _added.Count - 1; i >= 0; i--)
{
var left = _deleted[i];
var right = _added[i];
if (left.Content.Length > 1024 || right.Content.Length > 1024)
continue;
var chunks = Models.TextInlineChange.Compare(left.Content, right.Content);
if (chunks.Count > 4)
continue;
foreach (var chunk in chunks)
{
if (chunk.DeletedCount > 0)
left.Highlights.Add(new Models.TextRange(chunk.DeletedStart, chunk.DeletedCount));
if (chunk.AddedCount > 0)
right.Highlights.Add(new Models.TextRange(chunk.AddedStart, chunk.AddedCount));
}
}
}
_result.TextDiff.Lines.AddRange(_deleted);
_deleted.Clear();
}
if (_added.Count > 0)
{
_result.TextDiff.Lines.AddRange(_added);
_added.Clear();
}
}
private readonly Models.DiffResult _result = new Models.DiffResult();
private readonly List<Models.TextDiffLine> _deleted = new List<Models.TextDiffLine>();
private readonly List<Models.TextDiffLine> _added = new List<Models.TextDiffLine>();
private Models.TextDiffLine _last = null;
private int _oldLine = 0;
private int _newLine = 0;
}
}