Skip to content

Commit

Permalink
feat(log): Add smart 'github' option for commit links
Browse files Browse the repository at this point in the history
* Uses a github repository, if found, to link to commits in the
 changelog
* If no github option is specified, tries to read the homepage from the
 'repository' or 'homepage' field of grunt.config('pkg')
  • Loading branch information
ajoslin committed May 3, 2013
1 parent 04ecfce commit 6ac1083
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ Defaults to `true`. If true, prepend new log info to `dest`. If `false`, append
### templateFile
Template to use. See the [default template](https://raw.github.com/btford/grunt-conventional-changelog/master/template/changelog.md) (used if another isn't provided) for an example of how to write your own.

### github
The github repository to use to link to commits in the changelog. Defaults to trying to find a github repository in package.json.

Example configs:
```js
github: 'btford/grunt-conventional-changelog'
```
```js
github: 'http://github.com/angular/angular.js'
```



## Usage Examples
Expand Down
46 changes: 43 additions & 3 deletions tasks/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,39 @@ module.exports = function (grunt) {

var options = this.options({
// dest: 'CHANGELOG.md',
prepend: true // false to append
prepend: true, // false to append
});

var githubRepo;
var pkg = grunt.config('pkg');
//If github repo isn't given, try to read it from the package file
if (!options.github && pkg) {
if (pkg.repository) {
githubRepo = pkg.repository.url;
} else if (pkg.homepage) {
//If it's a github page, but not a *.github.(io|com) page
if (pkg.homepage.indexOf('github.com') > -1 &&
!pkg.homepage.match(/\.github\.(com|io)/)) {
githubRepo = pkg.homepage;
}
}
} else {
githubRepo = options.github;
}
function fixGithubRepo(githubRepo) {
//User could set option eg 'github: "btford/grunt-conventional-changelog'
if (githubRepo.indexOf('github.com') === -1) {
githubRepo = 'http://github.com/' + githubRepo;
}
return githubRepo
.replace(/^git:\/\//, 'http://') //get rid of git://
.replace(/\/$/, '') //get rid of trailing slash
.replace(/\.git$/, ''); //get rid of trailing .git
}
if (githubRepo) {
githubRepo = fixGithubRepo(githubRepo);
}

var template;
if (options.templateFile) {
template = grunt.file.read(options.templateFile);
Expand Down Expand Up @@ -79,7 +109,7 @@ module.exports = function (grunt) {

gitlog.forEach(function (logItem) {
var lines = logItem.split('\n');
var sha1 = lines.shift().substr(0,8); //Only need first 7 chars
var sha1 = lines.shift().substr(0,8); //Only need first 7 chars
var subject = lines.shift();

var changeMatch,
Expand Down Expand Up @@ -110,7 +140,17 @@ module.exports = function (grunt) {
data: {
changelog: changelog,
today: grunt.template.today('yyyy-mm-dd'),
version : grunt.config('pkg.version')
version : grunt.config('pkg.version'),
helpers: {
//Generates a commit link if we have a repo, else it generates a plain text commit sha1
commitLink: function(commit) {
if (githubRepo) {
return '[' + commit + '](' + githubRepo + '/commits/' + commit + ')';
} else {
return commit;
}
}
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions template/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<% if (_(changelog.feat).size() > 0) { %>## Features
<% _(changelog.feat).forEach(function(changes, scope) { %>### <%= scope%>
<% changes.forEach(function(change) { %>
* <%= change.msg%> (<%= change.sha1%>)
* <%= change.msg%> (<%= helpers.commitLink(change.sha1) %>)
<% }) %>
<% }) %><% } %>

<% if (_(changelog.fix).size() > 0) { %>## Bug fixes
<% _(changelog.fix).forEach(function(changes, scope) { %>### <%= scope%>
<% changes.forEach(function(change) { %>
* <%= change.msg%> (<%= change.sha1%>)
* <%= change.msg%> (<%= helpers.commitLink(change.sha1) %>)
<% }) %>
<% }) %><% } %>

Expand Down

0 comments on commit 6ac1083

Please sign in to comment.