Skip to content

Commit 1f9e9ff

Browse files
committed
fix(main): fix exec git error when not a git repository
1 parent fc85e4c commit 1f9e9ff

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

git-consistent

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const emoji = require('node-emoji')
1313

1414
const templateFileName = '.gitcommit_template'
1515
const definitionsFileName = '.git_consistent'
16-
const projectRoot = execSync(`git rev-parse --show-toplevel 2> /dev/null`).toString().trim()
1716

1817
const black = '\u001b[30m'
1918
const red = '\u001b[31m'
@@ -32,6 +31,14 @@ const errorColor = red
3231

3332
const version = '0.5.1'
3433

34+
const getProjectRoot = () => {
35+
try {
36+
return execSync(`git rev-parse --show-toplevel 2> /dev/null`).toString().trim()
37+
} catch (_e) {
38+
throw new Error(`Not a git repository`)
39+
}
40+
}
41+
3542
const loadDefinitions = (path) => {
3643
return yaml.safeLoad(fs.readFileSync(path, 'utf8'))
3744
}
@@ -285,6 +292,7 @@ const main = (program, template, definitions, terms) => {
285292
// Main
286293
//
287294
try {
295+
const projectRoot = getProjectRoot()
288296
const rootPath = projectRoot === "" ? process.env.HOME : projectRoot
289297
const templateFilePath = path.join(rootPath, templateFileName)
290298
const definitionsFilePath = path.join(rootPath, definitionsFileName)

0 commit comments

Comments
 (0)