From e8fcd6f6597a3b338a242aa7abc7ee11ddfe16f9 Mon Sep 17 00:00:00 2001 From: Vladimir Eremeev Date: Mon, 13 Jan 2025 09:38:02 +0300 Subject: [PATCH] [wip] Display file content --- src/ViewModels/Conflict.cs | 12 +++ src/Views/TextDiffView.axaml.cs | 108 ++++++++++++++++++++++++ src/Views/WorkingCopy.axaml | 141 +++++++++++++++++++++++++++++++- 3 files changed, 260 insertions(+), 1 deletion(-) diff --git a/src/ViewModels/Conflict.cs b/src/ViewModels/Conflict.cs index 1ccf4a33a..6e01d0f68 100644 --- a/src/ViewModels/Conflict.cs +++ b/src/ViewModels/Conflict.cs @@ -41,10 +41,21 @@ public bool IsResolved private set; } + public Models.Change Change + { + get => _change; + } + + public Repository Repository + { + get => _repo; + } + public Conflict(Repository repo, WorkingCopy wc, Models.Change change) { _wc = wc; _change = change; + _repo = repo; IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).ReadToEnd().IsSuccess; @@ -98,5 +109,6 @@ public void OpenExternalMergeTool() private WorkingCopy _wc = null; private Models.Change _change = null; + private Repository _repo = null; } } diff --git a/src/Views/TextDiffView.axaml.cs b/src/Views/TextDiffView.axaml.cs index 925c7622c..57b2c5104 100644 --- a/src/Views/TextDiffView.axaml.cs +++ b/src/Views/TextDiffView.axaml.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Globalization; using System.IO; +using System.Linq; using System.Text; using Avalonia; @@ -2016,4 +2017,111 @@ private void OnDiscardChunk(object _1, RoutedEventArgs _2) repo.SetWatcherEnabled(true); } } + + public class ConflictTextDiffPresenter: SingleSideTextDiffPresenter + { + public enum Side : int + { + Ours = 0, WorkingCopy = 1, Theirs = 2 + } + + public Side DisplaySide { + get; set; + } + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + } + protected override void OnUnloaded(RoutedEventArgs e) + { + base.OnUnloaded(e); + } + + protected override void OnDataContextChanged(EventArgs e) + { + base.OnDataContextChanged(e); + + if (DataContext is ViewModels.Conflict diff) + { + string conflict_start_marker = "<<<<<<< HEAD"; + string conflict_separator_marker = "======="; + string conflict_end_marker = ">>>>>>> "; + + var cmd = new Commands.QueryRefsContainsCommit(diff.Repository.FullPath, (diff.Theirs as Models.Commit)?.SHA); + var their_branches = cmd.Result().ToArray(); + + string filePath = Path.Combine(diff.Repository.FullPath, diff.Change.Path); + var lines =File.ReadAllLines(filePath).ToList(); + + bool shouldFilter = DisplaySide != Side.WorkingCopy; + while(shouldFilter) { + int idx_start = lines.IndexOf(conflict_start_marker); + if (idx_start == -1) { + shouldFilter = false; + break; + } + + int idx_sep = lines.IndexOf(conflict_separator_marker, idx_start); + if (idx_sep == -1) { + shouldFilter = false; + break; + } + + int idx_end = idx_sep + 1; + while(idx_end < lines.Count - 1) { + bool found = false; + for (int i = 0; i < their_branches.Length; i++) { + string ce_marker = conflict_end_marker + their_branches[i].Name; + if(lines[idx_end].StartsWith(ce_marker)) { + found = true; + break; + } + } + if (found) { + break; + } + idx_end++; + } + if (idx_end == lines.Count - 1) { + shouldFilter = false; + break; + } + + switch (DisplaySide) { + case Side.Ours: + lines.RemoveRange(idx_sep, idx_end - idx_sep + 1); + lines.RemoveAt(idx_start); + break; + case Side.WorkingCopy: + break; + case Side.Theirs: + lines.RemoveAt(idx_end); + lines.RemoveRange(idx_start, idx_sep - idx_start + 1); + break; + } + } + + var builder = new StringBuilder(); + foreach (var line in lines) + { + if (line.Length > 10000) + { + builder.Append(line.Substring(0, 1000)); + builder.Append($"...({line.Length - 1000} characters trimmed)"); + builder.AppendLine(); + } + else + { + builder.AppendLine(line); + } + } + + Text = builder.ToString(); + } + else + { + Text = string.Empty; + } + } + } } diff --git a/src/Views/WorkingCopy.axaml b/src/Views/WorkingCopy.axaml index c9b94d592..272490423 100644 --- a/src/Views/WorkingCopy.axaml +++ b/src/Views/WorkingCopy.axaml @@ -203,7 +203,146 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +