Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ But the comparison isn't quite as strict, generally leading to a shorter list of

### Options

* `--version` or `-v`: Only prints branch-diff's package.json version.
* `--group` or `-g`: Group commits by prefix, this uses the part of the commit summary that is usually used in Node.js core to indicate subsystem for example. Groups are made up of numbers, letters, `,` and `-`, followed by a `:`.
* `--commit-url`: A URL template which will be used to generate commit URLs for a repository not hosted in GitHub. `{ref}` is the placeholder that will be replaced with the commit, i.e. `--commit-url=https://gitlab.com/myUser/myRepo/commit/{ref}`. `{ghUser}` and `{ghRepo}` are available if they can be derived from package.json (Gitlab and Bitbucket URLs should be understood in package.json).
* `--end-ref=<ref>`: Only list comparison-branch commits up to the commit date of `<ref>`.
* `--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`.
* `--filter-release`: Exclude Node-style release commits from the list. e.g. `Working on v1.0.0` or `2015-10-21 Version 2.0.0`.
* `--find-matching-prs`: Use the GitHub API to find the pull requests that match commits that don't have `PR-URL` metadata in their message text.
* `--format`: Dictates what formatting the output will have. Possible options are:
- `simple` (default): Don't print full markdown output, good for console printing without the additional fluff.
- `sequence`: Hybrid format that can be pasted directly in the sequence editor of a `git rebase --interactive` session, with commit title, author, labels and PR URL as comments compatible with interactive rebase input.
- `sha`: Print only the 10-character truncated commit hashes.
- `plaintext`: A very simple form, without commit details, implies `--group`.
- `markdown`: A Markdown formatted from, with links and proper escaping.
- `messageonly`: Displays the commit message only, implies `--group`.
* `--filter-release`: Exclude Node-style release commits from the list. e.g. `Working on v1.0.0` or `2015-10-21 Version 2.0.0`.
* `--group` or `-g`: Group commits by prefix, this uses the part of the commit summary that is usually used in Node.js core to indicate subsystem for example. Groups are made up of numbers, letters, `,` and `-`, followed by a `:`.
* `--help` or `-h`: Print usage and options.
* `--patch-only`: An alias for `--exclude-label=semver-major,semver-minor`.
* `--quiet` or `-q`: Do not print to stdout.
* `--repo`: Override the auto-detected GitHub repository name derived from package.json
* `--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`.
* `--reverse`: Reverse the results, this is especially useful when piping output to `xargs`.
* `--commit-url`:A URL template which will be used to generate commit URLs for a repository not hosted in GitHub. `{ref}` is the placeholder that will be replaced with the commit, i.e. `--commit-url=https://gitlab.com/myUser/myRepo/commit/{ref}`. `{ghUser}` and `{ghRepo}` are available if they can be derived from package.json (Gitlab and Bitbucket URLs should be understood in package.json).
* `--sequence-drop=<label1>[,<label2>[,...]]`: When `--format=sequence`, will output the commits belonging to a PR with a label listed here as `drop` instead of `pick`. This can be useful for a human backporter to understand why a specific commit does not apply cleanly, to spot which commit might be creating conflicts.
* `--find-matching-prs`: Use the GitHub API to find the pull requests that match commits that don't have `PR-URL` metadata in their message text.
* `--quiet` or `-q`: Do not print to stdout.
* `--user`: Override the auto-detected GitHub user/org derived from package.json
* `--repo`: Override the auto-detected GitHub repository name derived from package.json
* `--version` or `-v`: Only prints branch-diff's package.json version.

Unknown options are an error. The `--sha`, `--plaintext`, `--markdown` and `--simple` shorthands were removed in v4; use `--format=<format>`.

Expand Down
61 changes: 41 additions & 20 deletions branch-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,51 @@ async function collect (repoPath, branch, startCommit, endRef) {
return commitList
}

// parseArgs ignores `arg` and `help`; they exist only to build the usage text.
const cliOptions = {
'commit-url': { type: 'string', arg: '<url>', help: 'commit URL template, with {ref}, {ghUser}, {ghRepo}' },
'end-ref': { type: 'string', arg: '<ref>', help: 'only list <comparison-branch> commits up to the\ncommit date of <ref>' },
'exclude-label': { type: 'string', multiple: true, arg: '<l,..>', help: 'drop commits from PRs with any of these labels' },
'filter-release': { type: 'boolean', help: 'drop Node-style release commits' },
'find-matching-prs': { type: 'boolean', help: 'look up PRs for commits without PR-URL metadata' },
format: { type: 'string', arg: '<format>', help: 'simple (default), sequence, sha, plaintext, markdown\nor messageonly' },
group: { type: 'boolean', short: 'g', help: 'group commits by subsystem prefix' },
help: { type: 'boolean', short: 'h', help: 'print this help' },
'patch-only': { type: 'boolean', help: 'alias for --exclude-label=semver-major,semver-minor' },
quiet: { type: 'boolean', short: 'q', help: 'do not print to stdout' },
repo: { type: 'string', arg: '<repo>', help: 'GitHub repository, default derived from package.json' },
'require-label': { type: 'string', multiple: true, arg: '<l,..>', help: 'keep only commits from PRs with one of these labels' },
reverse: { type: 'boolean', help: 'reverse the output order' },
'sequence-drop': { type: 'string', arg: '<l,..>', help: 'with --format=sequence, mark commits from PRs with\nthese labels as drop instead of pick' },
user: { type: 'string', arg: '<user>', help: 'GitHub user/org, default derived from package.json' },
version: { type: 'boolean', short: 'v', help: 'print the version' }
}

function usage () {
const flags = Object.entries(cliOptions).map(([name, { short, arg }]) =>
`${short ? `-${short}, ` : ''}--${name}${arg ? `=${arg}` : ''}`)
const width = Math.max(...flags.map((f) => f.length)) + 1
const lines = Object.values(cliOptions).map(({ help }, i) =>
` ${flags[i].padEnd(width)}${help.replaceAll('\n', `\n ${' '.repeat(width)}`)}`)
return `Usage: branch-diff [options] <base-branch> <comparison-branch>

List commits on <comparison-branch> that are not on <base-branch>, matching
loosely by summary and PR-URL (or author) so backported commits are excluded.

Options:
${lines.join('\n')}
`
}

async function main () {
const { values: argv, positionals } = parseArgs({
allowPositionals: true,
options: {
'commit-url': { type: 'string' },
'end-ref': { type: 'string' },
'exclude-label': { type: 'string', multiple: true },
'filter-release': { type: 'boolean' },
'find-matching-prs': { type: 'boolean' },
format: { type: 'string' },
group: { type: 'boolean', short: 'g' },
'patch-only': { type: 'boolean' },
quiet: { type: 'boolean', short: 'q' },
repo: { type: 'string' },
'require-label': { type: 'string', multiple: true },
reverse: { type: 'boolean' },
'sequence-drop': { type: 'string' },
user: { type: 'string' },
version: { type: 'boolean', short: 'v' }
}
})
const { values: argv, positionals } = parseArgs({ allowPositionals: true, options: cliOptions })
const [branch1, branch2] = positionals
const splitLabels = (labels = []) => labels.flatMap((l) => l.split(','))

if (argv.help) {
return process.stdout.write(usage())
}

if (argv.version) {
return console.log(`v ${require('./package.json').version}`)
}
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ test('--version prints the package version', async () => {
assert.deepEqual(await branchDiff('--version'), [`v ${version}`])
})

test('--help prints usage', async () => {
const help = await branchDiff('-h')
assert.equal(help[0], 'Usage: branch-diff [options] <base-branch> <comparison-branch>')
assert.ok(help.includes(' -g, --group group commits by subsystem prefix'))
assert.ok(help.every((line) => line.length <= 80))
})

test('--help and README list the same options, sorted', async () => {
const help = (await branchDiff('--help')).join('\n')
const readme = (await readFile(new URL('./README.md', import.meta.url), 'utf8')).split('### Options')[1]
const helpOptions = [...help.matchAll(/^ {2}(?:-\w, )?--([\w-]+)/gm)].map((m) => m[1])
const readmeOptions = [...readme.matchAll(/^\* `--([\w-]+)/gm)].map((m) => m[1])
assert.deepEqual(helpOptions, helpOptions.toSorted())
assert.deepEqual(readmeOptions, helpOptions)
})

test('missing branch names is an error', async () => {
await assert.rejects(branchDiff('main'), (err) => {
assert.equal(err.code, 1)
Expand Down
Loading