Source Generated Regex

This commit is contained in:
Enner Pérez 2024-03-16 04:09:27 -05:00
parent f93dec5e6a
commit c0a079de41
19 changed files with 101 additions and 64 deletions

View file

@ -2,9 +2,11 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class AssumeUnchanged {
class ViewCommand : Command {
private static readonly Regex REG = new Regex(@"^(\w)\s+(.+)$");
public partial class AssumeUnchanged {
partial class ViewCommand : Command {
[GeneratedRegex(@"^(\w)\s+(.+)$")]
private static partial Regex REG();
public ViewCommand(string repo) {
WorkingDirectory = repo;
@ -18,7 +20,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG.Match(line);
var match = REG().Match(line);
if (!match.Success) return;
if (match.Groups[1].Value == "h") {

View file

@ -3,8 +3,10 @@ using System.Text;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class Blame : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^\^?([0-9a-f]+)\s+.*\((.*)\s+(\d+)\s+[\-\+]?\d+\s+\d+\) (.*)");
public partial class Blame : Command {
[GeneratedRegex(@"^\^?([0-9a-f]+)\s+.*\((.*)\s+(\d+)\s+[\-\+]?\d+\s+\d+\) (.*)")]
private static partial Regex REG_FORMAT();
private static readonly DateTime UTC_START = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
public Blame(string repo, string file, string revision) {
@ -44,7 +46,7 @@ namespace SourceGit.Commands {
return;
}
var match = REG_FORMAT.Match(line);
var match = REG_FORMAT().Match(line);
if (!match.Success) return;
_content.AppendLine(match.Groups[4].Value);

View file

@ -6,7 +6,7 @@ using System.Text;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class Command {
public partial class Command {
public class CancelToken {
public bool Requested { get; set; } = false;
}
@ -70,7 +70,7 @@ namespace SourceGit.Commands {
if (e.Data.StartsWith("remote: Counting objects:", StringComparison.Ordinal)) return;
if (e.Data.StartsWith("remote: Compressing objects:", StringComparison.Ordinal)) return;
if (e.Data.StartsWith("Filtering content:", StringComparison.Ordinal)) return;
if (_progressRegex.IsMatch(e.Data)) return;
if (_progressRegex().IsMatch(e.Data)) return;
errs.Add(e.Data);
};
@ -142,6 +142,7 @@ namespace SourceGit.Commands {
protected virtual void OnReadline(string line) { }
private static readonly Regex _progressRegex = new Regex(@"\d+%");
[GeneratedRegex(@"\d+%")]
private static partial Regex _progressRegex();
}
}

View file

@ -2,8 +2,9 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class CompareRevisions : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^(\s?[\w\?]{1,4})\s+(.+)$");
public partial class CompareRevisions : Command {
[GeneratedRegex(@"^(\s?[\w\?]{1,4})\s+(.+)$")]
private static partial Regex REG_FORMAT();
public CompareRevisions(string repo, string start, string end) {
WorkingDirectory = repo;
@ -18,7 +19,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_FORMAT.Match(line);
var match = REG_FORMAT().Match(line);
if (!match.Success) return;
var change = new Models.Change() { Path = match.Groups[2].Value };

View file

@ -3,8 +3,9 @@ using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class Diff : Command {
private static readonly Regex REG_INDICATOR = new Regex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@");
public partial class Diff : Command {
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
private static partial Regex REG_INDICATOR();
private static readonly string PREFIX_LFS_NEW = "+version https://git-lfs.github.com/spec/";
private static readonly string PREFIX_LFS_DEL = "-version https://git-lfs.github.com/spec/";
private static readonly string PREFIX_LFS_MODIFY = " version https://git-lfs.github.com/spec/";
@ -57,7 +58,7 @@ namespace SourceGit.Commands {
}
if (_result.TextDiff.Lines.Count == 0) {
var match = REG_INDICATOR.Match(line);
var match = REG_INDICATOR().Match(line);
if (!match.Success) {
if (line.StartsWith("Binary", StringComparison.Ordinal)) _result.IsBinary = true;
return;
@ -96,7 +97,7 @@ namespace SourceGit.Commands {
_newLine++;
} else if (ch != '\\') {
ProcessInlineHighlights();
var match = REG_INDICATOR.Match(line);
var match = REG_INDICATOR().Match(line);
if (match.Success) {
_oldLine = int.Parse(match.Groups[1].Value);
_newLine = int.Parse(match.Groups[2].Value);

View file

@ -1,8 +1,9 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class IsBinary : Command {
private static readonly Regex REG_TEST = new Regex(@"^\-\s+\-\s+.*$");
public partial class IsBinary : Command {
[GeneratedRegex(@"^\-\s+\-\s+.*$")]
private static partial Regex REG_TEST();
public IsBinary(string repo, string commit, string path) {
WorkingDirectory = repo;
@ -12,7 +13,7 @@ namespace SourceGit.Commands {
}
public bool Result() {
return REG_TEST.IsMatch(ReadToEnd().StdOut);
return REG_TEST().IsMatch(ReadToEnd().StdOut);
}
}
}

View file

@ -3,10 +3,12 @@ using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryBranches : Command {
public partial class QueryBranches : Command {
private static readonly string PREFIX_LOCAL = "refs/heads/";
private static readonly string PREFIX_REMOTE = "refs/remotes/";
private static readonly Regex REG_AHEAD_BEHIND = new Regex(@"^(\d+)\s(\d+)$");
[GeneratedRegex(@"^(\d+)\s(\d+)$")]
private static partial Regex REG_AHEAD_BEHIND();
public QueryBranches(string repo) {
WorkingDirectory = repo;
@ -71,7 +73,7 @@ namespace SourceGit.Commands {
var rs = cmd.ReadToEnd();
if (!rs.IsSuccess) return string.Empty;
var match = REG_AHEAD_BEHIND.Match(rs.StdOut);
var match = REG_AHEAD_BEHIND().Match(rs.StdOut);
if (!match.Success) return string.Empty;
var ahead = int.Parse(match.Groups[1].Value);

View file

@ -2,8 +2,9 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryCommitChanges : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^(\s?[\w\?]{1,4})\s+(.+)$");
public partial class QueryCommitChanges : Command {
[GeneratedRegex(@"^(\s?[\w\?]{1,4})\s+(.+)$")]
private static partial Regex REG_FORMAT();
public QueryCommitChanges(string repo, string commitSHA) {
WorkingDirectory = repo;
@ -18,7 +19,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_FORMAT.Match(line);
var match = REG_FORMAT().Match(line);
if (!match.Success) return;
var change = new Models.Change() { Path = match.Groups[2].Value };

View file

@ -1,8 +1,10 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryFileSize : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^\d+\s+\w+\s+[0-9a-f]+\s+(\d+)\s+.*$");
public partial class QueryFileSize : Command {
[GeneratedRegex(@"^\d+\s+\w+\s+[0-9a-f]+\s+(\d+)\s+.*$")]
private static partial Regex REG_FORMAT();
public QueryFileSize(string repo, string file, string revision) {
WorkingDirectory = repo;
@ -15,7 +17,7 @@ namespace SourceGit.Commands {
var rs = ReadToEnd();
if (rs.IsSuccess) {
var match = REG_FORMAT.Match(rs.StdOut);
var match = REG_FORMAT().Match(rs.StdOut);
if (match.Success) {
return long.Parse(match.Groups[1].Value);
}

View file

@ -3,8 +3,9 @@ using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryLocalChanges : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^(\s?[\w\?]{1,4})\s+(.+)$");
public partial class QueryLocalChanges : Command {
[GeneratedRegex(@"^(\s?[\w\?]{1,4})\s+(.+)$")]
private static partial Regex REG_FORMAT();
private static readonly string[] UNTRACKED = [ "no", "all" ];
public QueryLocalChanges(string repo, bool includeUntracked = true) {
@ -19,7 +20,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_FORMAT.Match(line);
var match = REG_FORMAT().Match(line);
if (!match.Success) return;
if (line.EndsWith("/", StringComparison.Ordinal)) return; // Ignore changes with git-worktree

View file

@ -2,8 +2,9 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryRemotes : Command {
private static readonly Regex REG_REMOTE = new Regex(@"^([\w\.\-]+)\s*(\S+).*$");
public partial class QueryRemotes : Command {
[GeneratedRegex(@"^([\w\.\-]+)\s*(\S+).*$")]
private static partial Regex REG_REMOTE();
public QueryRemotes(string repo) {
WorkingDirectory = repo;
@ -17,7 +18,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_REMOTE.Match(line);
var match = REG_REMOTE().Match(line);
if (!match.Success) return;
var remote = new Models.Remote() {

View file

@ -2,8 +2,10 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryRevisionObjects : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^\d+\s+(\w+)\s+([0-9a-f]+)\s+(.*)$");
public partial class QueryRevisionObjects : Command {
[GeneratedRegex(@"^\d+\s+(\w+)\s+([0-9a-f]+)\s+(.*)$")]
private static partial Regex REG_FORMAT();
private List<Models.Object> objects = new List<Models.Object>();
public QueryRevisionObjects(string repo, string sha) {
@ -18,7 +20,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_FORMAT.Match(line);
var match = REG_FORMAT().Match(line);
if (!match.Success) return;
var obj = new Models.Object();

View file

@ -1,8 +1,9 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryStagedFileBlobGuid : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^\d+\s+([0-9a-f]+)\s+.*$");
public partial class QueryStagedFileBlobGuid : Command {
[GeneratedRegex(@"^\d+\s+([0-9a-f]+)\s+.*$")]
private static partial Regex REG_FORMAT();
public QueryStagedFileBlobGuid(string repo, string file) {
WorkingDirectory = repo;
@ -12,7 +13,7 @@ namespace SourceGit.Commands {
public string Result() {
var rs = ReadToEnd();
var match = REG_FORMAT.Match(rs.StdOut.Trim());
var match = REG_FORMAT().Match(rs.StdOut.Trim());
if (match.Success) {
return match.Groups[1].Value;
}

View file

@ -2,8 +2,10 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryStashChanges : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^(\s?[\w\?]{1,4})\s+(.+)$");
public partial class QueryStashChanges : Command {
[GeneratedRegex(@"^(\s?[\w\?]{1,4})\s+(.+)$")]
private static partial Regex REG_FORMAT();
public QueryStashChanges(string repo, string sha) {
WorkingDirectory = repo;
@ -17,7 +19,7 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_FORMAT.Match(line);
var match = REG_FORMAT().Match(line);
if (!match.Success) return;
var change = new Models.Change() { Path = match.Groups[2].Value };

View file

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryStashes : Command {
private static readonly Regex REG_STASH = new Regex(@"^Reflog: refs/(stash@\{\d+\}).*$");
public partial class QueryStashes : Command {
[GeneratedRegex(@"^Reflog: refs/(stash@\{\d+\}).*$")]
private static partial Regex REG_STASH();
public QueryStashes(string repo) {
WorkingDirectory = repo;
@ -28,7 +30,7 @@ namespace SourceGit.Commands {
if (_current == null) return;
if (line.StartsWith("Reflog: refs/stash@", StringComparison.Ordinal)) {
var match = REG_STASH.Match(line);
var match = REG_STASH().Match(line);
if (match.Success) _current.Name = match.Groups[1].Value;
} else if (line.StartsWith("Reflog message: ", StringComparison.Ordinal)) {
_current.Message = line.Substring(16);

View file

@ -2,9 +2,11 @@
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QuerySubmodules : Command {
private readonly Regex REG_FORMAT1 = new Regex(@"^[\-\+ ][0-9a-f]+\s(.*)\s\(.*\)$");
private readonly Regex REG_FORMAT2 = new Regex(@"^[\-\+ ][0-9a-f]+\s(.*)$");
public partial class QuerySubmodules : Command {
[GeneratedRegex(@"^[\-\+ ][0-9a-f]+\s(.*)\s\(.*\)$")]
private static partial Regex REG_FORMAT1();
[GeneratedRegex(@"^[\-\+ ][0-9a-f]+\s(.*)$")]
private static partial Regex REG_FORMAT2();
public QuerySubmodules(string repo) {
WorkingDirectory = repo;
@ -18,13 +20,13 @@ namespace SourceGit.Commands {
}
protected override void OnReadline(string line) {
var match = REG_FORMAT1.Match(line);
var match = REG_FORMAT1().Match(line);
if (match.Success) {
_submodules.Add(match.Groups[1].Value);
return;
}
match = REG_FORMAT2.Match(line);
match = REG_FORMAT2().Match(line);
if (match.Success) {
_submodules.Add(match.Groups[1].Value);
}

View file

@ -49,7 +49,7 @@ namespace SourceGit.Models {
}
}
public class TextDiff {
public partial class TextDiff {
public string File { get; set; } = string.Empty;
public List<TextDiffLine> Lines { get; set; } = new List<TextDiffLine>();
public int MaxLineNumber = 0;
@ -282,11 +282,14 @@ namespace SourceGit.Models {
builder.Append("\n");
System.IO.File.WriteAllText(output, builder.ToString());
}
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
private static partial Regex indicatorRegex();
private bool ProcessIndicatorForPatch(StringBuilder builder, TextDiffLine indicator, int idx, int start, int end, int ignoreRemoves, int ignoreAdds, bool revert, bool tailed) {
var indicatorRegex = new Regex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@");
var match = indicatorRegex.Match(indicator.Content);
var match = indicatorRegex().Match(indicator.Content);
var oldStart = int.Parse(match.Groups[1].Value);
var newStart = int.Parse(match.Groups[2].Value) + ignoreRemoves - ignoreAdds;
var oldCount = 0;
@ -336,9 +339,8 @@ namespace SourceGit.Models {
}
private bool ProcessIndicatorForPatchSingleSide(StringBuilder builder, TextDiffLine indicator, int idx, int start, int end, int ignoreRemoves, int ignoreAdds, bool revert, bool isOldSide, bool tailed) {
var indicatorRegex = new Regex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@");
var match = indicatorRegex.Match(indicator.Content);
var match = indicatorRegex().Match(indicator.Content);
var oldStart = int.Parse(match.Groups[1].Value);
var newStart = int.Parse(match.Groups[2].Value) + ignoreRemoves - ignoreAdds;
var oldCount = 0;

View file

@ -1,11 +1,20 @@
using System.Text.RegularExpressions;
namespace SourceGit.Models {
public class Remote {
public partial class Remote {
[GeneratedRegex(@"^http[s]?://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-\.]+\.git$")]
private static partial Regex regex1();
[GeneratedRegex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-]+/[\w\-\.]+\.git$")]
private static partial Regex regex2();
[GeneratedRegex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-\.]+\.git$")]
private static partial Regex regex3();
private static readonly Regex[] URL_FORMATS = [
new Regex(@"^http[s]?://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-\.]+\.git$"),
new Regex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-]+/[\w\-\.]+\.git$"),
new Regex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-\.]+\.git$"),
regex1(),
regex2(),
regex3(),
];
public string Name { get; set; }

View file

@ -3,8 +3,10 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SourceGit.ViewModels {
public class InitGitFlow : Popup {
private static readonly Regex TAG_PREFIX = new Regex(@"^[\w\-/\.]+$");
public partial class InitGitFlow : Popup {
[GeneratedRegex(@"^[\w\-/\.]+$")]
private static partial Regex TAG_PREFIX();
[Required(ErrorMessage = "Master branch name is required!!!")]
[RegularExpression(@"^[\w\-/\.]+$", ErrorMessage = "Bad branch name format!")]
@ -63,7 +65,7 @@ namespace SourceGit.ViewModels {
}
public static ValidationResult ValidateTagPrefix(string tagPrefix, ValidationContext ctx) {
if (!string.IsNullOrWhiteSpace(tagPrefix) && !TAG_PREFIX.IsMatch(tagPrefix)) {
if (!string.IsNullOrWhiteSpace(tagPrefix) && !TAG_PREFIX().IsMatch(tagPrefix)) {
return new ValidationResult("Bad tag prefix format!");
}