UNPKG

1.26 kBJavaScriptView Raw
1const gitLogParser = require('git-log-parser');
2const getStream = require('get-stream');
3const debug = require('debug')('semantic-release:get-commits');
4
5/**
6 * Retrieve the list of commits on the current branch since the commit sha associated with the last release, or all the commits of the current branch if there is no last released version.
7 *
8 * @param {Object} context semantic-release context.
9 *
10 * @return {Promise<Array<Object>>} The list of commits on the branch `branch` since the last release.
11 */
12module.exports = async ({cwd, env, lastRelease: {gitHead}, logger}) => {
13 if (gitHead) {
14 debug('Use gitHead: %s', gitHead);
15 } else {
16 logger.log('No previous release found, retrieving all commits');
17 }
18
19 Object.assign(gitLogParser.fields, {hash: 'H', message: 'B', gitTags: 'd', committerDate: {key: 'ci', type: Date}});
20 const commits = (await getStream.array(
21 gitLogParser.parse({_: `${gitHead ? gitHead + '..' : ''}HEAD`}, {cwd, env: {...process.env, ...env}})
22 )).map(commit => {
23 commit.message = commit.message.trim();
24 commit.gitTags = commit.gitTags.trim();
25 return commit;
26 });
27 logger.log(`Found ${commits.length} commits since last release`);
28 debug('Parsed commits: %o', commits);
29 return commits;
30};