From 88452bd74d4b6210837e785454424ec1d0521e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Sun, 12 Jan 2025 18:47:27 +0100 Subject: [PATCH 01/11] Add CommitInfo hardcoded line counter --- src/Resources/Locales/en_US.axaml | 1 + src/Views/CommitBaseInfo.axaml | 10 +++++--- src/Views/LinesChanged.axaml | 22 +++++++++++++++++ src/Views/LinesChanged.axaml.cs | 41 +++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 src/Views/LinesChanged.axaml create mode 100644 src/Views/LinesChanged.axaml.cs diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index cc32cc4a4..218be9a30 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -137,6 +137,7 @@ PARENTS REFS SHA + STATS Open in Browser Enter commit subject Description diff --git a/src/Views/CommitBaseInfo.axaml b/src/Views/CommitBaseInfo.axaml index 96d403835..55342d183 100644 --- a/src/Views/CommitBaseInfo.axaml +++ b/src/Views/CommitBaseInfo.axaml @@ -51,7 +51,7 @@ - + @@ -182,9 +182,13 @@ UseGraphColor="False"/> + + + + - - + + + + + + + + + + + + + + + + + diff --git a/src/Views/LinesChanged.axaml.cs b/src/Views/LinesChanged.axaml.cs new file mode 100644 index 000000000..aa2c8a605 --- /dev/null +++ b/src/Views/LinesChanged.axaml.cs @@ -0,0 +1,41 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace SourceGit.Views +{ + public partial class LinesChanged : UserControl + { + public static readonly DirectProperty AddedLinesProperty = + AvaloniaProperty.RegisterDirect(nameof(AddedLines), o => o.AddedLines); + + public static readonly DirectProperty RemovedLinesProperty = + AvaloniaProperty.RegisterDirect(nameof(RemovedLines), o => o.RemovedLines); + + private int _addedLines; + private int _removedLines; + + public int AddedLines + { + get => _addedLines; + set => SetAndRaise(AddedLinesProperty, ref _addedLines, value); + } + + public int RemovedLines + { + get => _removedLines; + set => SetAndRaise(RemovedLinesProperty, ref _removedLines, value); + } + + public LinesChanged() + { + InitializeComponent(); + this.DataContext = this; + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + } +} From 156bb318313bff403e4ac9343a7d540771155fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Sun, 12 Jan 2025 19:41:14 +0100 Subject: [PATCH 02/11] Make command for retreiving line count work --- src/Commands/QueryCommitChangedLines.cs | 43 +++++++++++++++++++++++++ src/ViewModels/CommitDetail.cs | 7 ++++ 2 files changed, 50 insertions(+) create mode 100644 src/Commands/QueryCommitChangedLines.cs diff --git a/src/Commands/QueryCommitChangedLines.cs b/src/Commands/QueryCommitChangedLines.cs new file mode 100644 index 000000000..d56051040 --- /dev/null +++ b/src/Commands/QueryCommitChangedLines.cs @@ -0,0 +1,43 @@ +using System.Text.RegularExpressions; + +namespace SourceGit.Commands +{ + public class QueryCommitChangedLines : Command + { + public QueryCommitChangedLines(string repo, string sha) + { + WorkingDirectory = repo; + Context = repo; + Args = $"show --numstat --oneline {sha}"; + } + + public (int, int) Result() + { + _addedLines = 0; + _removedLines = 0; + _firstLine = true; + Exec(); + return (_addedLines, _removedLines); + } + + protected override void OnReadline(string line) + { + if (_firstLine) { + _firstLine = false; + return; + } + + var parts = Regex.Split(line, @"\s+"); + + if (parts.Length >= 2) + { + _addedLines += int.Parse(parts[0]); + _removedLines += int.Parse(parts[1]); + } + } + + private int _addedLines; + private int _removedLines; + private bool _firstLine; + } +} diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index bf9f57c9f..455c530d5 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -635,6 +635,13 @@ private void Refresh() Dispatcher.UIThread.Invoke(() => FullMessage = fullMessage); }); + Task.Run(() => + { + var lines = new Commands.QueryCommitChangedLines(_repo.FullPath, _commit.SHA).Result(); + Console.WriteLine(lines); + //Dispatcher.UIThread.Invoke(() => FullMessage = fullMessage); + }); + Task.Run(() => { var signInfo = new Commands.QueryCommitSignInfo(_repo.FullPath, _commit.SHA, !_repo.HasAllowedSignersFile).Result(); From 399bd2f12b64090dfb0d825121ed448efb712f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:23:04 +0100 Subject: [PATCH 03/11] Style line counter --- src/Views/LinesChanged.axaml | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Views/LinesChanged.axaml b/src/Views/LinesChanged.axaml index 4104ca985..b8a32d00f 100644 --- a/src/Views/LinesChanged.axaml +++ b/src/Views/LinesChanged.axaml @@ -5,18 +5,22 @@ x:Name="ThisControl" x:DataType="local:LinesChanged"> - - - - - - + + + + + + + + - - - - - - + + + + + + + + From ac57c930cb5d28956726cdfb229b1027fb89e0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Sun, 19 Jan 2025 17:13:15 +0100 Subject: [PATCH 04/11] Display lines changed --- src/Commands/QueryCommitChangedLines.cs | 6 ++-- src/Models/Commit.cs | 3 ++ src/ViewModels/CommitDetail.cs | 8 +++-- src/Views/CommitBaseInfo.axaml | 21 ++++++++++++- src/Views/LinesChanged.axaml | 26 ---------------- src/Views/LinesChanged.axaml.cs | 41 ------------------------- 6 files changed, 32 insertions(+), 73 deletions(-) delete mode 100644 src/Views/LinesChanged.axaml delete mode 100644 src/Views/LinesChanged.axaml.cs diff --git a/src/Commands/QueryCommitChangedLines.cs b/src/Commands/QueryCommitChangedLines.cs index d56051040..018072ccf 100644 --- a/src/Commands/QueryCommitChangedLines.cs +++ b/src/Commands/QueryCommitChangedLines.cs @@ -31,8 +31,10 @@ protected override void OnReadline(string line) if (parts.Length >= 2) { - _addedLines += int.Parse(parts[0]); - _removedLines += int.Parse(parts[1]); + bool canParseAdded = int.TryParse(parts[0], out int addedLines); + bool canParseRemoved = int.TryParse(parts[1], out int removedLines); + if (canParseAdded) _addedLines += addedLines; + if (canParseRemoved) _removedLines += removedLines; } } diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 9bc7f0c3a..3840f62e9 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -45,6 +45,9 @@ public static double OpacityForNotMerged public Thickness Margin { get; set; } = new Thickness(0); public IBrush Brush => CommitGraph.Pens[Color].Brush; + public int AddedLines { get; set; } = 0; + public int RemovedLines { get; set; } = 0; + public void ParseDecorators(string data) { if (data.Length < 3) diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 455c530d5..d2cc0b06b 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -637,9 +637,11 @@ private void Refresh() Task.Run(() => { - var lines = new Commands.QueryCommitChangedLines(_repo.FullPath, _commit.SHA).Result(); - Console.WriteLine(lines); - //Dispatcher.UIThread.Invoke(() => FullMessage = fullMessage); + (var addedLines, var removedLines) = new Commands.QueryCommitChangedLines(_repo.FullPath, _commit.SHA).Result(); + Dispatcher.UIThread.Invoke(() => { + _commit.AddedLines = addedLines; + _commit.RemovedLines = removedLines; + }); }); Task.Run(() => diff --git a/src/Views/CommitBaseInfo.axaml b/src/Views/CommitBaseInfo.axaml index 55342d183..b62ecc924 100644 --- a/src/Views/CommitBaseInfo.axaml +++ b/src/Views/CommitBaseInfo.axaml @@ -184,7 +184,26 @@ - + + + + + + + + + + + + + + + + + + + + diff --git a/src/Views/LinesChanged.axaml b/src/Views/LinesChanged.axaml deleted file mode 100644 index b8a32d00f..000000000 --- a/src/Views/LinesChanged.axaml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Views/LinesChanged.axaml.cs b/src/Views/LinesChanged.axaml.cs deleted file mode 100644 index aa2c8a605..000000000 --- a/src/Views/LinesChanged.axaml.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Avalonia; -using Avalonia.Controls; -using Avalonia.Markup.Xaml; - -namespace SourceGit.Views -{ - public partial class LinesChanged : UserControl - { - public static readonly DirectProperty AddedLinesProperty = - AvaloniaProperty.RegisterDirect(nameof(AddedLines), o => o.AddedLines); - - public static readonly DirectProperty RemovedLinesProperty = - AvaloniaProperty.RegisterDirect(nameof(RemovedLines), o => o.RemovedLines); - - private int _addedLines; - private int _removedLines; - - public int AddedLines - { - get => _addedLines; - set => SetAndRaise(AddedLinesProperty, ref _addedLines, value); - } - - public int RemovedLines - { - get => _removedLines; - set => SetAndRaise(RemovedLinesProperty, ref _removedLines, value); - } - - public LinesChanged() - { - InitializeComponent(); - this.DataContext = this; - } - - private void InitializeComponent() - { - AvaloniaXamlLoader.Load(this); - } - } -} From 11ad7f56d9a2ba99f079457b0d6ac5aa5c43f403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:43:20 +0200 Subject: [PATCH 05/11] Fix counter issue --- src/Commands/QueryCommitChangedLines.cs | 30 ++++++------- src/Models/Commit.cs | 41 +++++++++++++++-- src/ViewModels/CommitDetail.cs | 60 ++++++++++++++++++++----- src/Views/CommitBaseInfo.axaml.cs | 20 +++++++++ 4 files changed, 123 insertions(+), 28 deletions(-) diff --git a/src/Commands/QueryCommitChangedLines.cs b/src/Commands/QueryCommitChangedLines.cs index 018072ccf..75a75c243 100644 --- a/src/Commands/QueryCommitChangedLines.cs +++ b/src/Commands/QueryCommitChangedLines.cs @@ -8,38 +8,38 @@ public QueryCommitChangedLines(string repo, string sha) { WorkingDirectory = repo; Context = repo; - Args = $"show --numstat --oneline {sha}"; + // Use shortstat for faster results, which is enough for our needs + Args = $"show --shortstat --oneline {sha}"; + _pattern = new Regex(@"(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?"); } public (int, int) Result() { _addedLines = 0; _removedLines = 0; - _firstLine = true; Exec(); return (_addedLines, _removedLines); } protected override void OnReadline(string line) { - if (_firstLine) { - _firstLine = false; - return; - } - - var parts = Regex.Split(line, @"\s+"); - - if (parts.Length >= 2) + var match = _pattern.Match(line); + if (match.Success) { - bool canParseAdded = int.TryParse(parts[0], out int addedLines); - bool canParseRemoved = int.TryParse(parts[1], out int removedLines); - if (canParseAdded) _addedLines += addedLines; - if (canParseRemoved) _removedLines += removedLines; + if (match.Groups[2].Success) + { + _addedLines = int.Parse(match.Groups[2].Value); + } + + if (match.Groups[3].Success) + { + _removedLines = int.Parse(match.Groups[3].Value); + } } } + private readonly Regex _pattern; private int _addedLines; private int _removedLines; - private bool _firstLine; } } diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 3840f62e9..5f401a14b 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; using Avalonia; using Avalonia.Media; @@ -13,8 +15,15 @@ public enum CommitSearchMethod ByFile, } - public class Commit + public class Commit : INotifyPropertyChanged { + public event PropertyChangedEventHandler PropertyChanged; + + protected void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + public static double OpacityForNotMerged { get; @@ -45,8 +54,34 @@ public static double OpacityForNotMerged public Thickness Margin { get; set; } = new Thickness(0); public IBrush Brush => CommitGraph.Pens[Color].Brush; - public int AddedLines { get; set; } = 0; - public int RemovedLines { get; set; } = 0; + private int _addedLines = 0; + private int _removedLines = 0; + + public int AddedLines + { + get => _addedLines; + set + { + if (_addedLines != value) + { + _addedLines = value; + OnPropertyChanged(); + } + } + } + + public int RemovedLines + { + get => _removedLines; + set + { + if (_removedLines != value) + { + _removedLines = value; + OnPropertyChanged(); + } + } + } public void ParseDecorators(string data) { diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index d2cc0b06b..639b92102 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -581,7 +581,7 @@ public ContextMenu CreateRevisionFileContextMenu(Models.Object file) menu.Items.Add(resetToThisRevision); menu.Items.Add(resetToFirstParent); - menu.Items.Add(new MenuItem() { Header = "-" }); + menu.Items.Add(new MenuItem { Header = "-" }); if (File.Exists(Path.Combine(fullPath))) TryToAddContextMenuItemsForGitLFS(menu, file.Path); @@ -629,19 +629,39 @@ private void Refresh() if (_commit == null) return; - Task.Run(() => + // Load line count data immediately with higher priority + var sha = _commit.SHA; + + // Check if we already have the line count data cached + if (_lineCountCache.TryGetValue(sha, out var lineCount)) { - var fullMessage = new Commands.QueryCommitFullMessage(_repo.FullPath, _commit.SHA).Result(); - Dispatcher.UIThread.Invoke(() => FullMessage = fullMessage); - }); + // Update immediately in the UI thread + _commit.AddedLines = lineCount.added; + _commit.RemovedLines = lineCount.removed; + } + else + { + // Start loading line count data with high priority + Task.Run(() => + { + (var addedLines, var removedLines) = new Commands.QueryCommitChangedLines(_repo.FullPath, sha).Result(); + _lineCountCache[sha] = (addedLines, removedLines); + + Dispatcher.UIThread.Invoke(() => { + if (_commit != null && _commit.SHA == sha) + { + _commit.AddedLines = addedLines; + _commit.RemovedLines = removedLines; + } + }); + }); + } + // Continue with other loading tasks... Task.Run(() => { - (var addedLines, var removedLines) = new Commands.QueryCommitChangedLines(_repo.FullPath, _commit.SHA).Result(); - Dispatcher.UIThread.Invoke(() => { - _commit.AddedLines = addedLines; - _commit.RemovedLines = removedLines; - }); + var fullMessage = new Commands.QueryCommitFullMessage(_repo.FullPath, _commit.SHA).Result(); + Dispatcher.UIThread.Invoke(() => FullMessage = fullMessage); }); Task.Run(() => @@ -694,6 +714,25 @@ private void Refresh() }); } + // Add a method to preload line count data for visible commits + public void PreloadLineCountData(List commitSHAs) + { + if (commitSHAs == null || commitSHAs.Count == 0) + return; + + Task.Run(() => + { + foreach (var sha in commitSHAs) + { + if (!_lineCountCache.ContainsKey(sha)) + { + var (addedLines, removedLines) = new Commands.QueryCommitChangedLines(_repo.FullPath, sha).Result(); + _lineCountCache[sha] = (addedLines, removedLines); + } + } + }); + } + private void RefreshVisibleChanges() { if (_changes == null) @@ -841,5 +880,6 @@ private void UpdateRevisionFileSearchSuggestion() private List _revisionFiles = []; private string _revisionFileSearchFilter = string.Empty; private bool _isRevisionFileSearchSuggestionOpen = false; + private Dictionary _lineCountCache = new Dictionary(); } } diff --git a/src/Views/CommitBaseInfo.axaml.cs b/src/Views/CommitBaseInfo.axaml.cs index ce1a7cfd0..ea0c09d40 100644 --- a/src/Views/CommitBaseInfo.axaml.cs +++ b/src/Views/CommitBaseInfo.axaml.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using Avalonia; @@ -70,6 +71,25 @@ public CommitBaseInfo() InitializeComponent(); } + protected override void OnDataContextChanged(EventArgs e) + { + base.OnDataContextChanged(e); + + // When the DataContext changes, we need to re-evaluate any bindings + // This ensures that when the Commit property changes, the UI is updated + if (DataContext is ViewModels.CommitDetail detail) + { + detail.PropertyChanged += (s, e) => + { + if (e.PropertyName == nameof(detail.Commit)) + { + // Force UI update for the commit lines + InvalidateVisual(); + } + }; + } + } + private void OnCopyCommitSHA(object sender, RoutedEventArgs e) { if (sender is Button { DataContext: Models.Commit commit }) From f8faf1ba2617486b7558e7685c505fdd1935e04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:52:01 +0200 Subject: [PATCH 06/11] Clean --- src/Commands/QueryCommitChangedLines.cs | 1 - src/Views/CommitBaseInfo.axaml.cs | 3 --- 2 files changed, 4 deletions(-) diff --git a/src/Commands/QueryCommitChangedLines.cs b/src/Commands/QueryCommitChangedLines.cs index 75a75c243..81786a665 100644 --- a/src/Commands/QueryCommitChangedLines.cs +++ b/src/Commands/QueryCommitChangedLines.cs @@ -8,7 +8,6 @@ public QueryCommitChangedLines(string repo, string sha) { WorkingDirectory = repo; Context = repo; - // Use shortstat for faster results, which is enough for our needs Args = $"show --shortstat --oneline {sha}"; _pattern = new Regex(@"(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?"); } diff --git a/src/Views/CommitBaseInfo.axaml.cs b/src/Views/CommitBaseInfo.axaml.cs index 58e10cbb5..5e20e04c0 100644 --- a/src/Views/CommitBaseInfo.axaml.cs +++ b/src/Views/CommitBaseInfo.axaml.cs @@ -66,15 +66,12 @@ protected override void OnDataContextChanged(EventArgs e) { base.OnDataContextChanged(e); - // When the DataContext changes, we need to re-evaluate any bindings - // This ensures that when the Commit property changes, the UI is updated if (DataContext is ViewModels.CommitDetail detail) { detail.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(detail.Commit)) { - // Force UI update for the commit lines InvalidateVisual(); } }; From 99267b1ba7ffdf3a107127c9fe5c3d9165b29409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:57:19 +0200 Subject: [PATCH 07/11] Fix merge --- src/ViewModels/CommitDetail.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 3a7b78aa9..2d7581334 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -919,11 +919,7 @@ private void CalcRevisionFileSearchSuggestion() private CancellationTokenSource _cancellationSource = null; private List _revisionFiles = null; private string _revisionFileSearchFilter = string.Empty; -<<<<<<< HEAD - private bool _isRevisionFileSearchSuggestionOpen = false; private Dictionary _lineCountCache = new Dictionary(); -======= private List _revisionFileSearchSuggestion = null; ->>>>>>> develop } } From f1fcdb89f3c3cda4cb5b01b85956cb2899cc181b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Fri, 11 Apr 2025 17:07:25 +0200 Subject: [PATCH 08/11] Clean PR --- src/ViewModels/CommitDetail.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 2d7581334..3a2da6028 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -614,7 +614,8 @@ private void Refresh() }); } - if (_cancellationSource is { IsCancellationRequested: false }) _cancellationSource.Cancel(); + if (_cancellationSource is { IsCancellationRequested: false }) + _cancellationSource.Cancel(); _cancellationSource = new CancellationTokenSource(); var token = _cancellationSource.Token; From 2cac661676cf235389861bda44f2c99622c17075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Sun, 13 Apr 2025 16:10:20 +0200 Subject: [PATCH 09/11] Split line counters in a new view --- src/Views/CommitBaseInfo.axaml | 23 ++--------------------- src/Views/LinesChanged.axaml | 28 ++++++++++++++++++++++++++++ src/Views/LinesChanged.axaml.cs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 src/Views/LinesChanged.axaml create mode 100644 src/Views/LinesChanged.axaml.cs diff --git a/src/Views/CommitBaseInfo.axaml b/src/Views/CommitBaseInfo.axaml index 6162556e2..b73e6e8d0 100644 --- a/src/Views/CommitBaseInfo.axaml +++ b/src/Views/CommitBaseInfo.axaml @@ -183,27 +183,8 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Views/LinesChanged.axaml.cs b/src/Views/LinesChanged.axaml.cs new file mode 100644 index 000000000..28576ab09 --- /dev/null +++ b/src/Views/LinesChanged.axaml.cs @@ -0,0 +1,31 @@ +using Avalonia; +using Avalonia.Controls; + +namespace SourceGit.Views +{ + public partial class LinesChanged : UserControl + { + public static readonly StyledProperty AddedCountProperty = + AvaloniaProperty.Register(nameof(AddedCount)); + + public static readonly StyledProperty RemovedCountProperty = + AvaloniaProperty.Register(nameof(RemovedCount)); + + public int AddedCount + { + get => GetValue(AddedCountProperty); + set => SetValue(AddedCountProperty, value); + } + + public int RemovedCount + { + get => GetValue(RemovedCountProperty); + set => SetValue(RemovedCountProperty, value); + } + + public LinesChanged() + { + InitializeComponent(); + } + } +} From 29a4babf4c8d8f5717b34b2e305a01b15080cc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Borr=C3=A0s=20Civil?= <70479573+BernatBC@users.noreply.github.com> Date: Sun, 13 Apr 2025 17:01:37 +0200 Subject: [PATCH 10/11] Base --- src/Commands/QueryFileChangedLines.cs | 57 +++++++++++++++++++++++++++ src/ViewModels/DiffContext.cs | 53 ++++++++++++++++++++++++- src/Views/DiffView.axaml | 2 + 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/Commands/QueryFileChangedLines.cs diff --git a/src/Commands/QueryFileChangedLines.cs b/src/Commands/QueryFileChangedLines.cs new file mode 100644 index 000000000..de6ee40b0 --- /dev/null +++ b/src/Commands/QueryFileChangedLines.cs @@ -0,0 +1,57 @@ +using System.Text.RegularExpressions; + +namespace SourceGit.Commands +{ + public class QueryFileChangedLines : Command + { + public QueryFileChangedLines(string repo, string revision1, string revision2, string filePath) + { + WorkingDirectory = repo; + Context = repo; + + if (string.IsNullOrEmpty(revision1) && string.IsNullOrEmpty(revision2)) + { + // Working copy changes (unstaged) + Args = $"diff --numstat -- \"{filePath}\""; + } + else if (string.IsNullOrEmpty(revision1) && revision2 == "--staged") + { + // Staged changes + Args = $"diff --cached --numstat -- \"{filePath}\""; + } + else + { + // Comparing two revisions + Args = $"diff --numstat {revision1} {revision2} -- \"{filePath}\""; + } + } + + public (int, int) Result() + { + _addedLines = 0; + _removedLines = 0; + Exec(); + return (_addedLines, _removedLines); + } + + protected override void OnReadline(string line) + { + var parts = line.Split('\t'); + if (parts.Length >= 2) + { + if (int.TryParse(parts[0], out int added)) + { + _addedLines = added; + } + + if (int.TryParse(parts[1], out int removed)) + { + _removedLines = removed; + } + } + } + + private int _addedLines; + private int _removedLines; + } +} diff --git a/src/ViewModels/DiffContext.cs b/src/ViewModels/DiffContext.cs index 6dd836bf3..53308f314 100644 --- a/src/ViewModels/DiffContext.cs +++ b/src/ViewModels/DiffContext.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; using System.Threading.Tasks; using Avalonia.Media.Imaging; @@ -8,9 +9,11 @@ using CommunityToolkit.Mvvm.ComponentModel; +using SourceGit.Models; + namespace SourceGit.ViewModels { - public class DiffContext : ObservableObject + public partial class DiffContext : ObservableObject { public string Title { @@ -51,6 +54,12 @@ public int UnifiedLines private set => SetProperty(ref _unifiedLines, value); } + [ObservableProperty] + private int _addedLines; + + [ObservableProperty] + private int _removedLines; + public DiffContext(string repo, Models.DiffOption option, DiffContext previous = null) { _repo = repo; @@ -71,6 +80,48 @@ public DiffContext(string repo, Models.DiffOption option, DiffContext previous = else _title = $"{_option.OrgPath} → {_option.Path}"; + AddedLines = 0; + RemovedLines = 0; + + Task.Run(() => + { + string oldRevision = ""; + string newRevision = ""; + string filePath = option.Path; + + if (option.Revisions.Count == 2) + { + oldRevision = option.Revisions[0]; + newRevision = option.Revisions[1]; + + if (string.IsNullOrEmpty(oldRevision) || string.IsNullOrEmpty(newRevision)) + { + var result = new Commands.QueryFileChangedLines(repo, oldRevision, newRevision, filePath).Result(); + + Dispatcher.UIThread.Invoke(() => + { + AddedLines = result.Item1; + RemovedLines = result.Item2; + }); + return; + } + } + + if (option.Revisions.Count == 1 && option.Revisions[0] == "STAGE") + { + oldRevision = "HEAD"; + newRevision = "--staged"; + } + + var lineChanges = new Commands.QueryFileChangedLines(repo, oldRevision, newRevision, filePath).Result(); + + Dispatcher.UIThread.Invoke(() => + { + AddedLines = lineChanges.Item1; + RemovedLines = lineChanges.Item2; + }); + }); + LoadDiffContent(); } diff --git a/src/Views/DiffView.axaml b/src/Views/DiffView.axaml index c4da158db..4442c81c2 100644 --- a/src/Views/DiffView.axaml +++ b/src/Views/DiffView.axaml @@ -34,6 +34,8 @@ + +