1 | 'use strict'
|
2 |
|
3 | const del = require('del')
|
4 | const debug = require('debug')('lint-staged:git')
|
5 |
|
6 | const execGit = require('./execGit')
|
7 |
|
8 | let workingCopyTree = null
|
9 | let indexTree = null
|
10 | let formattedIndexTree = null
|
11 |
|
12 | async function writeTree(options) {
|
13 | return execGit(['write-tree'], options)
|
14 | }
|
15 |
|
16 | async function getDiffForTrees(tree1, tree2, options) {
|
17 | debug(`Generating diff between trees ${tree1} and ${tree2}...`)
|
18 | return execGit(
|
19 | [
|
20 | 'diff-tree',
|
21 | '--ignore-submodules',
|
22 | '--binary',
|
23 | '--no-color',
|
24 | '--no-ext-diff',
|
25 | '--unified=0',
|
26 | tree1,
|
27 | tree2
|
28 | ],
|
29 | options
|
30 | )
|
31 | }
|
32 |
|
33 | async function hasPartiallyStagedFiles(options) {
|
34 | const stdout = await execGit(['status', '--porcelain'], options)
|
35 | if (!stdout) return false
|
36 |
|
37 | const changedFiles = stdout.split('\n')
|
38 | const partiallyStaged = changedFiles.filter(line => {
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 | const [index, workingTree] = line
|
45 | return index !== ' ' && workingTree !== ' ' && index !== '?' && workingTree !== '?'
|
46 | })
|
47 |
|
48 | return partiallyStaged.length > 0
|
49 | }
|
50 |
|
51 |
|
52 | async function gitStashSave(options) {
|
53 | debug('Stashing files...')
|
54 |
|
55 | indexTree = await writeTree(options)
|
56 |
|
57 | await execGit(['add', '.'], options)
|
58 |
|
59 | workingCopyTree = await writeTree(options)
|
60 |
|
61 | await execGit(['read-tree', indexTree], options)
|
62 |
|
63 | await execGit(['checkout-index', '-af'], options)
|
64 |
|
65 | debug('Done stashing files!')
|
66 | return [workingCopyTree, indexTree]
|
67 | }
|
68 |
|
69 | async function updateStash(options) {
|
70 | formattedIndexTree = await writeTree(options)
|
71 | return formattedIndexTree
|
72 | }
|
73 |
|
74 | async function applyPatchFor(tree1, tree2, options) {
|
75 | const diff = await getDiffForTrees(tree1, tree2, options)
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | const patch = `${diff}\n`
|
84 | if (patch) {
|
85 | try {
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | await execGit(
|
92 | ['apply', '-v', '--whitespace=nowarn', '--reject', '--recount', '--unidiff-zero'],
|
93 | {
|
94 | ...options,
|
95 | input: patch
|
96 | }
|
97 | )
|
98 | } catch (err) {
|
99 | debug('Could not apply patch to the stashed files cleanly')
|
100 | debug(err)
|
101 | debug('Patch content:')
|
102 | debug(patch)
|
103 | throw new Error('Could not apply patch to the stashed files cleanly.', err)
|
104 | }
|
105 | }
|
106 | }
|
107 |
|
108 | async function gitStashPop(options) {
|
109 | if (workingCopyTree === null) {
|
110 | throw new Error('Trying to restore from stash but could not find working copy stash.')
|
111 | }
|
112 |
|
113 | debug('Restoring working copy')
|
114 |
|
115 | await execGit(['read-tree', workingCopyTree], options)
|
116 |
|
117 | await execGit(['checkout-index', '-af'], options)
|
118 |
|
119 |
|
120 | if (indexTree !== null && formattedIndexTree === null) {
|
121 |
|
122 | debug('Restoring index')
|
123 | await execGit(['read-tree', indexTree], options)
|
124 | } else {
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | debug('Restoring index with formatting changes')
|
132 | await execGit(['read-tree', formattedIndexTree], options)
|
133 | try {
|
134 | await applyPatchFor(indexTree, formattedIndexTree, options)
|
135 | } catch (err) {
|
136 | debug(
|
137 | 'Found conflicts between formatters and local changes. Formatters changes will be ignored for conflicted hunks.'
|
138 | )
|
139 | |
140 |
|
141 |
|
142 |
|
143 | try {
|
144 | const rejFiles = await del(['*.rej'], options)
|
145 | debug('Deleted files and folders:\n', rejFiles.join('\n'))
|
146 | } catch (delErr) {
|
147 | debug('Error deleting *.rej files', delErr)
|
148 | }
|
149 | }
|
150 | }
|
151 |
|
152 | workingCopyTree = null
|
153 | indexTree = null
|
154 | formattedIndexTree = null
|
155 |
|
156 | return null
|
157 | }
|
158 |
|
159 | module.exports = {
|
160 | execGit,
|
161 | gitStashSave,
|
162 | gitStashPop,
|
163 | hasPartiallyStagedFiles,
|
164 | updateStash
|
165 | }
|