Skip to content

Commit dd47633

Browse files
committed
fix: wrong result in diffing unstaged with staged binary files
Signed-off-by: leo <longshuang@msn.cn>
1 parent f086438 commit dd47633

2 files changed

Lines changed: 45 additions & 37 deletions

File tree

src/ViewModels/DiffContext.cs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ private void LoadContent()
209209
var imgDiff = new Models.ImageDiff();
210210
var fullPath = Path.Combine(_repo, _option.Path);
211211

212-
if (_option.Revisions.Count == 2)
212+
if (_option.Revisions.Count == 2) // Two revisions are specified, compare them
213213
{
214-
if (_option.Revisions[0].Equals("-R", StringComparison.Ordinal))
214+
if (_option.Revisions[0].Equals("-R", StringComparison.Ordinal)) // `-R` means the old side is the working tree
215215
{
216216
var oldImage = await ImageSource.FromFileAsync(fullPath, imgDecoder).ConfigureAwait(false);
217217
imgDiff.Old = oldImage.Bitmap;
@@ -224,15 +224,24 @@ private void LoadContent()
224224
imgDiff.OldFileSize = oldImage.Size;
225225
}
226226

227-
var newImage = await ImageSource.FromRevisionAsync(_repo, _option.Revisions[1], _option.Path, imgDecoder).ConfigureAwait(false);
228-
imgDiff.New = newImage.Bitmap;
229-
imgDiff.NewFileSize = newImage.Size;
227+
if (string.IsNullOrEmpty(_option.Revisions[1])) // Empty string in the second revision means the new side is the working tree
228+
{
229+
var newImage = await ImageSource.FromFileAsync(fullPath, imgDecoder).ConfigureAwait(false);
230+
imgDiff.New = newImage.Bitmap;
231+
imgDiff.NewFileSize = newImage.Size;
232+
}
233+
else
234+
{
235+
var newImage = await ImageSource.FromRevisionAsync(_repo, _option.Revisions[1], _option.Path, imgDecoder).ConfigureAwait(false);
236+
imgDiff.New = newImage.Bitmap;
237+
imgDiff.NewFileSize = newImage.Size;
238+
}
230239
}
231-
else
240+
else if (_option.IsUnstaged) // Unstaged change compared to staged or HEAD
232241
{
233242
if (!oldPath.Equals("/dev/null", StringComparison.Ordinal))
234243
{
235-
var oldImage = await ImageSource.FromRevisionAsync(_repo, "HEAD", oldPath, imgDecoder).ConfigureAwait(false);
244+
var oldImage = await ImageSource.FromRevisionAsync(_repo, string.Empty, oldPath, imgDecoder).ConfigureAwait(false);
236245
imgDiff.Old = oldImage.Bitmap;
237246
imgDiff.OldFileSize = oldImage.Size;
238247
}
@@ -241,6 +250,16 @@ private void LoadContent()
241250
imgDiff.New = newImage.Bitmap;
242251
imgDiff.NewFileSize = newImage.Size;
243252
}
253+
else // Staged change compared to the last commit (HEAD)
254+
{
255+
var oldImage = await ImageSource.FromRevisionAsync(_repo, "HEAD", oldPath, imgDecoder).ConfigureAwait(false);
256+
imgDiff.Old = oldImage.Bitmap;
257+
imgDiff.OldFileSize = oldImage.Size;
258+
259+
var newImage = await ImageSource.FromRevisionAsync(_repo, string.Empty, oldPath, imgDecoder).ConfigureAwait(false);
260+
imgDiff.New = newImage.Bitmap;
261+
imgDiff.NewFileSize = newImage.Size;
262+
}
244263

245264
return imgDiff;
246265
}
@@ -254,45 +273,38 @@ private void LoadContent()
254273
binaryDiff.Repository = _repo;
255274
binaryDiff.FilePath = _option.Path;
256275

257-
if (_option.Revisions.Count == 2)
276+
if (_option.Revisions.Count == 2) // Two revisions are specified, compare them
258277
{
259-
if (_option.Revisions[0].Equals("-R", StringComparison.Ordinal))
260-
{
278+
if (_option.Revisions[0].Equals("-R", StringComparison.Ordinal)) // `-R` means the old side is the working tree
261279
binaryDiff.OldSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
262-
binaryDiff.NewSize = await new Commands.QueryFileSize(_repo, _option.Path, _option.Revisions[1]).GetResultAsync().ConfigureAwait(false);
263-
binaryDiff.NewRevision = _option.Revisions[1];
264-
}
265280
else
266-
{
267281
binaryDiff.OldSize = await new Commands.QueryFileSize(_repo, oldPath, _option.Revisions[0]).GetResultAsync().ConfigureAwait(false);
268-
if (string.IsNullOrEmpty(_option.Revisions[1]))
269-
{
270-
binaryDiff.NewSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
271-
binaryDiff.NewRevision = null;
272-
}
273-
else
274-
{
275-
binaryDiff.NewSize = await new Commands.QueryFileSize(_repo, _option.Path, _option.Revisions[1]).GetResultAsync().ConfigureAwait(false);
276-
binaryDiff.NewRevision = _option.Revisions[1];
277-
}
278-
}
279-
}
280-
else
281-
{
282-
if (!oldPath.Equals("/dev/null", StringComparison.Ordinal))
283-
binaryDiff.OldSize = await new Commands.QueryFileSize(_repo, oldPath, "HEAD").GetResultAsync().ConfigureAwait(false);
284282

285-
if (_option.IsUnstaged)
283+
if (string.IsNullOrEmpty(_option.Revisions[1])) // Empty string in the second revision means the new side is the working tree
286284
{
287285
binaryDiff.NewSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
288286
binaryDiff.NewRevision = null;
289287
}
290288
else
291289
{
292-
binaryDiff.NewSize = await new Commands.QueryFileSize(_repo, _option.Path, string.Empty).GetResultAsync().ConfigureAwait(false);
293-
binaryDiff.NewRevision = string.Empty;
290+
binaryDiff.NewSize = await new Commands.QueryFileSize(_repo, _option.Path, _option.Revisions[1]).GetResultAsync().ConfigureAwait(false);
291+
binaryDiff.NewRevision = _option.Revisions[1];
294292
}
295293
}
294+
else if (_option.IsUnstaged) // Unstaged change compared to staged or HEAD
295+
{
296+
if (!oldPath.Equals("/dev/null", StringComparison.Ordinal))
297+
binaryDiff.OldSize = await new Commands.QueryFileSize(_repo, oldPath, string.Empty).GetResultAsync().ConfigureAwait(false);
298+
299+
binaryDiff.NewSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
300+
binaryDiff.NewRevision = null;
301+
}
302+
else // Staged change compared to the last commit (HEAD)
303+
{
304+
binaryDiff.OldSize = await new Commands.QueryFileSize(_repo, oldPath, "HEAD").GetResultAsync().ConfigureAwait(false);
305+
binaryDiff.NewSize = await new Commands.QueryFileSize(_repo, _option.Path, string.Empty).GetResultAsync().ConfigureAwait(false);
306+
binaryDiff.NewRevision = string.Empty;
307+
}
296308

297309
return binaryDiff;
298310
}

src/ViewModels/ImageSource.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public static async Task<ImageSource> FromFileAsync(string fullpath, Models.Imag
4949

5050
public static async Task<ImageSource> FromRevisionAsync(string repo, string revision, string file, Models.ImageDecoder decoder)
5151
{
52-
// If revision is empty, it means we are reading file in worktree.
53-
if (string.IsNullOrEmpty(revision))
54-
return await FromFileAsync(Path.Combine(repo, file), decoder).ConfigureAwait(false);
55-
5652
var emptyTreeHash = Models.EmptyTreeHash.Guess(revision);
5753
if (emptyTreeHash.Equals(revision, StringComparison.Ordinal))
5854
return new ImageSource(null, 0);

0 commit comments

Comments
 (0)