Skip to content

Commit

Permalink
feat: bail on incompatible git versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Skeggs committed Jan 31, 2020
1 parent 9a17d65 commit 0a8eecc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
35 changes: 31 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"execa": "^4.0.0",
"husky": "^4.2.1",
"lodash": "^4.17.15",
"semver": "^7.1.1",
"toml": "^3.0.0",
"yargs": "^15.1.0"
},
Expand Down
15 changes: 15 additions & 0 deletions src/bin/git-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import execa from 'execa';
import semver from 'semver';

export const git = (...args) => execa('git', args).then(({ stdout }) => stdout);

async function getGitVersion() {
// This produces a string like "git version 2.25.0"
const version = await git('version');
return version.slice(version.lastIndexOf(' ') + 1);
}

export async function getCurrentBranch() {
if (semver.lt(await getGitVersion(), '2.22.0')) {
throw new Error('please upgrade git to at least 2.22.0');
}

return git('branch', '--show-current');
}
4 changes: 2 additions & 2 deletions src/bin/hook-commands/pre-push.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { commitlintOrExit } from '../commitlint';
import { expectEnabled, getMode } from '../config';
import { once } from 'lodash';
import { git } from '../git-utils';
import { git, getCurrentBranch } from '../git-utils';

const getRemoteBranch = once(async function getRemoteBranch() {
const headRef = await git('symbolic-ref', '-q', 'HEAD');
Expand Down Expand Up @@ -34,7 +34,7 @@ export default {
switch (mode) {
case 'unpushed': {
const [localBranch, remoteBranch] = await Promise.all([
git('branch', '--show-current'),
getCurrentBranch(),
getRemoteBranch(),
]);

Expand Down

0 comments on commit 0a8eecc

Please sign in to comment.