Skip to content

Commit

Permalink
Fix error when git log returns an empty string
Browse files Browse the repository at this point in the history
And add a bunch of debug instructions
  • Loading branch information
piuccio committed Jan 25, 2017
1 parent c739b44 commit 0881f5d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"node": true
},
"extends": "eslint:recommended",
"rules": {
"no-console": 0
}
}
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This are for instance the release notes generated from `joyent/node` running

git-release-notes v0.9.8..v0.9.9 html > changelog.html

<a href="https://github.com/ariatemplates/git-release-notes/raw/master/templates/node.png" target="_blank"><img src="https://github.com/ariatemplates/git-release-notes/raw/master/templates/node_thumb.png" alt="Node's release notes"></a>
[<img src="https://github.com/ariatemplates/git-release-notes/raw/master/templates/node_thumb.png" alt="Node's release notes">](https://github.com/ariatemplates/git-release-notes/raw/master/templates/node.png)

#### Custom template

Expand Down Expand Up @@ -68,7 +68,9 @@ For instance, [Aria Templates](https://github.com/ariatemplates/ariatemplates) h

In this case using

git-release-notes -t "^([a-z]+) #(\d+) (.*)$" -m type -m issue -m title v1.3.6..HEAD html
```
git-release-notes -t "^([a-z]+) #(\d+) (.*)$" -m type -m issue -m title v1.3.6..HEAD html
```

generates the additional fields on the commit object

Expand All @@ -79,4 +81,13 @@ generates the additional fields on the commit object

Another project using similar conventions is [AngularJs](https://github.com/angular/angular.js), [commit message conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#).

git-release-notes -t "^(\w*)(?:\(([\w\$\.]*)\))?\: (.*)$" -m type -m scope -m title v1.1.2..v1.1.3 markdown
```
git-release-notes -t "^(\w*)(?:\(([\w\$\.]*)\))?\: (.*)$" -m type -m scope -m title v1.1.2..v1.1.3 markdown
```


### Debug

If the output is not what you expect run

`DEBUG=release-notes:* git-release-notes ...`
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ var git = require("./lib/git");
var fs = require("fs");
var ejs = require("ejs");
var path = require("path");
var debug = require("debug")("release-notes:cli");

var template = argv._[1];
debug("Trying to locate template '%s'", template);
if (!fs.existsSync(template)) {
debug("Template file '%s' doesn't exist, maybe it's template name", template);
// Template name?
if (template.match(/[a-z]+(\.ejs)?/)) {
template = path.resolve(__dirname, "./templates/" + path.basename(template, ".ejs") + ".ejs");
Expand All @@ -51,31 +54,41 @@ if (!fs.existsSync(template)) {
process.exit(1);
}
}
debug("Trying to read template '%s'", template);
fs.readFile(template, function (err, templateContent) {
if (err) {
require("optimist").showHelp();
console.error("\nUnable to locate template file " + argv._[1]);
process.exit(5);
} else {
getOptions(function (options) {
debug("Running git log in '%s' on branch '%s' with range '%s'", options.p, options.b, argv._[0]);
git.log({
branch : options.b,
range : argv._[0],
title : new RegExp(options.t),
meaning : Array.isArray(options.m) ? options.m : [options.m],
cwd : options.p
}, function (commits) {
var output = ejs.render(templateContent.toString(), {
commits : commits
});
process.stdout.write(output + "\n");
debug("Got %d commits", commits.length);
if (commits.length) {
debug("Rendering template");
var output = ejs.render(templateContent.toString(), {
commits : commits
});
process.stdout.write(output + "\n");
} else {
console.error('No commits in the specified range');
process.exit(6);
}
});
});
}
});

function getOptions (callback) {
if (argv.f) {
debug("Trying to read configuration file '%s'", argv.f);
fs.readFile(argv.f, function (err, data) {
if (err) {
console.error("Unable to read configuration file\n" + err.message);
Expand All @@ -100,4 +113,4 @@ function getOptions (callback) {
} else {
callback(argv);
}
}
}
28 changes: 24 additions & 4 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var debug = require("debug")("release-notes:git");
var parser = require("debug")("release-notes:parser");

exports.log = function (options, callback) {
var spawn = require("child_process").spawn;
var gitArgs = ["log", "--no-color", "--no-merges", "--branches=" + options.branch, "--format=" + formatOptions, options.range];
debug("Spawning git with args %o", gitArgs);
var gitLog = spawn("git", gitArgs, {
cwd : options.cwd,
stdio : ["ignore", "pipe", process.stderr]
Expand All @@ -12,10 +16,17 @@ exports.log = function (options, callback) {
});

gitLog.on("exit", function (code) {
debug("Git command exited with code '%d'", code);
if (code === 0) {
// Build the list of commits from git log
var commits = processCommits(allCommits.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''), options);
callback(commits);
allCommits = normalizeNewlines(allCommits).trim();

if (allCommits) {
// Build the list of commits from git log
var commits = processCommits(allCommits, options);
callback(commits);
} else {
callback([]);
}
}
});
};
Expand All @@ -32,8 +43,11 @@ function processCommits (commitMessages, options) {
var stream = commitMessages.split("\n");
var commits = [];
var workingCommit;
parser("Iterating on %d lines", stream.length);
stream.forEach(function (rawLine) {
parser("Raw line\n\t%s", rawLine);
var line = parseLine(rawLine);
parser("Parsed line %o", line);
if (line.type === "new") {
workingCommit = {
messageLines : []
Expand All @@ -43,6 +57,7 @@ function processCommits (commitMessages, options) {
workingCommit.messageLines.push(line.message);
} else if (line.type === "title") {
var title = parseTitle(line.message, options);
parser("Parsed title %o", title);
for (var prop in title) {
workingCommit[prop] = title[prop];
}
Expand Down Expand Up @@ -82,6 +97,7 @@ function parseLine (line) {
function parseTitle (title, options) {
var expression = options.title;
var names = options.meaning;
parser("Parsing title '%s' with regular expression '%s' and meanings %o", title, expression, names);

var match = title.match(expression);
if (!match) {
Expand All @@ -97,4 +113,8 @@ function parseTitle (title, options) {
}
return builtObject;
}
}
}

function normalizeNewlines(message) {
return message.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
"compare",
"version"
],
"scripts": {
"lint": "eslint index.js lib",
"posttest": "npm run lint"
},
"version": "0.0.3",
"dependencies": {
"debug": "^2.6.0",
"ejs": "^2.5.5",
"optimist": "^0.6.1"
},
Expand All @@ -25,5 +30,8 @@
"url": "https://github.com/ariatemplates/git-release-notes"
},
"homepage": "https://github.com/ariatemplates/git-release-notes",
"preferGlobal": true
"preferGlobal": true,
"devDependencies": {
"eslint": "^3.14.0"
}
}

0 comments on commit 0881f5d

Please sign in to comment.