This commit is contained in:
Bernat Borràs Civil 2025-04-13 17:01:37 +02:00
parent 2cac661676
commit 29a4babf4c
3 changed files with 111 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -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 Avalonia.Threading;
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 @@ namespace SourceGit.ViewModels
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 @@ namespace SourceGit.ViewModels
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();
}

View file

@ -34,6 +34,8 @@
<!-- Toolbar Buttons -->
<StackPanel Grid.Column="3" Margin="8,0,0,0" Orientation="Horizontal" VerticalAlignment="Center">
<v:LinesChanged Grid.Row="4" Grid.Column="1" AddedCount="{Binding AddedLines}" RemovedCount="{Binding RemovedLines}" />
<Button Classes="icon_button"
Width="28"
Click="OnGotoFirstChange"