From 71d2b295da57f9da90a04fcb34d796c696508bf8 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 9 Aug 2024 09:41:24 +0800 Subject: [PATCH] refactor: combine `Commands.UpdateSubmoduleStatus` into `Commands.QuerySubmodules` --- src/Commands/QuerySubmodules.cs | 65 ++++++++++++++++++++------- src/Commands/UpdateSubmoduleStatus.cs | 43 ------------------ 2 files changed, 49 insertions(+), 59 deletions(-) delete mode 100644 src/Commands/UpdateSubmoduleStatus.cs diff --git a/src/Commands/QuerySubmodules.cs b/src/Commands/QuerySubmodules.cs index 24e040d5..5fd6e3d5 100644 --- a/src/Commands/QuerySubmodules.cs +++ b/src/Commands/QuerySubmodules.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Text; using System.Text.RegularExpressions; namespace SourceGit.Commands @@ -9,6 +10,8 @@ namespace SourceGit.Commands private static partial Regex REG_FORMAT1(); [GeneratedRegex(@"^[\-\+ ][0-9a-f]+\s(.*)$")] private static partial Regex REG_FORMAT2(); + [GeneratedRegex(@"^\s?[\w\?]{1,4}\s+(.+)$")] + private static partial Regex REG_FORMAT_STATUS(); public QuerySubmodules(string repo) { @@ -19,27 +22,57 @@ namespace SourceGit.Commands public List Result() { - Exec(); - new UpdateSubmoduleStatus(WorkingDirectory, _submodules).Result(); - return _submodules; - } + var submodules = new List(); + var rs = ReadToEnd(); + if (!rs.IsSuccess) + return submodules; - protected override void OnReadline(string line) - { - var match = REG_FORMAT1().Match(line); - if (match.Success) + var builder = new StringBuilder(); + var lines = rs.StdOut.Split('\n', System.StringSplitOptions.RemoveEmptyEntries); + foreach (var line in lines) { - _submodules.Add(new Models.Submodule() { Path = match.Groups[1].Value }); - return; + var match = REG_FORMAT1().Match(line); + if (match.Success) + { + var path = match.Groups[1].Value; + builder.Append($"\"{path}\" "); + submodules.Add(new Models.Submodule() { Path = path }); + continue; + } + + match = REG_FORMAT2().Match(line); + if (match.Success) + { + var path = match.Groups[1].Value; + builder.Append($"\"{path}\" "); + submodules.Add(new Models.Submodule() { Path = path }); + } } - match = REG_FORMAT2().Match(line); - if (match.Success) + if (submodules.Count > 0) { - _submodules.Add(new Models.Submodule() { Path = match.Groups[1].Value }); - } - } + Args = $"status -uno --porcelain -- {builder}"; + rs = ReadToEnd(); + if (!rs.IsSuccess) + return submodules; - private readonly List _submodules = new List(); + var dirty = new HashSet(); + 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; + } } } diff --git a/src/Commands/UpdateSubmoduleStatus.cs b/src/Commands/UpdateSubmoduleStatus.cs deleted file mode 100644 index 38b3c33e..00000000 --- a/src/Commands/UpdateSubmoduleStatus.cs +++ /dev/null @@ -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 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 _submodules = null; - private HashSet _changed = new HashSet(); - } -}