Skip to content

Commit

Permalink
Use git tags for version source and allow no tags
Browse files Browse the repository at this point in the history
  • Loading branch information
elestedt committed Jun 5, 2020
1 parent ac97878 commit 4599a6b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ See `dotnet-gitchanges\appsettings.json` for the default settings file. Any sett
"OverrideSource": "someOverrideSource.txt"
},
"FileSource": "someHistorialChanges.txt",
"MultiProject": false
"MultiProject": false,
"UseGitTags": false
}
```
***Descriptions***
Expand All @@ -101,6 +102,7 @@ See `dotnet-gitchanges\appsettings.json` for the default settings file. Any sett
| Repository.OverrideSource | Path to override source (see [Overriding repository changes](#overriding-repository-changes))
| FileSource | Path to file source (see [Existing repository](#existing-repository)).
| MultiProject | Specifies whether repository should be processed as a multi project repository.
| VersionFromGitTag | If true and the commit message does not contain a version, it will be taken from commit tag if possible. All untagged commits will be grouped under the nearest subsequent tagged version.

#### Custom Template
Currently the only supported templating syntax supported is [Mustache](http://mustache.github.io/mustache.5.html). See `dotnet-gitchanges\KeepAChangelogTemplate.Mustache` for the default template file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Gitchanges.Changes;
using Gitchanges.Configuration;
using Gitchanges.Readers.Parsers;
Expand All @@ -18,7 +19,31 @@ public class DefaultCommitParserTests
Version = "version:(.*)[\n]?",
Tag = "tag:(.*)[\n]?"
};


[Test]
public void VerifyParsesWithGitTags()
{
var gitTags = new Dictionary<string, string>();
gitTags.Add("THISCOULDBEASHA", "0.0.0");
var expectedChange = new DefaultChange("0.0.0", "Added", "Some Summary", DateTimeOffset.Now);
var parser = new DefaultCommitParser(_defaultPatterns, gitTags);

var actual = parser.Parse(MockCommit(expectedChange));
Assert.That(actual, Is.EqualTo(expectedChange));
}

[Test]
public void VerifyParsesWithGitTagsNoTagMatch()
{
var gitTags = new Dictionary<string, string>();
gitTags.Add("THISCOULDBEASHA_OTHER", "0.0.0");
var expectedChange = new DefaultChange("Unreleased", "Added", "Some Summary", DateTimeOffset.Now);
var parser = new DefaultCommitParser(_defaultPatterns, gitTags);

var actual = parser.Parse(MockCommit(expectedChange));
Assert.That(actual, Is.EqualTo(expectedChange));
}

[Test]
public void VerifyParsesWithoutReference()
{
Expand Down Expand Up @@ -93,6 +118,7 @@ private static Commit MockCommit(IChange change)
version: {change.Version}
tag: {change.Tag}
";
commitMock.SetupGet(x => x.Sha).Returns("THISCOULDBEASHA");
commitMock.SetupGet(x => x.MessageShort).Returns(change.Summary);
commitMock.SetupGet(x => x.Author).Returns(commitAuthor);
commitMock.SetupGet(x => x.Message).Returns(expectedMessage);
Expand Down
1 change: 1 addition & 0 deletions dotnet-gitchanges/Configuration/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class AppConfig
public RepositoryConfig Repository { get; set; }
public string FileSource { get; set; }
public bool MultiProject { get; set; }
public bool VersionFromGitTag { get; set; }
}
}
13 changes: 11 additions & 2 deletions dotnet-gitchanges/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,17 @@ static void Main(string[] args)
var overrideFileReader = new FileReader<OverrideChange>(appConfig.Repository.OverrideSource, new OverrideSourceRowParser(Console.Error));
idToOverrideChange = overrideFileReader.Values().ToDictionary<OverrideChange, string, IChange>(change => change.Id, change => change);
}

var gitReader = new GitReader<IChange>(repo, new DefaultCommitParser(appConfig.Parsing), idToOverrideChange);
Dictionary<string, string> commitShaToTagName = null;
if (appConfig.VersionFromGitTag)
{
commitShaToTagName = new Dictionary<string, string>();
foreach (var tag in repo.Tags)
{
commitShaToTagName.Add(tag.Target.Sha, tag.FriendlyName);
}
}
var commitParser = new DefaultCommitParser(appConfig.Parsing, commitShaToTagName);
var gitReader = new GitReader<IChange>(repo, commitParser, idToOverrideChange);
readers.Add(gitReader);

var cache = new ChangeCache();
Expand Down
32 changes: 24 additions & 8 deletions dotnet-gitchanges/Readers/Parsers/DefaultCommitParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Gitchanges.Changes;
using Gitchanges.Configuration;
Expand All @@ -9,24 +10,39 @@ namespace Gitchanges.Readers.Parsers
public class DefaultCommitParser : ICommitParser<IChange>
{
private const string Unreleased = "Unreleased";
private const string Uncategorized = "Uncategorized";
private readonly ParsingPatterns _patterns;
private string _lastVersion = Unreleased;
private IDictionary<string, string> _commitShaToTagName;

public DefaultCommitParser(ParsingPatterns patterns)
public DefaultCommitParser(ParsingPatterns patterns, IDictionary<string, string> commitShaToTagName = null)
{
_commitShaToTagName = commitShaToTagName;
_patterns = patterns;
}

public IChange Parse(Commit commit)
{
var reference = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Reference));
var version = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Version));
var tag = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Tag));

if (string.IsNullOrEmpty(version) || string.IsNullOrEmpty(tag)) return null;
var tag = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Tag), _commitShaToTagName != null ? Uncategorized : "");
string version = "";
if (_commitShaToTagName != null)
{
version = Unreleased;
if (_commitShaToTagName.ContainsKey(commit.Sha))
{
version = _commitShaToTagName[commit.Sha];
_lastVersion = version;
}
}
else
{
version = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Version));
if (string.IsNullOrEmpty(version)) return null;
}

version = HandleVersion(version);

return new DefaultChange(version, tag, commit.MessageShort, commit.Author.When, reference);
}

Expand All @@ -36,9 +52,9 @@ public IChange Parse(IChange overrideObject)
return overrideObject;
}

private static string GetMatchOrDefault(Match match)
private static string GetMatchOrDefault(Match match, string def = "")
{
return match.Success ? match.Groups[1].Value.Trim() : "";
return match.Success ? match.Groups[1].Value.Trim() : def;
}

private string HandleVersion(string version)
Expand Down
3 changes: 2 additions & 1 deletion dotnet-gitchanges/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"OverrideSource": ""
},
"FileSource": "",
"MultiProject": false
"MultiProject": false,
"VersionFromGitTag": false
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.9.0

0 comments on commit 4599a6b

Please sign in to comment.