-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathupdate-patch.ts
More file actions
59 lines (51 loc) · 1.67 KB
/
update-patch.ts
File metadata and controls
59 lines (51 loc) · 1.67 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
const fs = require('fs')
const path = require('path')
export function updatePatch(patchContent: string): string {
// Remove the trailing newline if it exists
let hadTrailingNewline = false
if (patchContent.endsWith('\n')) {
patchContent = patchContent.slice(0, -1)
hadTrailingNewline = true
}
const lines = patchContent.split('\n')
const updatedLines: string[] = []
let inHunk = false
let hunkLines: string[] = []
for (const line of lines) {
if (line.startsWith('@@')) {
if (inHunk) {
updatedLines.push(...hunkLines.slice(0, -3))
}
inHunk = true
hunkLines = [line]
} else if (inHunk) {
hunkLines.push(line)
} else {
updatedLines.push(line)
}
}
if (inHunk) {
const keepUntilIndex =
hunkLines.findLastIndex(
(line) => line.startsWith('+') || line.startsWith('-')
) + 1
updatedLines.push(...hunkLines.slice(0, keepUntilIndex))
}
// Add the newline back at the end
return updatedLines.join('\n') + (hadTrailingNewline ? '\n' : '')
}
function processDatasetPatches() {
const datasetDir = path.join(__dirname, '..', 'data', 'processed_dataset')
const dirs = fs.readdirSync(datasetDir)
for (const dir of dirs) {
const patchFile = path.join(datasetDir, dir, 'patch')
if (fs.existsSync(patchFile)) {
const patchContent = fs.readFileSync(patchFile, 'utf-8')
const updatedPatchContent = updatePatch(patchContent)
const updatedPatchFile = path.join(datasetDir, dir, 'updated_patch')
fs.writeFileSync(updatedPatchFile, updatedPatchContent)
console.log(`Updated patch file created: ${updatedPatchFile}`)
}
}
}
// processDatasetPatches()