diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index df4a6f813..c8570f29a 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -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 @@ -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([ diff --git a/test/file.spec.js b/test/file.spec.js index c49a7dbde..93f3430c9 100644 --- a/test/file.spec.js +++ b/test/file.spec.js @@ -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') }) }) diff --git a/test/gitWorkflow.spec.js b/test/gitWorkflow.spec.js index 972ee79ef..97d7cb5e9 100644 --- a/test/gitWorkflow.spec.js +++ b/test/gitWorkflow.spec.js @@ -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') + }) + }) })