sourcegit/src/Commands/QueryStashes.cs

48 lines
1.8 KiB
C#
Raw Normal View History

using System;
2021-04-29 05:05:55 -07:00
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public class QueryStashes : Command {
2021-04-29 05:05:55 -07:00
private static readonly Regex REG_STASH = new Regex(@"^Reflog: refs/(stash@\{\d+\}).*$");
public QueryStashes(string repo) {
WorkingDirectory = repo;
Context = repo;
2021-04-29 05:05:55 -07:00
Args = "stash list --pretty=raw";
}
public List<Models.Stash> Result() {
Exec();
if (_current != null) _stashes.Add(_current);
return _stashes;
2021-04-29 05:05:55 -07:00
}
protected override void OnReadline(string line) {
2021-04-29 05:05:55 -07:00
if (line.StartsWith("commit ", StringComparison.Ordinal)) {
if (_current != null && !string.IsNullOrEmpty(_current.Name)) _stashes.Add(_current);
_current = new Models.Stash() { SHA = line.Substring(7, 8) };
2021-04-29 05:05:55 -07:00
return;
}
if (_current == null) return;
2021-04-29 05:05:55 -07:00
if (line.StartsWith("Reflog: refs/stash@", StringComparison.Ordinal)) {
var match = REG_STASH.Match(line);
if (match.Success) _current.Name = match.Groups[1].Value;
2021-04-29 05:05:55 -07:00
} else if (line.StartsWith("Reflog message: ", StringComparison.Ordinal)) {
_current.Message = line.Substring(16);
2021-04-29 05:05:55 -07:00
} 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;
2021-04-29 05:05:55 -07:00
}
}
private List<Models.Stash> _stashes = new List<Models.Stash>();
private Models.Stash _current = null;
2021-04-29 05:05:55 -07:00
}
}