Skip to content

Commit dcf5093

Browse files
committed
enhance: avoid that diff view refresh more than one times
Signed-off-by: leo <[email protected]>
1 parent a5aa225 commit dcf5093

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

src/Commands/Diff.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public partial class Diff : Command
88
{
99
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
1010
private static partial Regex REG_INDICATOR();
11+
12+
[GeneratedRegex(@"^index\s([0-9a-f]{6,40})\.\.([0-9a-f]{6,40})(\s[1-9]{6})?")]
13+
private static partial Regex REG_HASH_CHANGE();
14+
1115
private const string PREFIX_LFS_NEW = "+version https://git-lfs.github.com/spec/";
1216
private const string PREFIX_LFS_DEL = "-version https://git-lfs.github.com/spec/";
1317
private const string PREFIX_LFS_MODIFY = " version https://git-lfs.github.com/spec/";
@@ -101,17 +105,31 @@ protected override void OnReadline(string line)
101105

102106
if (_result.TextDiff.Lines.Count == 0)
103107
{
104-
var match = REG_INDICATOR().Match(line);
105-
if (!match.Success)
108+
if (line.StartsWith("Binary", StringComparison.Ordinal))
106109
{
107-
if (line.StartsWith("Binary", StringComparison.Ordinal))
108-
_result.IsBinary = true;
110+
_result.IsBinary = true;
109111
return;
110112
}
111113

112-
_oldLine = int.Parse(match.Groups[1].Value);
113-
_newLine = int.Parse(match.Groups[2].Value);
114-
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0));
114+
if (string.IsNullOrEmpty(_result.OldHash))
115+
{
116+
var match = REG_HASH_CHANGE().Match(line);
117+
if (!match.Success)
118+
return;
119+
120+
_result.OldHash = match.Groups[1].Value;
121+
_result.NewHash = match.Groups[2].Value;
122+
}
123+
else
124+
{
125+
var match = REG_INDICATOR().Match(line);
126+
if (!match.Success)
127+
return;
128+
129+
_oldLine = int.Parse(match.Groups[1].Value);
130+
_newLine = int.Parse(match.Groups[2].Value);
131+
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0));
132+
}
115133
}
116134
else
117135
{

src/Models/DiffResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ public class DiffResult
674674
{
675675
public bool IsBinary { get; set; } = false;
676676
public bool IsLFS { get; set; } = false;
677+
public string OldHash { get; set; } = string.Empty;
678+
public string NewHash { get; set; } = string.Empty;
677679
public string OldMode { get; set; } = string.Empty;
678680
public string NewMode { get; set; } = string.Empty;
679681
public TextDiff TextDiff { get; set; } = null;

src/ViewModels/DiffContext.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ public string FileModeChange
3333
private set => SetProperty(ref _fileModeChange, value);
3434
}
3535

36-
public bool IsLoading
37-
{
38-
get => _isLoading;
39-
private set => SetProperty(ref _isLoading, value);
40-
}
41-
4236
public bool IsTextDiff
4337
{
4438
get => _isTextDiff;
@@ -68,6 +62,7 @@ public DiffContext(string repo, Models.DiffOption option, DiffContext previous =
6862
_content = previous._content;
6963
_unifiedLines = previous._unifiedLines;
7064
_ignoreWhitespace = previous._ignoreWhitespace;
65+
_info = previous._info;
7166
}
7267

7368
if (string.IsNullOrEmpty(_option.OrgPath) || _option.OrgPath == "/dev/null")
@@ -109,7 +104,6 @@ private void LoadDiffContent()
109104
{
110105
Content = null;
111106
IsTextDiff = false;
112-
IsLoading = false;
113107
return;
114108
}
115109

@@ -119,10 +113,14 @@ private void LoadDiffContent()
119113
// There is no way to tell a git-diff to use "ALL lines of context",
120114
// so instead we set a very high number for the "lines of context" parameter.
121115
var numLines = Preference.Instance.UseFullTextDiff ? 999999999 : _unifiedLines;
122-
123116
var latest = new Commands.Diff(_repo, _option, numLines, _ignoreWhitespace).Result();
124-
var rs = null as object;
117+
var info = new Info(_option, numLines, _ignoreWhitespace, latest);
118+
if (_info != null && info.IsSame(_info))
119+
return;
120+
121+
_info = info;
125122

123+
var rs = null as object;
126124
if (latest.TextDiff != null)
127125
{
128126
var count = latest.TextDiff.Lines.Count;
@@ -219,7 +217,6 @@ private void LoadDiffContent()
219217
FileModeChange = latest.FileModeChange;
220218
Content = rs;
221219
IsTextDiff = rs is Models.TextDiff;
222-
IsLoading = false;
223220
});
224221
});
225222
}
@@ -252,14 +249,41 @@ private Models.RevisionSubmodule QuerySubmoduleRevision(string repo, string sha)
252249
".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"
253250
};
254251

252+
private class Info
253+
{
254+
public string Argument { get; set; }
255+
public int UnifiedLines { get; set; }
256+
public bool IgnoreWhitespace { get; set; }
257+
public string OldHash { get; set; }
258+
public string NewHash { get; set; }
259+
260+
public Info(Models.DiffOption option, int unifiedLines, bool ignoreWhitespace, Models.DiffResult result)
261+
{
262+
Argument = option.ToString();
263+
UnifiedLines = unifiedLines;
264+
IgnoreWhitespace = ignoreWhitespace;
265+
OldHash = result.OldHash;
266+
NewHash = result.NewHash;
267+
}
268+
269+
public bool IsSame(Info other)
270+
{
271+
return Argument.Equals(other.Argument, StringComparison.Ordinal) &&
272+
UnifiedLines == other.UnifiedLines &&
273+
IgnoreWhitespace == other.IgnoreWhitespace &&
274+
OldHash.Equals(other.OldHash, StringComparison.Ordinal) &&
275+
NewHash.Equals(other.NewHash, StringComparison.Ordinal);
276+
}
277+
}
278+
255279
private readonly string _repo;
256280
private readonly Models.DiffOption _option = null;
257281
private string _title;
258282
private string _fileModeChange = string.Empty;
259283
private int _unifiedLines = 4;
260-
private bool _isLoading = true;
261284
private bool _isTextDiff = false;
262285
private bool _ignoreWhitespace = false;
263286
private object _content = null;
287+
private Info _info = null;
264288
}
265289
}

0 commit comments

Comments
 (0)