sourcegit/src/Commands/Statistics.cs
leo 8b3d129890
code_review: PR #711
* SourceGit.Commands.* should not reference code in SourceGit.ViewModels.

Signed-off-by: leo <longshuang@msn.cn>
2024-11-19 09:46:06 +08:00

48 lines
1.4 KiB
C#

using System;
namespace SourceGit.Commands
{
public class Statistics : Command
{
public Statistics(string repo, int max)
{
WorkingDirectory = repo;
Context = repo;
Args = $"log --date-order --branches --remotes -{max} --pretty=format:\"%ct$%aN\"";
}
public Models.Statistics Result()
{
var statistics = new Models.Statistics();
var rs = ReadToEnd();
if (!rs.IsSuccess)
return statistics;
var start = 0;
var end = rs.StdOut.IndexOf('\n', start);
while (end > 0)
{
ParseLine(statistics, rs.StdOut.Substring(start, end - start));
start = end + 1;
end = rs.StdOut.IndexOf('\n', start);
}
if (start < rs.StdOut.Length)
ParseLine(statistics, rs.StdOut.Substring(start));
statistics.Complete();
return statistics;
}
private void ParseLine(Models.Statistics statistics, string line)
{
var dateEndIdx = line.IndexOf('$', StringComparison.Ordinal);
if (dateEndIdx == -1)
return;
var dateStr = line.Substring(0, dateEndIdx);
if (double.TryParse(dateStr, out var date))
statistics.AddCommit(line.Substring(dateEndIdx + 1), date);
}
}
}