sourcegit/src/Commands/QueryCommits.cs

206 lines
6.7 KiB
C#
Raw Normal View History

using System;
2021-04-29 05:05:55 -07:00
using System.Collections.Generic;
namespace SourceGit.Commands
{
public class QueryCommits : Command
{
private const string GPGSIG_START = "gpgsig -----BEGIN PGP SIGNATURE-----";
private const string GPGSIG_END = " -----END PGP SIGNATURE-----";
2021-04-29 05:05:55 -07:00
private readonly List<Models.Commit> commits = new List<Models.Commit>();
2021-04-29 05:05:55 -07:00
private Models.Commit current = null;
private bool isSkipingGpgsig = false;
private bool isHeadFounded = false;
private readonly bool findFirstMerged = true;
2021-04-29 05:05:55 -07:00
public QueryCommits(string repo, string limits, bool needFindHead = true)
{
WorkingDirectory = repo;
2021-04-29 05:05:55 -07:00
Args = "log --date-order --decorate=full --pretty=raw " + limits;
findFirstMerged = needFindHead;
}
public List<Models.Commit> Result()
{
2021-04-29 05:05:55 -07:00
Exec();
if (current != null)
{
2021-04-29 05:05:55 -07:00
current.Message = current.Message.Trim();
commits.Add(current);
}
if (findFirstMerged && !isHeadFounded && commits.Count > 0)
{
2021-04-29 05:05:55 -07:00
MarkFirstMerged();
}
return commits;
}
protected override void OnReadline(string line)
{
if (isSkipingGpgsig)
{
if (line.StartsWith(GPGSIG_END, StringComparison.Ordinal))
isSkipingGpgsig = false;
2021-04-29 05:05:55 -07:00
return;
}
else if (line.StartsWith(GPGSIG_START, StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
isSkipingGpgsig = true;
return;
}
if (line.StartsWith("commit ", StringComparison.Ordinal))
{
if (current != null)
{
2021-04-29 05:05:55 -07:00
current.Message = current.Message.Trim();
commits.Add(current);
}
current = new Models.Commit();
line = line.Substring(7);
var decoratorStart = line.IndexOf('(', StringComparison.Ordinal);
if (decoratorStart < 0)
{
2021-04-29 05:05:55 -07:00
current.SHA = line.Trim();
}
else
{
2021-04-29 05:05:55 -07:00
current.SHA = line.Substring(0, decoratorStart).Trim();
current.IsMerged = ParseDecorators(current.Decorators, line.Substring(decoratorStart + 1));
if (!isHeadFounded)
isHeadFounded = current.IsMerged;
2021-04-29 05:05:55 -07:00
}
return;
}
if (current == null)
return;
2021-04-29 05:05:55 -07:00
if (line.StartsWith("tree ", StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
return;
}
else if (line.StartsWith("parent ", StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
current.Parents.Add(line.Substring("parent ".Length));
}
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.AuthorTime = time;
}
else if (line.StartsWith("committer ", StringComparison.Ordinal))
{
Models.User user = Models.User.Invalid;
ulong time = 0;
Models.Commit.ParseUserAndTime(line.Substring(10), ref user, ref time);
current.Committer = user;
current.CommitterTime = time;
}
else if (string.IsNullOrEmpty(current.Subject))
{
2021-04-29 05:05:55 -07:00
current.Subject = line.Trim();
}
else
{
2021-04-29 05:05:55 -07:00
current.Message += (line.Trim() + "\n");
}
}
private bool ParseDecorators(List<Models.Decorator> decorators, string data)
{
2021-04-29 05:05:55 -07:00
bool isHeadOfCurrent = false;
var subs = data.Split(new char[] { ',', ')', '(' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var sub in subs)
{
2021-04-29 05:05:55 -07:00
var d = sub.Trim();
if (d.StartsWith("tag: refs/tags/", StringComparison.Ordinal))
{
decorators.Add(new Models.Decorator()
{
2021-04-29 05:05:55 -07:00
Type = Models.DecoratorType.Tag,
Name = d.Substring(15).Trim(),
});
}
else if (d.EndsWith("/HEAD", StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
continue;
}
else if (d.StartsWith("HEAD -> refs/heads/", StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
isHeadOfCurrent = true;
decorators.Add(new Models.Decorator()
{
2021-04-29 05:05:55 -07:00
Type = Models.DecoratorType.CurrentBranchHead,
Name = d.Substring(19).Trim(),
});
}
else if (d.StartsWith("refs/heads/", StringComparison.Ordinal))
{
decorators.Add(new Models.Decorator()
{
2021-04-29 05:05:55 -07:00
Type = Models.DecoratorType.LocalBranchHead,
Name = d.Substring(11).Trim(),
});
}
else if (d.StartsWith("refs/remotes/", StringComparison.Ordinal))
{
decorators.Add(new Models.Decorator()
{
2021-04-29 05:05:55 -07:00
Type = Models.DecoratorType.RemoteBranchHead,
Name = d.Substring(13).Trim(),
});
}
}
decorators.Sort((l, r) =>
{
if (l.Type != r.Type)
{
return (int)l.Type - (int)r.Type;
}
else
{
return l.Name.CompareTo(r.Name);
}
});
2021-04-29 05:05:55 -07:00
return isHeadOfCurrent;
}
private void MarkFirstMerged()
{
Args = $"log --since=\"{commits[commits.Count - 1].CommitterTimeStr}\" --format=\"%H\"";
2021-04-29 05:05:55 -07:00
var rs = ReadToEnd();
var shas = rs.StdOut.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (shas.Length == 0)
return;
2021-04-29 05:05:55 -07:00
var set = new HashSet<string>();
foreach (var sha in shas)
set.Add(sha);
foreach (var c in commits)
{
if (set.Contains(c.SHA))
{
2021-04-29 05:05:55 -07:00
c.IsMerged = true;
break;
2021-04-29 05:05:55 -07:00
}
}
}
}
}