Skip to content

Commit 17d363e

Browse files
authored
feat: add plaintext option (#22)
1 parent 5f6f7b1 commit 17d363e

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ But the comparison isn't quite as strict, generally leading to a shorter list of
2929
* `--exclude-label`: Exclude any commits from the list that come from a GitHub pull request with the given label. Multiple `--exclude-label` options may be provided, they will also be split by `,`. e.g. `--exclude-label=semver-major,meta`.
3030
* `--require-label`: Only include commits in the list that come from a GitHub pull request with the given label. Multiple `--require-label` options may be provided, they will also be split by `,`. e.g. `--require-label=test,doc`.
3131
* `--patch-only`: An alias for `--exclude-label=semver-major,semver-minor`.
32-
* `--format`: Dictates what formatting the output will have. Possible options are: `simple`, `sha`. The default is to print markdown-formatted output.
32+
* `--format`: Dictates what formatting the output will have. Possible options are: `simple`, `plaintext`, and `sha`. The default is to print markdown-formatted output; `plaintext` also implies that commits will be grouped.
3333
- `simple`: Don't print full markdown output, good for console printing without the additional fluff.
3434
- `sha`: Print only the 10-character truncated commit shasums. Good for piping though additional tooling, such as `xargs git cherry-pick` for applying commits.
3535
* `--simple` or `-s`: An alias for `--format=simple`.

branch-diff.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const fs = require('fs')
1010
, pkgtoId = require('pkg-to-id')
1111
, stripAnsi = require('strip-ansi')
1212
, map = require('map-async')
13-
, commitToOutput = require('changelog-maker/commit-to-output')
13+
, { commitToOutput } = require('changelog-maker/commit-to-output')
1414
, collectCommitLabels = require('changelog-maker/collect-commit-labels')
1515
, groupCommits = require('changelog-maker/group-commits')
16-
, isReleaseCommit = require('changelog-maker/groups').isReleaseCommit
16+
, { isReleaseCommit, toGroups } = require('changelog-maker/groups')
1717
, gitexec = require('gitexec')
1818

1919
, pkgFile = path.join(process.cwd(), 'package.json')
@@ -28,7 +28,22 @@ const fs = require('fs')
2828
}
2929
, defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
3030

31+
const formatType = {
32+
PLAINTEXT: 'plaintext',
33+
MARKDOWN: 'markdown',
34+
SIMPLE: 'simple',
35+
SHA: 'sha'
36+
}
3137

38+
const getFormat = (argv) => {
39+
if (argv.format && Object.values(formatType).includes(argv.format)) {
40+
return argv.format
41+
} else if (argv.simple || argv.s) {
42+
return formatType.SIMPLE
43+
}
44+
return formatType.MARKDOWN
45+
}
46+
3247
function replace (s, m) {
3348
Object.keys(m).forEach(function (k) {
3449
s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
@@ -116,12 +131,28 @@ function diffCollected (options, branchCommits, callback) {
116131
})
117132
}
118133

119-
120134
function printCommits (list, format, reverse, commitUrl) {
121-
if (format === 'sha') {
135+
if (format === formatType.SHA) {
122136
list = list.map((commit) => `${commit.sha.substr(0, 10)}`)
137+
} else if (format === formatType.SIMPLE) {
138+
list = list.map((commit) => commitToOutput(commit, formatType.SIMPLE, ghId, commitUrl))
139+
} else if (format === formatType.PLAINTEXT) {
140+
// Plaintext format implies grouping.
141+
list = groupCommits(list)
142+
143+
const formatted = []
144+
let currentGroup
145+
for (const commit of list) {
146+
const commitGroup = toGroups(commit.summary)
147+
if (currentGroup !== commitGroup) {
148+
formatted.push(`${commitGroup}:`)
149+
currentGroup = commitGroup
150+
}
151+
formatted.push(commitToOutput(commit, formatType.PLAINTEXT, ghId, commitUrl))
152+
}
153+
list = formatted
123154
} else {
124-
list = list.map((commit) => commitToOutput(commit, format === 'simple', ghId, commitUrl))
155+
list = list.map((commit) => commitToOutput(commit, formatType.MARKDOWN, ghId, commitUrl))
125156
}
126157

127158
if (reverse)
@@ -156,7 +187,6 @@ if (require.main === module) {
156187
, argv = require('minimist')(process.argv.slice(2), minimistConfig)
157188
, branch1 = argv._[0]
158189
, branch2 = argv._[1]
159-
, format = argv.format
160190
, reverse = argv.reverse
161191
, group = argv.group || argv.g
162192
, endRef = argv['end-ref']
@@ -165,13 +195,11 @@ if (require.main === module) {
165195
, requireLabels = []
166196
, options
167197

198+
const format = getFormat(argv)
168199

169200
if (argv.version || argv.v)
170201
return console.log(`v ${require('./package.json').version}`)
171202

172-
if (argv.simple || argv.s)
173-
format = 'simple'
174-
175203
if (argv['patch-only'])
176204
excludeLabels = [ 'semver-minor', 'semver-major' ]
177205

@@ -188,7 +216,6 @@ if (require.main === module) {
188216
}
189217

190218
options = {
191-
simple: format === 'simple',
192219
group,
193220
excludeLabels,
194221
requireLabels,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "MIT",
1111
"dependencies": {
1212
"bl": "~3.0.0",
13-
"changelog-maker": "~2.3.0",
13+
"changelog-maker": "~2.4.0",
1414
"commit-stream": "~1.1.0",
1515
"deep-equal": "~1.0.1",
1616
"gitexec": "~1.0.0",

0 commit comments

Comments
 (0)