Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
credfeto committed Dec 8, 2023
1 parent 13b4711 commit bc62502
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
15 changes: 9 additions & 6 deletions src/Credfeto.ChangeLog.Cmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -174,9 +172,7 @@ private static async Task<int> Main(string[] args)

try
{
ParserResult<Options> parser = await Parser.Default.ParseArguments<Options>(args)
.WithNotParsed(NotParsed)
.WithParsedAsync(ParsedOkAsync);
ParserResult<Options> parser = await ParseOptionsAsync(args);

return parser.Tag == ParserResultType.Parsed
? SUCCESS
Expand All @@ -194,4 +190,11 @@ private static async Task<int> Main(string[] args)
return ERROR;
}
}

private static Task<ParserResult<Options>> ParseOptionsAsync(IEnumerable<string> args)
{
return Parser.Default.ParseArguments<Options>(args)
.WithNotParsed(NotParsed)
.WithParsedAsync(ParsedOkAsync);
}
}
7 changes: 6 additions & 1 deletion src/Credfeto.ChangeLog/BuildNumberHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
65 changes: 48 additions & 17 deletions src/Credfeto.ChangeLog/ChangeLogChecker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static async Task<bool> 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);

Expand All @@ -49,7 +50,7 @@ public static async Task<bool> ChangeLogModifiedInReleaseSectionAsync(string cha

int firstReleaseVersionIndex = position.Value;

Patch changes = repo.Diff.Compare<Patch>(oldTree: originBranch.Tip.Tree, newTree: repo.Head.Tip.Tree, compareOptions: CompareSettings.BuildCompareOptions);
Patch changes = repo.Diff.Compare<Patch>(oldTree: BranchTree(originBranch), newTree: HeadTree(repo), compareOptions: CompareSettings.BuildCompareOptions);

PatchEntryChanges? change = changes.FirstOrDefault(candidate => candidate.Path == changeLogInRepoPath);

Expand All @@ -64,6 +65,26 @@ public static async Task<bool> 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");
Expand All @@ -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<Match?>())
{
if (match is null)
{
Expand Down Expand Up @@ -109,7 +130,7 @@ private static string ExtractPatchDetails(string patch)

if (lastHunk != -1)
{
CompareHunk(lines: lines, lastHunk: lastHunk, out List<string> before, out List<string> after);
(List<string> before, List<string> after) = CompareHunk(lines: lines, lastHunk: lastHunk);

if (before.SequenceEqual(second: after, comparer: StringComparer.Ordinal))
{
Expand All @@ -120,48 +141,58 @@ private static string ExtractPatchDetails(string patch)
return string.Join(separator: Environment.NewLine, values: lines);
}

private static void CompareHunk(List<string> lines, int lastHunk, out List<string> before, out List<string> after)
private static (List<string> before, List<string> after) CompareHunk(List<string> lines, int lastHunk)
{
before = new();
after = new();
List<string> before = [];
List<string> 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<string> before, List<string> after) CouldNotProcessDiffLine(string line)
{
throw new DiffException($"Could not process diff line: {line}");
}

private static void RemoveLastLineIfBlank(List<string> 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);
}
}

Expand Down

0 comments on commit bc62502

Please sign in to comment.