From 940f809ac009987fee8c2982043018f7fcf7d2a6 Mon Sep 17 00:00:00 2001 From: Bengt Brodersen Date: Wed, 17 Aug 2022 11:50:34 +0200 Subject: [PATCH] fix: handle empty repository and no release tag --- lib/git.js | 11 +++++++++-- lib/gitCommitConvention.js | 2 +- package.json | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/git.js b/lib/git.js index eff8952..e57ac83 100644 --- a/lib/git.js +++ b/lib/git.js @@ -18,7 +18,7 @@ async function getLastTag( .then(output => output.trim()) .catch(err => { // return undefined if no tag was found - if (err.exitCode === 128) { + if (err.code === 128) { return; } throw err; @@ -31,7 +31,14 @@ const LOG_FIELD_SEPARATOR = '===LOG_FIELD_SEPARATOR==='; async function getCommitLog(from, to = 'HEAD') { const gitLogFormat = ["%h", "%aI", "%s", "%b"].join(LOG_FIELD_SEPARATOR) + LOG_COMMIT_DELIMITER; const gitLog = await execAsync(`git log --reverse --format=${gitLogFormat} ${from ? `${from}..` : ''}${to}`, {encoding: 'UTF-8'}) - .then(result => result.split(LOG_COMMIT_DELIMITER + '\n').slice(0, -1)); + .then(result => result.split(LOG_COMMIT_DELIMITER + '\n').slice(0, -1)) + .catch(err => { + // return empty log if no commits available + if (err.code === 128) { + return []; + } + throw err; + }); return gitLog.map(commit => commit.split(LOG_FIELD_SEPARATOR)) .map(fields => ({ diff --git a/lib/gitCommitConvention.js b/lib/gitCommitConvention.js index c773e91..98bf232 100644 --- a/lib/gitCommitConvention.js +++ b/lib/gitCommitConvention.js @@ -20,7 +20,7 @@ module.exports = function (convention, commitAnchor = 'HEAD') { this.getVersion = async function () { let version = { major: 0, - minor: 0, + minor: 1, patch: 0 }; diff --git a/package.json b/package.json index b3467b3..ad41398 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "git-conventional-commits", - "version": "2.1.0", + "version": "2.1.1", "description": "git conventional commits util", "licence": "GPLv3", "main": "cli.js",