Skip to content

Commit

Permalink
feat: add plaintext option (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored May 8, 2020
1 parent 5f6f7b1 commit 17d363e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ But the comparison isn't quite as strict, generally leading to a shorter list of
* `--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`.
* `--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`.
* `--patch-only`: An alias for `--exclude-label=semver-major,semver-minor`.
* `--format`: Dictates what formatting the output will have. Possible options are: `simple`, `sha`. The default is to print markdown-formatted output.
* `--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.
- `simple`: Don't print full markdown output, good for console printing without the additional fluff.
- `sha`: Print only the 10-character truncated commit shasums. Good for piping though additional tooling, such as `xargs git cherry-pick` for applying commits.
* `--simple` or `-s`: An alias for `--format=simple`.
Expand Down
47 changes: 37 additions & 10 deletions branch-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const fs = require('fs')
, pkgtoId = require('pkg-to-id')
, stripAnsi = require('strip-ansi')
, map = require('map-async')
, commitToOutput = require('changelog-maker/commit-to-output')
, { commitToOutput } = require('changelog-maker/commit-to-output')
, collectCommitLabels = require('changelog-maker/collect-commit-labels')
, groupCommits = require('changelog-maker/group-commits')
, isReleaseCommit = require('changelog-maker/groups').isReleaseCommit
, { isReleaseCommit, toGroups } = require('changelog-maker/groups')
, gitexec = require('gitexec')

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

const formatType = {
PLAINTEXT: 'plaintext',
MARKDOWN: 'markdown',
SIMPLE: 'simple',
SHA: 'sha'
}

const getFormat = (argv) => {
if (argv.format && Object.values(formatType).includes(argv.format)) {
return argv.format
} else if (argv.simple || argv.s) {
return formatType.SIMPLE
}
return formatType.MARKDOWN
}

function replace (s, m) {
Object.keys(m).forEach(function (k) {
s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
Expand Down Expand Up @@ -116,12 +131,28 @@ function diffCollected (options, branchCommits, callback) {
})
}


function printCommits (list, format, reverse, commitUrl) {
if (format === 'sha') {
if (format === formatType.SHA) {
list = list.map((commit) => `${commit.sha.substr(0, 10)}`)
} else if (format === formatType.SIMPLE) {
list = list.map((commit) => commitToOutput(commit, formatType.SIMPLE, ghId, commitUrl))
} else if (format === formatType.PLAINTEXT) {
// Plaintext format implies grouping.
list = groupCommits(list)

const formatted = []
let currentGroup
for (const commit of list) {
const commitGroup = toGroups(commit.summary)
if (currentGroup !== commitGroup) {
formatted.push(`${commitGroup}:`)
currentGroup = commitGroup
}
formatted.push(commitToOutput(commit, formatType.PLAINTEXT, ghId, commitUrl))
}
list = formatted
} else {
list = list.map((commit) => commitToOutput(commit, format === 'simple', ghId, commitUrl))
list = list.map((commit) => commitToOutput(commit, formatType.MARKDOWN, ghId, commitUrl))
}

if (reverse)
Expand Down Expand Up @@ -156,7 +187,6 @@ if (require.main === module) {
, argv = require('minimist')(process.argv.slice(2), minimistConfig)
, branch1 = argv._[0]
, branch2 = argv._[1]
, format = argv.format
, reverse = argv.reverse
, group = argv.group || argv.g
, endRef = argv['end-ref']
Expand All @@ -165,13 +195,11 @@ if (require.main === module) {
, requireLabels = []
, options

const format = getFormat(argv)

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

if (argv.simple || argv.s)
format = 'simple'

if (argv['patch-only'])
excludeLabels = [ 'semver-minor', 'semver-major' ]

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

options = {
simple: format === 'simple',
group,
excludeLabels,
requireLabels,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"dependencies": {
"bl": "~3.0.0",
"changelog-maker": "~2.3.0",
"changelog-maker": "~2.4.0",
"commit-stream": "~1.1.0",
"deep-equal": "~1.0.1",
"gitexec": "~1.0.0",
Expand Down

0 comments on commit 17d363e

Please sign in to comment.