From 45eee4acdd2a213672466369bcf0a04cd39ee0e1 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Mon, 2 Jul 2018 18:25:43 -0400 Subject: [PATCH] fix: fetch all tags even if the repo is not shallow --- index.js | 5 ++--- lib/git.js | 8 ++++---- test/git.test.js | 26 ++++++++++++++++++++++---- test/helpers/git-utils.js | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 15572a88..172d26e4 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ const getLastRelease = require('./lib/get-last-release'); const {extractErrors} = require('./lib/utils'); const getGitAuthUrl = require('./lib/get-git-auth-url'); const logger = require('./lib/logger'); -const {unshallow, verifyAuth, isBranchUpToDate, gitHead: getGitHead, tag, push} = require('./lib/git'); +const {fetch, verifyAuth, isBranchUpToDate, gitHead: getGitHead, tag, push} = require('./lib/git'); const getError = require('./lib/get-error'); const {COMMIT_NAME, COMMIT_EMAIL} = require('./lib/definitions/constants'); @@ -75,8 +75,7 @@ async function run(options, plugins) { logger.log('Call plugin %s', 'verify-conditions'); await plugins.verifyConditions({options, logger}, {settleAll: true}); - // Unshallow the repo in order to get all the tags - await unshallow(options.repositoryUrl); + await fetch(options.repositoryUrl); const lastRelease = await getLastRelease(options.tagFormat, logger); const commits = await getCommits(lastRelease.gitHead, options.branch, logger); diff --git a/lib/git.js b/lib/git.js index a93655eb..1e44ec98 100644 --- a/lib/git.js +++ b/lib/git.js @@ -49,15 +49,15 @@ async function isRefInHistory(ref) { } /** - * Unshallow the git repository (retriving every commits and tags). + * Unshallow the git repository if necessary and fetch all the tags. * * @param {String} repositoryUrl The remote repository URL. */ -async function unshallow(repositoryUrl) { +async function fetch(repositoryUrl) { try { await execa('git', ['fetch', '--unshallow', '--tags', repositoryUrl]); } catch (err) { - debug(err); + await execa('git', ['fetch', '--tags', repositoryUrl]); } } @@ -164,7 +164,7 @@ module.exports = { gitTagHead, gitTags, isRefInHistory, - unshallow, + fetch, gitHead, repoUrl, isGitRepo, diff --git a/test/git.test.js b/test/git.test.js index 1e7988d8..63d2a3a1 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -3,7 +3,7 @@ import tempy from 'tempy'; import { gitTagHead, isRefInHistory, - unshallow, + fetch, gitHead, repoUrl, tag, @@ -24,6 +24,7 @@ import { gitCommitTag, gitRemoteTagHead, gitPush, + gitDetachedHead, } from './helpers/git-utils'; // Save the current working diretory @@ -52,7 +53,7 @@ test.serial('Throw error if the last commit sha cannot be found', async t => { await t.throws(gitHead(), Error); }); -test.serial('Unshallow repository', async t => { +test.serial('Unshallow and fetch repository', async t => { // Create a git repository, set the current working directory at the root of the repo const repo = await gitRepo(); // Add commits to the master branch @@ -63,7 +64,7 @@ test.serial('Unshallow repository', async t => { // Verify the shallow clone contains only one commit t.is((await gitGetCommits()).length, 1); - await unshallow(repo); + await fetch(repo); // Verify the shallow clone contains all the commits t.is((await gitGetCommits()).length, 2); @@ -74,7 +75,24 @@ test.serial('Do not throw error when unshallow a complete repository', async t = const repo = await gitRepo(); // Add commits to the master branch await gitCommits(['First']); - await t.notThrows(unshallow(repo)); + await t.notThrows(fetch(repo)); +}); + +test.serial('Fetch all tags on a detached head repository', async t => { + const repo = await gitRepo(true); + + await gitCommits(['First']); + await gitTagVersion('v1.0.0'); + await gitCommits(['Second']); + await gitTagVersion('v1.0.1'); + const [commit] = await gitCommits(['Third']); + await gitTagVersion('v1.1.0'); + await gitPush(); + await gitDetachedHead(repo, commit.hash); + + await fetch(repo); + + t.deepEqual((await gitTags()).sort(), ['v1.0.0', 'v1.0.1', 'v1.1.0'].sort()); }); test.serial('Verify if the commit `sha` is in the direct history of the current branch', async t => { diff --git a/test/helpers/git-utils.js b/test/helpers/git-utils.js index 05c17cc5..c891070d 100644 --- a/test/helpers/git-utils.js +++ b/test/helpers/git-utils.js @@ -151,7 +151,7 @@ export async function gitDetachedHead(repositoryUrl, head) { process.chdir(dir); await execa('git', ['init']); await execa('git', ['remote', 'add', 'origin', repositoryUrl]); - await execa('git', ['fetch']); + await execa('git', ['fetch', repositoryUrl]); await execa('git', ['checkout', head]); return dir; }