Skip to content

Commit

Permalink
Add tag information on commit array (#51)
Browse files Browse the repository at this point in the history
* Add tag informations into commits array

* Better way to parse tags, works when there are other references
  • Loading branch information
piuccio authored Dec 27, 2018
1 parent a4a5a79 commit 23de6fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Several template variables are made available to the script running inside the t
* `committerEmail` committer email (%ce)
* `committerDate` committer date (%cD)
* `title` subject (%s)
* `tag` tag (%D)
* `messageLines` array of body lines (%b)

`dateFnsFormat` is the date-fns [format](https://date-fns.org/docs/format) function. See the [html-bootstrap](https://github.com/ariatemplates/git-release-notes/blob/master/templates/html-bootstrap.ejs) for sample usage.
Expand Down
18 changes: 15 additions & 3 deletions lib/__tests__/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ describe('log commits', () => {
'Add option for using "--merges" with git log',
]);
});

it('list tags', () => {
const OPTIONS = {
branch: 'testing-branch',
range: '575fef3..28b863e',
};

return expectTags(OPTIONS).toEqual([
'v2.1.0',
'no tag',
'v2.0.0'
]);
});
});

function expectCommits(options) {
return expect(gitLog(options).then((commits) => commits.map((c) => c.title))).resolves;
}
const expectCommits = (_) => expect(gitLog(_).then((commits) => commits.map((c) => c.title))).resolves;
const expectTags = (_) => expect(gitLog(_).then((commits) => commits.map((c) => c.tag || 'no tag'))).resolves;
21 changes: 18 additions & 3 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports.log = function (options) {
return new Promise(function (resolve, reject) {
var spawn = require("child_process").spawn;
var gitArgs = ["log", "--no-color"];
if (options.additionalOptions.length > 0) {
if (options.additionalOptions && options.additionalOptions.length > 0) {
options.additionalOptions.forEach(function(o) {
gitArgs.push("--" + o);
});
Expand All @@ -15,6 +15,7 @@ exports.log = function (options) {
gitArgs.push(
"--branches=" + options.branch,
"--format=" + formatOptions,
"--decorate=full",
options.range
);
debug("Spawning git with args %o", gitArgs);
Expand Down Expand Up @@ -52,7 +53,7 @@ var newCommit = "___";
var formatOptions = [
newCommit, "sha1:%H", "authorName:%an", "authorEmail:%ae", "authorDate:%aD",
"committerName:%cn", "committerEmail:%ce", "committerDate:%cD",
"title:%s", "%w(80,1,1)%b"
"title:%s", "%D", "%w(80,1,1)%b"
].join("%n");

function processCommits (commitMessages, options) {
Expand Down Expand Up @@ -82,6 +83,11 @@ function processCommits (commitMessages, options) {
// The parser doesn't return a title
workingCommit.title = line.message;
}
} else if (line.type === "tag") {
parser("Trying to parse tag %o", line.message);
var tag = parseTag(line.message);
parser("Parse tag %o", tag);
workingCommit[line.type] = tag;
} else {
workingCommit[line.type] = line.message;
}
Expand Down Expand Up @@ -113,7 +119,7 @@ function parseLine (line) {

function parseTitle (title, options) {
var expression = options.title;
var names = options.meaning;
var names = options.meaning || [];
parser("Parsing title '%s' with regular expression '%s' and meanings %o", title, expression, names);

var match = title.match(expression);
Expand All @@ -132,6 +138,15 @@ function parseTitle (title, options) {
}
}

function parseTag (line) {
var refs = line.split(/(refs)\/(tags|remotes|heads)\//);
var tagIndex = refs.findIndex((token, index, all) => all[index - 2] === 'refs' && all[index - 1] === 'tags');
if (tagIndex === refs.length - 1) {
return refs[tagIndex];
}
return refs[tagIndex].replace(/, $/, '');
}

function normalizeNewlines(message) {
return message.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
}

0 comments on commit 23de6fc

Please sign in to comment.