From 6fa9c85d1d03c8a9b4b3dea8be8f3857798c82c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20Ja=CC=88ppinen?= Date: Thu, 26 Sep 2019 14:48:06 +0300 Subject: [PATCH] refactor: improvements based on PR review --- lib/gitWorkflow.js | 50 ++++++++++++++++---------- lib/runAll.js | 4 +-- package.json | 4 +-- test/__snapshots__/runAll.spec.js.snap | 24 ++++++------- test/runAll.unmocked.spec.js | 5 ++- yarn.lock | 10 +++--- 6 files changed, 54 insertions(+), 43 deletions(-) diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index b6d5af1d9..66e6c3072 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -44,7 +44,7 @@ class GitWorkflow { /** * Create backup stashes, one of everything and one of only staged changes - * Leves stages files in index for running tasks + * Staged files are left in the index for running tasks * * @param {Object} [options] * @returns {Promise} @@ -83,14 +83,11 @@ class GitWorkflow { .split('\n') .map(file => normalize(path.resolve(this.cwd, file))) this.untrackedFiles = new Map() - const readPromises = [] - for (const file of untrackedFiles) { - // Read all untracked files into buffers, and save them into internal Map - readPromises.push( + await Promise.all( + untrackedFiles.map(file => readBufferFromFile(file).then(buffer => this.untrackedFiles.set(file, buffer)) ) - } - await Promise.all(readPromises) + ) debug('Done backing up untracked files!') } @@ -105,42 +102,57 @@ class GitWorkflow { '--unified=0', '--no-color', '--no-ext-diff', - '-p', + '--patch', original, - '-R' + '-R' // Show diff in reverse ]) debug('Done backing up original state!') } /** - * Resets everything and applies back unstaged and staged changes, - * possibly with modifications by tasks + * Applies back unstaged changes that have been hidden in the stash. + * In case of a merge-conflict retry with 3-way merge. * * @param {Object} [options] * @returns {Promise} */ async restoreUnstagedChanges() { debug('Restoring unstaged changes...') + if (this.unstagedDiff) { try { await this.execGit(gitApplyArgs, { input: `${this.unstagedDiff}\n` }) } catch (error) { - debug('Error when restoring changes:') + debug('Error while restoring changes:') debug(error) debug('Retrying with 3-way merge') - // Retry with `--3way` if normal apply fails - await this.execGit([...gitApplyArgs, '--3way'], { input: `${this.unstagedDiff}\n` }) + + try { + // Retry with `--3way` if normal apply fails + await this.execGit([...gitApplyArgs, '--3way'], { input: `${this.unstagedDiff}\n` }) + } catch (error2) { + debug('Error while restoring unstaged changes using 3-way merge:') + debug(error2) + throw new Error('Unstaged changes could not be restored due to a merge conflict!') + } } } debug('Done restoring unstaged changes!') if (this.untrackedFiles) { debug('Restoring untracked files...') - const writePromises = [] - this.untrackedFiles.forEach((buffer, file) => { - writePromises.push(writeBufferToFile(file, buffer)) - }) - await Promise.all(writePromises) + try { + // Iterate over Map and await for all to complete + const writePromises = [] + this.untrackedFiles.forEach((buffer, file) => { + writePromises.push(writeBufferToFile(file, buffer)) + }) + await Promise.all(writePromises) + } catch (error) { + debug('Error while restoring untracked changes:') + debug(error) + throw new Error('Untracked changes could not be restored due to an error!') + } debug('Done restoring untracked files!') } } diff --git a/lib/runAll.js b/lib/runAll.js index 05875a875..acfcfb90c 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -115,12 +115,12 @@ module.exports = async function runAll( task: () => new Listr(tasks, { ...listrOptions, concurrent: true, exitOnError: false }) }, { - title: 'Applying unstaged changes...', + title: 'Restoring unstaged changes...', skip: ctx => ctx.hasErrors && 'Skipped because of errors from tasks', task: () => git.restoreUnstagedChanges() }, { - title: 'Restoring original state due to errors...', + title: 'Reverting to original state...', enabled: ctx => ctx.hasErrors, task: () => git.restoreOriginalState() }, diff --git a/package.json b/package.json index e3a6a8d27..d62b83aab 100644 --- a/package.json +++ b/package.json @@ -68,8 +68,8 @@ "jest": "^24.8.0", "jest-snapshot-serializer-ansi": "^1.0.0", "jsonlint": "^1.6.3", - "prettier": "1.18.2", - "uuid": "^3.3.3" + "nanoid": "^2.1.1", + "prettier": "1.18.2" }, "config": { "commitizen": { diff --git a/test/__snapshots__/runAll.spec.js.snap b/test/__snapshots__/runAll.spec.js.snap index bbf2818ec..65f538180 100644 --- a/test/__snapshots__/runAll.spec.js.snap +++ b/test/__snapshots__/runAll.spec.js.snap @@ -10,8 +10,8 @@ LOG echo \\"sample\\" [started] LOG echo \\"sample\\" [completed] LOG Running tasks for *.js [completed] LOG Running tasks... [completed] -LOG Applying unstaged changes... [started] -LOG Applying unstaged changes... [completed] +LOG Restoring unstaged changes... [started] +LOG Restoring unstaged changes... [completed] LOG Cleaning up... [started] LOG Cleaning up... [completed]" `; @@ -33,11 +33,11 @@ LOG → LOG Running tasks for *.js [failed] LOG → LOG Running tasks... [failed] -LOG Applying unstaged changes... [started] -LOG Applying unstaged changes... [skipped] +LOG Restoring unstaged changes... [started] +LOG Restoring unstaged changes... [skipped] LOG → Skipped because of errors from tasks -LOG Restoring original state due to errors... [started] -LOG Restoring original state due to errors... [completed] +LOG Reverting to original state... [started] +LOG Reverting to original state... [completed] LOG Cleaning up... [started] LOG Cleaning up... [completed] LOG { @@ -64,11 +64,11 @@ LOG → LOG Running tasks for *.js [failed] LOG → LOG Running tasks... [failed] -LOG Applying unstaged changes... [started] -LOG Applying unstaged changes... [skipped] +LOG Restoring unstaged changes... [started] +LOG Restoring unstaged changes... [skipped] LOG → Skipped because of errors from tasks -LOG Restoring original state due to errors... [started] -LOG Restoring original state due to errors... [completed] +LOG Reverting to original state... [started] +LOG Reverting to original state... [completed] LOG Cleaning up... [started] LOG Cleaning up... [completed] LOG { @@ -103,8 +103,8 @@ LOG echo \\"sample\\" [started] LOG echo \\"sample\\" [completed] LOG Running tasks for *.js [completed] LOG Running tasks... [completed] -LOG Applying unstaged changes... [started] -LOG Applying unstaged changes... [completed] +LOG Restoring unstaged changes... [started] +LOG Restoring unstaged changes... [completed] LOG Cleaning up... [started] LOG Cleaning up... [completed]" `; diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js index 3e9e9be47..0381d3ba3 100644 --- a/test/runAll.unmocked.spec.js +++ b/test/runAll.unmocked.spec.js @@ -3,7 +3,7 @@ import makeConsoleMock from 'consolemock' import normalize from 'normalize-path' import os from 'os' import path from 'path' -import uuid from 'uuid' +import nanoid from 'nanoid' import execGitBase from '../lib/execGit' import runAll from '../lib/runAll' @@ -36,7 +36,7 @@ const osTmpDir = isAppveyor ? 'C:\\projects' : fs.realpathSync(os.tmpdir()) * @returns {Promise} */ const createTempDir = async () => { - const dirname = path.resolve(osTmpDir, 'lint-staged-test', uuid()) + const dirname = path.resolve(osTmpDir, 'lint-staged-test', nanoid()) await fs.ensureDir(dirname) return dirname } @@ -467,6 +467,5 @@ describe('runAll', () => { expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('test') expect(await readFile('test.js')).toEqual(testJsFilePretty) expect(await readFile('test-untracked.js')).toEqual(testJsFilePretty) - console = makeConsoleMock() }) }) diff --git a/yarn.lock b/yarn.lock index 16aff1e93..69c9503ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4173,6 +4173,11 @@ nan@^2.12.1: resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg== +nanoid@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.1.1.tgz#524fd4acd45c126e0c87cd43ab5ee8346e695df9" + integrity sha512-0YbJdaL4JFoejIOoawgLcYValFGJ2iyUuVDIWL3g8Es87SSOWFbWdRUMV3VMSiyPs3SQ3QxCIxFX00q5DLkMCw== + nanomatch@^1.2.9: version "1.2.13" resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119" @@ -5854,11 +5859,6 @@ uuid@^3.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA== -uuid@^3.3.3: - version "3.3.3" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866" - integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ== - v8-compile-cache@^2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.0.3.tgz#00f7494d2ae2b688cfe2899df6ed2c54bef91dbe"