diff --git a/README.md b/README.md
index 75ede69..61e4b2c 100644
--- a/README.md
+++ b/README.md
@@ -39,6 +39,7 @@ But the comparison isn't quite as strict, generally leading to a shorter list 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`.
+* `--path`: Path to limit the output to commits touching a subset of the repository. Can be passed multiple times. See https://git-scm.com/docs/git-log#_history_simplification.
* `--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`.
diff --git a/branch-diff.js b/branch-diff.js
index f83c143..dec57d6 100755
--- a/branch-diff.js
+++ b/branch-diff.js
@@ -29,7 +29,7 @@ const ghId = {
function replace (s, m) {
Object.keys(m).forEach(function (k) {
- s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
+ s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), () => m[k])
})
return s
}
@@ -51,7 +51,9 @@ export async function branchDiff (branch1, branch2, options) {
const repoPath = options.repoPath || process.cwd()
const commit = await findMergeBase(repoPath, branch1, branch2)
const branchCommits = await Promise.all([branch1, branch2].map(async (branch) => {
- return collect(repoPath, branch, commit, branch === branch2 && options.endRef)
+ return branch === branch2
+ ? collect(repoPath, branch, commit, options.endRef, options.paths)
+ : collect(repoPath, branch, commit)
}))
return await diffCollected(options, branchCommits)
}
@@ -114,15 +116,18 @@ async function diffCollected (options, branchCommits) {
return list
}
-async function collect (repoPath, branch, startCommit, endRef) {
+async function collect (repoPath, branch, startCommit, endRef, paths = []) {
const endrefcmd = endRef && replace(refcmd, { ref: endRef })
const untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
- const _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })
+ const logcmd = replace(gitcmd, { branch, startCommit, untilcmd })
+ const _gitcmd = paths.length ? `${logcmd} -- ${paths.map((_, i) => `"$BRANCH_DIFF_PATH_${i}"`).join(' ')}` : logcmd
const commitList = []
await spawnCollect(_gitcmd, [], {
shell: true,
cwd: repoPath,
+ // Passing it through the env to short-circuit the shell, in case it contains e.g. `$`
+ env: { ...process.env, ...Object.fromEntries(paths.map((val, i) => [`BRANCH_DIFF_PATH_${i}`, val])) },
stdio: ['ignore', 'pipe', 'inherit']
}, (stdout) => pipeline(
stdout,
@@ -148,6 +153,7 @@ const cliOptions = {
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' },
+ path: { type: 'string', multiple: true, arg: '
', help: 'only list commits touching\nthis git pathspec' },
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' },
@@ -203,6 +209,7 @@ async function main () {
const options = {
group: argv.group,
excludeLabels,
+ paths: argv.path,
requireLabels,
endRef: argv['end-ref']
}
diff --git a/test.js b/test.js
index 420f033..a637a6b 100644
--- a/test.js
+++ b/test.js
@@ -1,9 +1,9 @@
import { test, before, after } from 'node:test'
import assert from 'node:assert/strict'
import { execFile } from 'node:child_process'
-import { mkdtemp, readFile, rm } from 'node:fs/promises'
+import { access, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir, devNull } from 'node:os'
-import { join } from 'node:path'
+import { dirname, join } from 'node:path'
import { promisify, stripVTControlCharacters } from 'node:util'
import { fileURLToPath } from 'node:url'
@@ -29,7 +29,12 @@ async function git (...args) {
// Each commit gets a later date than the last so `--until` (used by
// `--end-ref`) is deterministic.
-async function commit (summary, { body = [], author = 'Alice ' } = {}) {
+async function commit (summary, { body = [], author = 'Alice ', files = [] } = {}) {
+ for (const file of files) {
+ await mkdir(join(repo, dirname(file)), { recursive: true })
+ await writeFile(join(repo, file), `${summary}\n`, { flag: 'a' })
+ await git('add', file)
+ }
const date = new Date(Date.UTC(2026, 0, 1) + (++tick) * 60_000).toISOString()
const [, name, email] = author.match(/^(.+) <(.+)>$/)
await execFileP('git', ['commit', '--allow-empty', '-q', '-m', summary, ...body.flatMap((b) => ['-m', b])], {
@@ -64,21 +69,22 @@ before(async () => {
await commit('build: shared base')
await git('checkout', '-q', '-b', 'release')
- await commit('fs: backport with matching PR-URL', { body: ['PR-URL: https://example.com/pr/1'] })
+ await commit('fs: backport with matching PR-URL', { body: ['PR-URL: https://example.com/pr/1'], files: ['lib/fs.js'] })
await commit('net: backport matched by author')
- await commit('doc: same summary, different PR-URL', { body: ['PR-URL: https://example.com/pr/3'] })
- await commit('test: only on release')
+ await commit('doc: same summary, different PR-URL', { body: ['PR-URL: https://example.com/pr/3'], files: ['doc/a.md'] })
+ await commit('test: only on release', { files: ['test/only-release.js'] })
await git('checkout', '-q', 'main')
- await commit('fs: backport with matching PR-URL', { body: ['PR-URL: https://example.com/pr/1/'] })
+ // The release backport above dropped the test change.
+ await commit('fs: backport with matching PR-URL', { body: ['PR-URL: https://example.com/pr/1/'], files: ['lib/fs.js', 'test/fs.js'] })
await commit('net: backport matched by author')
- await commit('doc: same summary, different PR-URL', { body: ['PR-URL: https://example.com/pr/4'] })
- await commit('net: backport matched by author', { author: 'Bob ' })
+ await commit('doc: same summary, different PR-URL', { body: ['PR-URL: https://example.com/pr/4'], files: ['doc/a.md'] })
+ await commit('net: backport matched by author', { author: 'Bob ', files: ['lib/net.js'] })
await commit('net: backport matched by author', { author: 'Alice ' })
- await commit('crypto: only on main')
+ await commit('crypto: only on main', { files: ['lib/crypto.js'] })
await commit('2026-01-02, Version 1.0.0 (Current)')
await commit('Working on v1.0.1')
- await commit('lib: after end ref')
+ await commit('lib: after end ref', { files: ['lib/after.js'] })
})
after(() => rm(repo, { recursive: true, force: true }))
@@ -134,6 +140,57 @@ test('--group orders commits by subsystem', async () => {
])
})
+test('--path limits the output to commits touching the given path', async () => {
+ assert.deepEqual(await summaries('release', 'main', '--path=lib'), [
+ 'lib: after end ref',
+ 'crypto: only on main',
+ 'net: backport matched by author'
+ ])
+})
+
+test('--path accepts paths containing a space', async () => {
+ assert.deepEqual(await summaries('release', 'main', '--path=doc lib/crypto.js'), [])
+})
+
+test('--path can be passed multiple times', async () => {
+ assert.deepEqual(await summaries('release', 'main', '--path=doc', '--path=lib/crypto.js'), [
+ 'crypto: only on main',
+ 'doc: same summary, different PR-URL'
+ ])
+})
+
+test('--path combines with --end-ref', async () => {
+ assert.deepEqual(await summaries('release', 'main', '--path=lib', '--end-ref=main~1'), [
+ 'crypto: only on main',
+ 'net: backport matched by author'
+ ])
+})
+
+test('--path matching nothing prints nothing', async () => {
+ assert.deepEqual(await branchDiff('release', 'main', '--path=nope'), [])
+})
+
+test('--path does not hide backports that touched fewer files', async () => {
+ // main's fs commit touches test/fs.js, its backport on release does not.
+ assert.deepEqual(await summaries('release', 'main', '--path=test'), [])
+})
+
+test('--path globs are git pathspecs, not shell globs', async () => {
+ // The working tree is main, which has test/fs.js but not test/only-release.js.
+ assert.deepEqual(await summaries('main', 'release', '--path=test/*.js'), ['test: only on release'])
+})
+
+test('--path supports pathspec magic', async () => {
+ assert.deepEqual(await summaries('release', 'main', '--path=:(exclude)lib'), [
+ 'doc: same summary, different PR-URL'
+ ])
+})
+
+test('--path is not evaluated by the shell', async () => {
+ await summaries('release', 'main', '--path=lib $(touch pwned) `touch pwned`; touch pwned')
+ await assert.rejects(access(join(repo, 'pwned')))
+})
+
test('--version prints the package version', async () => {
const { version } = JSON.parse(await readFile(new URL('./package.json', import.meta.url)))
assert.deepEqual(await branchDiff('--version'), [`v ${version}`])