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
|
|
|
|
|
{
|
|
|
|
|
public class RevisionCompare : ObservableObject
|
|
|
|
|
{
|
|
|
|
|
public Models.Commit StartPoint
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Commit EndPoint
|
|
|
|
|
{
|
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-03-17 18:37:06 -07:00
|
|
|
|
public List<FileTreeNode> ChangeTree
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _changeTree;
|
|
|
|
|
private set => SetProperty(ref _changeTree, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Change SelectedChange
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedChange;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _selectedChange, value))
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SelectedNode = null;
|
|
|
|
|
DiffContext = null;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SelectedNode = FileTreeNode.SelectByPath(_changeTree, value.Path);
|
2024-03-20 03:27:48 -07:00
|
|
|
|
DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, EndPoint.SHA, value), _diffContext);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public FileTreeNode SelectedNode
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedNode;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _selectedNode, value))
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SelectedChange = null;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SelectedChange = value.Backend as Models.Change;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
EndPoint = endPoint;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_changes = new Commands.CompareRevisions(_repo, startPoint.SHA, endPoint.SHA).Result();
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tree = FileTreeNode.Build(visible);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
VisibleChanges = visible;
|
|
|
|
|
ChangeTree = tree;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
if (_changeTree != null)
|
|
|
|
|
_changeTree.Clear();
|
2024-02-20 02:27:59 -08:00
|
|
|
|
_selectedChange = null;
|
|
|
|
|
_selectedNode = null;
|
|
|
|
|
_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-03-17 18:37:06 -07:00
|
|
|
|
public ContextMenu CreateChangeContextMenu(Models.Change change)
|
|
|
|
|
{
|
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-04-07 05:02:43 -07:00
|
|
|
|
var opt = new Models.DiffOption(StartPoint.SHA, EndPoint.SHA, change);
|
|
|
|
|
var type = Preference.Instance.ExternalMergeToolType;
|
|
|
|
|
var exec = Preference.Instance.ExternalMergeToolPath;
|
|
|
|
|
|
|
|
|
|
var tool = Models.ExternalMergeTools.Supported.Find(x => x.Type == type);
|
|
|
|
|
if (tool == null || !File.Exists(exec))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-04-07 05:02:43 -07:00
|
|
|
|
App.RaiseException(_repo, "Invalid merge tool in preference setting!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-04-07 05:02:43 -07:00
|
|
|
|
var args = tool.Type != 0 ? tool.DiffCmd : Preference.Instance.ExternalMergeToolDiffCmd;
|
|
|
|
|
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, opt));
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeTree = FileTreeNode.Build(_visibleChanges);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _repo = string.Empty;
|
|
|
|
|
private List<Models.Change> _changes = null;
|
|
|
|
|
private List<Models.Change> _visibleChanges = null;
|
|
|
|
|
private List<FileTreeNode> _changeTree = null;
|
|
|
|
|
private Models.Change _selectedChange = null;
|
|
|
|
|
private FileTreeNode _selectedNode = null;
|
|
|
|
|
private string _searchFilter = string.Empty;
|
|
|
|
|
private DiffContext _diffContext = null;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|