Skip to content

Commit

Permalink
Merge pull request #21 from Updater/fetch-git-tags
Browse files Browse the repository at this point in the history
Fetch git tags
  • Loading branch information
pmowrer authored Jan 9, 2018
2 parents 10df99d + d0f0ab8 commit c562f5c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
24 changes: 12 additions & 12 deletions src/git-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { pipeP, split } = require('ramda');
const execa = require('execa');
const debug = require('debug')('semantic-release-monorepo');
const debug = require('debug')('semantic-release:monorepo');

const git = async (args, options = {}) => {
const { stdout } = await execa('git', args, options);
Expand All @@ -23,7 +23,7 @@ const getCommitFiles = pipeP(
* @async
* @return {Promise<String>} System path of the git repository.
*/
const getGitRoot = () => git(['rev-parse', '--show-toplevel']);
const getRoot = () => git(['rev-parse', '--show-toplevel']);

/**
* Get the commit sha for a given tag.
Expand All @@ -32,14 +32,13 @@ const getGitRoot = () => git(['rev-parse', '--show-toplevel']);
* @param {string} tagName Tag name for which to retrieve the commit sha.
* @return {string} The commit sha of the tag in parameter or `null`.
*/
async function gitTagHead(tagName) {
try {
return await git(['rev-list', '-1', tagName]);
} catch (err) {
debug(err);
return null;
}
}
const getTagHead = tagName =>
git(['rev-list', '-1', tagName], { reject: false });

/**
* Fetch tags from the repository's origin.
*/
const fetchTags = () => git(['fetch', '--tags']);

/**
* Unshallow the git repository (retrieving every commit and tags).
Expand All @@ -50,7 +49,8 @@ const unshallow = () =>

module.exports = {
getCommitFiles,
getGitRoot,
gitTagHead,
getRoot,
getTagHead,
fetchTags,
unshallow,
};
4 changes: 2 additions & 2 deletions src/only-package-commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const { identity, memoizeWith, pipeP } = require('ramda');
const pkgUp = require('pkg-up');
const readPkg = require('read-pkg');
const debug = require('debug')('semantic-release:monorepo');
const { getCommitFiles, getGitRoot } = require('./git-utils');
const { getCommitFiles, getRoot } = require('./git-utils');
const { mapCommits } = require('./options-transforms');

const memoizedGetCommitFiles = memoizeWith(identity, getCommitFiles);

const getPackagePath = async () => {
const path = await pkgUp();
const gitRoot = await getGitRoot();
const gitRoot = await getRoot();
return path.replace('package.json', '').replace(`${gitRoot}/`, '');
};

Expand Down
53 changes: 33 additions & 20 deletions src/with-version-head.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
const {
always,
curryN,
equals,
isNil,
pipeP,
tap,
unless,
when,
} = require('ramda');
const { curryN, isEmpty, pipeP, tap, unless, when } = require('ramda');

const debug = require('debug')('semantic-release:monorepo');
const gitTag = require('./version-to-git-tag');
const { gitTagHead, unshallow } = require('./git-utils');
const { getTagHead, fetchTags, unshallow } = require('./git-utils');

const debugTap = message => tap(curryN(2, debug)(message));
const whenIsEmpty = when(isEmpty);
const unlessIsEmpty = unless(isEmpty);

/**
* Attempt to find the git tag for the given tag name.
* Will "unshallow" the repo and try again if not successful.
* If necessary, will fetch git tags and "unshallow".
* Adapted from: https://github.com/semantic-release/npm/blob/cf039fdafda1a5ce43c2a5f033160cd46487f102/lib/get-version-head.js
*/
const getTagHead = tagName =>
const findTagHead = tagName =>
pipeP(
gitTagHead,
when(isNil, pipeP(unshallow, always(tagName))),
when(equals(tagName), gitTagHead),
unless(isNil, tap(curryN(2, debug)('Use tagHead: %s')))
getTagHead,
whenIsEmpty(
// Fetching tags is likely pointless when running on CI since it's assumed
// that the repo has just been cloned (implying up-to-date tags).
// However, when running locally (e.g. `--dry-run`), it's not uncommon that
// tags are out of sync with origin, which is why we include this step.
pipeP(
fetchTags,
debugTap('Unable to find tagHead. Fetching tags and re-trying.'),
() => getTagHead(tagName)
)
),
whenIsEmpty(
// Unshallowing is likely only relevant on CI, where using a shallow clone
// is more performant than downloading the entire repo.
pipeP(
unshallow,
debugTap('Unable to find tagHead. Unshallowing and re-trying.'),
() => getTagHead(tagName)
)
),
unlessIsEmpty(debugTap('Use tagHead: %o'))
)(tagName);

/**
Expand All @@ -36,13 +49,13 @@ const getTagHead = tagName =>
* 2. We can use `semantic-release`'s fallback strategy, searching for a matching git tag,
* but we must update the git tag format to be compatible with the monorepo workflow.
**/
const withVersionHead = plugin => async (pluginConfig, options) => {
const release = await plugin(pluginConfig, options);
const withVersionHead = plugin => async (pluginConfig, config) => {
const release = await plugin(pluginConfig, config);

if (release) {
return {
...release,
gitHead: await getTagHead(await gitTag(release.version)),
gitHead: await findTagHead(await gitTag(release.version)),
};
}
};
Expand Down

0 comments on commit c562f5c

Please sign in to comment.