feature<WorkingCopy>: reload diff content when working copy changed

This commit is contained in:
leo 2021-01-05 16:59:35 +08:00
parent d110b9e451
commit 1de529237a
4 changed files with 371 additions and 182 deletions

View file

@ -22,7 +22,9 @@ namespace SourceGit.UI {
public static readonly Brush BG_DELETED = new SolidColorBrush(Color.FromArgb(60, 255, 0, 0));
public static readonly Brush BG_NORMAL = Brushes.Transparent;
private List<Git.Diff.LineChange> lineChanges = null;
private Git.Repository lastRepo = null;
private Option lastOpts = null;
private List<Git.Diff.LineChange> lastChanges = null;
private List<DataGrid> editors = new List<DataGrid>();
/// <summary>
@ -73,9 +75,8 @@ namespace SourceGit.UI {
/// </summary>
public void Reset() {
mask.Visibility = Visibility.Visible;
lineChanges = null;
foreach (var editor in editors) editorContainer.Children.Remove(editor);
editors.Clear();
ClearCache();
ClearEditor();
}
/// <summary>
@ -91,11 +92,11 @@ namespace SourceGit.UI {
sizeChange.Visibility = Visibility.Collapsed;
noChange.Visibility = Visibility.Collapsed;
editorContainer.Children.Clear();
editorLines.Children.Clear();
editorLines.ColumnDefinitions.Clear();
editors.Clear();
lineChanges = null;
ClearEditor();
ClearCache();
lastRepo = repo;
lastOpts = opts;
Task.Run(() => {
var args = $"{opts.ExtraArgs} ";
@ -122,7 +123,57 @@ namespace SourceGit.UI {
if (rs.IsBinary) {
SetBinaryChange(Git.Diff.GetSizeChange(repo, opts.RevisionRange, opts.Path, opts.OrgPath));
} else if (rs.Lines.Count > 0) {
lineChanges = rs.Lines;
lastChanges = rs.Lines;
SetTextChange();
} else {
SetSame();
}
});
}
/// <summary>
/// Reload diff content with last repository and options.
/// </summary>
public void Reload() {
if (lastRepo == null) {
Reset();
return;
}
loading.Visibility = Visibility.Visible;
mask.Visibility = Visibility.Collapsed;
sizeChange.Visibility = Visibility.Collapsed;
noChange.Visibility = Visibility.Collapsed;
var repo = lastRepo;
var opts = lastOpts;
Task.Run(() => {
var args = $"{opts.ExtraArgs} ";
if (opts.RevisionRange.Length > 0) args += $"{opts.RevisionRange[0]} ";
if (opts.RevisionRange.Length > 1) args += $"{opts.RevisionRange[1]} ";
args += "-- ";
if (!string.IsNullOrEmpty(opts.OrgPath)) args += $"\"{opts.OrgPath}\" ";
args += $"\"{opts.Path}\"";
if (repo.IsLFSFiltered(opts.Path)) {
var lc = Git.Diff.GetLFSChange(repo, args);
if (lc.IsValid) {
SetLFSChange(lc);
} else {
SetSame();
}
return;
}
var rs = Git.Diff.GetTextChange(repo, args);
if (rs.IsBinary) {
SetBinaryChange(Git.Diff.GetSizeChange(repo, opts.RevisionRange, opts.Path, opts.OrgPath));
} else if (rs.Lines.Count > 0) {
lastChanges = rs.Lines;
SetTextChange();
} else {
SetSame();
@ -151,7 +202,7 @@ namespace SourceGit.UI {
/// </summary>
/// <param name="rs"></param>
private void SetTextChange() {
if (lineChanges == null) return;
if (lastChanges == null) return;
var fgCommon = FindResource("Brush.FG") as Brush;
var fgIndicator = FindResource("Brush.FG2") as Brush;
@ -161,7 +212,7 @@ namespace SourceGit.UI {
if (App.Setting.UI.UseCombinedDiff) {
var blocks = new List<ChangeBlock>();
foreach (var line in lineChanges) {
foreach (var line in lastChanges) {
var block = new ChangeBlock();
block.Content = line.Content;
block.Mode = line.Mode;
@ -181,17 +232,15 @@ namespace SourceGit.UI {
loading.Visibility = Visibility.Collapsed;
textChangeOptions.Visibility = Visibility.Visible;
var createEditor = editors.Count == 0;
var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine);
var minWidth = editorContainer.ActualWidth - lineNumberWidth * 2;
if (editorContainer.ActualHeight < lineChanges.Count * 16) minWidth -= 8;
var editor = CreateTextEditor(new string[] { "OldLine", "NewLine" });
editor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[1].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[2].MinWidth = minWidth;
editor.ItemsSource = blocks;
if (editorContainer.ActualHeight < lastChanges.Count * 16) minWidth -= 8;
DataGrid editor;
if (createEditor) {
editor = CreateTextEditor(new string[] { "OldLine", "NewLine" });
editor.SetValue(Grid.ColumnSpanProperty, 2);
editorContainer.Children.Add(editor);
editors.Add(editor);
editorLines.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(lineNumberWidth) });
editorLines.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(lineNumberWidth) });
@ -205,12 +254,30 @@ namespace SourceGit.UI {
editorLines.Children.Add(split);
}
} else {
editor = editors[0] as DataGrid;
editorLines.ColumnDefinitions[0].Width = new GridLength(lineNumberWidth);
editorLines.ColumnDefinitions[0].Width = new GridLength(lineNumberWidth);
}
editor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[1].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[2].MinWidth = minWidth;
editor.ItemsSource = blocks;
if (createEditor) {
editorContainer.Children.Add(editor);
editors.Add(editor);
} else {
editor.UpdateLayout();
editorLines.UpdateLayout();
}
});
} else {
var oldSideBlocks = new List<ChangeBlock>();
var newSideBlocks = new List<ChangeBlock>();
foreach (var line in lineChanges) {
foreach (var line in lastChanges) {
var block = new ChangeBlock();
block.Content = line.Content;
block.Mode = line.Mode;
@ -244,29 +311,22 @@ namespace SourceGit.UI {
loading.Visibility = Visibility.Collapsed;
textChangeOptions.Visibility = Visibility.Visible;
var createEditor = editors.Count == 0;
var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine);
var minWidth = editorContainer.ActualWidth / 2 - lineNumberWidth;
if (editorContainer.ActualHeight < newSideBlocks.Count * 16) minWidth -= 8;
var oldEditor = CreateTextEditor(new string[] { "OldLine" });
DataGrid oldEditor = null;
DataGrid newEditor = null;
if (createEditor) {
oldEditor = CreateTextEditor(new string[] { "OldLine" });
oldEditor.SetValue(Grid.ColumnProperty, 0);
oldEditor.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(OnTwoSidesScroll));
oldEditor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
oldEditor.Columns[1].MinWidth = minWidth;
oldEditor.ItemsSource = oldSideBlocks;
var newEditor = CreateTextEditor(new string[] { "NewLine" });
newEditor = CreateTextEditor(new string[] { "NewLine" });
newEditor.SetValue(Grid.ColumnProperty, 1);
newEditor.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(OnTwoSidesScroll));
newEditor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
newEditor.Columns[1].MinWidth = minWidth;
newEditor.ItemsSource = newSideBlocks;
editorContainer.Children.Add(oldEditor);
editorContainer.Children.Add(newEditor);
editors.Add(oldEditor);
editors.Add(newEditor);
editorLines.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(lineNumberWidth) });
editorLines.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
@ -281,6 +341,34 @@ namespace SourceGit.UI {
Grid.SetColumn(split, i);
editorLines.Children.Add(split);
}
} else {
oldEditor = editors[0];
newEditor = editors[1];
editorLines.ColumnDefinitions[0].Width = new GridLength(lineNumberWidth);
editorLines.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
editorLines.ColumnDefinitions[2].Width = new GridLength(lineNumberWidth);
editorLines.ColumnDefinitions[3].Width = new GridLength(1, GridUnitType.Star);
}
oldEditor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
oldEditor.Columns[1].MinWidth = minWidth;
oldEditor.ItemsSource = oldSideBlocks;
newEditor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
newEditor.Columns[1].MinWidth = minWidth;
newEditor.ItemsSource = newSideBlocks;
if (createEditor) {
editorContainer.Children.Add(oldEditor);
editorContainer.Children.Add(newEditor);
editors.Add(oldEditor);
editors.Add(newEditor);
} else {
oldEditor.UpdateLayout();
newEditor.UpdateLayout();
editorLines.UpdateLayout();
}
});
}
}
@ -491,10 +579,28 @@ namespace SourceGit.UI {
return formatted.Width + 16;
}
/// <summary>
/// Clear cached data.
/// </summary>
private void ClearCache() {
lastRepo = null;
lastOpts = null;
lastChanges = null;
}
/// <summary>
/// Clear editor.
/// </summary>
private void ClearEditor() {
editorContainer.Children.Clear();
editorLines.Children.Clear();
editorLines.ColumnDefinitions.Clear();
editors.Clear();
}
#endregion
#region EVENTS
/// <summary>
/// Auto fit text change diff size.
/// </summary>
@ -513,18 +619,23 @@ namespace SourceGit.UI {
editor.Columns[2].Width = DataGridLength.SizeToCells;
editor.UpdateLayout();
} else {
var offOld = editors[0].NonFrozenColumnsViewportHorizontalOffset;
var offNew = editors[1].NonFrozenColumnsViewportHorizontalOffset;
var oldEditor = editors[0];
var newEditor = editors[1];
var offOld = oldEditor.NonFrozenColumnsViewportHorizontalOffset;
var offNew = newEditor.NonFrozenColumnsViewportHorizontalOffset;
var minWidth = total / 2 - Math.Min(offOld, offNew);
var scroller = GetVisualChild<ScrollViewer>(editors[0]);
var scroller = GetVisualChild<ScrollViewer>(oldEditor);
if (scroller != null && scroller.ComputedVerticalScrollBarVisibility == Visibility.Visible) minWidth -= 8;
editors[0].Columns[1].MinWidth = minWidth;
editors[0].Columns[1].Width = DataGridLength.SizeToCells;
editors[1].Columns[1].MinWidth = minWidth;
editors[1].Columns[1].Width = DataGridLength.SizeToCells;
editors[0].UpdateLayout();
editors[1].UpdateLayout();
oldEditor.Columns[1].MinWidth = minWidth;
oldEditor.Columns[1].Width = DataGridLength.SizeToCells;
oldEditor.UpdateLayout();
newEditor.Columns[1].MinWidth = minWidth;
newEditor.Columns[1].Width = DataGridLength.SizeToCells;
newEditor.UpdateLayout();
}
}
@ -543,22 +654,19 @@ namespace SourceGit.UI {
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnTwoSidesScroll(object sender, ScrollChangedEventArgs e) {
if (e.VerticalChange != 0) {
foreach (var editor in editors) {
var scroller = GetVisualChild<ScrollViewer>(editor);
if (scroller != null && scroller.VerticalOffset != e.VerticalOffset) {
if (scroller == null) continue;
if (e.VerticalChange != 0 && scroller.VerticalOffset != e.VerticalOffset) {
scroller.ScrollToVerticalOffset(e.VerticalOffset);
}
}
} else {
foreach (var editor in editors) {
var scroller = GetVisualChild<ScrollViewer>(editor);
if (scroller != null && scroller.HorizontalOffset != e.HorizontalOffset) {
if (e.HorizontalChange != 0 && scroller.HorizontalOffset != e.HorizontalOffset) {
scroller.ScrollToHorizontalOffset(e.HorizontalOffset);
}
}
}
}
/// <summary>
/// Go to next difference.
@ -624,11 +732,7 @@ namespace SourceGit.UI {
private void ChangeDiffMode(object sender, RoutedEventArgs e) {
if (!IsLoaded) return;
editorContainer.Children.Clear();
editorLines.Children.Clear();
editorLines.ColumnDefinitions.Clear();
editors.Clear();
ClearEditor();
SetTextChange();
}

View file

@ -96,7 +96,7 @@ namespace SourceGit.UI {
Dispatcher.Invoke(() => {
txtSearch.Text = "";
commitList.ItemsSource = new List<Git.Commit>(cachedCommits);
commitList.ItemsSource = cachedCommits;
SetLoadingEnabled(false);
});
}

View file

@ -73,6 +73,7 @@
<TreeView
Grid.Row="1"
x:Name="unstagedTree"
ItemsSource="{Binding ElementName=me, Path=UnstagedTreeData, IsAsync=True}"
Background="{StaticResource Brush.BG2}"
Visibility="{Binding Source={x:Static source:App.Setting}, Path=UI.UnstageFileDisplayMode, Converter={StaticResource FilesDisplayModeToTree}}"
FontFamily="Consolas"
@ -127,6 +128,7 @@
<DataGrid
Grid.Row="1"
x:Name="unstagedList"
ItemsSource="{Binding ElementName=me, Path=UnstagedListData, IsAsync=True}"
Visibility="{Binding Source={x:Static source:App.Setting}, Path=UI.UnstageFileDisplayMode, Converter={StaticResource FilesDisplayModeToList}}"
RowHeight="24"
SelectionChanged="UnstagedListSelectionChanged"
@ -229,6 +231,7 @@
<TreeView
Grid.Row="1"
x:Name="stageTree"
ItemsSource="{Binding ElementName=me, Path=StagedTreeData, IsAsync=True}"
Visibility="{Binding Source={x:Static source:App.Setting}, Path=UI.StagedFileDisplayMode, Converter={StaticResource FilesDisplayModeToTree}}"
Background="{StaticResource Brush.BG2}"
FontFamily="Consolas"
@ -283,6 +286,7 @@
<DataGrid
Grid.Row="1"
x:Name="stageList"
ItemsSource="{Binding ElementName=me, Path=StagedListData, IsAsync=True}"
RowHeight="24"
Visibility="{Binding Source={x:Static source:App.Setting}, Path=UI.StagedFileDisplayMode, Converter={StaticResource FilesDisplayModeToList}}"
SelectionChanged="StagedListSelectionChanged"

View file

@ -1,8 +1,10 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -26,7 +28,7 @@ namespace SourceGit.UI {
public bool IsFile { get; set; } = false;
public bool IsNodeExpanded { get; set; } = true;
public Git.Change Change { get; set; } = null;
public List<Node> Children { get; set; } = new List<Node>();
public ObservableCollection<Node> Children { get; set; } = new ObservableCollection<Node>();
}
/// <summary>
@ -40,14 +42,39 @@ namespace SourceGit.UI {
public string CommitMessage { get; set; }
/// <summary>
/// Has conflict object?
/// Cached unstaged changes in list/grid view.
/// </summary>
private bool hasConflict = false;
public ObservableCollection<Git.Change> UnstagedListData { get; set; }
/// <summary>
/// Cached unstaged changes in TreeView.
/// </summary>
public ObservableCollection<Node> UnstagedTreeData { get; set; }
/// <summary>
/// Cached staged changes in list/grid view.
/// </summary>
public ObservableCollection<Git.Change> StagedListData { get; set; }
/// <summary>
/// Cached staged changes in TreeView.
/// </summary>
public ObservableCollection<Node> StagedTreeData { get; set; }
/// <summary>
/// Last view change
/// </summary>
public Git.Change LastViewChange { get; set; }
/// <summary>
/// Constructor.
/// </summary>
public WorkingCopy() {
UnstagedListData = new ObservableCollection<Git.Change>();
UnstagedTreeData = new ObservableCollection<Node>();
StagedListData = new ObservableCollection<Git.Change>();
StagedTreeData = new ObservableCollection<Node>();
InitializeComponent();
}
@ -58,24 +85,35 @@ namespace SourceGit.UI {
public bool SetData(List<Git.Change> changes) {
List<Git.Change> staged = new List<Git.Change>();
List<Git.Change> unstaged = new List<Git.Change>();
hasConflict = false;
bool hasConflict = false;
bool removeLastViewChange = true;
foreach (var c in changes) {
hasConflict = hasConflict || c.IsConflit;
if (c.Index != Git.Change.Status.None && c.Index != Git.Change.Status.Untracked) {
if (c.IsAddedToIndex) {
staged.Add(c);
if (LastViewChange != null && LastViewChange.IsAddedToIndex && c.Path == LastViewChange.Path) {
LastViewChange = c;
removeLastViewChange = false;
}
}
if (c.WorkTree != Git.Change.Status.None) {
unstaged.Add(c);
if (LastViewChange != null && !LastViewChange.IsAddedToIndex && c.Path == LastViewChange.Path) {
LastViewChange = c;
removeLastViewChange = false;
}
}
}
SetData(unstaged, true);
SetData(staged, false);
if (removeLastViewChange) LastViewChange = null;
Dispatcher.Invoke(() => {
UpdateData(unstaged, UnstagedListData, UnstagedTreeData);
UpdateData(staged, StagedListData, StagedTreeData);
var current = Repo.CurrentBranch();
if (current != null && !string.IsNullOrEmpty(current.Upstream) && chkAmend.IsChecked != true) {
btnCommitAndPush.Visibility = Visibility.Visible;
@ -83,13 +121,55 @@ namespace SourceGit.UI {
btnCommitAndPush.Visibility = Visibility.Collapsed;
}
if (LastViewChange != null) {
if (LastViewChange.IsConflit) {
mergePanel.Visibility = Visibility.Visible;
diffViewer.Reset();
} else {
mergePanel.Visibility = Visibility.Collapsed;
diffViewer.Reload();
}
} else {
mergePanel.Visibility = Visibility.Collapsed;
diffViewer.Reset();
}
});
return hasConflict;
}
/// <summary>
/// Update data.
/// </summary>
/// <param name="changes"></param>
/// <param name="list"></param>
/// <param name="tree"></param>
public void UpdateData(List<Git.Change> changes, ObservableCollection<Git.Change> list, ObservableCollection<Node> tree) {
for (int i = list.Count - 1; i >= 0; i--) {
var exist = list[i];
if (changes.FirstOrDefault(one => one.Path == exist.Path) != null) continue;
list.RemoveAt(i);
RemoveTreeNode(tree, exist);
}
foreach (var c in changes) {
if (list.FirstOrDefault(one => one.Path == c.Path) != null) continue;
bool added = false;
for (int i = 0; i < list.Count; i++) {
if (c.Path.CompareTo(list[i].Path) < 0) {
list.Insert(i, c);
added = true;
break;
}
}
if (!added) list.Add(c);
InsertTreeNode(tree, c);
}
}
/// <summary>
/// Try to load merge message.
/// </summary>
@ -116,10 +196,10 @@ namespace SourceGit.UI {
/// </summary>
public void Cleanup() {
Repo = null;
unstagedList.ItemsSource = null;
unstagedList.ItemsSource = null;
stageList.ItemsSource = null;
stageTree.ItemsSource = null;
UnstagedListData.Clear();
UnstagedTreeData.Clear();
StagedListData.Clear();
StagedTreeData.Clear();
diffViewer.Reset();
}
@ -143,6 +223,8 @@ namespace SourceGit.UI {
return;
}
LastViewChange = node.Change;
DiffViewer.Option opt;
switch (node.Change.WorkTree) {
case Git.Change.Status.Added:
@ -174,6 +256,8 @@ namespace SourceGit.UI {
return;
}
LastViewChange = change;
DiffViewer.Option opt;
switch (change.WorkTree) {
case Git.Change.Status.Added:
@ -543,6 +627,8 @@ namespace SourceGit.UI {
var node = selected[0].DataContext as Node;
if (!node.IsFile) return;
LastViewChange = node.Change;
diffViewer.Diff(Repo, new DiffViewer.Option() {
ExtraArgs = "--cached",
Path = node.FilePath,
@ -563,6 +649,7 @@ namespace SourceGit.UI {
if (selected.Count != 1) return;
var change = selected[0] as Git.Change;
LastViewChange = change;
diffViewer.Diff(Repo, new DiffViewer.Option() {
ExtraArgs = "--cached",
Path = change.Path,
@ -816,14 +903,15 @@ namespace SourceGit.UI {
}
private async void Commit(object sender, RoutedEventArgs e) {
var amend = chkAmend.IsChecked == true;
Repo.RecordCommitMessage(CommitMessage);
if (hasConflict) {
foreach (var c in UnstagedListData) {
if (c.IsConflit) {
App.RaiseError("You have unsolved conflicts in your working copy!");
return;
}
}
var amend = chkAmend.IsChecked == true;
Repo.RecordCommitMessage(CommitMessage);
if (stageTree.Items.Count == 0) {
App.RaiseError("Nothing to commit!");
@ -838,14 +926,15 @@ namespace SourceGit.UI {
}
private async void CommitAndPush(object sender, RoutedEventArgs e) {
var amend = chkAmend.IsChecked == true;
Repo.RecordCommitMessage(CommitMessage);
if (hasConflict) {
foreach (var c in UnstagedListData) {
if (c.IsConflit) {
App.RaiseError("You have unsolved conflicts in your working copy!");
return;
}
}
var amend = chkAmend.IsChecked == true;
Repo.RecordCommitMessage(CommitMessage);
if (stageTree.Items.Count == 0) {
App.RaiseError("Nothing to commit!");
@ -966,69 +1055,75 @@ namespace SourceGit.UI {
Helpers.TreeViewHelper.SelectWholeTree(tree);
}
private void SetData(List<Git.Change> changes, bool unstaged) {
List<Node> source = new List<Node>();
Dictionary<string, Node> folders = new Dictionary<string, Node>();
bool isExpendDefault = changes.Count <= 50;
private Node InsertTreeNode(ObservableCollection<Node> nodes, string name, string path, bool isFile = false, Git.Change change = null) {
Node node = new Node();
node.Name = name;
node.FilePath = path;
node.IsFile = isFile;
node.Change = change;
foreach (var c in changes) {
var subs = c.Path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
bool isAdded = false;
if (node.IsFile) {
for (var i = 0; i < nodes.Count; i++) {
var cur = nodes[i];
if (!cur.IsFile) continue;
if (node.FilePath.CompareTo(cur.FilePath) > 0) continue;
nodes.Insert(i, node);
isAdded = true;
break;
}
} else {
for (var i = 0; i < nodes.Count; i++) {
var cur = nodes[i];
if (cur.IsFile || node.FilePath.CompareTo(cur.FilePath) < 0) {
nodes.Insert(i, node);
isAdded = true;
break;
}
}
}
if (!isAdded) nodes.Add(node);
return node;
}
private void InsertTreeNode(ObservableCollection<Node> nodes, Git.Change change) {
string[] subs = change.Path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
if (subs.Length == 1) {
Node node = new Node();
node.FilePath = c.Path;
node.IsFile = true;
node.Name = c.Path;
node.Change = c;
source.Add(node);
InsertTreeNode(nodes, change.Path, change.Path, true, change);
} else {
Node lastFolder = null;
var folder = "";
for (int i = 0; i < subs.Length - 1; i++) {
folder += (subs[i] + "/");
if (folders.ContainsKey(folder)) {
lastFolder = folders[folder];
} else if (lastFolder == null) {
lastFolder = new Node();
lastFolder.FilePath = folder;
lastFolder.Name = subs[i];
lastFolder.IsNodeExpanded = isExpendDefault;
source.Add(lastFolder);
folders.Add(folder, lastFolder);
} else {
var folderNode = new Node();
folderNode.FilePath = folder;
folderNode.Name = subs[i];
folderNode.IsNodeExpanded = isExpendDefault;
folders.Add(folder, folderNode);
lastFolder.Children.Add(folderNode);
lastFolder = folderNode;
Node last = nodes.FirstOrDefault(o => o.Name == subs[0]);
if (last == null) last = InsertTreeNode(nodes, subs[0], subs[0]);
for (int i = 1; i < subs.Length - 1; i++) {
var p = last.Children.FirstOrDefault(o => o.Name == subs[i]);
if (p == null) p = InsertTreeNode(last.Children, subs[i], last.FilePath + "/" + subs[i]);
last = p;
}
InsertTreeNode(last.Children, subs[subs.Length - 1], change.Path, true, change);
}
}
Node node = new Node();
node.FilePath = c.Path;
node.Name = subs[subs.Length - 1];
node.IsFile = true;
node.Change = c;
lastFolder.Children.Add(node);
private bool RemoveTreeNode(ObservableCollection<Node> nodes, Git.Change change) {
for (int i = nodes.Count - 1; i >= 0; i--) {
if (nodes[i].FilePath == change.Path) {
nodes.RemoveAt(i);
return true;
}
if (nodes[i].IsFile) continue;
if (RemoveTreeNode(nodes[i].Children, change)) {
if (nodes[i].Children.Count == 0) nodes.RemoveAt(i);
return true;
}
}
folders.Clear();
SortTreeNodes(source);
Dispatcher.Invoke(() => {
if (unstaged) {
unstagedList.ItemsSource = changes;
unstagedTree.ItemsSource = source;
} else {
stageList.ItemsSource = changes;
stageTree.ItemsSource = source;
}
});
return false;
}
private Node FindNodeByPath(List<Node> nodes, string filePath) {
private Node FindNodeByPath(ObservableCollection<Node> nodes, string filePath) {
foreach (var node in nodes) {
if (node.FilePath == filePath) return node;
var found = FindNodeByPath(node.Children, filePath);
@ -1037,20 +1132,6 @@ namespace SourceGit.UI {
return null;
}
private void SortTreeNodes(List<Node> list) {
list.Sort((l, r) => {
if (l.IsFile) {
return r.IsFile ? l.FilePath.CompareTo(r.FilePath) : 1;
} else {
return r.IsFile ? -1 : l.FilePath.CompareTo(r.FilePath);
}
});
foreach (var sub in list) {
if (sub.Children.Count > 0) SortTreeNodes(sub.Children);
}
}
private void TreeMouseWheel(object sender, MouseWheelEventArgs e) {
var scroll = Helpers.TreeViewHelper.GetScrollViewer(sender as TreeView);
if (scroll == null) return;