5 Commits

2 changed files with 112 additions and 39 deletions

View File

@ -93,12 +93,20 @@ public class CommitDetail
{ {
var branchResult = repo.Branches[branchName]; var branchResult = repo.Branches[branchName];
try
{
if (branchResult == null) if (branchResult == null)
{ {
branchResult = repo.Branches[$"origin/{branchName}"]; branchResult = repo.Branches[$"origin/{branchName}"];
var remoteBranch = repo.CreateBranch(branchName, branchResult.Tip); var remoteBranch = repo.CreateBranch(branchName, branchResult.Tip);
repo.Branches.Update(remoteBranch, b => b.UpstreamBranch = $"refs/heads/{branchName}"); repo.Branches.Update(remoteBranch, b => b.UpstreamBranch = $"refs/heads/{branchName}");
} }
}
catch (System.Exception)
{
Console.WriteLine($"Cannot fetch {branchName} branch.");
Environment.Exit(1);
}
foreach (var c in branchResult.Commits) foreach (var c in branchResult.Commits)
{ {
@ -120,8 +128,36 @@ public class CommitDetail
{ {
using (var repo = new Repository(Directory.GetCurrentDirectory())) using (var repo = new Repository(Directory.GetCurrentDirectory()))
{ {
var tagResult = repo.Tags[tagName]; try
System.Console.WriteLine(tagResult); {
var tagResult = repo.Tags[tagName].Target.Sha;
var commitFilter = new CommitFilter
{
IncludeReachableFrom = tagResult,
};
var query = repo.Commits.QueryBy(commitFilter);
foreach (var c in query)
{
if (!_authors.Contains(c.Author.Name))
{
_authors.Add(c.Author.Name);
}
}
foreach (var a in _authors)
{
int commitCount = query.Where(r => r.Author.Name == a).Count();
_commitDetails.Add(a, commitCount);
}
}
catch (System.Exception)
{
Console.WriteLine($"Cannot find the tag {tagName}");
Environment.Exit(3);
}
} }
} }
} }

View File

@ -16,13 +16,20 @@ static class Program
); );
branchOption.AddAlias("-b"); branchOption.AddAlias("-b");
var tagOption = new Option<string>(
"--tag",
"Specify the tag to filter by"
);
tagOption.AddAlias("-t");
var rootCommand = new RootCommand("Get a tally of contributors' commits") var rootCommand = new RootCommand("Get a tally of contributors' commits")
{ {
outputOption, outputOption,
branchOption, branchOption,
tagOption,
}; };
rootCommand.SetHandler((outputOptionValue, branchOptionValue) => { rootCommand.SetHandler((outputOptionValue, branchOptionValue, tagOptionValue) => {
CommitDetail commits = new CommitDetail(); CommitDetail commits = new CommitDetail();
switch (outputOptionValue) switch (outputOptionValue)
@ -31,6 +38,10 @@ static class Program
StdOutDataService outDataService = new StdOutDataService(); StdOutDataService outDataService = new StdOutDataService();
DataAccess dataAccess = new DataAccess(outDataService); DataAccess dataAccess = new DataAccess(outDataService);
if (branchOptionValue != null && tagOptionValue != null) {
Console.WriteLine("Please specify either a branch or a tag");
Environment.Exit(2);
} else if (branchOptionValue != null && tagOptionValue == null) {
switch (branchOptionValue) switch (branchOptionValue)
{ {
case null: case null:
@ -43,10 +54,19 @@ static class Program
break; break;
} }
break; break;
} else if (branchOptionValue == null && tagOptionValue != null) {
commits.GetCommitsByTag(tagOptionValue);
dataAccess.WriteData(commits.CommitDetails);
}
break;
case "xlsx": case "xlsx":
ExcelDataService excelDataService = new ExcelDataService(); ExcelDataService excelDataService = new ExcelDataService();
DataAccess dataAccessExcelCase = new DataAccess(excelDataService); DataAccess dataAccessExcelCase = new DataAccess(excelDataService);
if (branchOptionValue != null && tagOptionValue != null) {
Console.WriteLine("Please specify either a branch or a tag.");
Environment.Exit(2);
} else if (branchOptionValue != null && tagOptionValue == null) {
switch (branchOptionValue) switch (branchOptionValue)
{ {
case null: case null:
@ -59,6 +79,11 @@ static class Program
break; break;
} }
break; break;
} else if (branchOptionValue == null && tagOptionValue != null) {
commits.GetCommitsByTag(tagOptionValue);
dataAccessExcelCase.WriteData(commits.CommitDetails);
}
break;
case "pdf": case "pdf":
PdfDataService pdfDataService = new PdfDataService(); PdfDataService pdfDataService = new PdfDataService();
DataAccess dataAccessPdfCase = new DataAccess(pdfDataService); DataAccess dataAccessPdfCase = new DataAccess(pdfDataService);
@ -79,6 +104,10 @@ static class Program
StdOutDataService stdOutDataService = new StdOutDataService(); StdOutDataService stdOutDataService = new StdOutDataService();
DataAccess dataAccessNullCase = new DataAccess(stdOutDataService); DataAccess dataAccessNullCase = new DataAccess(stdOutDataService);
if (branchOptionValue != null && tagOptionValue != null) {
Console.WriteLine("Please specify either a branch or a tag.");
Environment.Exit(2);
} else if (branchOptionValue != null && tagOptionValue == null) {
switch (branchOptionValue) switch (branchOptionValue)
{ {
case null: case null:
@ -91,13 +120,21 @@ static class Program
break; break;
} }
break; break;
} else if (branchOptionValue == null && tagOptionValue != null) {
commits.GetCommitsByTag(tagOptionValue);
dataAccessNullCase.WriteData(commits.CommitDetails);
} else {
commits.GetCurrentCommitsByName();
dataAccessNullCase.WriteData(commits.CommitDetails);
}
break;
default: default:
System.Console.WriteLine("This should not happen..."); System.Console.WriteLine("This should not happen...");
Environment.Exit(90); Environment.Exit(4);
break; break;
} }
}, },
outputOption, branchOption); outputOption, branchOption, tagOption);
rootCommand.Invoke(args); rootCommand.Invoke(args);
} }