From db7a984cbd52010fba638b28feee618ce907363f Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Sat, 28 Nov 2026 21:25:31 +1100 Subject: [PATCH 1/3] feat: add --help Closes: #20 --- README.md | 2 ++ branch-diff.js | 61 +++++++++++++++++++++++++++++++++----------------- test.js | 7 ++++++ 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index adfb356..96ca77b 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ But the comparison isn't quite as strict, generally leading to a shorter list of ### Options +* `--help` or `-h`: Print usage and 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 `:`. * `--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`. @@ -37,6 +38,7 @@ But the comparison isn't quite as strict, generally leading to a shorter list of - `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`. +* `--end-ref=`: Only list comparison-branch commits up to the commit date of ``. * `--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=[,[,...]]`: 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. diff --git a/branch-diff.js b/branch-diff.js index 636d24e..dd11ccf 100755 --- a/branch-diff.js +++ b/branch-diff.js @@ -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 = { + format: { type: 'string', arg: '', help: 'simple (default), sequence, sha, plaintext, markdown\nor messageonly' }, + group: { type: 'boolean', short: 'g', help: 'group commits by subsystem prefix' }, + reverse: { type: 'boolean', help: 'reverse the output order' }, + 'exclude-label': { type: 'string', multiple: true, arg: '', help: 'drop commits from PRs with any of these labels' }, + 'require-label': { type: 'string', multiple: true, arg: '', help: 'keep only commits from PRs with one of these labels' }, + 'patch-only': { type: 'boolean', help: 'alias for --exclude-label=semver-major,semver-minor' }, + 'filter-release': { type: 'boolean', help: 'drop Node-style release commits' }, + 'end-ref': { type: 'string', arg: '', help: 'only list commits up to the\ncommit date of ' }, + 'sequence-drop': { type: 'string', arg: '', help: 'with --format=sequence, mark commits from PRs with\nthese labels as drop instead of pick' }, + 'find-matching-prs': { type: 'boolean', help: 'look up PRs for commits without PR-URL metadata' }, + 'commit-url': { type: 'string', arg: '', help: 'commit URL template, with {ref}, {ghUser}, {ghRepo}' }, + user: { type: 'string', arg: '', help: 'GitHub user/org, default derived from package.json' }, + repo: { type: 'string', arg: '', help: 'GitHub repository, default derived from package.json' }, + quiet: { type: 'boolean', short: 'q', help: 'do not print to stdout' }, + version: { type: 'boolean', short: 'v', help: 'print the version' }, + help: { type: 'boolean', short: 'h', help: 'print this help' } +} + +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] + +List commits on that are not on , 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}`) } diff --git a/test.js b/test.js index 1ce2962..f161f14 100644 --- a/test.js +++ b/test.js @@ -139,6 +139,13 @@ 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] ') + assert.ok(help.includes(' -g, --group group commits by subsystem prefix')) + assert.ok(help.every((line) => line.length <= 80)) +}) + test('missing branch names is an error', async () => { await assert.rejects(branchDiff('main'), (err) => { assert.equal(err.code, 1) From f7576f0817305168b421452e98ec3b587c9877da Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Sat, 28 Nov 2026 21:27:49 +1100 Subject: [PATCH 2/3] fixup! feat: add --help --- README.md | 22 +++++++++++----------- branch-diff.js | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 96ca77b..75ede69 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,11 @@ But the comparison isn't quite as strict, generally leading to a shorter list of ### Options -* `--help` or `-h`: Print usage and 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=`: Only list comparison-branch commits up to the commit date 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`. +* `--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. @@ -37,15 +36,16 @@ But the comparison isn't quite as strict, generally leading to a shorter list of - `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`. -* `--end-ref=`: Only list comparison-branch commits up to the commit date of ``. +* `--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=[,[,...]]`: 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=`. diff --git a/branch-diff.js b/branch-diff.js index dd11ccf..f83c143 100755 --- a/branch-diff.js +++ b/branch-diff.js @@ -139,22 +139,22 @@ async function collect (repoPath, branch, startCommit, endRef) { // parseArgs ignores `arg` and `help`; they exist only to build the usage text. const cliOptions = { + 'commit-url': { type: 'string', arg: '', help: 'commit URL template, with {ref}, {ghUser}, {ghRepo}' }, + 'end-ref': { type: 'string', arg: '', help: 'only list commits up to the\ncommit date of ' }, + 'exclude-label': { type: 'string', multiple: true, arg: '', 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: '', help: 'simple (default), sequence, sha, plaintext, markdown\nor messageonly' }, group: { type: 'boolean', short: 'g', help: 'group commits by subsystem prefix' }, - reverse: { type: 'boolean', help: 'reverse the output order' }, - 'exclude-label': { type: 'string', multiple: true, arg: '', help: 'drop commits from PRs with any of these labels' }, - 'require-label': { type: 'string', multiple: true, arg: '', help: 'keep only commits from PRs with one of these labels' }, + help: { type: 'boolean', short: 'h', help: 'print this help' }, 'patch-only': { type: 'boolean', help: 'alias for --exclude-label=semver-major,semver-minor' }, - 'filter-release': { type: 'boolean', help: 'drop Node-style release commits' }, - 'end-ref': { type: 'string', arg: '', help: 'only list commits up to the\ncommit date of ' }, + quiet: { type: 'boolean', short: 'q', help: 'do not print to stdout' }, + repo: { type: 'string', arg: '', help: 'GitHub repository, default derived from package.json' }, + 'require-label': { type: 'string', multiple: true, arg: '', help: 'keep only commits from PRs with one of these labels' }, + reverse: { type: 'boolean', help: 'reverse the output order' }, 'sequence-drop': { type: 'string', arg: '', help: 'with --format=sequence, mark commits from PRs with\nthese labels as drop instead of pick' }, - 'find-matching-prs': { type: 'boolean', help: 'look up PRs for commits without PR-URL metadata' }, - 'commit-url': { type: 'string', arg: '', help: 'commit URL template, with {ref}, {ghUser}, {ghRepo}' }, user: { type: 'string', arg: '', help: 'GitHub user/org, default derived from package.json' }, - repo: { type: 'string', arg: '', help: 'GitHub repository, default derived from package.json' }, - quiet: { type: 'boolean', short: 'q', help: 'do not print to stdout' }, - version: { type: 'boolean', short: 'v', help: 'print the version' }, - help: { type: 'boolean', short: 'h', help: 'print this help' } + version: { type: 'boolean', short: 'v', help: 'print the version' } } function usage () { From ba02cd6a977a4c82d3c4548a605f3dbad90d791d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Sat, 28 Nov 2026 21:29:46 +1100 Subject: [PATCH 3/3] fixup! fixup! feat: add --help --- test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test.js b/test.js index f161f14..420f033 100644 --- a/test.js +++ b/test.js @@ -146,6 +146,15 @@ test('--help prints usage', async () => { 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)