Fix command not working in some cases

This commit is contained in:
Bernat Borràs Civil 2025-04-13 17:12:36 +02:00
parent 29a4babf4c
commit 6a73ecc879
2 changed files with 68 additions and 4 deletions

View file

@ -1,4 +1,4 @@
using System.Text.RegularExpressions;
using System.IO;
namespace SourceGit.Commands
{
@ -8,16 +8,27 @@ namespace SourceGit.Commands
{
WorkingDirectory = repo;
Context = repo;
_repo = repo;
_filePath = filePath;
// Handle various diff scenarios
if (string.IsNullOrEmpty(revision1) && string.IsNullOrEmpty(revision2))
{
// Working copy changes (unstaged)
Args = $"diff --numstat -- \"{filePath}\"";
_checkNewWorkingDirFile = true;
}
else if (string.IsNullOrEmpty(revision1) && revision2 == "--staged")
{
// Staged changes
Args = $"diff --cached --numstat -- \"{filePath}\"";
_checkNewStagedFile = true;
}
else if (string.IsNullOrEmpty(revision1) || revision1 == "/dev/null")
{
// New file case - we'll count lines manually
_isNewFile = true;
_newRevision = revision2;
}
else
{
@ -30,6 +41,56 @@ namespace SourceGit.Commands
{
_addedLines = 0;
_removedLines = 0;
// Check for new files first
if (_isNewFile || _checkNewWorkingDirFile || _checkNewStagedFile)
{
int lineCount = 0;
if (_isNewFile && !string.IsNullOrEmpty(_newRevision))
{
var stream = QueryFileContent.Run(_repo, _newRevision, _filePath);
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
lineCount++;
}
}
}
else
{
var fullPath = Path.Combine(_repo, _filePath);
if (File.Exists(fullPath))
{
if (_checkNewWorkingDirFile || _checkNewStagedFile)
{
Exec();
if (_addedLines == 0 && _removedLines == 0)
{
var lines = File.ReadAllLines(fullPath);
lineCount = lines.Length;
}
else
{
return (_addedLines, _removedLines);
}
}
else
{
var lines = File.ReadAllLines(fullPath);
lineCount = lines.Length;
}
}
}
if (lineCount > 0)
{
return (lineCount, 0);
}
}
Exec();
return (_addedLines, _removedLines);
}
@ -51,6 +112,12 @@ namespace SourceGit.Commands
}
}
private readonly string _repo;
private readonly string _filePath;
private readonly bool _isNewFile = false;
private readonly string _newRevision = null;
private readonly bool _checkNewWorkingDirFile = false;
private readonly bool _checkNewStagedFile = false;
private int _addedLines;
private int _removedLines;
}

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
@ -9,8 +8,6 @@ using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using SourceGit.Models;
namespace SourceGit.ViewModels
{
public partial class DiffContext : ObservableObject