Skip to content

Commit

Permalink
Merge pull request #15 from credfeto/feat/fix-whitespace-at-eof-diff-…
Browse files Browse the repository at this point in the history
…error

Handling diff detecting that there's a difference in that the file ends with a new line in one version and not the other
  • Loading branch information
credfeto authored Nov 26, 2020
2 parents 54e995e + 2229f85 commit 1457c93
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Please ADD ALL Changes to the UNRELEASED SECTION and not a specific release
- Command to create a new release from unreleased content
- Detection of changelog if it is in the git repo
### Fixed
- Diff of whitespace at EOF is reported as error
### Changed
### Removed
### Deployment Changes
Expand All @@ -32,4 +33,4 @@ Releases that have at least been deployed to staging, BUT NOT necessarily releas
- Support for extracting the changelog for a specific release
- Support for extracting the changelog for a unreleased content

## [0.0.0] - Project created
## [0.0.0] - Project created
71 changes: 70 additions & 1 deletion src/Credfeto.ChangeLog/ChangeLogChecker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -71,7 +72,7 @@ public static async Task<bool> ChangeLogModifiedInReleaseSectionAsync(string cha
private static bool CheckForChangesAfterFirstRelease(PatchEntryChanges change, int firstReleaseVersionIndex)
{
Console.WriteLine("Change Details");
string patchDetails = change.Patch;
string patchDetails = ExtractPatchDetails(change.Patch);
Console.WriteLine(patchDetails);

MatchCollection matches = HunkPositionRegex.Matches(patchDetails);
Expand Down Expand Up @@ -104,6 +105,74 @@ private static bool CheckForChangesAfterFirstRelease(PatchEntryChanges change, i
return true;
}

private static string ExtractPatchDetails(string patch)
{
Console.WriteLine(patch);
List<string> lines = patch.Split('\n')
.ToList();

RemoveLastLineIfBlank(lines);

int lastHunk = lines.FindLastIndex(x => HunkPositionRegex.IsMatch(x));

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

if (before.SequenceEqual(after))
{
lines.RemoveRange(index: lastHunk, lines.Count - lastHunk);
}
}

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)
{
before = new List<string>();
after = new List<string>();

foreach (string line in lines.Skip(lastHunk + 1))
{
switch (line[0])
{
case '+':
after.Add(line.Substring(1));

break;

case '-':
before.Add(line.Substring(1));

break;

case '\\':
if (line == @"\ No newline at end of file")
{
break;
}

throw new DiffException($"Could not process diff line: {line}");

default: 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 (string.IsNullOrEmpty(lines[lastLine]))
{
lines.RemoveAt(lastLine);
}
}
}

private static string GetFullChangeLogFilePath(string changeLogFileName)
{
FileInfo changeLog = new(changeLogFileName);
Expand Down
22 changes: 22 additions & 0 deletions src/Credfeto.ChangeLog/Exceptions/DiffException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Credfeto.ChangeLog.Management.Exceptions
{
public sealed class DiffException : Exception
{
public DiffException()
: this("Could not process diff")
{
}

public DiffException(string message)
: base(message)
{
}

public DiffException(string message, Exception innerException)
: base(message: message, innerException: innerException)
{
}
}
}

0 comments on commit 1457c93

Please sign in to comment.