refactor: combine Commands.UpdateSubmoduleStatus into Commands.QuerySubmodules

This commit is contained in:
leo 2024-08-09 09:41:24 +08:00
parent 3969111393
commit 71d2b295da
No known key found for this signature in database
2 changed files with 49 additions and 59 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace SourceGit.Commands namespace SourceGit.Commands
@ -9,6 +10,8 @@ namespace SourceGit.Commands
private static partial Regex REG_FORMAT1(); private static partial Regex REG_FORMAT1();
[GeneratedRegex(@"^[\-\+ ][0-9a-f]+\s(.*)$")] [GeneratedRegex(@"^[\-\+ ][0-9a-f]+\s(.*)$")]
private static partial Regex REG_FORMAT2(); private static partial Regex REG_FORMAT2();
[GeneratedRegex(@"^\s?[\w\?]{1,4}\s+(.+)$")]
private static partial Regex REG_FORMAT_STATUS();
public QuerySubmodules(string repo) public QuerySubmodules(string repo)
{ {
@ -19,27 +22,57 @@ namespace SourceGit.Commands
public List<Models.Submodule> Result() public List<Models.Submodule> Result()
{ {
Exec(); var submodules = new List<Models.Submodule>();
new UpdateSubmoduleStatus(WorkingDirectory, _submodules).Result(); var rs = ReadToEnd();
return _submodules; if (!rs.IsSuccess)
} return submodules;
protected override void OnReadline(string line) var builder = new StringBuilder();
var lines = rs.StdOut.Split('\n', System.StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{ {
var match = REG_FORMAT1().Match(line); var match = REG_FORMAT1().Match(line);
if (match.Success) if (match.Success)
{ {
_submodules.Add(new Models.Submodule() { Path = match.Groups[1].Value }); var path = match.Groups[1].Value;
return; builder.Append($"\"{path}\" ");
submodules.Add(new Models.Submodule() { Path = path });
continue;
} }
match = REG_FORMAT2().Match(line); match = REG_FORMAT2().Match(line);
if (match.Success) if (match.Success)
{ {
_submodules.Add(new Models.Submodule() { Path = match.Groups[1].Value }); var path = match.Groups[1].Value;
builder.Append($"\"{path}\" ");
submodules.Add(new Models.Submodule() { Path = path });
} }
} }
private readonly List<Models.Submodule> _submodules = new List<Models.Submodule>(); if (submodules.Count > 0)
{
Args = $"status -uno --porcelain -- {builder}";
rs = ReadToEnd();
if (!rs.IsSuccess)
return submodules;
var dirty = new HashSet<string>();
lines = rs.StdOut.Split('\n', System.StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var match = REG_FORMAT_STATUS().Match(line);
if (match.Success)
{
var path = match.Groups[1].Value;
dirty.Add(path);
}
}
foreach (var submodule in submodules)
submodule.IsDirty = dirty.Contains(submodule.Path);
}
return submodules;
}
} }
} }

View file

@ -1,43 +0,0 @@
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>();
}
}