DrillSergeant/BLL/CommitDetail.cs

180 lines
4.9 KiB
C#
Raw Normal View History

2022-07-31 17:38:23 -05:00
using LibGit2Sharp;
public class CommitDetail
{
private List<string>? _authors;
private SortedList<string, int>? _commitDetails;
private string _currentBranch;
2022-12-24 21:11:12 -06:00
private string? _outputOption;
private string? _branchOption;
private string? _tagOption;
2022-12-24 16:46:20 -06:00
private string? _selectedDir;
2022-07-31 17:38:23 -05:00
2022-12-24 16:46:20 -06:00
public List<string>? Authors
{
2022-07-31 17:38:23 -05:00
get { return _authors; }
set { _authors = value; }
}
2022-12-24 16:46:20 -06:00
2022-07-31 17:38:23 -05:00
public SortedList<string, int>? CommitDetails
{
get { return _commitDetails; }
set { _commitDetails = value; }
}
public string CurrentBranch
{
get { return _currentBranch; }
set { _currentBranch = value; }
}
2022-12-24 21:11:12 -06:00
public string? SelectedDir
2022-12-24 16:46:20 -06:00
{
get { return _selectedDir; }
set { _selectedDir = value; }
}
2022-12-24 21:11:12 -06:00
public CommitDetail(string? dir)
2022-07-31 17:38:23 -05:00
{
2022-12-24 21:38:53 -06:00
_selectedDir = !String.IsNullOrEmpty(dir) ? dir : Directory.GetCurrentDirectory();
2022-07-31 17:38:23 -05:00
_authors = new List<string>();
_commitDetails = new SortedList<string, int>();
_currentBranch = GetCurrentBranch();
2022-12-24 21:11:12 -06:00
// unused, might be used later for a refactor
_outputOption = null;
_branchOption = null;
_tagOption = null;
2022-07-31 17:38:23 -05:00
}
2022-12-24 16:46:20 -06:00
public void GetCurrentCommitsByName()
2022-07-31 17:38:23 -05:00
{
2022-12-24 16:46:20 -06:00
using (var repo = new Repository(_selectedDir))
2022-07-31 17:38:23 -05:00
{
foreach (var c in repo.Commits)
{
if (!_authors.Contains(c.Author.Name))
{
_authors.Add(c.Author.Name);
}
}
2022-12-24 16:46:20 -06:00
2022-07-31 17:38:23 -05:00
foreach (var a in _authors)
{
int commitCount = repo.Commits.Where(r => r.Author.Name == a).Count();
_commitDetails.Add(a, commitCount);
}
}
}
public void GetCurrentCommitsByEmail()
2022-07-31 17:38:23 -05:00
{
2022-12-24 16:46:20 -06:00
using (var repo = new Repository(_selectedDir))
2022-07-31 17:38:23 -05:00
{
foreach (var c in repo.Commits)
{
if (!_authors.Contains(c.Author.Email))
{
_authors.Add(c.Author.Email);
}
}
2022-12-24 16:46:20 -06:00
2022-07-31 17:38:23 -05:00
foreach (var a in _authors)
{
int commitCount = repo.Commits.Where(r => r.Author.Email == a).Count();
_commitDetails.Add(a, commitCount);
}
}
}
public int GetCommitTotal()
{
2022-12-24 16:46:20 -06:00
using (var repo = new Repository(_selectedDir))
2022-07-31 17:38:23 -05:00
{
return repo.Commits.Count();
}
}
public string GetCurrentBranch()
{
2022-12-24 16:46:20 -06:00
using (var repo = new Repository(_selectedDir))
{
return repo.Head.Reference.TargetIdentifier;
}
}
public void GetCommitsByBranch(string branchName)
{
2022-12-24 16:46:20 -06:00
using (var repo = new Repository(_selectedDir))
{
var branchResult = repo.Branches[branchName];
2022-08-23 18:31:39 -05:00
try
{
2022-08-23 18:31:39 -05:00
if (branchResult == null)
{
branchResult = repo.Branches[$"origin/{branchName}"];
var remoteBranch = repo.CreateBranch(branchName, branchResult.Tip);
repo.Branches.Update(remoteBranch, b => b.UpstreamBranch = $"refs/heads/{branchName}");
2022-12-24 16:46:20 -06:00
}
2022-08-23 18:31:39 -05:00
}
catch (System.Exception)
{
Console.WriteLine($"Cannot fetch {branchName} branch.");
Environment.Exit(1);
}
foreach (var c in branchResult.Commits)
{
if (!_authors.Contains(c.Author.Name))
{
_authors.Add(c.Author.Name);
}
}
2022-12-24 16:46:20 -06:00
foreach (var a in _authors)
{
int commitCount = branchResult.Commits.Where(r => r.Author.Name == a).Count();
_commitDetails.Add(a, commitCount);
}
}
}
2022-12-24 16:46:20 -06:00
public void GetCommitsByTag(string tagName)
{
2022-12-24 16:46:20 -06:00
using (var repo = new Repository(_selectedDir))
{
2022-08-23 18:31:39 -05:00
try
2022-08-23 17:56:56 -05:00
{
2022-08-23 18:31:39 -05:00
var tagResult = repo.Tags[tagName].Target.Sha;
2022-12-24 16:46:20 -06:00
2022-08-23 18:31:39 -05:00
var commitFilter = new CommitFilter
{
IncludeReachableFrom = tagResult,
};
2022-08-23 17:56:56 -05:00
2022-08-23 18:31:39 -05:00
var query = repo.Commits.QueryBy(commitFilter);
2022-08-23 17:56:56 -05:00
2022-08-23 18:31:39 -05:00
foreach (var c in query)
2022-08-23 17:56:56 -05:00
{
2022-08-23 18:31:39 -05:00
if (!_authors.Contains(c.Author.Name))
{
_authors.Add(c.Author.Name);
}
2022-08-23 17:56:56 -05:00
}
2022-08-23 18:31:39 -05:00
foreach (var a in _authors)
{
int commitCount = query.Where(r => r.Author.Name == a).Count();
_commitDetails.Add(a, commitCount);
}
}
catch (System.Exception)
2022-08-23 17:56:56 -05:00
{
2022-08-23 18:31:39 -05:00
Console.WriteLine($"Cannot find the tag {tagName}");
Environment.Exit(3);
2022-08-23 17:56:56 -05:00
}
}
}
2022-07-31 17:38:23 -05:00
}