Repository navigation
Expand file tree
/
Copy pathfile-patch.js
More file actions
247 lines (219 loc) · 6.55 KB
/
Copy pathfile-patch.js
File metadata and controls
247 lines (219 loc) · 6.55 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/** @babel */
import Hunk from './hunk'
import {Emitter} from 'atom'
import {Diff} from 'diff'
export default class FilePatch {
constructor (oldPath, newPath, oldMode, newMode, status, hunks) {
this.oldPath = oldPath
this.newPath = newPath
this.oldMode = oldMode
this.newMode = newMode
this.status = status
this.hunks = hunks
Object.defineProperty(this, 'destroyed', {value: false, enumerable: false, writable: true})
Object.defineProperty(this, 'emitter', {value: new Emitter(), enumerable: false})
}
copy () {
return new FilePatch(
this.getOldPath(),
this.getNewPath(),
this.getOldMode(),
this.getNewMode(),
this.getStatus(),
this.getHunks().map(h => h.copy())
)
}
destroy () {
this.destroyed = true
this.emitter.emit('did-destroy')
this.emitter.dispose()
}
update (filePatch) {
if (this.getId() !== filePatch.getId()) {
throw new Error(`Cannot update file patch! This FilePatch Id: ${this.getId()} Other FilePatch Id: ${filePatch.getId()}`)
}
const newHunks = filePatch.getHunks()
let oldHunkIndex = 0
let newHunkIndex = 0
for (let diff of diffHunks(this.hunks, newHunks)) {
if (diff.removed) {
this.hunks.splice(oldHunkIndex, diff.count)
} else if (diff.added) {
this.hunks.splice(oldHunkIndex, 0, ...newHunks.slice(newHunkIndex, newHunkIndex + diff.count))
newHunkIndex += diff.count
oldHunkIndex += diff.count
} else {
for (let i = 0; i < diff.count; i++) {
this.hunks[oldHunkIndex].update(newHunks[newHunkIndex])
newHunkIndex++
oldHunkIndex++
}
}
}
this.oldMode = filePatch.getOldMode()
this.newMode = filePatch.getNewMode()
this.emitter.emit('did-update')
}
onDidDestroy (callback) {
return this.emitter.on('did-destroy', callback)
}
onDidUpdate (callback) {
return this.emitter.on('did-update', callback)
}
isDestroyed () {
return this.destroyed
}
getId () {
return `a/${this.getOldPath()} b/${this.getNewPath()}`
}
getOldPath () {
return this.oldPath
}
getNewPath () {
return this.newPath
}
getDescriptionPath () {
if (this.status === 'renamed') {
return `${this.getOldPath()} → ${this.getNewPath()}`
} else if (this.status === 'added') {
return this.getNewPath()
} else if (this.status === 'removed' || this.status === 'modified') {
return this.getOldPath()
}
}
getOldMode () {
return this.oldMode
}
getNewMode () {
return this.newMode
}
getStatus () {
return this.status
}
getHunks () {
return this.hunks
}
getStagePatchForHunk (selectedHunk) {
return this.getStagePatchForLines(new Set(selectedHunk.getLines()))
}
getStagePatchForLines (selectedLines) {
let delta = 0
const hunks = []
for (let hunk of this.getHunks()) {
const newStartRow = hunk.getNewStartRow() + delta
let newLineNumber = newStartRow
const lines = []
for (let line of hunk.getLines()) {
if (selectedLines.has(line)) {
if (line.getStatus() === 'removed') {
lines.push(line.copy())
} else {
lines.push(line.copy({newLineNumber: newLineNumber++}))
}
} else if (line.getStatus() === 'removed') {
lines.push(line.copy({newLineNumber: newLineNumber++, status: 'unchanged'}))
} else if (line.getStatus() === 'unchanged') {
lines.push(line.copy({newLineNumber: newLineNumber++}))
}
}
const newRowCount = newLineNumber - newStartRow
hunks.push(new Hunk(hunk.getOldStartRow(), newStartRow, hunk.getOldRowCount(), newRowCount, lines))
delta += newRowCount - hunk.getNewRowCount()
}
return new FilePatch(
this.getOldPath(),
this.getNewPath(),
this.getOldMode(),
this.getNewMode(),
this.getStatus(),
hunks
)
}
getUnstagePatch () {
let invertedStatus
switch (this.getStatus()) {
case 'modified':
invertedStatus = 'modified'
break
case 'added':
invertedStatus = 'removed'
break
case 'removed':
invertedStatus = 'added'
break
case 'renamed':
invertedStatus = 'renamed'
break
default:
throw new Error(`Unknown Status: ${this.getStatus()}`)
}
const invertedHunks = this.getHunks().map(h => h.invert())
return new FilePatch(
this.getNewPath(),
this.getOldPath(),
this.getNewMode(),
this.getOldMode(),
invertedStatus,
invertedHunks
)
}
getUnstagePatchForHunk (hunk) {
return this.getUnstagePatchForLines(new Set(hunk.getLines()))
}
getUnstagePatchForLines (selectedLines) {
let delta = 0
const hunks = []
for (let hunk of this.getHunks()) {
const oldStartRow = hunk.getOldStartRow() + delta
let oldLineNumber = oldStartRow
const lines = []
for (let line of hunk.getLines()) {
if (selectedLines.has(line)) {
if (line.getStatus() === 'added') {
lines.push(line.copy())
} else {
lines.push(line.copy({oldLineNumber: oldLineNumber++}))
}
} else if (line.getStatus() === 'added') {
lines.push(line.copy({oldLineNumber: oldLineNumber++, status: 'unchanged'}))
} else if (line.getStatus() === 'unchanged') {
lines.push(line.copy({oldLineNumber: oldLineNumber++}))
}
}
const oldRowCount = oldLineNumber - oldStartRow
hunks.push(new Hunk(oldStartRow, hunk.getNewStartRow(), oldRowCount, hunk.getNewRowCount(), lines))
delta += oldRowCount - hunk.getOldRowCount()
}
return new FilePatch(
this.getOldPath(),
this.getNewPath(),
this.getOldMode(),
this.getNewMode(),
this.getStatus(),
hunks
).getUnstagePatch()
}
toString () {
return this.getHunks().map(h => h.toString()).join('')
}
}
function diffHunks (oldHunks, newHunks) {
let idCounter = 0
const hunksById = new Map()
const hunkDiff = new Diff()
hunkDiff.equals = (l, r) => {
const left = hunksById.get(l.trim())
const right = hunksById.get(r.trim())
return (left != null && right != null) ? left.sharesLinesWith(right) : false
}
hunkDiff.tokenize = (hunks) => hunks.map(h => {
const id = (idCounter++).toString()
hunksById.set(id, h)
return id + '\n'
})
const results = hunkDiff.diff(oldHunks, newHunks)
for (let diff of results) {
diff.value = diff.value.trim().split('\n').map(h => hunksById.get(h))
}
return results
}