mirror of
https://fastgit.cc/github.com/sourcegit-scm/sourcegit
synced 2026-04-30 13:51:53 +08:00
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 <longshuang@msn.cn>
This commit is contained in:
@@ -35,7 +35,26 @@ namespace SourceGit.Commands
|
||||
|
||||
public Models.DiffResult Result()
|
||||
{
|
||||
Exec();
|
||||
var rs = ReadToEnd();
|
||||
if (!rs.IsSuccess)
|
||||
{
|
||||
_result.TextDiff = null;
|
||||
return _result;
|
||||
}
|
||||
|
||||
var start = 0;
|
||||
var end = rs.StdOut.IndexOf('\n', start);
|
||||
while (end > 0)
|
||||
{
|
||||
var line = rs.StdOut.Substring(start, end - start);
|
||||
ParseLine(line);
|
||||
|
||||
start = end + 1;
|
||||
end = rs.StdOut.IndexOf('\n', start);
|
||||
}
|
||||
|
||||
if (start < rs.StdOut.Length)
|
||||
ParseLine(rs.StdOut.Substring(start));
|
||||
|
||||
if (_result.IsBinary || _result.IsLFS)
|
||||
{
|
||||
@@ -54,8 +73,11 @@ namespace SourceGit.Commands
|
||||
return _result;
|
||||
}
|
||||
|
||||
protected override void OnReadline(string line)
|
||||
private void ParseLine(string line)
|
||||
{
|
||||
if (_result.IsBinary)
|
||||
return;
|
||||
|
||||
if (line.StartsWith("old mode ", StringComparison.Ordinal))
|
||||
{
|
||||
_result.OldMode = line.Substring(9);
|
||||
@@ -80,9 +102,6 @@ namespace SourceGit.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
if (_result.IsBinary)
|
||||
return;
|
||||
|
||||
if (_result.IsLFS)
|
||||
{
|
||||
var ch = line[0];
|
||||
|
||||
@@ -207,50 +207,7 @@ namespace SourceGit.ViewModels
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if old and new hashes differ but no text changes found
|
||||
if (!string.IsNullOrEmpty(latest.OldHash) &&
|
||||
!string.IsNullOrEmpty(latest.NewHash) &&
|
||||
latest.OldHash != latest.NewHash)
|
||||
{
|
||||
// If hashes differ but no text diff found, it's likely an EOL change
|
||||
// Create a text diff to show the file content
|
||||
var textDiff = new Models.TextDiff()
|
||||
{
|
||||
Repo = _repo,
|
||||
Option = _option,
|
||||
File = _option.Path
|
||||
};
|
||||
// Query the file content to show
|
||||
var fileContent = Commands.QueryFileContent.Run(_repo, "HEAD", _option.Path);
|
||||
using var reader = new StreamReader(fileContent);
|
||||
var line = string.Empty;
|
||||
int lineNumber = 1;
|
||||
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
textDiff.Lines.Add(new Models.TextDiffLine(
|
||||
Models.TextDiffLineType.Normal,
|
||||
line,
|
||||
lineNumber,
|
||||
lineNumber));
|
||||
|
||||
lineNumber++;
|
||||
}
|
||||
|
||||
if (textDiff.Lines.Count > 0)
|
||||
{
|
||||
textDiff.MaxLineNumber = lineNumber - 1;
|
||||
rs = textDiff;
|
||||
}
|
||||
else
|
||||
{
|
||||
rs = new Models.NoOrEOLChange();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rs = new Models.NoOrEOLChange();
|
||||
}
|
||||
rs = new Models.NoOrEOLChange();
|
||||
}
|
||||
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
|
||||
@@ -1253,11 +1253,12 @@ namespace SourceGit.Views
|
||||
{
|
||||
builder.Append(line.Content.Substring(0, 1000));
|
||||
builder.Append($"...({line.Content.Length - 1000} character trimmed)");
|
||||
builder.AppendLine();
|
||||
builder.Append('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.AppendLine(line.Content);
|
||||
builder.Append(line.Content);
|
||||
builder.Append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1492,11 +1493,12 @@ namespace SourceGit.Views
|
||||
{
|
||||
builder.Append(line.Content.Substring(0, 1000));
|
||||
builder.Append($"...({line.Content.Length - 1000} characters trimmed)");
|
||||
builder.AppendLine();
|
||||
builder.Append('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.AppendLine(line.Content);
|
||||
builder.Append(line.Content);
|
||||
builder.Append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user