diff --git a/lib/git.js b/lib/git.js index 26c00457..97a3c99d 100644 --- a/lib/git.js +++ b/lib/git.js @@ -65,8 +65,8 @@ async function getCommits(from, to, execaOpts) { async function getBranches(repositoryUrl, execaOpts) { return (await execa('git', ['ls-remote', '--heads', repositoryUrl], execaOpts)).stdout .split('\n') - .map(branch => branch.match(/^.+refs\/heads\/(?.+)$/)[1]) - .filter(Boolean); + .filter(Boolean) + .map(branch => branch.match(/^.+refs\/heads\/(?.+)$/)[1]); } /** diff --git a/test/git.test.js b/test/git.test.js index 5640dcdc..08a11107 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -33,6 +33,7 @@ import { gitAddNote, gitGetNote, gitFetch, + initGit, } from './helpers/git-utils'; 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()); }); +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 => { // Create a git repository, set the current working directory at the root of the repo const {cwd} = await gitRepo(); diff --git a/test/helpers/git-utils.js b/test/helpers/git-utils.js index 40a08dd1..e38672fd 100644 --- a/test/helpers/git-utils.js +++ b/test/helpers/git-utils.js @@ -7,7 +7,7 @@ import getStream from 'get-stream'; import {GIT_NOTE_REF} from '../../lib/definitions/constants'; /** - * Commit message informations. + * Commit message information. * * @typedef {Object} Commit * @property {String} branch The commit branch. @@ -15,21 +15,34 @@ import {GIT_NOTE_REF} from '../../lib/definitions/constants'; * @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. - * 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 `false`, creates a regular repository and initialize it. Change the current working directory to the repository root. + * If `withRemote` is `true`, creates a shallow clone. Change the current working directory to the clone 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 {String} [branch='master'] The branch to initialize. * @return {String} The path of the clone if `withRemote` is `true`, the path of the repository otherwise. */ export async function gitRepo(withRemote, branch = 'master') { - let cwd = tempy.directory(); - - await execa('git', ['init', ...(withRemote ? ['--bare'] : [])], {cwd}); - - const repositoryUrl = fileUrl(cwd); + let {cwd, repositoryUrl} = await initGit(withRemote); if (withRemote) { await initBareRepo(repositoryUrl, branch); cwd = await gitShallowClone(repositoryUrl, branch);