-
Notifications
You must be signed in to change notification settings - Fork 900
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (102 loc) · 3.59 KB
/
Program.cs
File metadata and controls
123 lines (102 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
class Program
{
static readonly Regex SectionRegex = new(@"^\+\s*##\s+(.*)");
static readonly Regex ItemRegex = new(@"^[+-]\s*-\s*(.*)");
static void Main(string[] args)
{
// Default: last commit
string commitRange = args.Length > 0 ? args[0] : "HEAD~1..HEAD";
var diffLines = RunGitDiff(commitRange);
var notes = ParseDiff(diffLines);
PrintReleaseNotes(notes);
}
static List<string> RunGitDiff(string range)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = $"diff {range}",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
var lines = new List<string>();
while (!process.StandardOutput.EndOfStream)
{
lines.Add(process.StandardOutput.ReadLine()!);
}
process.WaitForExit();
return lines;
}
static Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> ParseDiff(
List<string> lines)
{
var notes = new Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>>();
string? currentFile = null;
string? currentSection = null;
foreach (var line in lines)
{
if (line.StartsWith("diff --git"))
{
currentFile = null;
currentSection = null;
continue;
}
if (line.StartsWith("+++ b/") && line.EndsWith(".md"))
{
currentFile = line.Replace("+++ b/", "");
notes.TryAdd(currentFile, new());
continue;
}
if (currentFile != null && SectionRegex.IsMatch(line))
{
currentSection = SectionRegex.Match(line).Groups[1].Value;
notes[currentFile].TryAdd(currentSection, new());
continue;
}
if (currentFile != null && currentSection != null && ItemRegex.IsMatch(line))
{
var item = ItemRegex.Match(line).Groups[1].Value;
var changeType = line.StartsWith("+") ? "Added" : "Removed";
if (!notes[currentFile][currentSection].ContainsKey(changeType))
{
notes[currentFile][currentSection][changeType] = new();
}
notes[currentFile][currentSection][changeType].Add(item);
}
}
return notes;
}
static void PrintReleaseNotes(
Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> notes)
{
foreach (var file in notes)
{
var title = System.IO.Path.GetFileNameWithoutExtension(file.Key)
.Replace("-", " ");
title = char.ToUpper(title[0]) + title[1..];
Console.WriteLine($"\n## {title}\n");
foreach (var section in file.Value)
{
Console.WriteLine($"### {section.Key}");
foreach (var change in section.Value)
{
Console.WriteLine($"**{change.Key}**");
foreach (var item in change.Value)
{
Console.WriteLine($"- {item}");
}
}
Console.WriteLine();
}
}
}
}