Skip to content

Commit

Permalink
perf: further optimize by reusing previous job
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Oct 2, 2021
1 parent ff0cc0d commit 3066a35
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 45 deletions.
23 changes: 1 addition & 22 deletions lib/gitWorkflow.js
Expand Up @@ -7,7 +7,6 @@ const execGit = require('./execGit')
const { readFile, unlink, writeFile } = require('./file')
const {
GitError,
RestoreOriginalStateError,
ApplyEmptyCommitError,
HideUnstagedChangesError,
RestoreMergeStatusError,
Expand Down Expand Up @@ -209,26 +208,6 @@ class GitWorkflow {
}
}

/**
* Restore original HEAD state for partially staged files in case of errors
*/
async restorePartiallyStagedFiles(ctx) {
try {
debug('Restoring original state...')

// Flush all unstaged changes. Since they were diffed before
// running tasks, these will include only changes generated by tasks
await this.hideUnstagedChanges(ctx)

const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
await this.execGit(['apply', ...GIT_APPLY_ARGS, unstagedPatch])

debug('Done restoring original state!')
} catch (error) {
handleError(error, ctx, RestoreOriginalStateError)
}
}

/** Add all task modifications to index for files that were staged before running. */
async applyModifications(ctx) {
debug('Adding task modifications to index...')
Expand Down Expand Up @@ -256,7 +235,7 @@ class GitWorkflow {
* this is probably because of conflicts between new task modifications.
* 3-way merge usually fixes this, and in case it doesn't we should just give up and throw.
*/
async restoreUnstagedChanges(ctx) {
async restorePartialChanges(ctx) {
debug('Restoring unstaged changes...')
const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
try {
Expand Down
12 changes: 6 additions & 6 deletions lib/runAll.js
Expand Up @@ -30,7 +30,7 @@ const {
hasPartiallyStagedFiles,
restoreOriginalStateEnabled,
restoreOriginalStateSkipped,
restoreUnstagedChangesSkipped,
restorePartialChangesSkipped,
} = require('./state')
const { GitRepoError, GetStagedFilesError, GitError } = require('./symbols')

Expand Down Expand Up @@ -220,8 +220,8 @@ const runAll = async (
},
...listrTasks,
{
title: 'Reverting to original state because of errors...',
task: (ctx) => git.restorePartiallyStagedFiles(ctx),
title: 'Reverting because of errors...',
task: (ctx) => git.hideUnstagedChanges(ctx),
enabled: restoreOriginalStateEnabled,
skip: restoreOriginalStateSkipped,
},
Expand All @@ -231,10 +231,10 @@ const runAll = async (
skip: applyModificationsSkipped,
},
{
title: 'Restoring unstaged changes to partially staged files...',
task: (ctx) => git.restoreUnstagedChanges(ctx),
title: 'Restoring partial changes...',
task: (ctx) => git.restorePartialChanges(ctx),
enabled: hasPartiallyStagedFiles,
skip: restoreUnstagedChangesSkipped,
skip: restorePartialChangesSkipped,
},
{
title: 'Cleaning up...',
Expand Down
13 changes: 2 additions & 11 deletions lib/state.js
Expand Up @@ -4,7 +4,6 @@ const { GIT_ERROR, TASK_ERROR } = require('./messages')
const {
ApplyEmptyCommitError,
TaskError,
RestoreOriginalStateError,
GitError,
RestoreUnstagedChangesError,
} = require('./symbols')
Expand Down Expand Up @@ -32,15 +31,11 @@ const applyModificationsSkipped = (ctx) => {
}
}

const restoreUnstagedChangesSkipped = (ctx) => {
const restorePartialChangesSkipped = (ctx) => {
// Should be skipped in case of git errors
if (ctx.errors.has(GitError)) {
return GIT_ERROR
}
// Should be skipped when tasks fail
if (ctx.errors.has(TaskError)) {
return TASK_ERROR
}
}

const restoreOriginalStateEnabled = (ctx) => ctx.shouldBackup && ctx.errors.has(TaskError)
Expand All @@ -67,17 +62,13 @@ const cleanupSkipped = (ctx) => {
) {
return GIT_ERROR
}
// Should be skipped when reverting to original state fails
if (ctx.errors.has(RestoreOriginalStateError)) {
return GIT_ERROR
}
}

module.exports = {
getInitialState,
hasPartiallyStagedFiles,
applyModificationsSkipped,
restoreUnstagedChangesSkipped,
restorePartialChangesSkipped,
restoreOriginalStateEnabled,
restoreOriginalStateSkipped,
cleanupEnabled,
Expand Down
2 changes: 0 additions & 2 deletions lib/symbols.js
Expand Up @@ -8,7 +8,6 @@ const GitRepoError = Symbol('GitRepoError')
const HideUnstagedChangesError = Symbol('HideUnstagedChangesError')
const InvalidOptionsError = new Error('Invalid Options')
const RestoreMergeStatusError = Symbol('RestoreMergeStatusError')
const RestoreOriginalStateError = Symbol('RestoreOriginalStateError')
const RestoreUnstagedChangesError = Symbol('RestoreUnstagedChangesError')
const TaskError = Symbol('TaskError')

Expand All @@ -21,7 +20,6 @@ module.exports = {
InvalidOptionsError,
HideUnstagedChangesError,
RestoreMergeStatusError,
RestoreOriginalStateError,
RestoreUnstagedChangesError,
TaskError,
}
2 changes: 1 addition & 1 deletion test/__mocks__/gitWorkflow.js
Expand Up @@ -2,7 +2,7 @@ const stub = {
prepare: jest.fn().mockImplementation(() => Promise.resolve()),
hideUnstagedChanges: jest.fn().mockImplementation(() => Promise.resolve()),
applyModifications: jest.fn().mockImplementation(() => Promise.resolve()),
restoreUnstagedChanges: jest.fn().mockImplementation(() => Promise.resolve()),
restorePartialChanges: jest.fn().mockImplementation(() => Promise.resolve()),
restoreOriginalState: jest.fn().mockImplementation(() => Promise.resolve()),
cleanup: jest.fn().mockImplementation(() => Promise.resolve()),
}
Expand Down
6 changes: 3 additions & 3 deletions test/state.spec.js
Expand Up @@ -2,7 +2,7 @@ import {
applyModificationsSkipped,
cleanupSkipped,
restoreOriginalStateSkipped,
restoreUnstagedChangesSkipped,
restorePartialChangesSkipped,
} from '../lib/state'
import { GitError, RestoreOriginalStateError } from '../lib/symbols'

Expand All @@ -18,9 +18,9 @@ describe('applyModificationsSkipped', () => {
})
})

describe('restoreUnstagedChangesSkipped', () => {
describe('restorePartialChangesSkipped', () => {
it('should return error message when there is an unkown git error', () => {
const result = restoreUnstagedChangesSkipped({ errors: new Set([GitError]) })
const result = restorePartialChangesSkipped({ errors: new Set([GitError]) })
expect(typeof result === 'string').toEqual(true)
})
})
Expand Down

0 comments on commit 3066a35

Please sign in to comment.