Skip to content

Commit

Permalink
fix: make cleaning untracked files resurrected due to git bug testable
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 29, 2020
1 parent 2225111 commit 9842f76
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
18 changes: 13 additions & 5 deletions lib/gitWorkflow.js
Expand Up @@ -107,6 +107,18 @@ class GitWorkflow {
}
}

/**
* List and delete untracked files
*/
async cleanUntrackedFiles() {
const lsFiles = await this.execGit(['ls-files', '--others', '--exclude-standard'])
const untrackedFiles = lsFiles
.split('\n')
.filter(Boolean)
.map(file => path.resolve(this.gitDir, file))
await Promise.all(untrackedFiles.map(file => unlink(file)))
}

/**
* Create backup stashes, one of everything and one of only staged changes
* Staged files are left in the index for running tasks
Expand Down Expand Up @@ -136,11 +148,7 @@ class GitWorkflow {

// There is a bug in git =< 2.13.0 where `--keep-index` resurrects deleted files.
// These files should be listed and deleted before proceeding.
const untrackedFiles = (await execGit(['ls-files', '--others', '--exclude-standard']))
.split('\n')
.filter(Boolean)
.map(file => path.resolve(this.gitDir, file))
await Promise.all(untrackedFiles.map(file => unlink(file)))
await this.cleanUntrackedFiles()

// Get a diff of unstaged changes by diffing the saved stash against what's left on disk.
await this.execGit([
Expand Down
4 changes: 2 additions & 2 deletions test/file.spec.js
Expand Up @@ -2,12 +2,12 @@ import { unlink, readFile } from '../lib/file'

describe('unlink', () => {
it('should throw when second argument is false and file is not found', async () => {
await expect(unlink('example', false)).rejects.toThrowError()
await expect(unlink('example', false)).rejects.toThrow('ENOENT')
})
})

describe('readFile', () => {
it('should throw when second argument is false and file is not found', async () => {
await expect(readFile('example', false)).rejects.toThrowError()
await expect(readFile('example', false)).rejects.toThrow('ENOENT')
})
})
15 changes: 15 additions & 0 deletions test/gitWorkflow.spec.js
Expand Up @@ -91,4 +91,19 @@ describe('gitWorkflow', () => {
})
})
})

describe('cleanUntrackedFiles', () => {
it('should remove untracked files', async () => {
const tempFile = path.resolve(cwd, 'tempFile')
await fs.writeFile(tempFile, 'Hello')

const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git')
})

await gitWorkflow.cleanUntrackedFiles()
await expect(fs.access(tempFile)).rejects.toThrow('ENOENT')
})
})
})

0 comments on commit 9842f76

Please sign in to comment.