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-03-17 18:37:06 -07:00
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
2024-05-27 06:05:15 -07:00
|
|
|
|
public class CompareTargetWorktree
|
|
|
|
|
{
|
|
|
|
|
public string SHA => string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class RevisionCompare : ObservableObject
|
|
|
|
|
{
|
|
|
|
|
public Models.Commit StartPoint
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 06:05:15 -07:00
|
|
|
|
public object EndPoint
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, _endPoint, value[0]), _diffContext);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else
|
2024-05-28 06:19:53 -07:00
|
|
|
|
DiffContext = null;
|
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;
|
|
|
|
|
StartPoint = startPoint;
|
2024-05-27 06:05:15 -07:00
|
|
|
|
|
|
|
|
|
if (endPoint == null)
|
|
|
|
|
{
|
|
|
|
|
EndPoint = new CompareTargetWorktree();
|
|
|
|
|
_endPoint = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
EndPoint = endPoint;
|
|
|
|
|
_endPoint = endPoint.SHA;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
2024-05-27 06:05:15 -07:00
|
|
|
|
_changes = new Commands.CompareRevisions(_repo, startPoint.SHA, _endPoint).Result();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
var visible = _changes;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(_searchFilter))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 18:53:07 -07:00
|
|
|
|
Dispatcher.UIThread.Invoke(() => VisibleChanges = visible);
|
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-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-02-05 23:08:37 -08:00
|
|
|
|
var repo = Preference.FindRepository(_repo);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (repo != null)
|
|
|
|
|
repo.NavigateToCommit(commitSHA);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
diffWithMerger.Icon = App.CreateMenuIcon("Icons.Diff");
|
|
|
|
|
diffWithMerger.Click += (_, ev) =>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-27 06:05:15 -07:00
|
|
|
|
var opt = new Models.DiffOption(StartPoint.SHA, _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-04-07 05:02:43 -07:00
|
|
|
|
explore.Icon = App.CreateMenuIcon("Icons.Folder.Open");
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _repo = string.Empty;
|
2024-05-27 06:05:15 -07:00
|
|
|
|
private string _endPoint = string.Empty;
|
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
|
|
|
|
}
|