2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-07-23 04:13:32 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Threading;
|
2024-07-23 04:13:32 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class RevisionCompare : ObservableObject
|
|
|
|
|
{
|
2024-07-23 04:13:32 -07:00
|
|
|
|
public object StartPoint
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-23 04:13:32 -07:00
|
|
|
|
get => _startPoint;
|
|
|
|
|
private set => SetProperty(ref _startPoint, value);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 04:13:32 -07:00
|
|
|
|
public object EndPoint
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-23 04:13:32 -07:00
|
|
|
|
get => _endPoint;
|
|
|
|
|
private set => SetProperty(ref _endPoint, value);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Change> VisibleChanges
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _visibleChanges;
|
|
|
|
|
private set => SetProperty(ref _visibleChanges, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 06:19:53 -07:00
|
|
|
|
public List<Models.Change> SelectedChanges
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-28 06:19:53 -07:00
|
|
|
|
get => _selectedChanges;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
2024-05-28 06:19:53 -07:00
|
|
|
|
if (SetProperty(ref _selectedChanges, value))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-28 06:19:53 -07:00
|
|
|
|
if (value != null && value.Count == 1)
|
2024-07-23 04:13:32 -07:00
|
|
|
|
{
|
|
|
|
|
var option = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), value[0]);
|
|
|
|
|
DiffContext = new DiffContext(_repo, option, _diffContext);
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else
|
2024-07-23 04:13:32 -07:00
|
|
|
|
{
|
2024-05-28 06:19:53 -07:00
|
|
|
|
DiffContext = null;
|
2024-07-23 04:13:32 -07:00
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string SearchFilter
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _searchFilter;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _searchFilter, value))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
RefreshVisible();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public DiffContext DiffContext
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _diffContext;
|
|
|
|
|
private set => SetProperty(ref _diffContext, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public RevisionCompare(string repo, Models.Commit startPoint, Models.Commit endPoint)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
2024-07-23 04:13:32 -07:00
|
|
|
|
_startPoint = (object)startPoint ?? new Models.CompareTargetWorktree();
|
|
|
|
|
_endPoint = (object)endPoint ?? new Models.CompareTargetWorktree();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-07-23 02:41:33 -07:00
|
|
|
|
Task.Run(Refresh);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void Cleanup()
|
|
|
|
|
{
|
2024-02-20 02:27:59 -08:00
|
|
|
|
_repo = null;
|
2024-07-23 04:13:32 -07:00
|
|
|
|
_startPoint = null;
|
|
|
|
|
_endPoint = null;
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_changes != null)
|
|
|
|
|
_changes.Clear();
|
|
|
|
|
if (_visibleChanges != null)
|
|
|
|
|
_visibleChanges.Clear();
|
2024-05-28 06:19:53 -07:00
|
|
|
|
if (_selectedChanges != null)
|
|
|
|
|
_selectedChanges.Clear();
|
2024-02-20 02:27:59 -08:00
|
|
|
|
_searchFilter = null;
|
|
|
|
|
_diffContext = null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void NavigateTo(string commitSHA)
|
|
|
|
|
{
|
2024-06-30 20:57:13 -07:00
|
|
|
|
var repo = App.FindOpenedRepository(_repo);
|
|
|
|
|
repo?.NavigateToCommit(commitSHA);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 04:13:32 -07:00
|
|
|
|
public void Swap()
|
|
|
|
|
{
|
|
|
|
|
(StartPoint, EndPoint) = (_endPoint, _startPoint);
|
2024-07-23 04:30:16 -07:00
|
|
|
|
SelectedChanges = [];
|
2024-07-23 04:13:32 -07:00
|
|
|
|
Task.Run(Refresh);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void ClearSearchFilter()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SearchFilter = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 06:19:53 -07:00
|
|
|
|
public ContextMenu CreateChangeContextMenu()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-28 06:19:53 -07:00
|
|
|
|
if (_selectedChanges == null || _selectedChanges.Count != 1)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var change = _selectedChanges[0];
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
|
|
2024-04-07 05:02:43 -07:00
|
|
|
|
var diffWithMerger = new MenuItem();
|
|
|
|
|
diffWithMerger.Header = App.Text("DiffWithMerger");
|
2024-07-15 00:40:15 -07:00
|
|
|
|
diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
|
2024-04-07 05:02:43 -07:00
|
|
|
|
diffWithMerger.Click += (_, ev) =>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-23 04:13:32 -07:00
|
|
|
|
var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);
|
2024-06-17 21:10:38 -07:00
|
|
|
|
var toolType = Preference.Instance.ExternalMergeToolType;
|
|
|
|
|
var toolPath = Preference.Instance.ExternalMergeToolPath;
|
2024-04-07 05:02:43 -07:00
|
|
|
|
|
2024-06-17 21:10:38 -07:00
|
|
|
|
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, toolType, toolPath, opt));
|
2024-04-07 05:02:43 -07:00
|
|
|
|
ev.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(diffWithMerger);
|
|
|
|
|
|
|
|
|
|
if (change.Index != Models.ChangeState.Deleted)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var full = Path.GetFullPath(Path.Combine(_repo, change.Path));
|
|
|
|
|
var explore = new MenuItem();
|
|
|
|
|
explore.Header = App.Text("RevealFile");
|
2024-07-15 00:40:15 -07:00
|
|
|
|
explore.Icon = App.CreateMenuIcon("Icons.Explore");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
explore.IsEnabled = File.Exists(full);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
explore.Click += (_, ev) =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Native.OS.OpenInFileManager(full, true);
|
|
|
|
|
ev.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(explore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var copyPath = new MenuItem();
|
|
|
|
|
copyPath.Header = App.Text("CopyPath");
|
2024-04-07 05:02:43 -07:00
|
|
|
|
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
|
2024-03-17 18:37:06 -07:00
|
|
|
|
copyPath.Click += (_, ev) =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
App.CopyText(change.Path);
|
|
|
|
|
ev.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(copyPath);
|
2024-05-31 21:34:16 -07:00
|
|
|
|
|
|
|
|
|
var copyFileName = new MenuItem();
|
|
|
|
|
copyFileName.Header = App.Text("CopyFileName");
|
|
|
|
|
copyFileName.Icon = App.CreateMenuIcon("Icons.Copy");
|
2024-05-31 07:37:36 -07:00
|
|
|
|
copyFileName.Click += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
App.CopyText(Path.GetFileName(change.Path));
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(copyFileName);
|
2024-06-06 00:31:02 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void RefreshVisible()
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_changes == null)
|
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (string.IsNullOrEmpty(_searchFilter))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
VisibleChanges = _changes;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var visible = new List<Models.Change>();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var c in _changes)
|
|
|
|
|
{
|
|
|
|
|
if (c.Path.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
|
2024-02-05 23:08:37 -08:00
|
|
|
|
visible.Add(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VisibleChanges = visible;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 02:41:33 -07:00
|
|
|
|
private void Refresh()
|
|
|
|
|
{
|
2024-07-23 04:13:32 -07:00
|
|
|
|
_changes = new Commands.CompareRevisions(_repo, GetSHA(_startPoint), GetSHA(_endPoint)).Result();
|
2024-07-23 02:41:33 -07:00
|
|
|
|
|
|
|
|
|
var visible = _changes;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(_searchFilter))
|
|
|
|
|
{
|
|
|
|
|
visible = [];
|
|
|
|
|
foreach (var c in _changes)
|
|
|
|
|
{
|
|
|
|
|
if (c.Path.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
visible.Add(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Invoke(() => VisibleChanges = visible);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 04:13:32 -07:00
|
|
|
|
private string GetSHA(object obj)
|
2024-07-23 02:41:33 -07:00
|
|
|
|
{
|
2024-07-23 04:13:32 -07:00
|
|
|
|
return obj is Models.Commit commit ? commit.SHA : string.Empty;
|
2024-07-23 02:41:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 09:30:31 -07:00
|
|
|
|
private string _repo;
|
2024-07-23 04:13:32 -07:00
|
|
|
|
private object _startPoint = null;
|
|
|
|
|
private object _endPoint = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private List<Models.Change> _changes = null;
|
|
|
|
|
private List<Models.Change> _visibleChanges = null;
|
2024-05-28 06:19:53 -07:00
|
|
|
|
private List<Models.Change> _selectedChanges = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private string _searchFilter = string.Empty;
|
|
|
|
|
private DiffContext _diffContext = null;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|