Skip to content

Commit 687435b

Browse files
committed
feat: add debug logs for git commands
1 parent 7e785fa commit 687435b

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

lib/get-config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ module.exports = async (opts, logger) => {
4242
};
4343
}
4444

45+
const repositoryUrl = (await pkgRepoUrl()) || (await repoUrl());
46+
4547
// Set default options values if not defined yet
4648
options = {
4749
branch: 'master',
48-
repositoryUrl: getGitAuthUrl((await pkgRepoUrl()) || (await repoUrl())),
50+
repositoryUrl: repositoryUrl ? getGitAuthUrl(repositoryUrl) : repositoryUrl,
4951
tagFormat: `v\${version}`,
5052
// Remove `null` and `undefined` options so they can be replaced with default ones
5153
...pickBy(options, option => !isUndefined(option) && !isNull(option)),

lib/git.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const debug = require('debug')('semantic-release:get-version-head');
99
* @return {string} The commit sha of the tag in parameter or `null`.
1010
*/
1111
async function gitTagHead(tagName) {
12-
return execa.stdout('git', ['rev-list', '-1', tagName], {reject: false});
12+
try {
13+
return await execa.stdout('git', ['rev-list', '-1', tagName]);
14+
} catch (err) {
15+
debug(err);
16+
}
1317
}
1418

1519
/**
@@ -28,10 +32,14 @@ async function gitTags() {
2832
*
2933
* @param {string} ref The reference to look for.
3034
*
31-
* @return {boolean} `true` if the reference is in the history of the current branch, `false` otherwise.
35+
* @return {boolean} `true` if the reference is in the history of the current branch, falsy otherwise.
3236
*/
3337
async function isRefInHistory(ref) {
34-
return (await execa('git', ['merge-base', '--is-ancestor', ref, 'HEAD'], {reject: false})).code === 0;
38+
try {
39+
return (await execa('git', ['merge-base', '--is-ancestor', ref, 'HEAD'])).code === 0;
40+
} catch (err) {
41+
debug(err);
42+
}
3543
}
3644

3745
/**
@@ -52,14 +60,22 @@ async function gitHead() {
5260
* @return {string} The value of the remote git URL.
5361
*/
5462
async function repoUrl() {
55-
return execa.stdout('git', ['remote', 'get-url', 'origin'], {reject: false});
63+
try {
64+
return await execa.stdout('git', ['remote', 'get-url', 'origin']);
65+
} catch (err) {
66+
debug(err);
67+
}
5668
}
5769

5870
/**
59-
* @return {Boolean} `true` if the current working directory is in a git repository, `false` otherwise.
71+
* @return {Boolean} `true` if the current working directory is in a git repository, falsy otherwise.
6072
*/
6173
async function isGitRepo() {
62-
return (await execa('git', ['rev-parse', '--git-dir'], {reject: false})).code === 0;
74+
try {
75+
return (await execa('git', ['rev-parse', '--git-dir'])).code === 0;
76+
} catch (err) {
77+
debug(err);
78+
}
6379
}
6480

6581
/**
@@ -68,10 +84,14 @@ async function isGitRepo() {
6884
* @param {String} origin The remote repository URL.
6985
* @param {String} branch The repositoru branch for which to verify write access.
7086
*
71-
* @return {Boolean} `true` is authorized to push, `false` otherwise.
87+
* @return {Boolean} `true` is authorized to push, falsy otherwise.
7288
*/
7389
async function verifyAuth(origin, branch) {
74-
return (await execa('git', ['push', '--dry-run', origin, `HEAD:${branch}`], {reject: false})).code === 0;
90+
try {
91+
return (await execa('git', ['push', '--dry-run', origin, `HEAD:${branch}`])).code === 0;
92+
} catch (err) {
93+
debug(err);
94+
}
7595
}
7696

7797
/**
@@ -117,10 +137,14 @@ async function deleteTag(origin, tagName) {
117137
*
118138
* @method verifyTagName
119139
* @param {string} tagName the tag name to verify.
120-
* @return {boolean} `true` if valid, `false` otherwise.
140+
* @return {boolean} `true` if valid, falsy otherwise.
121141
*/
122142
async function verifyTagName(tagName) {
123-
return (await execa('git', ['check-ref-format', `refs/tags/${tagName}`], {reject: false})).code === 0;
143+
try {
144+
return (await execa('git', ['check-ref-format', `refs/tags/${tagName}`])).code === 0;
145+
} catch (err) {
146+
debug(err);
147+
}
124148
}
125149

126150
module.exports = {

test/git.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ test.serial('Verify if the commit `sha` is in the direct history of the current
8888
await gitCheckout('master', false);
8989

9090
t.true(await isRefInHistory(commits[0].hash));
91-
t.false(await isRefInHistory(otherCommits[0].hash));
91+
t.falsy(await isRefInHistory(otherCommits[0].hash));
9292
});
9393

9494
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 => {
169169
t.true(await isGitRepo());
170170
});
171171

172-
test.serial('Return "false" if not in a Git repository', async t => {
172+
test.serial('Return falsy if not in a Git repository', async t => {
173173
const dir = tempy.directory();
174174
process.chdir(dir);
175175

176-
t.false(await isGitRepo());
176+
t.falsy(await isGitRepo());
177177
});
178178

179179
test.serial('Return "true" for valid tag names', async t => {
@@ -183,11 +183,11 @@ test.serial('Return "true" for valid tag names', async t => {
183183
t.true(await verifyTagName('tag/name'));
184184
});
185185

186-
test.serial('Return "false" for invalid tag names', async t => {
187-
t.false(await verifyTagName('?1.0.0'));
188-
t.false(await verifyTagName('*1.0.0'));
189-
t.false(await verifyTagName('[1.0.0]'));
190-
t.false(await verifyTagName('1.0.0..'));
186+
test.serial('Return falsy for invalid tag names', async t => {
187+
t.falsy(await verifyTagName('?1.0.0'));
188+
t.falsy(await verifyTagName('*1.0.0'));
189+
t.falsy(await verifyTagName('[1.0.0]'));
190+
t.falsy(await verifyTagName('1.0.0..'));
191191
});
192192

193193
test.serial('Throws error if obtaining the tags fails', async t => {

0 commit comments

Comments
 (0)