Skip to content

Commit

Permalink
feat: add unique hash to backup stash message
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Dec 27, 2024
1 parent c52cc92 commit 22fe89d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-ties-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'lint-staged': minor
---

Add unique hash to each backup stash message; this refers to the dangling commit created with "git stash create"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install --save-dev lint-staged # requires further setup
```
$ git commit
✔ Backed up original state in git stash
✔ Backed up original state in git stash (5bda95f)
❯ Running tasks for staged files...
❯ packages/frontend/.lintstagedrc.json — 1 file
↓ *.js — no files [SKIPPED]
Expand Down
22 changes: 17 additions & 5 deletions lib/gitWorkflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ export class GitWorkflow {
*/
async getBackupStash(ctx) {
const stashes = await this.execGit(['stash', 'list'])
const index = stashes.split('\n').findIndex((line) => line.includes(STASH))

const index = stashes
.split('\n')
.findIndex((line) => line.includes(STASH) && line.includes(ctx.backupHash))

if (index === -1) {
ctx.errors.add(GetBackupStashError)
throw new Error('lint-staged automatic backup is missing!')
Expand Down Expand Up @@ -223,11 +227,19 @@ export class GitWorkflow {
// Save stash of all staged files.
// The `stash create` command creates a dangling commit without removing any files,
// and `stash store` saves it as an actual stash.
const hash = await this.execGit(['stash', 'create'])
await this.execGit(['stash', 'store', '--quiet', '--message', STASH, hash])
const stashHash = await this.execGit(['stash', 'create'])
ctx.backupHash = await this.execGit(['rev-parse', '--short', stashHash])
await this.execGit([
'stash',
'store',
'--quiet',
'--message',
`${STASH} (${ctx.backupHash})`,
ctx.backupHash,
])

debugLog('Done backing up original state in "git stash"!')
task.title = 'Backed up original state in git stash'
task.title = `Backed up original state in git stash (${ctx.backupHash})`
debugLog(task.title)
} catch (error) {
handleError(error, ctx)
}
Expand Down
1 change: 1 addition & 0 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
export const getInitialState = ({ quiet = false } = {}) => ({
hasPartiallyStagedFiles: null,
shouldBackup: null,
backupHash: null,
shouldHidePartiallyStaged: true,
errors: new Set([]),
events: new EventEmitter(),
Expand Down
4 changes: 1 addition & 3 deletions test/integration/git-lock-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ describe('lint-staged', () => {
await removeFile(`.git/index.lock`)

// Luckily there is a stash
expect(await execGit(['stash', 'list'])).toMatchInlineSnapshot(
`"stash@{0}: lint-staged automatic backup"`
)
expect(await execGit(['stash', 'list'])).toMatch('stash@{0}: lint-staged automatic backup')
await execGit(['reset', '--hard'])
await execGit(['stash', 'pop', '--index'])

Expand Down
6 changes: 4 additions & 2 deletions test/unit/getBackupStash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ describe('gitWorkflow', () => {
it('should throw when stash not found even when other stashes are', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
ctx.backupHash = 'not-found'

execGit.mockResolvedValueOnce('stash@{0}: some random stuff')
execGit.mockResolvedValueOnce(`stash@{1}: ${STASH} (abc123)`)

await expect(gitWorkflow.getBackupStash(ctx)).rejects.toThrow(
'lint-staged automatic backup is missing!'
Expand All @@ -44,11 +45,12 @@ describe('gitWorkflow', () => {
it('should return ref to the backup stash', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
ctx.backupHash = 'abc123'

execGit.mockResolvedValueOnce(
[
'stash@{0}: some random stuff',
`stash@{1}: ${STASH}`,
`stash@{1}: ${STASH} (${ctx.backupHash})`,
'stash@{2}: other random stuff',
].join('\n')
)
Expand Down

0 comments on commit 22fe89d

Please sign in to comment.