Skip to content

Commit

Permalink
Handle no previous Git tags (#322)
Browse files Browse the repository at this point in the history
Fixes #316
  • Loading branch information
itaisteinherz authored and sindresorhus committed Jan 12, 2019
1 parent 535e190 commit 966d08d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 16 additions & 0 deletions lib/git-util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
'use strict';
const execa = require('execa');

const latestTag = () => execa.stdout('git', ['describe', '--abbrev=0']);
const firstCommit = () => execa.stdout('git', ['rev-list', '--max-parents=0', 'HEAD']);

exports.latestTagOrFirstCommit = async () => {
let latest;
try {
// In case a previous tag exists, we use it to compare the current repo status to.
latest = await latestTag();
} catch (_) {
// Otherwise, we fallback to using the first commit for comparison.
latest = await firstCommit();
}

return latest;
};

exports.hasUpstream = async () => {
const {stdout} = await execa('git', ['status', '--short', '--branch', '--porcelain=2']);
return /^# branch\.upstream [\w\-/]+$/m.test(stdout);
Expand Down
7 changes: 4 additions & 3 deletions lib/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require('chalk');
const githubUrlFromGit = require('github-url-from-git');
const isScoped = require('is-scoped');
const util = require('./util');
const {latestTagOrFirstCommit} = require('./git-util');
const version = require('./version');

const prettyVersionDiff = (oldVersion, inc) => {
Expand All @@ -30,8 +31,8 @@ const prettyVersionDiff = (oldVersion, inc) => {
};

const printCommitLog = async repoUrl => {
const {stdout: latestTag} = await execa('git', ['describe', '--abbrev=0']);
const {stdout: log} = await execa('git', ['log', '--format=%s %h', `${latestTag}..HEAD`]);
const latest = await latestTagOrFirstCommit();
const {stdout: log} = await execa('git', ['log', '--format=%s %h', `${latest}..HEAD`]);

if (!log) {
return false;
Expand All @@ -46,7 +47,7 @@ const printCommitLog = async repoUrl => {
})
.join('\n');

const commitRange = util.linkifyCommitRange(repoUrl, `${latestTag}...master`);
const commitRange = util.linkifyCommitRange(repoUrl, `${latest}...master`);

console.log(`${chalk.bold('Commits:')}\n${history}\n\n${chalk.bold('Commit Range:')}\n${commitRange}\n`);

Expand Down

0 comments on commit 966d08d

Please sign in to comment.