fix: correct error when remote repository has no branches

This commit is contained in:
Jozef Cipa 2020-01-27 17:58:29 +01:00 committed by Pierre Vanduynslager
parent b54b20d412
commit c6b10766a7
No known key found for this signature in database
GPG Key ID: E7684268B8C7E0A3
3 changed files with 29 additions and 10 deletions

View File

@ -65,8 +65,8 @@ async function getCommits(from, to, execaOpts) {
async function getBranches(repositoryUrl, execaOpts) { async function getBranches(repositoryUrl, execaOpts) {
return (await execa('git', ['ls-remote', '--heads', repositoryUrl], execaOpts)).stdout return (await execa('git', ['ls-remote', '--heads', repositoryUrl], execaOpts)).stdout
.split('\n') .split('\n')
.map(branch => branch.match(/^.+refs\/heads\/(?<branch>.+)$/)[1]) .filter(Boolean)
.filter(Boolean); .map(branch => branch.match(/^.+refs\/heads\/(?<branch>.+)$/)[1]);
} }
/** /**

View File

@ -33,6 +33,7 @@ import {
gitAddNote, gitAddNote,
gitGetNote, gitGetNote,
gitFetch, gitFetch,
initGit,
} from './helpers/git-utils'; } from './helpers/git-utils';
test('Get the last commit sha', async t => { test('Get the last commit sha', async t => {
@ -182,6 +183,11 @@ test('Get all branches', async t => {
t.deepEqual((await getBranches(repositoryUrl, {cwd})).sort(), ['master', 'second-branch', 'third-branch'].sort()); t.deepEqual((await getBranches(repositoryUrl, {cwd})).sort(), ['master', 'second-branch', 'third-branch'].sort());
}); });
test('Return empty array if there are no branches', async t => {
const {cwd, repositoryUrl} = await initGit(true);
t.deepEqual(await getBranches(repositoryUrl, {cwd}), []);
});
test('Get the commit sha for a given tag', async t => { test('Get the commit sha for a given tag', async t => {
// Create a git repository, set the current working directory at the root of the repo // Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo(); const {cwd} = await gitRepo();

View File

@ -7,7 +7,7 @@ import getStream from 'get-stream';
import {GIT_NOTE_REF} from '../../lib/definitions/constants'; import {GIT_NOTE_REF} from '../../lib/definitions/constants';
/** /**
* Commit message informations. * Commit message information.
* *
* @typedef {Object} Commit * @typedef {Object} Commit
* @property {String} branch The commit branch. * @property {String} branch The commit branch.
@ -15,21 +15,34 @@ import {GIT_NOTE_REF} from '../../lib/definitions/constants';
* @property {String} message The commit message. * @property {String} message The commit message.
*/ */
/**
* Initialize git repository
* If `withRemote` is `true`, creates a bare repository and initialize it.
* If `withRemote` is `false`, creates a regular repository and initialize it.
*
* @param {Boolean} withRemote `true` to create a shallow clone of a bare repository.
* @return {String} The path of the repository
*/
export async function initGit(withRemote) {
const cwd = tempy.directory();
await execa('git', ['init', ...(withRemote ? ['--bare'] : [])], {cwd});
const repositoryUrl = fileUrl(cwd);
return {cwd, repositoryUrl};
}
/** /**
* Create a temporary git repository. * Create a temporary git repository.
* If `withRemote` is `true`, creates a bare repository, initialize it and create a shallow clone. Change the current working directory to the clone root. * If `withRemote` is `true`, creates a shallow clone. Change the current working directory to the clone root.
* If `withRemote` is `false`, creates a regular repository and initialize it. Change the current working directory to the repository root. * If `withRemote` is `false`, just change the current working directory to the repository root.
*
* *
* @param {Boolean} withRemote `true` to create a shallow clone of a bare repository. * @param {Boolean} withRemote `true` to create a shallow clone of a bare repository.
* @param {String} [branch='master'] The branch to initialize. * @param {String} [branch='master'] The branch to initialize.
* @return {String} The path of the clone if `withRemote` is `true`, the path of the repository otherwise. * @return {String} The path of the clone if `withRemote` is `true`, the path of the repository otherwise.
*/ */
export async function gitRepo(withRemote, branch = 'master') { export async function gitRepo(withRemote, branch = 'master') {
let cwd = tempy.directory(); let {cwd, repositoryUrl} = await initGit(withRemote);
await execa('git', ['init', ...(withRemote ? ['--bare'] : [])], {cwd});
const repositoryUrl = fileUrl(cwd);
if (withRemote) { if (withRemote) {
await initBareRepo(repositoryUrl, branch); await initBareRepo(repositoryUrl, branch);
cwd = await gitShallowClone(repositoryUrl, branch); cwd = await gitShallowClone(repositoryUrl, branch);