Skip to content

Commit e89dbd8

Browse files
committed
code_review: PR #1177
- use `Command.ReadToEnd` instead of `Command.Exec` to avoid git trims line endings. - use `StringBuilder.Append('\n')` instead of `StringBuilder.AppendLine()` to restore original line endings (we split the original diff output by `\n` not `\r') - there's no need to show file content (the `StreamReader.ReadLine()` will trim line endings) Signed-off-by: leo <[email protected]>
1 parent 81820e7 commit e89dbd8

File tree

3 files changed

+31
-53
lines changed

3 files changed

+31
-53
lines changed

src/Commands/Diff.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,26 @@ public Diff(string repo, Models.DiffOption opt, int unified, bool ignoreWhitespa
3535

3636
public Models.DiffResult Result()
3737
{
38-
Exec();
38+
var rs = ReadToEnd();
39+
if (!rs.IsSuccess)
40+
{
41+
_result.TextDiff = null;
42+
return _result;
43+
}
44+
45+
var start = 0;
46+
var end = rs.StdOut.IndexOf('\n', start);
47+
while (end > 0)
48+
{
49+
var line = rs.StdOut.Substring(start, end - start);
50+
ParseLine(line);
51+
52+
start = end + 1;
53+
end = rs.StdOut.IndexOf('\n', start);
54+
}
55+
56+
if (start < rs.StdOut.Length)
57+
ParseLine(rs.StdOut.Substring(start));
3958

4059
if (_result.IsBinary || _result.IsLFS)
4160
{
@@ -54,8 +73,11 @@ public Models.DiffResult Result()
5473
return _result;
5574
}
5675

57-
protected override void OnReadline(string line)
76+
private void ParseLine(string line)
5877
{
78+
if (_result.IsBinary)
79+
return;
80+
5981
if (line.StartsWith("old mode ", StringComparison.Ordinal))
6082
{
6183
_result.OldMode = line.Substring(9);
@@ -80,9 +102,6 @@ protected override void OnReadline(string line)
80102
return;
81103
}
82104

83-
if (_result.IsBinary)
84-
return;
85-
86105
if (_result.IsLFS)
87106
{
88107
var ch = line[0];

src/ViewModels/DiffContext.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -207,50 +207,7 @@ private void LoadDiffContent()
207207
}
208208
else
209209
{
210-
// Check if old and new hashes differ but no text changes found
211-
if (!string.IsNullOrEmpty(latest.OldHash) &&
212-
!string.IsNullOrEmpty(latest.NewHash) &&
213-
latest.OldHash != latest.NewHash)
214-
{
215-
// If hashes differ but no text diff found, it's likely an EOL change
216-
// Create a text diff to show the file content
217-
var textDiff = new Models.TextDiff()
218-
{
219-
Repo = _repo,
220-
Option = _option,
221-
File = _option.Path
222-
};
223-
// Query the file content to show
224-
var fileContent = Commands.QueryFileContent.Run(_repo, "HEAD", _option.Path);
225-
using var reader = new StreamReader(fileContent);
226-
var line = string.Empty;
227-
int lineNumber = 1;
228-
229-
while ((line = reader.ReadLine()) != null)
230-
{
231-
textDiff.Lines.Add(new Models.TextDiffLine(
232-
Models.TextDiffLineType.Normal,
233-
line,
234-
lineNumber,
235-
lineNumber));
236-
237-
lineNumber++;
238-
}
239-
240-
if (textDiff.Lines.Count > 0)
241-
{
242-
textDiff.MaxLineNumber = lineNumber - 1;
243-
rs = textDiff;
244-
}
245-
else
246-
{
247-
rs = new Models.NoOrEOLChange();
248-
}
249-
}
250-
else
251-
{
252-
rs = new Models.NoOrEOLChange();
253-
}
210+
rs = new Models.NoOrEOLChange();
254211
}
255212

256213
Dispatcher.UIThread.Post(() =>

src/Views/TextDiffView.axaml.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,11 +1253,12 @@ protected override void OnDataContextChanged(EventArgs e)
12531253
{
12541254
builder.Append(line.Content.Substring(0, 1000));
12551255
builder.Append($"...({line.Content.Length - 1000} character trimmed)");
1256-
builder.AppendLine();
1256+
builder.Append('\n');
12571257
}
12581258
else
12591259
{
1260-
builder.AppendLine(line.Content);
1260+
builder.Append(line.Content);
1261+
builder.Append('\n');
12611262
}
12621263
}
12631264

@@ -1492,11 +1493,12 @@ protected override void OnDataContextChanged(EventArgs e)
14921493
{
14931494
builder.Append(line.Content.Substring(0, 1000));
14941495
builder.Append($"...({line.Content.Length - 1000} characters trimmed)");
1495-
builder.AppendLine();
1496+
builder.Append('\n');
14961497
}
14971498
else
14981499
{
1499-
builder.AppendLine(line.Content);
1500+
builder.Append(line.Content);
1501+
builder.Append('\n');
15001502
}
15011503
}
15021504

0 commit comments

Comments
 (0)