/
gitWorkflow.js
108 lines (92 loc) Β· 2.96 KB
/
gitWorkflow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const debug = require('debug')('lint-staged:git')
const execGit = require('./execGit')
const STASH = 'lint-staged automatic backup'
let unstagedDiff
/**
* Get name of backup stash
*
* @param {Object} [options]
* @returns {Promise<Object>}
*/
async function getBackupStash(options) {
const stashList = (await execGit(['stash', 'list'], options)).split('\n')
const index = stashList.findIndex(line => line.includes(STASH))
return `stash@{${index}}`
}
/**
* Create backup stashes, one of everything and one of only staged changes
* Leves stages files in index for running tasks
*
* @param {Object} [options]
* @returns {Promise<void>}
*/
async function stashBackup(options) {
debug('Backing up original state...')
// Get stash of entire original state, including unstaged changes
// Keep index so that tasks only work on those files
await execGit(['stash', 'save', '--quiet', '--include-untracked', '--keep-index', STASH], options)
// Since only staged files are now present, get a diff of unstaged changes
// by comparing current index against original stash, but in reverse
const original = await getBackupStash(options)
unstagedDiff = await execGit(
['diff', '--unified=0', '--no-color', '--no-ext-diff', '-p', original, '-R'],
options
)
debug('Done backing up original state!')
}
const gitApplyArgs = ['apply', '-v', '--whitespace=nowarn', '--recount', '--unidiff-zero']
/**
* Resets everything and applies back unstaged and staged changes,
* possibly with modifications by tasks
*
* @param {Object} [options]
* @returns {Promise<void>}
*/
async function restoreUnstagedChanges(options) {
debug('Restoring unstaged changes...')
if (unstagedDiff) {
try {
await execGit(gitApplyArgs, { ...options, input: `${unstagedDiff}\n` })
} catch (error) {
debug('Error when restoring changes:')
debug(error)
debug('Retrying with 3-way merge')
// Retry with `--3way` if normal apply fails
await execGit([...gitApplyArgs, '--3way'], { ...options, input: `${unstagedDiff}\n` })
}
}
debug('Done restoring unstaged changes!')
}
/**
* Restore original HEAD state in case of errors
*
* @param {Object} [options]
* @returns {Promise<void>}
*/
async function restoreOriginalState(options) {
debug('Restoring original state...')
const original = await getBackupStash(options)
await execGit(['reset', '--hard', 'HEAD'], options)
await execGit(['stash', 'apply', '--quiet', '--index', original], options)
debug('Done restoring original state!')
}
/**
* Drop the created stashes after everything has run
*
* @param {Object} [options]
* @returns {Promise<void>}
*/
async function dropBackup(options) {
debug('Dropping backup stash...')
const original = await getBackupStash(options)
await execGit(['stash', 'drop', '--quiet', original], options)
debug('Done dropping backup stash!')
}
module.exports = {
execGit,
stashBackup,
restoreUnstagedChanges,
restoreOriginalState,
dropBackup
}