2021-04-29 05:05:55 -07:00
|
|
|
|
using System;
|
2021-07-28 00:02:06 -07:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-29 05:42:44 -07:00
|
|
|
|
using System.Linq;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Diff命令(用于文件文件比对)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Diff : Command {
|
|
|
|
|
private static readonly Regex REG_INDICATOR = new Regex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@");
|
|
|
|
|
private Models.TextChanges changes = new Models.TextChanges();
|
2021-07-28 00:02:06 -07:00
|
|
|
|
private List<Models.TextChanges.Line> deleted = new List<Models.TextChanges.Line>();
|
|
|
|
|
private List<Models.TextChanges.Line> added = new List<Models.TextChanges.Line>();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
private int oldLine = 0;
|
|
|
|
|
private int newLine = 0;
|
2021-08-11 04:16:00 -07:00
|
|
|
|
private int lineIndex = 0;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
public Diff(string repo, string args) {
|
|
|
|
|
Cwd = repo;
|
2021-07-28 00:04:00 -07:00
|
|
|
|
Args = $"diff --ignore-cr-at-eol --unified=4 {args}";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Models.TextChanges Result() {
|
|
|
|
|
Exec();
|
2021-07-28 00:02:06 -07:00
|
|
|
|
ProcessChanges();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
if (changes.IsBinary) changes.Lines.Clear();
|
2021-08-11 04:16:00 -07:00
|
|
|
|
lineIndex = 0;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
return changes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnReadline(string line) {
|
|
|
|
|
if (changes.IsBinary) return;
|
|
|
|
|
|
|
|
|
|
if (changes.Lines.Count == 0) {
|
|
|
|
|
var match = REG_INDICATOR.Match(line);
|
|
|
|
|
if (!match.Success) {
|
|
|
|
|
if (line.StartsWith("Binary", StringComparison.Ordinal)) changes.IsBinary = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oldLine = int.Parse(match.Groups[1].Value);
|
|
|
|
|
newLine = int.Parse(match.Groups[2].Value);
|
2021-08-11 04:16:00 -07:00
|
|
|
|
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Indicator, line, "", ""));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
} else {
|
2021-05-27 00:35:45 -07:00
|
|
|
|
if (line.Length == 0) {
|
2021-07-28 00:02:06 -07:00
|
|
|
|
ProcessChanges();
|
2021-08-11 04:16:00 -07:00
|
|
|
|
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Normal, "", $"{oldLine}", $"{newLine}"));
|
2021-05-27 00:35:45 -07:00
|
|
|
|
oldLine++;
|
|
|
|
|
newLine++;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var ch = line[0];
|
|
|
|
|
if (ch == '-') {
|
2021-08-11 04:16:00 -07:00
|
|
|
|
deleted.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Deleted, line.Substring(1), $"{oldLine}", ""));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
oldLine++;
|
|
|
|
|
} else if (ch == '+') {
|
2021-08-11 04:16:00 -07:00
|
|
|
|
added.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Added, line.Substring(1), "", $"{newLine}"));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
newLine++;
|
|
|
|
|
} else if (ch != '\\') {
|
2021-07-28 00:02:06 -07:00
|
|
|
|
ProcessChanges();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var match = REG_INDICATOR.Match(line);
|
|
|
|
|
if (match.Success) {
|
|
|
|
|
oldLine = int.Parse(match.Groups[1].Value);
|
|
|
|
|
newLine = int.Parse(match.Groups[2].Value);
|
2021-08-11 04:16:00 -07:00
|
|
|
|
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Indicator, line, "", ""));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
} else {
|
2021-08-11 04:16:00 -07:00
|
|
|
|
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Normal, line.Substring(1), $"{oldLine}", $"{newLine}"));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
oldLine++;
|
|
|
|
|
newLine++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-05 00:54:00 -07:00
|
|
|
|
|
2021-07-28 00:02:06 -07:00
|
|
|
|
private void ProcessChanges() {
|
2021-07-29 05:42:44 -07:00
|
|
|
|
if (deleted.Any()) {
|
2021-07-28 00:02:06 -07:00
|
|
|
|
if (added.Count == deleted.Count) {
|
|
|
|
|
for (int i = added.Count - 1; i >= 0; i--) {
|
|
|
|
|
var left = deleted[i];
|
|
|
|
|
var right = added[i];
|
2021-07-28 00:26:41 -07:00
|
|
|
|
|
2021-07-29 01:47:30 -07:00
|
|
|
|
if (left.Content.Length > 1024 || right.Content.Length > 1024) continue;
|
|
|
|
|
|
2021-07-29 05:42:44 -07:00
|
|
|
|
var chunks = Models.TextCompare.Process(left.Content, right.Content);
|
|
|
|
|
if (chunks.Count > 4) continue;
|
2021-07-29 01:47:30 -07:00
|
|
|
|
|
2021-07-29 05:42:44 -07:00
|
|
|
|
foreach (var chunk in chunks) {
|
|
|
|
|
if (chunk.DeletedCount > 0) {
|
|
|
|
|
left.Highlights.Add(new Models.TextChanges.HighlightRange(chunk.DeletedStart, chunk.DeletedCount));
|
2021-07-29 01:47:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 05:42:44 -07:00
|
|
|
|
if (chunk.AddedCount > 0) {
|
|
|
|
|
right.Highlights.Add(new Models.TextChanges.HighlightRange(chunk.AddedStart, chunk.AddedCount));
|
2021-07-29 01:47:30 -07:00
|
|
|
|
}
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changes.Lines.AddRange(deleted);
|
|
|
|
|
deleted.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 05:42:44 -07:00
|
|
|
|
if (added.Any()) {
|
2021-07-28 00:02:06 -07:00
|
|
|
|
changes.Lines.AddRange(added);
|
|
|
|
|
added.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|