Skip to content

Commit 3066a35

Browse files
committed
perf: further optimize by reusing previous job
1 parent ff0cc0d commit 3066a35

File tree

6 files changed

+13
-45
lines changed

6 files changed

+13
-45
lines changed

lib/gitWorkflow.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const execGit = require('./execGit')
77
const { readFile, unlink, writeFile } = require('./file')
88
const {
99
GitError,
10-
RestoreOriginalStateError,
1110
ApplyEmptyCommitError,
1211
HideUnstagedChangesError,
1312
RestoreMergeStatusError,
@@ -209,26 +208,6 @@ class GitWorkflow {
209208
}
210209
}
211210

212-
/**
213-
* Restore original HEAD state for partially staged files in case of errors
214-
*/
215-
async restorePartiallyStagedFiles(ctx) {
216-
try {
217-
debug('Restoring original state...')
218-
219-
// Flush all unstaged changes. Since they were diffed before
220-
// running tasks, these will include only changes generated by tasks
221-
await this.hideUnstagedChanges(ctx)
222-
223-
const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
224-
await this.execGit(['apply', ...GIT_APPLY_ARGS, unstagedPatch])
225-
226-
debug('Done restoring original state!')
227-
} catch (error) {
228-
handleError(error, ctx, RestoreOriginalStateError)
229-
}
230-
}
231-
232211
/** Add all task modifications to index for files that were staged before running. */
233212
async applyModifications(ctx) {
234213
debug('Adding task modifications to index...')
@@ -256,7 +235,7 @@ class GitWorkflow {
256235
* this is probably because of conflicts between new task modifications.
257236
* 3-way merge usually fixes this, and in case it doesn't we should just give up and throw.
258237
*/
259-
async restoreUnstagedChanges(ctx) {
238+
async restorePartialChanges(ctx) {
260239
debug('Restoring unstaged changes...')
261240
const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
262241
try {

lib/runAll.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const {
3030
hasPartiallyStagedFiles,
3131
restoreOriginalStateEnabled,
3232
restoreOriginalStateSkipped,
33-
restoreUnstagedChangesSkipped,
33+
restorePartialChangesSkipped,
3434
} = require('./state')
3535
const { GitRepoError, GetStagedFilesError, GitError } = require('./symbols')
3636

@@ -220,8 +220,8 @@ const runAll = async (
220220
},
221221
...listrTasks,
222222
{
223-
title: 'Reverting to original state because of errors...',
224-
task: (ctx) => git.restorePartiallyStagedFiles(ctx),
223+
title: 'Reverting because of errors...',
224+
task: (ctx) => git.hideUnstagedChanges(ctx),
225225
enabled: restoreOriginalStateEnabled,
226226
skip: restoreOriginalStateSkipped,
227227
},
@@ -231,10 +231,10 @@ const runAll = async (
231231
skip: applyModificationsSkipped,
232232
},
233233
{
234-
title: 'Restoring unstaged changes to partially staged files...',
235-
task: (ctx) => git.restoreUnstagedChanges(ctx),
234+
title: 'Restoring partial changes...',
235+
task: (ctx) => git.restorePartialChanges(ctx),
236236
enabled: hasPartiallyStagedFiles,
237-
skip: restoreUnstagedChangesSkipped,
237+
skip: restorePartialChangesSkipped,
238238
},
239239
{
240240
title: 'Cleaning up...',

lib/state.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const { GIT_ERROR, TASK_ERROR } = require('./messages')
44
const {
55
ApplyEmptyCommitError,
66
TaskError,
7-
RestoreOriginalStateError,
87
GitError,
98
RestoreUnstagedChangesError,
109
} = require('./symbols')
@@ -32,15 +31,11 @@ const applyModificationsSkipped = (ctx) => {
3231
}
3332
}
3433

35-
const restoreUnstagedChangesSkipped = (ctx) => {
34+
const restorePartialChangesSkipped = (ctx) => {
3635
// Should be skipped in case of git errors
3736
if (ctx.errors.has(GitError)) {
3837
return GIT_ERROR
3938
}
40-
// Should be skipped when tasks fail
41-
if (ctx.errors.has(TaskError)) {
42-
return TASK_ERROR
43-
}
4439
}
4540

4641
const restoreOriginalStateEnabled = (ctx) => ctx.shouldBackup && ctx.errors.has(TaskError)
@@ -67,17 +62,13 @@ const cleanupSkipped = (ctx) => {
6762
) {
6863
return GIT_ERROR
6964
}
70-
// Should be skipped when reverting to original state fails
71-
if (ctx.errors.has(RestoreOriginalStateError)) {
72-
return GIT_ERROR
73-
}
7465
}
7566

7667
module.exports = {
7768
getInitialState,
7869
hasPartiallyStagedFiles,
7970
applyModificationsSkipped,
80-
restoreUnstagedChangesSkipped,
71+
restorePartialChangesSkipped,
8172
restoreOriginalStateEnabled,
8273
restoreOriginalStateSkipped,
8374
cleanupEnabled,

lib/symbols.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const GitRepoError = Symbol('GitRepoError')
88
const HideUnstagedChangesError = Symbol('HideUnstagedChangesError')
99
const InvalidOptionsError = new Error('Invalid Options')
1010
const RestoreMergeStatusError = Symbol('RestoreMergeStatusError')
11-
const RestoreOriginalStateError = Symbol('RestoreOriginalStateError')
1211
const RestoreUnstagedChangesError = Symbol('RestoreUnstagedChangesError')
1312
const TaskError = Symbol('TaskError')
1413

@@ -21,7 +20,6 @@ module.exports = {
2120
InvalidOptionsError,
2221
HideUnstagedChangesError,
2322
RestoreMergeStatusError,
24-
RestoreOriginalStateError,
2523
RestoreUnstagedChangesError,
2624
TaskError,
2725
}

test/__mocks__/gitWorkflow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const stub = {
22
prepare: jest.fn().mockImplementation(() => Promise.resolve()),
33
hideUnstagedChanges: jest.fn().mockImplementation(() => Promise.resolve()),
44
applyModifications: jest.fn().mockImplementation(() => Promise.resolve()),
5-
restoreUnstagedChanges: jest.fn().mockImplementation(() => Promise.resolve()),
5+
restorePartialChanges: jest.fn().mockImplementation(() => Promise.resolve()),
66
restoreOriginalState: jest.fn().mockImplementation(() => Promise.resolve()),
77
cleanup: jest.fn().mockImplementation(() => Promise.resolve()),
88
}

test/state.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
applyModificationsSkipped,
33
cleanupSkipped,
44
restoreOriginalStateSkipped,
5-
restoreUnstagedChangesSkipped,
5+
restorePartialChangesSkipped,
66
} from '../lib/state'
77
import { GitError, RestoreOriginalStateError } from '../lib/symbols'
88

@@ -18,9 +18,9 @@ describe('applyModificationsSkipped', () => {
1818
})
1919
})
2020

21-
describe('restoreUnstagedChangesSkipped', () => {
21+
describe('restorePartialChangesSkipped', () => {
2222
it('should return error message when there is an unkown git error', () => {
23-
const result = restoreUnstagedChangesSkipped({ errors: new Set([GitError]) })
23+
const result = restorePartialChangesSkipped({ errors: new Set([GitError]) })
2424
expect(typeof result === 'string').toEqual(true)
2525
})
2626
})

0 commit comments

Comments
 (0)