feature: update blame data when clicking/navigating commits (#1408)

* initial work on allowing navigation by clicking on commits in the blame window
* cleanup
This commit is contained in:
johanw1232 2025-06-10 03:30:12 +02:00 committed by GitHub
parent 11a46dbc93
commit 0ea4021a64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 108 additions and 9 deletions

View file

@ -12,8 +12,8 @@ namespace SourceGit.ViewModels
{
public string Title
{
get;
private set;
get => _title;
private set => SetProperty(ref _title, value);
}
public bool IsBinary
@ -21,6 +21,15 @@ namespace SourceGit.ViewModels
get => _data != null && _data.IsBinary;
}
public bool CanMoveBack
{
get => _shaHistoryIndex > 0 && _shaHistory.Count > 1;
}
public bool CanMoveForward
{
get => _shaHistoryIndex < _shaHistory.Count - 1;
}
public Models.BlameData Data
{
get => _data;
@ -30,20 +39,60 @@ namespace SourceGit.ViewModels
public Blame(string repo, string file, string revision)
{
_repo = repo;
_file = file;
SetBlameData($"{revision.AsSpan(0, 10)}", true);
}
private void SetBlameData(string commitSHA, bool resetHistoryForward)
{
Title = $"{_file} @ {commitSHA}";
Title = $"{file} @ {revision.AsSpan(0, 10)}";
Task.Run(() =>
{
var result = new Commands.Blame(repo, file, revision).Result();
var result = new Commands.Blame(_repo, _file, commitSHA).Result();
Dispatcher.UIThread.Invoke(() =>
{
Data = result;
OnPropertyChanged(nameof(IsBinary));
});
});
if (resetHistoryForward)
{
if (_shaHistoryIndex < _shaHistory.Count - 1)
_shaHistory.RemoveRange(_shaHistoryIndex + 1, _shaHistory.Count - _shaHistoryIndex - 1);
if (_shaHistory.Count == 0 || _shaHistory[_shaHistoryIndex] != commitSHA)
{
_shaHistory.Add(commitSHA);
_shaHistoryIndex = _shaHistory.Count - 1;
}
}
OnPropertyChanged(nameof(CanMoveBack));
OnPropertyChanged(nameof(CanMoveForward));
}
public void NavigateToCommit(string commitSHA)
public void Back()
{
--_shaHistoryIndex;
if (_shaHistoryIndex < 0)
_shaHistoryIndex = 0;
NavigateToCommit(_shaHistory[_shaHistoryIndex], false);
}
public void Forward()
{
++_shaHistoryIndex;
if (_shaHistoryIndex >= _shaHistory.Count)
_shaHistoryIndex = _shaHistory.Count - 1;
NavigateToCommit(_shaHistory[_shaHistoryIndex], false);
}
public void NavigateToCommit(string commitSHA, bool resetHistoryForward)
{
var launcher = App.GetLauncher();
if (launcher == null)
@ -54,6 +103,7 @@ namespace SourceGit.ViewModels
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
{
repo.NavigateToCommit(commitSHA);
SetBlameData(commitSHA, resetHistoryForward);
break;
}
}
@ -69,7 +119,11 @@ namespace SourceGit.ViewModels
return msg;
}
private readonly string _repo;
private string _repo;
private string _file;
private string _title;
private int _shaHistoryIndex = 0;
private List<string> _shaHistory = [];
private Models.BlameData _data = null;
private Dictionary<string, string> _commitMessages = new Dictionary<string, string>();
}

View file

@ -42,8 +42,24 @@
</Grid>
<!-- File -->
<Border Grid.Row="1" Padding="8,0">
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
<Border Grid.Row="1" Padding="8,0" >
<Grid>
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal">
<Button Classes="icon_button"
IsEnabled="{Binding CanMoveBack}"
Width="28"
Click="HistoryBack">
<Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.TriangleLeft}"/>
</Button>
<Button Classes="icon_button"
IsEnabled="{Binding CanMoveForward}"
Width="28"
Click="HistoryForward">
<Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.TriangleRight}"/>
</Button>
</StackPanel>
</Grid>
</Border>
<!-- Body -->

View file

@ -225,7 +225,7 @@ namespace SourceGit.Views
{
if (DataContext is ViewModels.Blame blame)
{
blame.NavigateToCommit(info.CommitSHA);
blame.NavigateToCommit(info.CommitSHA, true);
}
e.Handled = true;
@ -433,6 +433,8 @@ namespace SourceGit.Views
public Blame()
{
InitializeComponent();
AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true);
}
protected override void OnClosed(EventArgs e)
@ -440,5 +442,32 @@ namespace SourceGit.Views
base.OnClosed(e);
GC.Collect();
}
private void HistoryBack(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.Blame blame)
{
blame.Back();
}
}
private void HistoryForward(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.Blame blame)
{
blame.Forward();
}
}
private void MouseUpHandler(object sender, PointerReleasedEventArgs e)
{
if (e.InitialPressMouseButton == MouseButton.XButton1)
{
HistoryBack(null, null);
}
else if (e.InitialPressMouseButton == MouseButton.XButton2)
{
HistoryForward(null, null);
}
}
}
}