Skip to content

Commit

Permalink
fix: print a better warning when the initial commit is missing
Browse files Browse the repository at this point in the history
lint-staged uses git stashes internally, and git stashes require the initial commit to work
  • Loading branch information
iiroj committed Jan 21, 2020
1 parent 4d2900c commit 293547d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
5 changes: 4 additions & 1 deletion lib/gitWorkflow.js
Expand Up @@ -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()
Expand All @@ -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)
}
}
Expand Down
23 changes: 13 additions & 10 deletions lib/runAll.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/runAll.unmocked.2.spec.js
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion test/runAll.unmocked.spec.js
Expand Up @@ -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
Expand Down Expand Up @@ -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."
`)
})
})

0 comments on commit 293547d

Please sign in to comment.