2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
2021-07-28 00:02:06 -07:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public partial class Diff : Command
|
|
|
|
|
{
|
2024-03-16 02:09:27 -07:00
|
|
|
|
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
|
|
|
|
|
private static partial Regex REG_INDICATOR();
|
2024-03-31 01:54:29 -07:00
|
|
|
|
private const string PREFIX_LFS_NEW = "+version https://git-lfs.github.com/spec/";
|
|
|
|
|
private const string PREFIX_LFS_DEL = "-version https://git-lfs.github.com/spec/";
|
|
|
|
|
private const string PREFIX_LFS_MODIFY = " version https://git-lfs.github.com/spec/";
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-05-26 19:29:15 -07:00
|
|
|
|
public Diff(string repo, Models.DiffOption opt, int unified)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-22 22:58:57 -07:00
|
|
|
|
_result.TextDiff = new Models.TextDiff()
|
|
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
|
Repo = repo,
|
|
|
|
|
Option = opt,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2024-05-26 19:29:15 -07:00
|
|
|
|
Args = $"diff --ignore-cr-at-eol --unified={unified} {opt}";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.DiffResult Result()
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Exec();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_result.IsBinary || _result.IsLFS)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.TextDiff = null;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
ProcessInlineHighlights();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_result.TextDiff.Lines.Count == 0)
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.TextDiff = null;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.TextDiff.MaxLineNumber = Math.Max(_newLine, _oldLine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _result;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
protected override void OnReadline(string line)
|
|
|
|
|
{
|
2024-04-12 06:38:36 -07:00
|
|
|
|
if (line.StartsWith("old mode ", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-04-13 21:27:09 -07:00
|
|
|
|
_result.OldMode = line.Substring(9);
|
2024-04-12 06:38:36 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (line.StartsWith("new mode ", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-04-13 21:27:09 -07:00
|
|
|
|
_result.NewMode = line.Substring(9);
|
2024-04-12 06:38:36 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_result.IsBinary)
|
|
|
|
|
return;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_result.IsLFS)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var ch = line[0];
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (ch == '-')
|
|
|
|
|
{
|
|
|
|
|
if (line.StartsWith("-oid sha256:", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-03-13 19:55:25 -07:00
|
|
|
|
_result.LFSDiff.Old.Oid = line.Substring(12);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (line.StartsWith("-size ", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-03-13 19:55:25 -07:00
|
|
|
|
_result.LFSDiff.Old.Size = long.Parse(line.Substring(6));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (ch == '+')
|
|
|
|
|
{
|
|
|
|
|
if (line.StartsWith("+oid sha256:", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-03-13 19:55:25 -07:00
|
|
|
|
_result.LFSDiff.New.Oid = line.Substring(12);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (line.StartsWith("+size ", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-03-13 19:55:25 -07:00
|
|
|
|
_result.LFSDiff.New.Size = long.Parse(line.Substring(6));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (line.StartsWith(" size ", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.LFSDiff.New.Size = _result.LFSDiff.Old.Size = long.Parse(line.Substring(6));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_result.TextDiff.Lines.Count == 0)
|
|
|
|
|
{
|
2024-03-16 02:09:27 -07:00
|
|
|
|
var match = REG_INDICATOR().Match(line);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!match.Success)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (line.StartsWith("Binary", StringComparison.Ordinal))
|
|
|
|
|
_result.IsBinary = true;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_oldLine = int.Parse(match.Groups[1].Value);
|
|
|
|
|
_newLine = int.Parse(match.Groups[2].Value);
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (line.Length == 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
ProcessInlineHighlights();
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Normal, "", _oldLine, _newLine));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_oldLine++;
|
|
|
|
|
_newLine++;
|
2021-05-27 00:35:45 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var ch = line[0];
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (ch == '-')
|
|
|
|
|
{
|
|
|
|
|
if (_oldLine == 1 && _newLine == 0 && line.StartsWith(PREFIX_LFS_DEL, StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-03-07 00:54:09 -08:00
|
|
|
|
_result.IsLFS = true;
|
|
|
|
|
_result.LFSDiff = new Models.LFSDiff();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_deleted.Add(new Models.TextDiffLine(Models.TextDiffLineType.Deleted, line.Substring(1), _oldLine, 0));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_oldLine++;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (ch == '+')
|
|
|
|
|
{
|
|
|
|
|
if (_oldLine == 0 && _newLine == 1 && line.StartsWith(PREFIX_LFS_NEW, StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-03-07 00:54:09 -08:00
|
|
|
|
_result.IsLFS = true;
|
|
|
|
|
_result.LFSDiff = new Models.LFSDiff();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_added.Add(new Models.TextDiffLine(Models.TextDiffLineType.Added, line.Substring(1), 0, _newLine));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_newLine++;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (ch != '\\')
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
ProcessInlineHighlights();
|
2024-03-16 02:09:27 -07:00
|
|
|
|
var match = REG_INDICATOR().Match(line);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_oldLine = int.Parse(match.Groups[1].Value);
|
|
|
|
|
_newLine = int.Parse(match.Groups[2].Value);
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (_oldLine == 1 && _newLine == 1 && line.StartsWith(PREFIX_LFS_MODIFY, StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.IsLFS = true;
|
|
|
|
|
_result.LFSDiff = new Models.LFSDiff();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Normal, line.Substring(1), _oldLine, _newLine));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_oldLine++;
|
|
|
|
|
_newLine++;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-05 00:54:00 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void ProcessInlineHighlights()
|
|
|
|
|
{
|
|
|
|
|
if (_deleted.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_added.Count == _deleted.Count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = _added.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var left = _deleted[i];
|
|
|
|
|
var right = _added[i];
|
2021-07-28 00:26:41 -07:00
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (left.Content.Length > 1024 || right.Content.Length > 1024)
|
|
|
|
|
continue;
|
2021-07-29 01:47:30 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var chunks = Models.TextInlineChange.Compare(left.Content, right.Content);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (chunks.Count > 4)
|
|
|
|
|
continue;
|
2021-07-29 01:47:30 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var chunk in chunks)
|
|
|
|
|
{
|
|
|
|
|
if (chunk.DeletedCount > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
left.Highlights.Add(new Models.TextInlineRange(chunk.DeletedStart, chunk.DeletedCount));
|
2021-07-29 01:47:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (chunk.AddedCount > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
right.Highlights.Add(new Models.TextInlineRange(chunk.AddedStart, chunk.AddedCount));
|
2021-07-29 01:47:30 -07:00
|
|
|
|
}
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.TextDiff.Lines.AddRange(_deleted);
|
|
|
|
|
_deleted.Clear();
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_added.Count > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_result.TextDiff.Lines.AddRange(_added);
|
|
|
|
|
_added.Clear();
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
|
private readonly Models.DiffResult _result = new Models.DiffResult();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly List<Models.TextDiffLine> _deleted = new List<Models.TextDiffLine>();
|
|
|
|
|
private readonly List<Models.TextDiffLine> _added = new List<Models.TextDiffLine>();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private int _oldLine = 0;
|
|
|
|
|
private int _newLine = 0;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|