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-09-26 20:17:02 -07:00
|
|
|
|
public QueryCommits(string repo, string filter, Models.CommitSearchMethod method, bool onlyCurrentBranch)
|
2024-07-14 19:17:26 -07:00
|
|
|
|
{
|
2024-09-26 20:17:02 -07:00
|
|
|
|
string search = onlyCurrentBranch ? string.Empty : "--branches --remotes ";
|
2024-08-28 01:56:43 -07:00
|
|
|
|
|
|
|
|
|
if (method == Models.CommitSearchMethod.ByUser)
|
|
|
|
|
{
|
2024-09-26 20:17:02 -07:00
|
|
|
|
search += $"-i --author=\"{filter}\" --committer=\"{filter}\"";
|
2024-08-28 01:56:43 -07:00
|
|
|
|
}
|
|
|
|
|
else if (method == Models.CommitSearchMethod.ByFile)
|
2024-07-14 19:17:26 -07:00
|
|
|
|
{
|
2024-09-26 20:17:02 -07:00
|
|
|
|
search += $"-- \"{filter}\"";
|
2024-07-14 19:17:26 -07:00
|
|
|
|
}
|
2024-07-17 05:54:09 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var argsBuilder = new StringBuilder();
|
2024-09-26 20:17:02 -07:00
|
|
|
|
argsBuilder.Append(search);
|
|
|
|
|
|
2024-08-28 01:56:43 -07:00
|
|
|
|
var words = filter.Split(new[] { ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
2024-07-17 05:54:09 -07:00
|
|
|
|
foreach (var word in words)
|
|
|
|
|
{
|
|
|
|
|
var escaped = word.Trim().Replace("\"", "\\\"", StringComparison.Ordinal);
|
|
|
|
|
argsBuilder.Append($"--grep=\"{escaped}\" ");
|
|
|
|
|
}
|
|
|
|
|
argsBuilder.Append("--all-match -i");
|
2024-09-26 20:17:02 -07:00
|
|
|
|
|
2024-07-17 05:54:09 -07:00
|
|
|
|
search = argsBuilder.ToString();
|
|
|
|
|
}
|
2024-07-14 19:17:26 -07:00
|
|
|
|
|
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2024-09-26 20:17:02 -07:00
|
|
|
|
Args = $"log -1000 --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 " + 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:
|
2024-07-18 18:29:16 -07:00
|
|
|
|
_current.ParseDecorators(line);
|
|
|
|
|
if (_current.IsMerged && !_isHeadFounded)
|
|
|
|
|
_isHeadFounded = true;
|
2024-06-06 21:31:10 -07:00
|
|
|
|
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-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
|
|
|
|
}
|