2024-03-27 06:38:38 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Avalonia;
|
2024-03-27 18:34:08 -07:00
|
|
|
|
using Avalonia.Media.Imaging;
|
2024-02-28 19:29:54 -08:00
|
|
|
|
using Avalonia.Threading;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class DiffContext : ObservableObject
|
|
|
|
|
{
|
|
|
|
|
public string RepositoryPath
|
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
|
get => _repo;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Change WorkingCopyChange
|
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
|
get => _option.WorkingCopyChange;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool IsUnstaged
|
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
|
get => _option.IsUnstaged;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string FilePath
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _option.Path;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool IsOrgFilePathVisible
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => !string.IsNullOrWhiteSpace(_option.OrgPath) && _option.OrgPath != "/dev/null";
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string OrgFilePath
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _option.OrgPath;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool IsLoading
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _isLoading;
|
|
|
|
|
private set => SetProperty(ref _isLoading, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool IsTextDiff
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _isTextDiff;
|
|
|
|
|
private set => SetProperty(ref _isTextDiff, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public object Content
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _content;
|
|
|
|
|
private set => SetProperty(ref _content, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Vector SyncScrollOffset
|
|
|
|
|
{
|
2024-02-28 19:29:54 -08:00
|
|
|
|
get => _syncScrollOffset;
|
|
|
|
|
set => SetProperty(ref _syncScrollOffset, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
|
public DiffContext(string repo, Models.DiffOption option, DiffContext previous = null)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
_option = option;
|
2024-03-20 05:34:24 -07:00
|
|
|
|
|
|
|
|
|
if (previous != null)
|
|
|
|
|
{
|
|
|
|
|
_isTextDiff = previous._isTextDiff;
|
|
|
|
|
_content = previous._content;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
OnPropertyChanged(nameof(FilePath));
|
|
|
|
|
OnPropertyChanged(nameof(IsOrgFilePathVisible));
|
|
|
|
|
OnPropertyChanged(nameof(OrgFilePath));
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var latest = new Commands.Diff(repo, option).Result();
|
2024-03-27 06:38:38 -07:00
|
|
|
|
var rs = null as object;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-27 06:38:38 -07:00
|
|
|
|
if (latest.TextDiff != null)
|
|
|
|
|
{
|
|
|
|
|
latest.TextDiff.File = _option.Path;
|
|
|
|
|
rs = latest.TextDiff;
|
|
|
|
|
}
|
|
|
|
|
else if (latest.IsBinary)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
|
2024-03-27 06:38:38 -07:00
|
|
|
|
var ext = Path.GetExtension(oldPath);
|
|
|
|
|
|
|
|
|
|
if (IMG_EXTS.Contains(ext))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-27 06:38:38 -07:00
|
|
|
|
var imgDiff = new Models.ImageDiff();
|
|
|
|
|
if (option.Revisions.Count == 2)
|
|
|
|
|
{
|
|
|
|
|
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[0], oldPath);
|
|
|
|
|
imgDiff.New = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[1], oldPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-27 18:34:08 -07:00
|
|
|
|
var fullPath = Path.Combine(repo, _option.Path);
|
2024-03-27 06:38:38 -07:00
|
|
|
|
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, "HEAD", oldPath);
|
2024-03-27 18:34:08 -07:00
|
|
|
|
imgDiff.New = File.Exists(fullPath) ? new Bitmap(fullPath) : null;
|
2024-03-27 06:38:38 -07:00
|
|
|
|
}
|
|
|
|
|
rs = imgDiff;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-27 06:38:38 -07:00
|
|
|
|
var binaryDiff = new Models.BinaryDiff();
|
|
|
|
|
if (option.Revisions.Count == 2)
|
|
|
|
|
{
|
|
|
|
|
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, option.Revisions[0]).Result();
|
|
|
|
|
binaryDiff.NewSize = new Commands.QueryFileSize(repo, _option.Path, option.Revisions[1]).Result();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-27 18:34:08 -07:00
|
|
|
|
var fullPath = Path.Combine(repo, _option.Path);
|
2024-03-27 06:38:38 -07:00
|
|
|
|
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, "HEAD").Result();
|
2024-03-27 18:34:08 -07:00
|
|
|
|
binaryDiff.NewSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
|
2024-03-27 06:38:38 -07:00
|
|
|
|
}
|
|
|
|
|
rs = binaryDiff;
|
2024-03-28 02:46:03 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-27 06:38:38 -07:00
|
|
|
|
else if (latest.IsLFS)
|
|
|
|
|
{
|
|
|
|
|
rs = latest.LFSDiff;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rs = new Models.NoOrEOLChange();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-27 06:38:38 -07:00
|
|
|
|
Content = rs;
|
|
|
|
|
IsTextDiff = latest.TextDiff != null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
IsLoading = false;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 05:02:43 -07:00
|
|
|
|
public void OpenExternalMergeTool()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var type = Preference.Instance.ExternalMergeToolType;
|
|
|
|
|
var exec = Preference.Instance.ExternalMergeToolPath;
|
|
|
|
|
|
|
|
|
|
var tool = Models.ExternalMergeTools.Supported.Find(x => x.Type == type);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (tool == null || !File.Exists(exec))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
App.RaiseException(_repo, "Invalid merge tool in preference setting!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var args = tool.Type != 0 ? tool.DiffCmd : Preference.Instance.ExternalMergeToolDiffCmd;
|
2024-04-07 05:02:43 -07:00
|
|
|
|
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 06:38:38 -07:00
|
|
|
|
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
|
|
|
|
|
{
|
|
|
|
|
".ico", ".bmp", ".jpg", ".png", ".jpeg"
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly string _repo = string.Empty;
|
|
|
|
|
private readonly Models.DiffOption _option = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private bool _isLoading = true;
|
|
|
|
|
private bool _isTextDiff = false;
|
|
|
|
|
private object _content = null;
|
2024-02-28 19:29:54 -08:00
|
|
|
|
private Vector _syncScrollOffset = Vector.Zero;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|