From 687435b9a2b261f06dd0359fc8e3926066e94b85 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Tue, 6 Feb 2018 13:33:11 -0500 Subject: [PATCH] feat: add debug logs for git commands --- lib/get-config.js | 4 +++- lib/git.js | 44 ++++++++++++++++++++++++++++++++++---------- test/git.test.js | 16 ++++++++-------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/lib/get-config.js b/lib/get-config.js index cad06cad..65587a2d 100644 --- a/lib/get-config.js +++ b/lib/get-config.js @@ -42,10 +42,12 @@ module.exports = async (opts, logger) => { }; } + const repositoryUrl = (await pkgRepoUrl()) || (await repoUrl()); + // Set default options values if not defined yet options = { branch: 'master', - repositoryUrl: getGitAuthUrl((await pkgRepoUrl()) || (await repoUrl())), + repositoryUrl: repositoryUrl ? getGitAuthUrl(repositoryUrl) : repositoryUrl, tagFormat: `v\${version}`, // Remove `null` and `undefined` options so they can be replaced with default ones ...pickBy(options, option => !isUndefined(option) && !isNull(option)), diff --git a/lib/git.js b/lib/git.js index 31d32712..24f39b7a 100644 --- a/lib/git.js +++ b/lib/git.js @@ -9,7 +9,11 @@ const debug = require('debug')('semantic-release:get-version-head'); * @return {string} The commit sha of the tag in parameter or `null`. */ async function gitTagHead(tagName) { - return execa.stdout('git', ['rev-list', '-1', tagName], {reject: false}); + try { + return await execa.stdout('git', ['rev-list', '-1', tagName]); + } catch (err) { + debug(err); + } } /** @@ -28,10 +32,14 @@ async function gitTags() { * * @param {string} ref The reference to look for. * - * @return {boolean} `true` if the reference is in the history of the current branch, `false` otherwise. + * @return {boolean} `true` if the reference is in the history of the current branch, falsy otherwise. */ async function isRefInHistory(ref) { - return (await execa('git', ['merge-base', '--is-ancestor', ref, 'HEAD'], {reject: false})).code === 0; + try { + return (await execa('git', ['merge-base', '--is-ancestor', ref, 'HEAD'])).code === 0; + } catch (err) { + debug(err); + } } /** @@ -52,14 +60,22 @@ async function gitHead() { * @return {string} The value of the remote git URL. */ async function repoUrl() { - return execa.stdout('git', ['remote', 'get-url', 'origin'], {reject: false}); + try { + return await execa.stdout('git', ['remote', 'get-url', 'origin']); + } catch (err) { + debug(err); + } } /** - * @return {Boolean} `true` if the current working directory is in a git repository, `false` otherwise. + * @return {Boolean} `true` if the current working directory is in a git repository, falsy otherwise. */ async function isGitRepo() { - return (await execa('git', ['rev-parse', '--git-dir'], {reject: false})).code === 0; + try { + return (await execa('git', ['rev-parse', '--git-dir'])).code === 0; + } catch (err) { + debug(err); + } } /** @@ -68,10 +84,14 @@ async function isGitRepo() { * @param {String} origin The remote repository URL. * @param {String} branch The repositoru branch for which to verify write access. * - * @return {Boolean} `true` is authorized to push, `false` otherwise. + * @return {Boolean} `true` is authorized to push, falsy otherwise. */ async function verifyAuth(origin, branch) { - return (await execa('git', ['push', '--dry-run', origin, `HEAD:${branch}`], {reject: false})).code === 0; + try { + return (await execa('git', ['push', '--dry-run', origin, `HEAD:${branch}`])).code === 0; + } catch (err) { + debug(err); + } } /** @@ -117,10 +137,14 @@ async function deleteTag(origin, tagName) { * * @method verifyTagName * @param {string} tagName the tag name to verify. - * @return {boolean} `true` if valid, `false` otherwise. + * @return {boolean} `true` if valid, falsy otherwise. */ async function verifyTagName(tagName) { - return (await execa('git', ['check-ref-format', `refs/tags/${tagName}`], {reject: false})).code === 0; + try { + return (await execa('git', ['check-ref-format', `refs/tags/${tagName}`])).code === 0; + } catch (err) { + debug(err); + } } module.exports = { diff --git a/test/git.test.js b/test/git.test.js index f56a2842..853ee2aa 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -88,7 +88,7 @@ test.serial('Verify if the commit `sha` is in the direct history of the current await gitCheckout('master', false); t.true(await isRefInHistory(commits[0].hash)); - t.false(await isRefInHistory(otherCommits[0].hash)); + t.falsy(await isRefInHistory(otherCommits[0].hash)); }); test.serial('Get the commit sha for a given tag or falsy if the tag does not exists', async t => { @@ -169,11 +169,11 @@ test.serial('Return "true" if in a Git repository', async t => { t.true(await isGitRepo()); }); -test.serial('Return "false" if not in a Git repository', async t => { +test.serial('Return falsy if not in a Git repository', async t => { const dir = tempy.directory(); process.chdir(dir); - t.false(await isGitRepo()); + t.falsy(await isGitRepo()); }); test.serial('Return "true" for valid tag names', async t => { @@ -183,11 +183,11 @@ test.serial('Return "true" for valid tag names', async t => { t.true(await verifyTagName('tag/name')); }); -test.serial('Return "false" for invalid tag names', async t => { - t.false(await verifyTagName('?1.0.0')); - t.false(await verifyTagName('*1.0.0')); - t.false(await verifyTagName('[1.0.0]')); - t.false(await verifyTagName('1.0.0..')); +test.serial('Return falsy for invalid tag names', async t => { + t.falsy(await verifyTagName('?1.0.0')); + t.falsy(await verifyTagName('*1.0.0')); + t.falsy(await verifyTagName('[1.0.0]')); + t.falsy(await verifyTagName('1.0.0..')); }); test.serial('Throws error if obtaining the tags fails', async t => {