2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Collections.Generic;
|
2024-07-14 19:17:26 -07:00
|
|
|
|
using System.Text;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public class QueryCommits : Command
|
|
|
|
|
{
|
|
|
|
|
public QueryCommits(string repo, string limits, bool needFindHead = true)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
WorkingDirectory = repo;
|
2024-05-27 02:21:28 -07:00
|
|
|
|
Context = repo;
|
2024-07-14 19:17:26 -07:00
|
|
|
|
Args = "log --date-order --no-show-signature --decorate=full --pretty=format:%H%n%P%n%D%n%aN±%aE%n%at%n%cN±%cE%n%ct%n%s " + limits;
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_findFirstMerged = needFindHead;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 05:54:09 -07:00
|
|
|
|
public QueryCommits(string repo, int maxCount, string messageFilter, bool isFile)
|
2024-07-14 19:17:26 -07:00
|
|
|
|
{
|
2024-07-17 05:54:09 -07:00
|
|
|
|
string search;
|
|
|
|
|
if (isFile)
|
2024-07-14 19:17:26 -07:00
|
|
|
|
{
|
2024-07-17 05:54:09 -07:00
|
|
|
|
search = $"-- \"{messageFilter}\"";
|
2024-07-14 19:17:26 -07:00
|
|
|
|
}
|
2024-07-17 05:54:09 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var argsBuilder = new StringBuilder();
|
|
|
|
|
var words = messageFilter.Split(new[] { ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (var word in words)
|
|
|
|
|
{
|
|
|
|
|
var escaped = word.Trim().Replace("\"", "\\\"", StringComparison.Ordinal);
|
|
|
|
|
argsBuilder.Append($"--grep=\"{escaped}\" ");
|
|
|
|
|
}
|
|
|
|
|
argsBuilder.Append("--all-match -i");
|
|
|
|
|
search = argsBuilder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 19:17:26 -07:00
|
|
|
|
|
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2024-07-17 05:54:09 -07:00
|
|
|
|
Args = $"log -{maxCount} --date-order --no-show-signature --decorate=full --pretty=format:%H%n%P%n%D%n%aN±%aE%n%at%n%cN±%cE%n%ct%n%s --branches --remotes " + search;
|
2024-07-14 19:17:26 -07:00
|
|
|
|
_findFirstMerged = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Commit> Result()
|
|
|
|
|
{
|
2024-06-06 21:31:10 -07:00
|
|
|
|
var rs = ReadToEnd();
|
|
|
|
|
if (!rs.IsSuccess)
|
|
|
|
|
return _commits;
|
|
|
|
|
|
|
|
|
|
var nextPartIdx = 0;
|
|
|
|
|
var start = 0;
|
|
|
|
|
var end = rs.StdOut.IndexOf('\n', start);
|
|
|
|
|
while (end > 0)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-06-06 21:31:10 -07:00
|
|
|
|
var line = rs.StdOut.Substring(start, end - start);
|
|
|
|
|
switch (nextPartIdx)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
_current = new Models.Commit() { SHA = line };
|
|
|
|
|
_commits.Add(_current);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
ParseParent(line);
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
ParseDecorators(line);
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
_current.Author = Models.User.FindOrAdd(line);
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
_current.AuthorTime = ulong.Parse(line);
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
_current.Committer = Models.User.FindOrAdd(line);
|
|
|
|
|
break;
|
|
|
|
|
case 6:
|
|
|
|
|
_current.CommitterTime = ulong.Parse(line);
|
2024-06-07 21:19:48 -07:00
|
|
|
|
break;
|
|
|
|
|
case 7:
|
|
|
|
|
_current.Subject = line;
|
2024-06-07 23:45:18 -07:00
|
|
|
|
nextPartIdx = -1;
|
2024-06-07 21:19:48 -07:00
|
|
|
|
break;
|
2024-06-06 21:31:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextPartIdx++;
|
2024-06-13 18:46:30 -07:00
|
|
|
|
|
2024-06-06 21:31:10 -07:00
|
|
|
|
start = end + 1;
|
|
|
|
|
end = rs.StdOut.IndexOf('\n', start);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 20:08:20 -07:00
|
|
|
|
if (start < rs.StdOut.Length)
|
|
|
|
|
_current.Subject = rs.StdOut.Substring(start);
|
|
|
|
|
|
2024-06-06 21:31:10 -07:00
|
|
|
|
if (_findFirstMerged && !_isHeadFounded && _commits.Count > 0)
|
|
|
|
|
MarkFirstMerged();
|
|
|
|
|
|
|
|
|
|
return _commits;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 05:25:16 -07:00
|
|
|
|
private void ParseParent(string data)
|
|
|
|
|
{
|
|
|
|
|
if (data.Length < 8)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var idx = data.IndexOf(' ', StringComparison.Ordinal);
|
|
|
|
|
if (idx == -1)
|
|
|
|
|
{
|
|
|
|
|
_current.Parents.Add(data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_current.Parents.Add(data.Substring(0, idx));
|
|
|
|
|
_current.Parents.Add(data.Substring(idx + 1));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 20:46:31 -07:00
|
|
|
|
private void ParseDecorators(string data)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-06-06 05:25:16 -07:00
|
|
|
|
if (data.Length < 3)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-06-04 20:46:31 -07:00
|
|
|
|
var subs = data.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var sub in subs)
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var d = sub.Trim();
|
2024-07-14 09:30:31 -07:00
|
|
|
|
if (d.EndsWith("/HEAD", StringComparison.Ordinal))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (d.StartsWith("tag: refs/tags/", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_current.Decorators.Add(new Models.Decorator()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Type = Models.DecoratorType.Tag,
|
2024-06-06 21:31:10 -07:00
|
|
|
|
Name = d.Substring(15),
|
2021-04-29 05:05:55 -07:00
|
|
|
|
});
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (d.StartsWith("HEAD -> refs/heads/", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_current.IsMerged = true;
|
|
|
|
|
_current.Decorators.Add(new Models.Decorator()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Type = Models.DecoratorType.CurrentBranchHead,
|
2024-06-06 21:31:10 -07:00
|
|
|
|
Name = d.Substring(19),
|
2021-04-29 05:05:55 -07:00
|
|
|
|
});
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
2024-05-25 11:05:32 -07:00
|
|
|
|
else if (d.Equals("HEAD"))
|
|
|
|
|
{
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_current.IsMerged = true;
|
|
|
|
|
_current.Decorators.Add(new Models.Decorator()
|
2024-05-25 11:05:32 -07:00
|
|
|
|
{
|
|
|
|
|
Type = Models.DecoratorType.CurrentCommitHead,
|
2024-06-06 21:31:10 -07:00
|
|
|
|
Name = d,
|
2024-05-25 11:05:32 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else if (d.StartsWith("refs/heads/", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_current.Decorators.Add(new Models.Decorator()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Type = Models.DecoratorType.LocalBranchHead,
|
2024-06-06 21:31:10 -07:00
|
|
|
|
Name = d.Substring(11),
|
2021-04-29 05:05:55 -07:00
|
|
|
|
});
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (d.StartsWith("refs/remotes/", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_current.Decorators.Add(new Models.Decorator()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Type = Models.DecoratorType.RemoteBranchHead,
|
2024-06-06 21:31:10 -07:00
|
|
|
|
Name = d.Substring(13),
|
2021-04-29 05:05:55 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 20:46:31 -07:00
|
|
|
|
_current.Decorators.Sort((l, r) =>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
|
|
|
|
if (l.Type != r.Type)
|
2021-04-30 02:03:22 -07:00
|
|
|
|
return (int)l.Type - (int)r.Type;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else
|
2024-06-07 21:19:48 -07:00
|
|
|
|
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
|
2021-04-30 02:03:22 -07:00
|
|
|
|
});
|
|
|
|
|
|
2024-06-04 20:46:31 -07:00
|
|
|
|
if (_current.IsMerged && !_isHeadFounded)
|
|
|
|
|
_isHeadFounded = true;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void MarkFirstMerged()
|
|
|
|
|
{
|
2024-07-14 09:30:31 -07:00
|
|
|
|
Args = $"log --since=\"{_commits[^1].CommitterTimeStr}\" --format=\"%H\"";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
var rs = ReadToEnd();
|
2024-07-14 09:30:31 -07:00
|
|
|
|
var shas = rs.StdOut.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (shas.Length == 0)
|
|
|
|
|
return;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2021-04-29 18:03:35 -07:00
|
|
|
|
var set = new HashSet<string>();
|
2024-03-31 01:54:29 -07:00
|
|
|
|
foreach (var sha in shas)
|
|
|
|
|
set.Add(sha);
|
2021-04-29 18:03:35 -07:00
|
|
|
|
|
2024-06-04 20:46:31 -07:00
|
|
|
|
foreach (var c in _commits)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
|
|
|
|
if (set.Contains(c.SHA))
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
c.IsMerged = true;
|
2021-04-29 18:03:35 -07:00
|
|
|
|
break;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-04 20:46:31 -07:00
|
|
|
|
|
|
|
|
|
private List<Models.Commit> _commits = new List<Models.Commit>();
|
|
|
|
|
private Models.Commit _current = null;
|
2024-06-06 21:31:10 -07:00
|
|
|
|
private bool _findFirstMerged = false;
|
2024-06-04 20:46:31 -07:00
|
|
|
|
private bool _isHeadFounded = false;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|