Skip to content

Commit

Permalink
Add --tag-prefix option
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
cookpete committed Jan 15, 2018
1 parent b6edd20 commit f77627e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options:
-i, --issue-url [url] # override url for issues, use {id} for issue id
--issue-pattern [regex] # override regex pattern for issues in commit messages
--starting-commit [hash] # starting commit to use for changelog generation
--tag-prefix [prefix] # prefix used in version tags, default: v
-V, --version # output the version number
-h, --help # output usage information

Expand Down Expand Up @@ -65,6 +66,13 @@ By default, changelogs will link to the appropriate pages for commits, issues an
auto-changelog --issue-url https://www.redmine.org/issues/{id}
```

Use `--tag-prefix [prefix]` if you prefix your version tags with a certain string:

```bash
# When all versions are tagged like my-package/1.2.3
auto-changelog --tag-prefix my-package/
```

You can also set any option in `package.json` under the `auto-changelog` key, using camelCase options:

```js
Expand Down
9 changes: 4 additions & 5 deletions src/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const MESSAGE_SEPARATOR = '__AUTO_CHANGELOG_MESSAGE_SEPARATOR__'
const LOG_FORMAT = COMMIT_SEPARATOR + '%H%n%D%n%aI%n%an%n%ae%n%B' + MESSAGE_SEPARATOR
const MATCH_COMMIT = /(.*)\n(.*)\n(.*)\n(.*)\n(.*)\n([\S\s]+)/
const MATCH_STATS = /(\d+) files? changed(?:, (\d+) insertions?...)?(?:, (\d+) deletions?...)?/
const TAG_PREFIX = 'tag: '

// https://help.github.com/articles/closing-issues-via-commit-messages
const DEFAULT_FIX_PATTERN = /(?:close[sd]?|fixe?[sd]?|resolve[sd]?)\s(?:#(\d+)|(https?:\/\/.+?\/(?:issues|pull|pull-requests|merge_requests)\/(\d+)))/gi
Expand Down Expand Up @@ -48,7 +47,7 @@ function parseCommit (commit, origin, options = {}) {
author,
email,
date,
tag: getTag(refs),
tag: getTag(refs, options),
subject: getSubject(message),
message: message.trim(),
fixes: getFixes(message, origin, options),
Expand All @@ -58,11 +57,11 @@ function parseCommit (commit, origin, options = {}) {
}
}

function getTag (refs) {
function getTag (refs, options) {
if (!refs) return null
for (let ref of refs.split(', ')) {
if (ref.indexOf(TAG_PREFIX) === 0) {
return ref.replace(TAG_PREFIX, '')
if (ref.indexOf(`tag: ${options.tagPrefix}`) === 0) {
return ref.replace('tag: ', '')
}
}
return null
Expand Down
7 changes: 4 additions & 3 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const DEFAULT_OPTIONS = {
output: 'CHANGELOG.md',
template: 'compact',
remote: 'origin',
commitLimit: 3
commitLimit: 3,
tagPrefix: 'v'
}

const NPM_VERSION_TAG_PREFIX = 'v'
const PACKAGE_OPTIONS_KEY = 'auto-changelog'

function getOptions (argv, pkg) {
Expand All @@ -31,6 +31,7 @@ function getOptions (argv, pkg) {
.option('-i, --issue-url [url]', `override url for issues, use {id} for issue id`)
.option('--issue-pattern [regex]', `override regex pattern for issues in commit messages`)
.option('--starting-commit [hash]', `starting commit to use for changelog generation`)
.option('--tag-prefix [prefix]', `prefix used in version tags, default: ${DEFAULT_OPTIONS.tagPrefix}`)
.version(version)
.parse(argv)

Expand Down Expand Up @@ -58,7 +59,7 @@ function getLatestVersion (options, pkg) {
return options.latestVersion
}
if (options.package) {
return NPM_VERSION_TAG_PREFIX + pkg.version
return options.tagPrefix + pkg.version
}
return null
}
Expand Down
8 changes: 6 additions & 2 deletions test/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ const parseCommits = __get__('parseCommits')
const getFixes = __get__('getFixes')
const getMerge = __get__('getMerge')

const options = {
tagPrefix: 'v'
}

describe('fetchCommits', () => {
it('fetches commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
mock('cmd', () => gitLog)
expect(await fetchCommits(origins.github)).to.deep.equal(commits)
expect(await fetchCommits(origins.github, options)).to.deep.equal(commits)
unmock('cmd')
})
})

describe('parseCommits', () => {
it('parses commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
expect(parseCommits(gitLog, origins.github)).to.deep.equal(commits)
expect(parseCommits(gitLog, origins.github, options)).to.deep.equal(commits)
})

it('parses bitbucket commit', async () => {
Expand Down

0 comments on commit f77627e

Please sign in to comment.