Skip to content

Commit ee7a667

Browse files
committed
Add --include-branch option
Fixes #38
1 parent e8b7a83 commit ee7a667

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Options:
3737
--ignore-commit-pattern [regex] # pattern to ignore when parsing commits
3838
--starting-commit [hash] # starting commit to use for changelog generation
3939
--tag-prefix [prefix] # prefix used in version tags, default: v
40+
--include-branch [branch] # one or more branches to include commits from, comma separated
4041
-V, --version # output the version number
4142
-h, --help # output usage information
4243

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"commander": "^2.9.0",
4545
"fs-extra": "^5.0.0",
4646
"handlebars": "^4.0.11",
47+
"lodash.uniqby": "^4.7.0",
4748
"parse-github-url": "^1.0.1",
4849
"semver": "^5.1.0"
4950
},

src/commits.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const MERGE_PATTERNS = [
1818
/Merge branch .+ into .+\n\n(.+)[\S\s]+See merge request [^!]*!(\d+)/ // GitLab merge
1919
]
2020

21-
export async function fetchCommits (remote, options) {
22-
const log = await cmd(`git log --shortstat --pretty=format:${LOG_FORMAT}`)
21+
export async function fetchCommits (remote, options, branch = null) {
22+
const command = branch ? `git log ${branch}` : 'git log'
23+
const log = await cmd(`${command} --shortstat --pretty=format:${LOG_FORMAT}`)
2324
return parseCommits(log, remote, options)
2425
}
2526

src/releases.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export function parseReleases (commits, remote, latestVersion, options) {
3838
return releases
3939
}
4040

41+
export function sortReleases (a, b) {
42+
return a.tag && b.tag ? semver.rcompare(a.tag, b.tag) : 0
43+
}
44+
4145
function newRelease (tag = null, date = new Date().toISOString()) {
4246
const release = {
4347
commits: [],

src/run.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Command } from 'commander'
22
import { readJson, writeFile, pathExists } from 'fs-extra'
33
import semver from 'semver'
4+
import uniqBy from 'lodash.uniqby'
45

56
import { version } from '../package.json'
67
import { fetchRemote } from './remote'
78
import { fetchCommits } from './commits'
8-
import { parseReleases } from './releases'
9+
import { parseReleases, sortReleases } from './releases'
910
import { compileTemplate } from './template'
1011
import { parseLimit } from './utils'
1112

@@ -33,6 +34,7 @@ function getOptions (argv, pkg) {
3334
.option('--ignore-commit-pattern [regex]', `pattern to ignore when parsing commits`)
3435
.option('--starting-commit [hash]', `starting commit to use for changelog generation`)
3536
.option('--tag-prefix [prefix]', `prefix used in version tags`)
37+
.option('--include-branch [branch]', `one or more branches to include commits from, comma separated`, str => str.split(','))
3638
.version(version)
3739
.parse(argv)
3840

@@ -66,13 +68,27 @@ function getLatestVersion (options, pkg, commits) {
6668
return null
6769
}
6870

71+
async function getReleases (commits, remote, latestVersion, options) {
72+
let releases = parseReleases(commits, remote, latestVersion, options)
73+
if (options.includeBranch) {
74+
for (const branch of options.includeBranch) {
75+
const commits = await fetchCommits(remote, options, branch)
76+
releases = [
77+
...releases,
78+
...parseReleases(commits, remote, latestVersion, options)
79+
]
80+
}
81+
}
82+
return uniqBy(releases, 'tag').sort(sortReleases)
83+
}
84+
6985
export default async function run (argv) {
7086
const pkg = await pathExists('package.json') && await readJson('package.json')
7187
const options = getOptions(argv, pkg)
7288
const remote = await fetchRemote(options.remote)
7389
const commits = await fetchCommits(remote, options)
7490
const latestVersion = getLatestVersion(options, pkg, commits)
75-
const releases = parseReleases(commits, remote, latestVersion, options)
91+
const releases = await getReleases(commits, remote, latestVersion, options)
7692
const log = await compileTemplate(options.template, { releases })
7793
await writeFile(options.output, log)
7894
return `${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}`

test/run.js

+16
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ describe('run', () => {
140140
return run(['', '', '--unreleased'])
141141
})
142142

143+
it('supports includeBranch option', () => {
144+
mock('fetchCommits', (remote, options, branch) => {
145+
if (branch === 'another-branch') {
146+
return commits.concat({
147+
date: '2015-12-15T12:03:09.000Z',
148+
tag: 'v0.2.0'
149+
})
150+
}
151+
return commits
152+
})
153+
mock('writeFile', (output, log) => {
154+
expect(log).to.include('v0.2.0')
155+
})
156+
return run(['', '', '--include-branch', 'another-branch'])
157+
})
158+
143159
it('does not error when using latest version option', () => {
144160
return run(['', '', '--latest-version', '3.0.0'])
145161
})

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,10 @@ lodash.cond@^4.3.0:
21882188
version "4.5.2"
21892189
resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5"
21902190

2191+
lodash.uniqby@^4.7.0:
2192+
version "4.7.0"
2193+
resolved "https://registry.yarnpkg.com/lodash.uniqby/-/lodash.uniqby-4.7.0.tgz#d99c07a669e9e6d24e1362dfe266c67616af1302"
2194+
21912195
lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
21922196
version "4.17.4"
21932197
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

0 commit comments

Comments
 (0)