Skip to content

Commit

Permalink
fix: correct error when remote repository has no branches
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefcipa authored and pvdlg committed Jan 27, 2020
1 parent b54b20d commit c6b1076
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/git.js
Expand Up @@ -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\/(?<branch>.+)$/)[1])
.filter(Boolean);
.filter(Boolean)
.map(branch => branch.match(/^.+refs\/heads\/(?<branch>.+)$/)[1]);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions test/git.test.js
Expand Up @@ -33,6 +33,7 @@ import {
gitAddNote,
gitGetNote,
gitFetch,
initGit,
} from './helpers/git-utils';

test('Get the last commit sha', async t => {
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 21 additions & 8 deletions test/helpers/git-utils.js
Expand Up @@ -7,29 +7,42 @@ 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.
* @property {String} hash The commit hash.
* @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);
Expand Down

0 comments on commit c6b1076

Please sign in to comment.