From bc6250299288203e81319d7fede751a45196e2be Mon Sep 17 00:00:00 2001 From: Mark Ridgwell Date: Fri, 8 Dec 2023 13:40:42 +0000 Subject: [PATCH] Code cleanup --- src/Credfeto.ChangeLog.Cmd/Program.cs | 15 +++-- src/Credfeto.ChangeLog/BuildNumberHelpers.cs | 7 ++- src/Credfeto.ChangeLog/ChangeLogChecker.cs | 65 +++++++++++++++----- 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/Credfeto.ChangeLog.Cmd/Program.cs b/src/Credfeto.ChangeLog.Cmd/Program.cs index 0ed21353..599e088c 100644 --- a/src/Credfeto.ChangeLog.Cmd/Program.cs +++ b/src/Credfeto.ChangeLog.Cmd/Program.cs @@ -107,9 +107,7 @@ private static async Task CheckInsertPositionAsync(Options options, Cancellation string changeLog = FindChangeLog(options); Console.WriteLine($"Using Changelog {changeLog}"); Console.WriteLine($"Branch: {originBranchName}"); - bool valid = await ChangeLogChecker.ChangeLogModifiedInReleaseSectionAsync(changeLogFileName: changeLog, - originBranchName: originBranchName, - cancellationToken: cancellationToken); + bool valid = await ChangeLogChecker.ChangeLogModifiedInReleaseSectionAsync(changeLogFileName: changeLog, originBranchName: originBranchName, cancellationToken: cancellationToken); if (valid) { @@ -174,9 +172,7 @@ private static async Task Main(string[] args) try { - ParserResult parser = await Parser.Default.ParseArguments(args) - .WithNotParsed(NotParsed) - .WithParsedAsync(ParsedOkAsync); + ParserResult parser = await ParseOptionsAsync(args); return parser.Tag == ParserResultType.Parsed ? SUCCESS @@ -194,4 +190,11 @@ private static async Task Main(string[] args) return ERROR; } } + + private static Task> ParseOptionsAsync(IEnumerable args) + { + return Parser.Default.ParseArguments(args) + .WithNotParsed(NotParsed) + .WithParsedAsync(ParsedOkAsync); + } } \ No newline at end of file diff --git a/src/Credfeto.ChangeLog/BuildNumberHelpers.cs b/src/Credfeto.ChangeLog/BuildNumberHelpers.cs index 0fed12f2..763fba01 100644 --- a/src/Credfeto.ChangeLog/BuildNumberHelpers.cs +++ b/src/Credfeto.ChangeLog/BuildNumberHelpers.cs @@ -16,6 +16,11 @@ internal static class BuildNumberHelpers return null; } - return new(version); + if (!Version.TryParse(input: version, out Version? parsedVersion)) + { + return null; + } + + return parsedVersion; } } \ No newline at end of file diff --git a/src/Credfeto.ChangeLog/ChangeLogChecker.cs b/src/Credfeto.ChangeLog/ChangeLogChecker.cs index ae0a31ad..c3ff2e93 100644 --- a/src/Credfeto.ChangeLog/ChangeLogChecker.cs +++ b/src/Credfeto.ChangeLog/ChangeLogChecker.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Linq; @@ -29,7 +30,7 @@ public static async Task ChangeLogModifiedInReleaseSectionAsync(string cha using (Repository repo = GitRepository.OpenRepository(changelogDir)) { - string sha = repo.Head.Tip.Sha; + string sha = HeadSha(repo); Branch? originBranch = repo.Branches.FirstOrDefault(b => b.FriendlyName == originBranchName); @@ -49,7 +50,7 @@ public static async Task ChangeLogModifiedInReleaseSectionAsync(string cha int firstReleaseVersionIndex = position.Value; - Patch changes = repo.Diff.Compare(oldTree: originBranch.Tip.Tree, newTree: repo.Head.Tip.Tree, compareOptions: CompareSettings.BuildCompareOptions); + Patch changes = repo.Diff.Compare(oldTree: BranchTree(originBranch), newTree: HeadTree(repo), compareOptions: CompareSettings.BuildCompareOptions); PatchEntryChanges? change = changes.FirstOrDefault(candidate => candidate.Path == changeLogInRepoPath); @@ -64,6 +65,26 @@ public static async Task ChangeLogModifiedInReleaseSectionAsync(string cha return true; } + private static Tree BranchTree(Branch branch) + { + return branch.Tip.Tree; + } + + private static Tree HeadTree(Repository repo) + { + return BranchTree(repo.Head); + } + + private static string BranchSha(Branch branch) + { + return branch.Tip.Sha; + } + + private static string HeadSha(Repository repo) + { + return BranchSha(repo.Head); + } + private static bool CheckForChangesAfterFirstRelease(PatchEntryChanges change, int firstReleaseVersionIndex) { Console.WriteLine("Change Details"); @@ -72,7 +93,7 @@ private static bool CheckForChangesAfterFirstRelease(PatchEntryChanges change, i MatchCollection matches = CommonRegex.GitHunkPosition.Matches(patchDetails); - foreach (Match? match in matches) + foreach (Match? match in matches.OfType()) { if (match is null) { @@ -109,7 +130,7 @@ private static string ExtractPatchDetails(string patch) if (lastHunk != -1) { - CompareHunk(lines: lines, lastHunk: lastHunk, out List before, out List after); + (List before, List after) = CompareHunk(lines: lines, lastHunk: lastHunk); if (before.SequenceEqual(second: after, comparer: StringComparer.Ordinal)) { @@ -120,48 +141,58 @@ private static string ExtractPatchDetails(string patch) return string.Join(separator: Environment.NewLine, values: lines); } - private static void CompareHunk(List lines, int lastHunk, out List before, out List after) + private static (List before, List after) CompareHunk(List lines, int lastHunk) { - before = new(); - after = new(); + List before = []; + List after = []; foreach (string line in lines.Skip(lastHunk + 1)) { switch (line[0]) { case '+': - after.Add(line.Substring(1)); + after.Add(line[1..]); break; case '-': - before.Add(line.Substring(1)); + before.Add(line[1..]); break; case '\\': - if (line == @"\ No newline at end of file") + if (StringComparer.Ordinal.Equals(x: line, y: @"\ No newline at end of file")) { break; } - throw new DiffException($"Could not process diff line: {line}"); + return CouldNotProcessDiffLine(line); - default: throw new DiffException($"Could not process diff line: {line}"); + default: return CouldNotProcessDiffLine(line); } } + + return (before, after); + } + + [DoesNotReturn] + private static (List before, List after) CouldNotProcessDiffLine(string line) + { + throw new DiffException($"Could not process diff line: {line}"); } private static void RemoveLastLineIfBlank(List lines) { int lastLine = lines.Count - 1; - if (lastLine >= 0) + if (lastLine < 0) { - if (string.IsNullOrEmpty(lines[lastLine])) - { - lines.RemoveAt(lastLine); - } + return; + } + + if (string.IsNullOrEmpty(lines[lastLine])) + { + lines.RemoveAt(lastLine); } }