Skip to content

Commit da0cba0

Browse files
committed
bin: use --no-mailmap when loading commits
Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 41b8777 commit da0cba0

2 files changed

Lines changed: 83 additions & 7 deletions

File tree

bin/cmd.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { exec } from 'node:child_process'
3+
import { execFile } from 'node:child_process'
44
import fs from 'node:fs'
55
import http from 'node:http'
66
import https from 'node:https'
@@ -76,10 +76,11 @@ function load (sha, cb) {
7676
if (parsed != null) {
7777
return loadPatch(parsed, cb)
7878
}
79-
exec(`git show --quiet --format=medium ${sha}`, (err, stdout, stderr) => {
80-
if (err) return cb(err)
81-
cb(null, stdout.trim())
82-
})
79+
execFile('git', ['show', '--no-mailmap', '--quiet', '--format=medium', sha],
80+
(err, stdout, stderr) => {
81+
if (err) return cb(err)
82+
cb(null, stdout.trim())
83+
})
8384
}
8485

8586
function loadPatch (uri, cb) {

test/cli-test.js

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { test } from 'tap'
2-
import { readFileSync } from 'node:fs'
3-
import { spawn } from 'node:child_process'
2+
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
3+
import { execFileSync, spawn } from 'node:child_process'
4+
import { tmpdir } from 'node:os'
5+
import { join } from 'node:path'
6+
import { fileURLToPath } from 'node:url'
47
import subsystems from '../lib/rules/subsystem.js'
58

9+
const cmdPath = fileURLToPath(new URL('../bin/cmd.js', import.meta.url))
10+
11+
function git (cwd, args, env = undefined) {
12+
return execFileSync('git', args, {
13+
cwd,
14+
env: env === undefined ? process.env : { ...process.env, ...env },
15+
encoding: 'utf8'
16+
})
17+
}
18+
619
test('Test cli flags', (t) => {
720
t.test('test list-subsystems', (tt) => {
821
const ls = spawn('./bin/cmd.js', ['--list-subsystems'], {
@@ -358,5 +371,67 @@ test('Test cli flags', (t) => {
358371
})
359372
})
360373

374+
t.test('test sha ignores mailmap when validating sign-off', (tt) => {
375+
tt.plan(6)
376+
377+
const repoDir = mkdtempSync(join(tmpdir(), 'core-validate-commit-'))
378+
const env = {
379+
GIT_AUTHOR_NAME: 'Matteo Collina',
380+
GIT_AUTHOR_EMAIL: 'hello@matteocollina.com',
381+
GIT_COMMITTER_NAME: 'Matteo Collina',
382+
GIT_COMMITTER_EMAIL: 'hello@matteocollina.com'
383+
}
384+
385+
git(repoDir, ['init', '-b', 'main'])
386+
git(repoDir, ['config', 'user.name', 'Test User'])
387+
git(repoDir, ['config', 'user.email', 'test@example.com'])
388+
389+
writeFileSync(join(repoDir, '.mailmap'),
390+
'Matteo Collina <matteo.collina@gmail.com> <hello@matteocollina.com>\n')
391+
writeFileSync(join(repoDir, 'README.md'), 'test\n')
392+
393+
git(repoDir, ['add', '.mailmap', 'README.md'])
394+
git(repoDir, ['commit', '-m',
395+
'doc: add mailmap sign-off fixture\n\n' +
396+
'Signed-off-by: Matteo Collina <hello@matteocollina.com>\n' +
397+
'PR-URL: https://github.com/nodejs/node/pull/1234\n' +
398+
'Reviewed-By: Rich Trott <rtrott@gmail.com>'
399+
], env)
400+
401+
const defaultShow = git(repoDir, ['show', '--quiet', '--format=medium', 'HEAD'])
402+
const rawShow = git(repoDir, ['show', '--no-mailmap', '--quiet', '--format=medium', 'HEAD'])
403+
404+
tt.match(defaultShow,
405+
/Author:\s+Matteo Collina <matteo\.collina@gmail\.com>/,
406+
'git show applies mailmap by default')
407+
tt.match(rawShow,
408+
/Author:\s+Matteo Collina <hello@matteocollina\.com>/,
409+
'git show --no-mailmap preserves the raw author email')
410+
411+
const ls = spawn(process.execPath, [cmdPath, '--no-validate-metadata', 'HEAD'], {
412+
cwd: repoDir,
413+
env: { ...process.env, FORCE_COLOR: 0 }
414+
})
415+
let compiledData = ''
416+
let errorData = ''
417+
418+
ls.stdout.on('data', (data) => {
419+
compiledData += data
420+
})
421+
422+
ls.stderr.on('data', (data) => {
423+
errorData += data
424+
})
425+
426+
ls.on('close', (code) => {
427+
tt.equal(code, 0, 'CLI exits with zero code on success')
428+
tt.equal(errorData, '', 'no error output')
429+
tt.match(compiledData, /has valid Signed-off-by/, 'sign-off remains valid')
430+
tt.notMatch(compiledData,
431+
/email does not match the commit author email/,
432+
'mailmap does not trigger a false mismatch warning')
433+
})
434+
})
435+
361436
t.end()
362437
})

0 commit comments

Comments
 (0)