using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace SourceGit.UI {
///
/// Diff with selected 2 commits.
///
public partial class TwoCommitsDiff : UserControl {
private Git.Repository repo = null;
private string sha1 = null;
private string sha2 = null;
private List cachedChanges = new List();
private List displayChanges = new List();
private string changeFilter = null;
///
/// Node for file tree.
///
public class Node {
public string FilePath { get; set; } = "";
public string OriginalPath { get; set; } = "";
public string Name { get; set; } = "";
public bool IsFile { get; set; } = false;
public bool IsNodeExpanded { get; set; } = true;
public Git.Change Change { get; set; } = null;
public Git.Commit.Object CommitObject { get; set; } = null;
public List Children { get; set; } = new List();
}
///
/// Constructor.
///
public TwoCommitsDiff() {
InitializeComponent();
}
///
/// Show.
///
///
///
///
public void SetData(Git.Repository repo, string sha1, string sha2) {
this.repo = repo;
this.sha1 = sha1;
this.sha2 = sha2;
txtTitle.Content = $"COMMIT : {sha1} -> {sha2}";
Task.Run(() => LoadChanges(true));
}
///
/// Cleanup.
///
public void Cleanup() {
repo = null;
cachedChanges.Clear();
displayChanges.Clear();
}
private void LoadChanges(bool reload = false) {
if (reload) {
cachedChanges.Clear();
repo.RunCommand($"diff --name-status {sha1} {sha2}", line => {
var c = Git.Change.Parse(line, true);
if (c != null) cachedChanges.Add(c);
});
}
displayChanges.Clear();
if (string.IsNullOrEmpty(changeFilter)) {
displayChanges.AddRange(cachedChanges);
} else {
foreach (var c in cachedChanges) {
if (c.Path.ToUpper().Contains(changeFilter)) displayChanges.Add(c);
}
}
List changeTreeSource = new List();
Dictionary folders = new Dictionary();
bool isDefaultExpanded = displayChanges.Count < 50;
foreach (var c in displayChanges) {
var sepIdx = c.Path.IndexOf('/');
if (sepIdx == -1) {
Node node = new Node();
node.FilePath = c.Path;
node.IsFile = true;
node.Name = c.Path;
node.Change = c;
node.IsNodeExpanded = isDefaultExpanded;
if (c.OriginalPath != null) node.OriginalPath = c.OriginalPath;
changeTreeSource.Add(node);
} else {
Node lastFolder = null;
var start = 0;
while (sepIdx != -1) {
var folder = c.Path.Substring(0, sepIdx);
if (folders.ContainsKey(folder)) {
lastFolder = folders[folder];
} else if (lastFolder == null) {
lastFolder = new Node();
lastFolder.FilePath = folder;
lastFolder.Name = folder.Substring(start);
lastFolder.IsNodeExpanded = isDefaultExpanded;
changeTreeSource.Add(lastFolder);
folders.Add(folder, lastFolder);
} else {
var folderNode = new Node();
folderNode.FilePath = folder;
folderNode.Name = folder.Substring(start);
folderNode.IsNodeExpanded = isDefaultExpanded;
folders.Add(folder, folderNode);
lastFolder.Children.Add(folderNode);
lastFolder = folderNode;
}
start = sepIdx + 1;
sepIdx = c.Path.IndexOf('/', start);
}
Node node = new Node();
node.FilePath = c.Path;
node.Name = c.Path.Substring(start);
node.IsFile = true;
node.Change = c;
if (c.OriginalPath != null) node.OriginalPath = c.OriginalPath;
lastFolder.Children.Add(node);
}
}
folders.Clear();
SortTreeNodes(changeTreeSource);
Dispatcher.Invoke(() => {
changeList2.ItemsSource = null;
changeList2.ItemsSource = displayChanges;
changeTree.ItemsSource = changeTreeSource;
diffViewer.Reset();
});
}
private void SearchChangeFileTextChanged(object sender, TextChangedEventArgs e) {
changeFilter = txtChangeFilter.Text.ToUpper();
Task.Run(() => LoadChanges());
}
private void ChangeTreeItemSelected(object sender, RoutedPropertyChangedEventArgs