diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index 25c947730..31b1e77e9 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -111,7 +111,7 @@ class GitWorkflow { // Save stash of entire original state, including unstaged and untracked changes. // `--keep-index leaves only staged files on disk, for tasks.` - await this.execGit(['stash', 'save', '--quiet', '--include-untracked', '--keep-index', STASH]) + await this.execGit(['stash', 'save', '--include-untracked', '--keep-index', STASH]) // Restore meta information about ongoing git merge await this.restoreMergeStatus() @@ -134,6 +134,9 @@ class GitWorkflow { debug('Done backing up original state!') } catch (error) { + if (error.message && error.message.includes('You do not have the initial commit yet')) { + ctx.emptyGitRepo = true + } handleError(error, ctx) } } diff --git a/lib/runAll.js b/lib/runAll.js index ec0ed40bc..735c88d66 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -187,19 +187,22 @@ module.exports = async function runAll( ${symbols.warning} ${chalk.yellow(`lint-staged prevented an empty git commit. Use the --allow-empty option to continue, or check your task configuration`)} `) - } - - // Show help text about manual restore in case of git errors. - // No sense to show this if the backup stash itself is missing. - else if (error.context.gitError && !error.context.gitGetBackupStashError) { - logger.error(` - ${symbols.error} ${chalk.red(`lint-staged failed due to a git error. - Any lost modifications can be restored from a git stash: + } else if (error.context.gitError && !error.context.gitGetBackupStashError) { + logger.error(`\n ${symbols.error} ${chalk.red(`lint-staged failed due to a git error.`)}`) + + if (error.context.emptyGitRepo) { + logger.error( + `\n The initial commit is needed for lint-staged to work. + Please use the --no-verify flag to skip running lint-staged.` + ) + } else { + // No sense to show this if the backup stash itself is missing. + logger.error(` Any lost modifications can be restored from a git stash: > git stash list stash@{0}: On master: automatic lint-staged backup - > git stash pop stash@{0}`)} -`) + > git stash pop stash@{0}\n`) + } } throw error diff --git a/test/runAll.unmocked.2.spec.js b/test/runAll.unmocked.2.spec.js index 47b7805a0..202449e33 100644 --- a/test/runAll.unmocked.2.spec.js +++ b/test/runAll.unmocked.2.spec.js @@ -120,7 +120,7 @@ describe('runAll', () => { LOG → Skipped because of previous git error. ERROR × lint-staged failed due to a git error. - Any lost modifications can be restored from a git stash: + ERROR Any lost modifications can be restored from a git stash: > git stash list stash@{0}: On master: automatic lint-staged backup diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js index 302afad8d..2d6f01cbd 100644 --- a/test/runAll.unmocked.spec.js +++ b/test/runAll.unmocked.spec.js @@ -383,7 +383,7 @@ describe('runAll', () => { " ERROR × lint-staged failed due to a git error. - Any lost modifications can be restored from a git stash: + ERROR Any lost modifications can be restored from a git stash: > git stash list stash@{0}: On master: automatic lint-staged backup @@ -774,3 +774,28 @@ describe('runAll', () => { expect(await readFile('test.js', submoduleDir)).toEqual(testJsFilePretty) }) }) + +describe('runAll', () => { + it('Should throw when run on an empty git repo without an initial commit', async () => { + const tmpDir = await createTempDir() + const cwd = normalize(tmpDir) + const logger = makeConsoleMock() + + await execGit('init', { cwd }) + await execGit(['config', 'user.name', '"test"'], { cwd }) + await execGit(['config', 'user.email', '"test@test.com"'], { cwd }) + await appendFile('test.js', testJsFilePretty, cwd) + await execGit(['add', 'test.js'], { cwd }) + await expect( + runAll({ config: { '*.js': 'prettier --list-different' }, cwd, quiet: true }, logger) + ).rejects.toThrowErrorMatchingInlineSnapshot(`"Something went wrong"`) + expect(logger.printHistory()).toMatchInlineSnapshot(` + " + ERROR + × lint-staged failed due to a git error. + ERROR + The initial commit is needed for lint-staged to work. + Please use the --no-verify flag to skip running lint-staged." + `) + }) +})