Skip to content

Commit cee9115

Browse files
authored
refacto: diff result parsing (#2562)
* Pass unmodified 'lineBytes' into ParseChunkBodyLine(), and clarify the use of its indicator-stripped subset by adding new variable 'rawContent' (matching the existing variable 'content'). * Move assignment of '_isInChunk = true' from (callee) ParseChunkStartLine() into (caller) ParseLine(), which makes it easier to see how the variable is used (since it is also checked and set to false in this outer scope). * Clarify the phrasing of two related comments.
1 parent 87a506c commit cee9115

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/Commands/Diff.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,23 @@ private void ParseLine(ArraySegment<byte> lineBytes)
144144
if (line.Length == 0)
145145
return;
146146

147-
// If we are reading a chunk body, try to read the current line as body first (because
148-
// the number of chunk body is greater than the number of chunk indicator in most time.
147+
// If we are reading a chunk-body, try to read the current line as chunk-body first (because
148+
// there are usually more chunk-body lines than chunk-indicator lines).
149149
if (_isInChunk)
150150
{
151-
if (ParseChunkBodyLine(line, lineBytes[1..]))
151+
if (ParseChunkBodyLine(line, lineBytes))
152152
return;
153153

154154
ProcessInlineHighlights();
155155
_isInChunk = false;
156156
}
157157

158-
// If the current line is not a chunk body, try to parse it as chunk indicator
158+
// If the current line is not a chunk-body, try to parse it as chunk-indicator
159159
if (ParseChunkStartLine(line))
160+
{
161+
_isInChunk = true;
160162
return;
163+
}
161164

162165
// Fallback to diff headers to support type-changed diff (multiple headers).
163166
ParseDiffHeaderLine(line);
@@ -193,7 +196,6 @@ private bool ParseChunkStartLine(string line)
193196
_newLine = int.Parse(match.Groups[2].Value);
194197
_last = new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, null, 0, 0);
195198
_result.TextDiff.Lines.Add(_last);
196-
_isInChunk = true;
197199
return true;
198200
}
199201

@@ -204,13 +206,14 @@ private bool ParseChunkBodyLine(string line, ArraySegment<byte> lineBytes)
204206
{
205207
var prefix = line[0];
206208
var content = line.Substring(1);
209+
var rawContent = lineBytes[1..].ToArray();
207210
if (ParseLFSChange(prefix, content))
208211
return true;
209212

210213
if (prefix == PREFIX_DELETED)
211214
{
212215
_result.TextDiff.DeletedLines++;
213-
_last = new Models.TextDiffLine(Models.TextDiffLineType.Deleted, content, lineBytes.ToArray(), _oldLine, 0);
216+
_last = new Models.TextDiffLine(Models.TextDiffLineType.Deleted, content, rawContent, _oldLine, 0);
214217
_deleted.Add(_last);
215218
_oldLine++;
216219
return true;
@@ -219,7 +222,7 @@ private bool ParseChunkBodyLine(string line, ArraySegment<byte> lineBytes)
219222
if (prefix == PREFIX_ADDED)
220223
{
221224
_result.TextDiff.AddedLines++;
222-
_last = new Models.TextDiffLine(Models.TextDiffLineType.Added, content, lineBytes.ToArray(), 0, _newLine);
225+
_last = new Models.TextDiffLine(Models.TextDiffLineType.Added, content, rawContent, 0, _newLine);
223226
_added.Add(_last);
224227
_newLine++;
225228
return true;
@@ -229,7 +232,7 @@ private bool ParseChunkBodyLine(string line, ArraySegment<byte> lineBytes)
229232
{
230233
ProcessInlineHighlights();
231234

232-
_last = new Models.TextDiffLine(Models.TextDiffLineType.Normal, content, lineBytes.ToArray(), _oldLine, _newLine);
235+
_last = new Models.TextDiffLine(Models.TextDiffLineType.Normal, content, rawContent, _oldLine, _newLine);
233236
_result.TextDiff.Lines.Add(_last);
234237
_oldLine++;
235238
_newLine++;

0 commit comments

Comments
 (0)