2024-03-17 18:37:06 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Avalonia;
|
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 IsNoChange
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _isNoChange;
|
|
|
|
|
private set => SetProperty(ref _isNoChange, 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-17 18:37:06 -07:00
|
|
|
|
public DiffContext(string repo, Models.DiffOption option)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
_option = option;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
var binaryDiff = null as Models.BinaryDiff;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (latest.IsBinary)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
binaryDiff = new Models.BinaryDiff();
|
|
|
|
|
|
|
|
|
|
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (option.Revisions.Count == 2)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, option.Revisions[0]).Result();
|
|
|
|
|
binaryDiff.NewSize = new Commands.QueryFileSize(repo, _option.Path, option.Revisions[1]).Result();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, "HEAD").Result();
|
|
|
|
|
binaryDiff.NewSize = new FileInfo(Path.Combine(repo, _option.Path)).Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
Dispatcher.UIThread.InvokeAsync(() =>
|
|
|
|
|
{
|
|
|
|
|
if (latest.IsBinary)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Content = binaryDiff;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (latest.IsLFS)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Content = latest.LFSDiff;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (latest.TextDiff != null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
latest.TextDiff.File = _option.Path;
|
|
|
|
|
Content = latest.TextDiff;
|
|
|
|
|
IsTextDiff = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
IsTextDiff = false;
|
|
|
|
|
IsNoChange = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsLoading = false;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public async void OpenExternalMergeTool()
|
|
|
|
|
{
|
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;
|
|
|
|
|
await Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
|
|
|
|
|
}
|
|
|
|
|
|
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 _isNoChange = false;
|
|
|
|
|
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-17 18:37:06 -07:00
|
|
|
|
}
|