fix: fetch tags on repo cached by the CI

This commit is contained in:
Pierre Vanduynslager
2020-01-15 13:43:44 -05:00
parent 28b54800cd
commit 6b5b02ea75
3 changed files with 52 additions and 6 deletions
+12 -5
View File
@@ -86,15 +86,22 @@ async function isRefExists(ref, execaOpts) {
}
/**
* Unshallow the git repository if necessary and fetch all the tags.
* Fetch all the tags from a branch. Unshallow if necessary.
* This will update the local branch from the latest on the remote if:
* - The branch is not the one that triggered the CI
* - The CI created a detached head
*
* Otherwise it just calls `git fetch` without specifying the `refspec` option to avoid overwritting the head commit set by the CI.
*
* The goal is to retrieve the informations on all the release branches without "disturbing" the CI, leaving the trigger branch or the detached head intact.
*
* @param {String} repositoryUrl The remote repository URL.
* @param {String} branch The repository branch to fetch.
* @param {Object} [execaOpts] Options to pass to `execa`.
*/
async function fetch(repositoryUrl, branch, ciBranch, execaOpts) {
const isLocalExists =
(await execa('git', ['rev-parse', '--verify', branch], {...execaOpts, reject: false})).exitCode === 0;
const isDetachedHead =
(await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {...execaOpts, reject: false})).stdout === 'HEAD';
try {
await execa(
@@ -103,7 +110,7 @@ async function fetch(repositoryUrl, branch, ciBranch, execaOpts) {
'fetch',
'--unshallow',
'--tags',
...(branch === ciBranch && isLocalExists
...(branch === ciBranch && !isDetachedHead
? [repositoryUrl]
: ['--update-head-ok', repositoryUrl, `+refs/heads/${branch}:refs/heads/${branch}`]),
],
@@ -115,7 +122,7 @@ async function fetch(repositoryUrl, branch, ciBranch, execaOpts) {
[
'fetch',
'--tags',
...(branch === ciBranch && isLocalExists
...(branch === ciBranch && !isDetachedHead
? [repositoryUrl]
: ['--update-head-ok', repositoryUrl, `+refs/heads/${branch}:refs/heads/${branch}`]),
],