Skip to content

Commit

Permalink
fix: look for .git dir in parents of input dir (#7)
Browse files Browse the repository at this point in the history
PR #2 introduced a regression that broke support for non-project-root input dirs and the test suite was not smart enough to find it, this fixes that
  • Loading branch information
nexdrew committed May 25, 2018
1 parent 715e15f commit bcc374b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,27 @@ function determineBuildId (id, inputDir) {
if (id) return Promise.resolve(id)
return new Promise((resolve, reject) => {
const cp = require('child_process')
cp.execFile('git', [`--git-dir=${inputDir}/.git`, `--work-tree=${inputDir}`, 'rev-parse', 'HEAD'], (err, stdout, stderr) => {

// inputDir may not be the project root so look for .git dir in parent dirs too
let dir = inputDir
const root = path.parse(dir).root
let attempts = 0 // protect against infinite tight loop if libs misbehave
while (dir !== root && attempts < 999) {
attempts++
try {
fs.accessSync(path.join(dir, '.git'), (fs.constants || fs).R_OK)
break
} catch (_) {
dir = path.dirname(dir)
}
}
if (dir === root || attempts >= 999) dir = inputDir

cp.execFile('git', [`--git-dir=${path.join(dir, '.git')}`, `--work-tree=${dir}`, 'rev-parse', 'HEAD'], (err, stdout, stderr) => {
if (err) return reject(err)
if (stderr) return reject(String(stderr).trim())
if (stdout) return resolve(String(stdout).trim())
reject(`No output from command: git --git-dir=${inputDir}/.git --work-tree=${inputDir} rev-parse HEAD`)
reject(`No output from command: git --git-dir=${path.join(dir, '.git')} --work-tree=${dir} rev-parse HEAD`)
})
})
}
Expand Down
14 changes: 11 additions & 3 deletions test/git
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#!/usr/bin/env node
if (!String(process.argv[3]).includes('fixture3')) {
console.log('0123456789abcdef0123456789abcdef01234567')
}

// --git-dir=${inputDir}/.git --work-tree=${inputDir}
require('sywac')
.dir('--git-dir <dir>', { mustExist: true })
.outputSettings({ showHelpOnError: false })
.parseAndExit()
.then(argv => {
if (!process.env.NBI_TEST_FIXTURE || !String(process.env.NBI_TEST_FIXTURE).includes('fixture3')) {
console.log('0123456789abcdef0123456789abcdef01234567')
}
})
2 changes: 1 addition & 1 deletion test/test-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tap.test('supports custom --id', t => {

tap.test('does not need git with custom --id', t => {
return exec('npm', 'run build', fixturePath).then(() => {
return cli('--id 123456', fixturePath, '/dne')
return cli('--id 123456', fixturePath, { PATH: '/dne' })
}).then(io => {
t.notOk(io.err)
t.notOk(io.stderr)
Expand Down
4 changes: 2 additions & 2 deletions test/test-others.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ tap.test('errs on invalid json', t => {
})

tap.test('errs on no git output', t => {
return cli('', path.resolve(__dirname, 'fixture3')).then(io => {
return cli('', path.resolve(__dirname, 'fixture3'), { NBI_TEST_FIXTURE: 'fixture3' }).then(io => {
t.equal(io.err.code, 1)
t.match(io.stderr, /No output from command: git/)
t.notOk(io.stdout)
})
})

tap.test('errs without --id and without git', t => {
return cli('', path.resolve(__dirname, 'fixture3'), '/dne').then(io => {
return cli('', path.resolve(__dirname, 'fixture3'), { NBI_TEST_FIXTURE: 'fixture3', PATH: '/dne' }).then(io => {
t.equal(io.err.code, 1)
t.match(io.stderr, /Unexpected error/)
t.notOk(io.stdout)
Expand Down
12 changes: 6 additions & 6 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ function exec (file, args, cwd, env, alwaysResolve) {
})
}

function mockedGitEnv (envPath) {
const env = Object.assign({}, process.env)
env.PATH = envPath || [__dirname].concat(env.PATH.split(path.delimiter)).join(path.delimiter)
return env
function mockedGitEnv (env) {
env = env || {}
if (!env.PATH) env.PATH = [__dirname].concat(process.env.PATH.split(path.delimiter)).join(path.delimiter)
return Object.assign({}, process.env, env)
}

function cli (args, cwd, envPath) {
return exec(cliPath, args, cwd, mockedGitEnv(envPath), true)
function cli (args, cwd, env) {
return exec(cliPath, args, cwd, mockedGitEnv(env), true)
}

function readTextFile (file) {
Expand Down

0 comments on commit bcc374b

Please sign in to comment.