refactor: rewrite submodule to support IsDirty state (#339)

This commit is contained in:
leo 2024-08-08 21:11:10 +08:00
parent eb441852b0
commit 1fe2be11a7
No known key found for this signature in database
10 changed files with 110 additions and 19 deletions

View file

@ -17,9 +17,10 @@ namespace SourceGit.Commands
Args = "submodule status"; Args = "submodule status";
} }
public List<string> Result() public List<Models.Submodule> Result()
{ {
Exec(); Exec();
new UpdateSubmoduleStatus(WorkingDirectory, _submodules).Result();
return _submodules; return _submodules;
} }
@ -28,17 +29,17 @@ namespace SourceGit.Commands
var match = REG_FORMAT1().Match(line); var match = REG_FORMAT1().Match(line);
if (match.Success) if (match.Success)
{ {
_submodules.Add(match.Groups[1].Value); _submodules.Add(new Models.Submodule() { Path = match.Groups[1].Value });
return; return;
} }
match = REG_FORMAT2().Match(line); match = REG_FORMAT2().Match(line);
if (match.Success) if (match.Success)
{ {
_submodules.Add(match.Groups[1].Value); _submodules.Add(new Models.Submodule() { Path = match.Groups[1].Value });
} }
} }
private readonly List<string> _submodules = new List<string>(); private readonly List<Models.Submodule> _submodules = new List<Models.Submodule>();
} }
} }

View file

@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace SourceGit.Commands
{
public partial class UpdateSubmoduleStatus : Command
{
[GeneratedRegex(@"^\s?[\w\?]{1,4}\s+(.+)$")]
private static partial Regex REG_FORMAT();
public UpdateSubmoduleStatus(string repo, List<Models.Submodule> submodules)
{
var pathes = new StringBuilder();
foreach (var submodule in submodules)
pathes.Append($"\"{submodule.Path}\" ");
_submodules = submodules;
WorkingDirectory = repo;
Context = repo;
Args = $"status -uno --porcelain -- {pathes}";
}
public void Result()
{
Exec();
foreach (var submodule in _submodules)
submodule.IsDirty = _changed.Contains(submodule.Path);
}
protected override void OnReadline(string line)
{
var match = REG_FORMAT().Match(line);
if (match.Success)
_changed.Add(match.Groups[1].Value);
}
private List<Models.Submodule> _submodules = null;
private HashSet<string> _changed = new HashSet<string>();
}
}

View file

@ -1,8 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.Models namespace SourceGit.Models

8
src/Models/Submodule.cs Normal file
View file

@ -0,0 +1,8 @@
namespace SourceGit.Models
{
public class Submodule
{
public string Path { get; set; } = "";
public bool IsDirty { get; set; } = false;
}
}

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -70,6 +71,16 @@ namespace SourceGit.Models
} }
} }
public void UpdateSubmodules(List<Submodule> submodules)
{
lock (_lockSubmodule)
{
_submodules.Clear();
foreach (var submodule in submodules)
_submodules.Add(submodule.Path);
}
}
public void MarkBranchDirtyManually() public void MarkBranchDirtyManually()
{ {
_updateBranch = DateTime.Now.ToFileTime() - 1; _updateBranch = DateTime.Now.ToFileTime() - 1;
@ -168,7 +179,7 @@ namespace SourceGit.Models
return; return;
var name = e.Name.Replace("\\", "/"); var name = e.Name.Replace("\\", "/");
if (name.StartsWith("modules", StringComparison.Ordinal)) if (name.StartsWith("modules", StringComparison.Ordinal) && name.EndsWith("HEAD", StringComparison.Ordinal))
{ {
_updateSubmodules = DateTime.Now.AddSeconds(1).ToFileTime(); _updateSubmodules = DateTime.Now.AddSeconds(1).ToFileTime();
} }
@ -201,6 +212,19 @@ namespace SourceGit.Models
var name = e.Name.Replace("\\", "/"); var name = e.Name.Replace("\\", "/");
if (name == ".git" || name.StartsWith(".git/", StringComparison.Ordinal)) if (name == ".git" || name.StartsWith(".git/", StringComparison.Ordinal))
return; return;
lock (_submodules)
{
foreach (var submodule in _submodules)
{
if (name.StartsWith(submodule, StringComparison.Ordinal))
{
_updateSubmodules = DateTime.Now.AddSeconds(1).ToFileTime();
return;
}
}
}
_updateWC = DateTime.Now.AddSeconds(1).ToFileTime(); _updateWC = DateTime.Now.AddSeconds(1).ToFileTime();
} }
@ -214,5 +238,8 @@ namespace SourceGit.Models
private long _updateSubmodules = 0; private long _updateSubmodules = 0;
private long _updateStashes = 0; private long _updateStashes = 0;
private long _updateTags = 0; private long _updateTags = 0;
private object _lockSubmodule = new object();
private List<string> _submodules = new List<string>();
} }
} }

View file

@ -68,6 +68,7 @@
<StreamGeometry x:Key="Icons.MacOS.Maximize">M0 4 0 20 16 20 0 4M4 0 20 0 20 16 4 0z</StreamGeometry> <StreamGeometry x:Key="Icons.MacOS.Maximize">M0 4 0 20 16 20 0 4M4 0 20 0 20 16 4 0z</StreamGeometry>
<StreamGeometry x:Key="Icons.Menu">M192 192m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM192 512m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM192 832m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM864 160H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 480H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 800H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32z</StreamGeometry> <StreamGeometry x:Key="Icons.Menu">M192 192m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM192 512m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM192 832m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM864 160H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 480H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 800H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32z</StreamGeometry>
<StreamGeometry x:Key="Icons.Merge">M824 645V307c0-56-46-102-102-102h-102V102l-154 154 154 154V307h102v338c-46 20-82 67-82 123 0 72 61 133 133 133 72 0 133-61 133-133 0-56-36-102-82-123zm-51 195c-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72s-31 72-72 72zM384 256c0-72-61-133-133-133-72 0-133 61-133 133 0 56 36 102 82 123v266C154 666 118 712 118 768c0 72 61 133 133 133 72 0 133-61 133-133 0-56-36-102-82-123V379C348 358 384 312 384 256zM323 768c0 41-31 72-72 72-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72zM251 328c-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72s-31 72-72 72z</StreamGeometry> <StreamGeometry x:Key="Icons.Merge">M824 645V307c0-56-46-102-102-102h-102V102l-154 154 154 154V307h102v338c-46 20-82 67-82 123 0 72 61 133 133 133 72 0 133-61 133-133 0-56-36-102-82-123zm-51 195c-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72s-31 72-72 72zM384 256c0-72-61-133-133-133-72 0-133 61-133 133 0 56 36 102 82 123v266C154 666 118 712 118 768c0 72 61 133 133 133 72 0 133-61 133-133 0-56-36-102-82-123V379C348 358 384 312 384 256zM323 768c0 41-31 72-72 72-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72zM251 328c-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72s-31 72-72 72z</StreamGeometry>
<StreamGeometry x:Key="Icons.Modified">M896 64H128C96 64 64 96 64 128v768c0 32 32 64 64 64h768c32 0 64-32 64-64V128c0-32-32-64-64-64z m-64 736c0 16-17 32-32 32H224c-18 0-32-12-32-32V224c0-16 16-32 32-32h576c15 0 32 16 32 32v576zM512 384c-71 0-128 57-128 128s57 128 128 128 128-57 128-128-57-128-128-128z</StreamGeometry>
<StreamGeometry x:Key="Icons.Move">M299 811 299 725 384 725 384 811 299 811M469 811 469 725 555 725 555 811 469 811M640 811 640 725 725 725 725 811 640 811M299 640 299 555 384 555 384 640 299 640M469 640 469 555 555 555 555 640 469 640M640 640 640 555 725 555 725 640 640 640M299 469 299 384 384 384 384 469 299 469M469 469 469 384 555 384 555 469 469 469M640 469 640 384 725 384 725 469 640 469M299 299 299 213 384 213 384 299 299 299M469 299 469 213 555 213 555 299 469 299M640 299 640 213 725 213 725 299 640 299Z</StreamGeometry> <StreamGeometry x:Key="Icons.Move">M299 811 299 725 384 725 384 811 299 811M469 811 469 725 555 725 555 811 469 811M640 811 640 725 725 725 725 811 640 811M299 640 299 555 384 555 384 640 299 640M469 640 469 555 555 555 555 640 469 640M640 640 640 555 725 555 725 640 640 640M299 469 299 384 384 384 384 469 299 469M469 469 469 384 555 384 555 469 469 469M640 469 640 384 725 384 725 469 640 469M299 299 299 213 384 213 384 299 299 299M469 299 469 213 555 213 555 299 469 299M640 299 640 213 725 213 725 299 640 299Z</StreamGeometry>
<StreamGeometry x:Key="Icons.OpenWith">M683 409v204L1024 308 683 0v191c-413 0-427 526-427 526c117-229 203-307 427-307zm85 492H102V327h153s38-63 114-122H51c-28 0-51 27-51 61v697c0 34 23 61 51 61h768c28 0 51-27 51-61V614l-102 100v187z</StreamGeometry> <StreamGeometry x:Key="Icons.OpenWith">M683 409v204L1024 308 683 0v191c-413 0-427 526-427 526c117-229 203-307 427-307zm85 492H102V327h153s38-63 114-122H51c-28 0-51 27-51 61v697c0 34 23 61 51 61h768c28 0 51-27 51-61V614l-102 100v187z</StreamGeometry>
<StreamGeometry x:Key="Icons.Paste">M544 85c49 0 90 37 95 85h75a96 96 0 0196 89L811 267a32 32 0 01-28 32L779 299a32 32 0 01-32-28L747 267a32 32 0 00-28-32L715 235h-91a96 96 0 01-80 42H395c-33 0-62-17-80-42L224 235a32 32 0 00-32 28L192 267v576c0 16 12 30 28 32l4 0h128a32 32 0 0132 28l0 4a32 32 0 01-32 32h-128a96 96 0 01-96-89L128 843V267a96 96 0 0189-96L224 171h75a96 96 0 0195-85h150zm256 256a96 96 0 0196 89l0 7v405a96 96 0 01-89 96L800 939h-277a96 96 0 01-96-89L427 843v-405a96 96 0 0189-96L523 341h277zm-256-192H395a32 32 0 000 64h150a32 32 0 100-64z</StreamGeometry> <StreamGeometry x:Key="Icons.Paste">M544 85c49 0 90 37 95 85h75a96 96 0 0196 89L811 267a32 32 0 01-28 32L779 299a32 32 0 01-32-28L747 267a32 32 0 00-28-32L715 235h-91a96 96 0 01-80 42H395c-33 0-62-17-80-42L224 235a32 32 0 00-32 28L192 267v576c0 16 12 30 28 32l4 0h128a32 32 0 0132 28l0 4a32 32 0 01-32 32h-128a96 96 0 01-96-89L128 843V267a96 96 0 0189-96L224 171h75a96 96 0 0195-85h150zm256 256a96 96 0 0196 89l0 7v405a96 96 0 01-89 96L800 939h-277a96 96 0 01-96-89L427 843v-405a96 96 0 0189-96L523 341h277zm-256-192H395a32 32 0 000 64h150a32 32 0 100-64z</StreamGeometry>

View file

@ -136,7 +136,7 @@ namespace SourceGit.ViewModels
private set => SetProperty(ref _visibleTags, value); private set => SetProperty(ref _visibleTags, value);
} }
public List<string> Submodules public List<Models.Submodule> Submodules
{ {
get => _submodules; get => _submodules;
private set => SetProperty(ref _submodules, value); private set => SetProperty(ref _submodules, value);
@ -778,6 +778,9 @@ namespace SourceGit.ViewModels
public void RefreshSubmodules() public void RefreshSubmodules()
{ {
var submodules = new Commands.QuerySubmodules(_fullpath).Result(); var submodules = new Commands.QuerySubmodules(_fullpath).Result();
if (_watcher != null)
_watcher.UpdateSubmodules(submodules);
Dispatcher.UIThread.Invoke(() => Submodules = submodules); Dispatcher.UIThread.Invoke(() => Submodules = submodules);
} }
@ -1992,7 +1995,7 @@ namespace SourceGit.ViewModels
private List<Models.Worktree> _worktrees = new List<Models.Worktree>(); private List<Models.Worktree> _worktrees = new List<Models.Worktree>();
private List<Models.Tag> _tags = new List<Models.Tag>(); private List<Models.Tag> _tags = new List<Models.Tag>();
private List<Models.Tag> _visibleTags = new List<Models.Tag>(); private List<Models.Tag> _visibleTags = new List<Models.Tag>();
private List<string> _submodules = new List<string>(); private List<Models.Submodule> _submodules = new List<Models.Submodule>();
private bool _includeUntracked = true; private bool _includeUntracked = true;
private InProgressContext _inProgressContext = null; private InProgressContext _inProgressContext = null;

View file

@ -7,8 +7,9 @@ namespace SourceGit.ViewModels
{ {
public List<string> Submodules public List<string> Submodules
{ {
get => _repo.Submodules; get;
} private set;
} = new List<string>();
public string SelectedSubmodule public string SelectedSubmodule
{ {
@ -43,7 +44,11 @@ namespace SourceGit.ViewModels
public UpdateSubmodules(Repository repo) public UpdateSubmodules(Repository repo)
{ {
_repo = repo; _repo = repo;
SelectedSubmodule = repo.Submodules.Count > 0 ? repo.Submodules[0] : string.Empty;
foreach (var submodule in _repo.Submodules)
Submodules.Add(submodule.Path);
SelectedSubmodule = Submodules.Count > 0 ? Submodules[0] : string.Empty;
View = new Views.UpdateSubmodules() { DataContext = this }; View = new Views.UpdateSubmodules() { DataContext = this };
} }

View file

@ -349,9 +349,14 @@
<DataGridTemplateColumn Width="*" Header="NAME"> <DataGridTemplateColumn Width="*" Header="NAME">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<Border Padding="0,0,4,0"> <Grid Background="Transparent" Margin="0,0,4,0" ColumnDefinitions="*,8">
<TextBlock Text="{Binding}" ClipToBounds="True" Classes="primary" TextTrimming="CharacterEllipsis"/> <TextBlock Grid.Column="0" Text="{Binding Path}" ClipToBounds="True" Classes="primary" TextTrimming="CharacterEllipsis"/>
</Border> <Path Grid.Column="1"
Width="8" Height="8"
Fill="Goldenrod"
Data="{StaticResource Icons.Modified}"
IsVisible="{Binding IsDirty}"/>
</Grid>
</DataTemplate> </DataTemplate>
</DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> </DataGridTemplateColumn>

View file

@ -123,9 +123,9 @@ namespace SourceGit.Views
private void OnSubmoduleContextRequested(object sender, ContextRequestedEventArgs e) private void OnSubmoduleContextRequested(object sender, ContextRequestedEventArgs e)
{ {
if (sender is DataGrid { SelectedItem: string submodule } grid && DataContext is ViewModels.Repository repo) if (sender is DataGrid { SelectedItem: Models.Submodule submodule } grid && DataContext is ViewModels.Repository repo)
{ {
var menu = repo.CreateContextMenuForSubmodule(submodule); var menu = repo.CreateContextMenuForSubmodule(submodule.Path);
grid.OpenContextMenu(menu); grid.OpenContextMenu(menu);
} }
@ -134,9 +134,9 @@ namespace SourceGit.Views
private void OnDoubleTappedSubmodule(object sender, TappedEventArgs e) private void OnDoubleTappedSubmodule(object sender, TappedEventArgs e)
{ {
if (sender is DataGrid { SelectedItem: string submodule } && DataContext is ViewModels.Repository repo) if (sender is DataGrid { SelectedItem: Models.Submodule submodule } && DataContext is ViewModels.Repository repo)
{ {
repo.OpenSubmodule(submodule); repo.OpenSubmodule(submodule.Path);
} }
e.Handled = true; e.Handled = true;