Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
15 changes: 11 additions & 4 deletions branch-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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: '<p ..>', help: 'only list <comparison-branch> commits touching\nthis git pathspec' },
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' },
Expand Down Expand Up @@ -203,6 +209,7 @@ async function main () {
const options = {
group: argv.group,
excludeLabels,
paths: argv.path,
requireLabels,
endRef: argv['end-ref']
}
Expand Down
79 changes: 68 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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 <alice@example.com>' } = {}) {
async function commit (summary, { body = [], author = 'Alice <alice@example.com>', 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])], {
Expand Down Expand Up @@ -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 <alice@example.com>' })
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 <alice@example.com>', files: ['lib/net.js'] })
await commit('net: backport matched by author', { author: 'Alice <alice@elsewhere.example>' })
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 }))
Expand Down Expand Up @@ -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}`])
Expand Down
Loading