From 702188bc22ebd38bde4ea7b12ea874a494a73c52 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sat, 24 Dec 2022 17:46:20 -0500 Subject: [PATCH 1/3] wip: starting work on dir option --- BLL/CommitDetail.cs | 42 ++++---- DrillSergeant.csproj | 2 +- Program.cs | 222 ++++++++++++++++++++++++------------------- 3 files changed, 148 insertions(+), 118 deletions(-) diff --git a/BLL/CommitDetail.cs b/BLL/CommitDetail.cs index b9702f6..4c33525 100644 --- a/BLL/CommitDetail.cs +++ b/BLL/CommitDetail.cs @@ -5,13 +5,14 @@ public class CommitDetail private List? _authors; private SortedList? _commitDetails; private string _currentBranch; + private string? _selectedDir; - public List? Authors - { + public List? Authors + { get { return _authors; } set { _authors = value; } } - + public SortedList? CommitDetails { get { return _commitDetails; } @@ -24,16 +25,23 @@ public class CommitDetail set { _currentBranch = value; } } - public CommitDetail() + public string SelectedDir + { + get { return _selectedDir; } + set { _selectedDir = value; } + } + + public CommitDetail(string dir) { _authors = new List(); _commitDetails = new SortedList(); _currentBranch = GetCurrentBranch(); + _selectedDir = Directory.Exists(dir) ? dir : Directory.GetCurrentDirectory(); } - public void GetCurrentCommitsByName() + public void GetCurrentCommitsByName() { - using (var repo = new Repository(Directory.GetCurrentDirectory())) + using (var repo = new Repository(_selectedDir)) { foreach (var c in repo.Commits) { @@ -42,7 +50,7 @@ public class CommitDetail _authors.Add(c.Author.Name); } } - + foreach (var a in _authors) { int commitCount = repo.Commits.Where(r => r.Author.Name == a).Count(); @@ -53,7 +61,7 @@ public class CommitDetail public void GetCurrentCommitsByEmail() { - using (var repo = new Repository(Directory.GetCurrentDirectory())) + using (var repo = new Repository(_selectedDir)) { foreach (var c in repo.Commits) { @@ -62,7 +70,7 @@ public class CommitDetail _authors.Add(c.Author.Email); } } - + foreach (var a in _authors) { int commitCount = repo.Commits.Where(r => r.Author.Email == a).Count(); @@ -73,7 +81,7 @@ public class CommitDetail public int GetCommitTotal() { - using (var repo = new Repository(Directory.GetCurrentDirectory())) + using (var repo = new Repository(_selectedDir)) { return repo.Commits.Count(); } @@ -81,7 +89,7 @@ public class CommitDetail public string GetCurrentBranch() { - using (var repo = new Repository(Directory.GetCurrentDirectory())) + using (var repo = new Repository(_selectedDir)) { return repo.Head.Reference.TargetIdentifier; } @@ -89,7 +97,7 @@ public class CommitDetail public void GetCommitsByBranch(string branchName) { - using (var repo = new Repository(Directory.GetCurrentDirectory())) + using (var repo = new Repository(_selectedDir)) { var branchResult = repo.Branches[branchName]; @@ -100,7 +108,7 @@ public class CommitDetail branchResult = repo.Branches[$"origin/{branchName}"]; var remoteBranch = repo.CreateBranch(branchName, branchResult.Tip); repo.Branches.Update(remoteBranch, b => b.UpstreamBranch = $"refs/heads/{branchName}"); - } + } } catch (System.Exception) { @@ -115,7 +123,7 @@ public class CommitDetail _authors.Add(c.Author.Name); } } - + foreach (var a in _authors) { int commitCount = branchResult.Commits.Where(r => r.Author.Name == a).Count(); @@ -123,15 +131,15 @@ public class CommitDetail } } } - + public void GetCommitsByTag(string tagName) { - using (var repo = new Repository(Directory.GetCurrentDirectory())) + using (var repo = new Repository(_selectedDir)) { try { var tagResult = repo.Tags[tagName].Target.Sha; - + var commitFilter = new CommitFilter { IncludeReachableFrom = tagResult, diff --git a/DrillSergeant.csproj b/DrillSergeant.csproj index a246cc4..bddfa44 100644 --- a/DrillSergeant.csproj +++ b/DrillSergeant.csproj @@ -7,7 +7,7 @@ enable enable drillsergeant - 1.1.0 + 1.2.0 diff --git a/Program.cs b/Program.cs index a156caa..d70fe7e 100644 --- a/Program.cs +++ b/Program.cs @@ -22,119 +22,141 @@ static class Program ); tagOption.AddAlias("-t"); + var dirOption = new Option( + "--file", + "Specify a git directory" + ); + dirOption.AddAlias("-f"); + var rootCommand = new RootCommand("Get a tally of contributors' commits") { outputOption, branchOption, tagOption, + dirOption, }; - rootCommand.SetHandler((outputOptionValue, branchOptionValue, tagOptionValue) => { - CommitDetail commits = new CommitDetail(); - - switch (outputOptionValue) + rootCommand.SetHandler((outputOptionValue, branchOptionValue, tagOptionValue, dirOptionValue) => + { + CommitDetail commits = new CommitDetail(dirOptionValue); + + switch (outputOptionValue) + { + case "stdout": + StdOutDataService outDataService = new StdOutDataService(); + DataAccess dataAccess = new DataAccess(outDataService); + + if (branchOptionValue != null && tagOptionValue != null) { - case "stdout": - StdOutDataService outDataService = new StdOutDataService(); - 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) - { - case null: - commits.GetCurrentCommitsByName(); - dataAccess.WriteData(commits.CommitDetails); - break; - default: - commits.GetCommitsByBranch(branchOptionValue); - dataAccess.WriteData(commits.CommitDetails); - break; - } - break; - } else if (branchOptionValue == null && tagOptionValue != null) { - commits.GetCommitsByTag(tagOptionValue); - dataAccess.WriteData(commits.CommitDetails); - } - break; - case "xlsx": - ExcelDataService excelDataService = new 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) - { - case null: - commits.GetCurrentCommitsByName(); - dataAccessExcelCase.WriteData(commits.CommitDetails); - break; - default: - commits.GetCommitsByBranch(branchOptionValue); - dataAccessExcelCase.WriteData(commits.CommitDetails); - break; - } - break; - } else if (branchOptionValue == null && tagOptionValue != null) { - commits.GetCommitsByTag(tagOptionValue); - dataAccessExcelCase.WriteData(commits.CommitDetails); - } - break; - case "pdf": - PdfDataService pdfDataService = new PdfDataService(); - DataAccess dataAccessPdfCase = new DataAccess(pdfDataService); - - switch (branchOptionValue) - { - case null: - commits.GetCurrentCommitsByName(); - dataAccessPdfCase.WriteData(commits.CommitDetails); - break; - default: - commits.GetCommitsByBranch(branchOptionValue); - dataAccessPdfCase.WriteData(commits.CommitDetails); - break; - } - break; - case null: - StdOutDataService stdOutDataService = new 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) - { - case null: - commits.GetCurrentCommitsByName(); - dataAccessNullCase.WriteData(commits.CommitDetails); - break; - default: - commits.GetCommitsByBranch(branchOptionValue); - dataAccessNullCase.WriteData(commits.CommitDetails); - break; - } - break; - } else if (branchOptionValue == null && tagOptionValue != null) { - commits.GetCommitsByTag(tagOptionValue); - dataAccessNullCase.WriteData(commits.CommitDetails); - } else { + Console.WriteLine("Please specify either a branch or a tag"); + Environment.Exit(2); + } + else if (branchOptionValue != null && tagOptionValue == null) + { + switch (branchOptionValue) + { + case null: commits.GetCurrentCommitsByName(); - dataAccessNullCase.WriteData(commits.CommitDetails); - } + dataAccess.WriteData(commits.CommitDetails); + break; + default: + commits.GetCommitsByBranch(branchOptionValue); + dataAccess.WriteData(commits.CommitDetails); + break; + } + } + else if (branchOptionValue == null && tagOptionValue != null) + { + commits.GetCommitsByTag(tagOptionValue); + dataAccess.WriteData(commits.CommitDetails); + } + break; + case "xlsx": + ExcelDataService excelDataService = new 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) + { + case null: + commits.GetCurrentCommitsByName(); + dataAccessExcelCase.WriteData(commits.CommitDetails); + break; + default: + commits.GetCommitsByBranch(branchOptionValue); + dataAccessExcelCase.WriteData(commits.CommitDetails); + break; + } + } + else if (branchOptionValue == null && tagOptionValue != null) + { + commits.GetCommitsByTag(tagOptionValue); + dataAccessExcelCase.WriteData(commits.CommitDetails); + } + break; + case "pdf": + PdfDataService pdfDataService = new PdfDataService(); + DataAccess dataAccessPdfCase = new DataAccess(pdfDataService); + + switch (branchOptionValue) + { + case null: + commits.GetCurrentCommitsByName(); + dataAccessPdfCase.WriteData(commits.CommitDetails); break; default: - System.Console.WriteLine("This should not happen..."); - Environment.Exit(4); + commits.GetCommitsByBranch(branchOptionValue); + dataAccessPdfCase.WriteData(commits.CommitDetails); break; } - }, - outputOption, branchOption, tagOption); + break; + case null: + StdOutDataService stdOutDataService = new 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) + { + case null: + commits.GetCurrentCommitsByName(); + dataAccessNullCase.WriteData(commits.CommitDetails); + break; + default: + commits.GetCommitsByBranch(branchOptionValue); + dataAccessNullCase.WriteData(commits.CommitDetails); + break; + } + } + else if (branchOptionValue == null && tagOptionValue != null) + { + commits.GetCommitsByTag(tagOptionValue); + dataAccessNullCase.WriteData(commits.CommitDetails); + } + else + { + commits.GetCurrentCommitsByName(); + dataAccessNullCase.WriteData(commits.CommitDetails); + } + break; + default: + System.Console.WriteLine("This should not happen..."); + Environment.Exit(4); + break; + } + }, + outputOption, branchOption, tagOption, dirOption); rootCommand.Invoke(args); } From 384391add132e29258146d0781efb36554317438 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sat, 24 Dec 2022 22:11:12 -0500 Subject: [PATCH 2/3] working file option --- BLL/CommitDetail.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/BLL/CommitDetail.cs b/BLL/CommitDetail.cs index 4c33525..4194a87 100644 --- a/BLL/CommitDetail.cs +++ b/BLL/CommitDetail.cs @@ -5,6 +5,9 @@ public class CommitDetail private List? _authors; private SortedList? _commitDetails; private string _currentBranch; + private string? _outputOption; + private string? _branchOption; + private string? _tagOption; private string? _selectedDir; public List? Authors @@ -25,18 +28,28 @@ public class CommitDetail set { _currentBranch = value; } } - public string SelectedDir + public string? SelectedDir { get { return _selectedDir; } set { _selectedDir = value; } } - public CommitDetail(string dir) + public CommitDetail(string? dir) { + if (!String.IsNullOrEmpty(dir)) { + _selectedDir = dir; + } else { + _selectedDir = Directory.GetCurrentDirectory(); + } + _authors = new List(); _commitDetails = new SortedList(); _currentBranch = GetCurrentBranch(); - _selectedDir = Directory.Exists(dir) ? dir : Directory.GetCurrentDirectory(); + + // unused, might be used later for a refactor + _outputOption = null; + _branchOption = null; + _tagOption = null; } public void GetCurrentCommitsByName() From 434ba07d592ff388cabab10ad9f6b11979354539 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sat, 24 Dec 2022 22:38:53 -0500 Subject: [PATCH 3/3] modifications to dirOption --- BLL/CommitDetail.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/BLL/CommitDetail.cs b/BLL/CommitDetail.cs index 4194a87..3c8b34c 100644 --- a/BLL/CommitDetail.cs +++ b/BLL/CommitDetail.cs @@ -36,12 +36,7 @@ public class CommitDetail public CommitDetail(string? dir) { - if (!String.IsNullOrEmpty(dir)) { - _selectedDir = dir; - } else { - _selectedDir = Directory.GetCurrentDirectory(); - } - + _selectedDir = !String.IsNullOrEmpty(dir) ? dir : Directory.GetCurrentDirectory(); _authors = new List(); _commitDetails = new SortedList(); _currentBranch = GetCurrentBranch();