Skip to content

Commit d095feb

Browse files
authored
Merge pull request #4590 from 9swampy/FormatCommitMsgForPlantUml
Format the commitMsg so it doesn't break PlantUml
2 parents f8a95ab + 8d46d70 commit d095feb

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/GitVersion.Core.Tests/IntegrationTests/RepositoryFixtureExtensions.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public static void MakeACommit(this RepositoryFixtureBase fixture, string commit
1313

1414
var participant = GetParticipant(fixture.Repository.Head.FriendlyName);
1515
if (participant != null)
16-
diagramBuilder?.AppendLineFormat("{0} -> {0}: Commit '{1}'", participant, commitMsg);
17-
return;
16+
{
17+
AddTheCommitMessage(fixture, commitMsg, diagramBuilder, participant);
18+
}
1819

1920
string? GetParticipant(string participantName) =>
2021
(string?)typeof(SequenceDiagram).GetMethod("GetParticipant", BindingFlags.Instance | BindingFlags.NonPublic)
@@ -23,4 +24,17 @@ public static void MakeACommit(this RepositoryFixtureBase fixture, string commit
2324
participantName
2425
]);
2526
}
27+
28+
private static void AddTheCommitMessage(RepositoryFixtureBase fixture, string commitMsg, StringBuilder? diagramBuilder, string participant)
29+
{
30+
if (commitMsg.Length < 40)
31+
{
32+
diagramBuilder?.AppendLineFormat("{0} -> {0}: Commit '{1}'", participant, commitMsg);
33+
}
34+
else
35+
{
36+
var formattedCommitMsg = string.Join(System.Environment.NewLine, $"Commit '{commitMsg}'".SplitIntoLines(60));
37+
fixture.SequenceDiagram.NoteOver(formattedCommitMsg, participant);
38+
}
39+
}
2640
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace GitVersion.Core.Tests.IntegrationTests;
2+
3+
public static class StringExtensions
4+
{
5+
public static IEnumerable<string> SplitIntoLines(this string str, int maxLineLength)
6+
{
7+
if (string.IsNullOrEmpty(str)) yield break;
8+
9+
foreach (var line in SplitByNewlines(str))
10+
{
11+
foreach (var wrapped in WrapWithWordBoundaries(line, maxLineLength))
12+
{
13+
yield return wrapped;
14+
}
15+
}
16+
}
17+
18+
private static IEnumerable<string> SplitByNewlines(string str)
19+
=> str.Split(["\r\n", "\n"], StringSplitOptions.None);
20+
21+
private static IEnumerable<string> WrapWithWordBoundaries(string line, int maxLength)
22+
{
23+
if (string.IsNullOrWhiteSpace(line))
24+
{
25+
yield return string.Empty;
26+
yield break;
27+
}
28+
29+
var index = 0;
30+
while (index < line.Length)
31+
{
32+
var wrapAt = GetWrapIndex(line, index, maxLength);
33+
yield return line.Substring(index, wrapAt - index).TrimEnd();
34+
index = wrapAt;
35+
}
36+
}
37+
38+
private static int GetWrapIndex(string line, int start, int maxLength)
39+
{
40+
var remaining = line.Length - start;
41+
if (remaining <= maxLength)
42+
{
43+
return line.Length;
44+
}
45+
46+
var end = start + maxLength;
47+
var lastBreak = line.LastIndexOfAny([' ', '-'], end - 1, maxLength);
48+
return lastBreak > start ? lastBreak + 1 : end;
49+
}
50+
}

0 commit comments

Comments
 (0)