enhance: improve commit and stash parsing time

This commit is contained in:
leo 2024-06-05 11:46:31 +08:00
parent 57540b960a
commit ce9a3dad2f
No known key found for this signature in database
GPG key ID: B528468E49CD0E58
6 changed files with 117 additions and 230 deletions

View file

@ -1,64 +1,48 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace SourceGit.Commands
{
public partial class QueryStashes : Command
public class QueryStashes : Command
{
[GeneratedRegex(@"^Reflog: refs/(stash@\{\d+\}).*$")]
private static partial Regex REG_STASH();
public QueryStashes(string repo)
{
WorkingDirectory = repo;
Context = repo;
Args = "stash list --pretty=raw";
Args = "stash list --pretty=format:%H%n%ct%n%gd%n%s";
}
public List<Models.Stash> Result()
{
Exec();
if (_current != null)
_stashes.Add(_current);
return _stashes;
}
protected override void OnReadline(string line)
{
if (line.StartsWith("commit ", StringComparison.Ordinal))
switch (_nextLineIdx)
{
if (_current != null && !string.IsNullOrEmpty(_current.Name))
case 0:
_current = new Models.Stash() { SHA = line };
_stashes.Add(_current);
_current = new Models.Stash() { SHA = line.Substring(7, 8) };
return;
break;
case 1:
_current.Time = ulong.Parse(line);
break;
case 2:
_current.Name = line;
break;
case 3:
_current.Message = line;
break;
}
if (_current == null)
return;
if (line.StartsWith("Reflog: refs/stash@", StringComparison.Ordinal))
{
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);
}
else if (line.StartsWith("author ", StringComparison.Ordinal))
{
Models.User user = Models.User.Invalid;
ulong time = 0;
Models.Commit.ParseUserAndTime(line.Substring(7), ref user, ref time);
_current.Author = user;
_current.Time = time;
}
_nextLineIdx++;
if (_nextLineIdx > 3)
_nextLineIdx = 0;
}
private readonly List<Models.Stash> _stashes = new List<Models.Stash>();
private Models.Stash _current = null;
private int _nextLineIdx = 0;
}
}