-
Notifications
You must be signed in to change notification settings - Fork 16
/
ptch.js
executable file
·106 lines (96 loc) · 3.78 KB
/
ptch.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
const fs = require('fs');
const readline = require('readline');
const changes = [];
let currentChange = null;
function applyChanges() {
const fileChanges = {};
const lineShifts = {};
// Sort changes by file and then by ascending start line
changes.sort((a, b) => {
if (a.file !== b.file) return a.file.localeCompare(b.file);
return a.start - b.start;
});
for (const change of changes) {
if (!fileChanges[change.file]) {
fileChanges[change.file] = fs.readFileSync(change.file, 'utf8').split('\n');
lineShifts[change.file] = 0;
}
const lines = fileChanges[change.file];
const shift = lineShifts[change.file];
if (change.type === 'remove') {
const adjustedStart = change.start + shift;
const adjustedEnd = change.end + shift;
if (isNaN(adjustedStart) || isNaN(adjustedEnd)) {
console.error(`Invalid adjusted line range: start=${adjustedStart}, end=${adjustedEnd}`);
continue;
}
console.log(`Removing lines ${adjustedStart}-${adjustedEnd} from ${change.file}:`);
console.log(lines.slice(adjustedStart - 1, adjustedEnd).join('\n'));
lines.splice(adjustedStart - 1, adjustedEnd - adjustedStart + 1);
lineShifts[change.file] -= (adjustedEnd - adjustedStart + 1);
} else if (change.type === 'add') {
const adjustedStart = change.start + shift;
if (isNaN(adjustedStart)) {
console.error(`Invalid adjusted start line: ${adjustedStart}`);
continue;
}
console.log(`Adding at line ${adjustedStart} in ${change.file}:`);
const contextStart = Math.max(0, adjustedStart - 4);
console.log('Context:');
console.log(lines.slice(contextStart, adjustedStart - 1).join('\n'));
console.log('New content:');
console.log(change.content.join('\n'));
lines.splice(adjustedStart - 1, 0, ...change.content);
lineShifts[change.file] += change.content.length;
}
}
for (const [file, lines] of Object.entries(fileChanges)) {
fs.writeFileSync(file, lines.join('\n'));
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
if (line.startsWith('--- ')) {
if (currentChange) {
changes.push(currentChange);
}
const parts = line.substring(4).split(':');
const file = parts[0];
if (parts[1].includes(' ')) {
const [start, end] = parts[1].split(' ').map(Number);
if (isNaN(start) || isNaN(end)) {
console.error(`Invalid line range: ${parts[1]} in file ${file}`);
currentChange = null;
return;
}
currentChange = { file, type: 'remove', start, end };
console.log(`Preparing to remove lines ${start}-${end} from ${file}`);
} else {
const start = Number(parts[1]);
if (isNaN(start)) {
console.error(`Invalid line number: ${parts[1]} in file ${file}`);
currentChange = null;
return;
}
currentChange = { file, type: 'add', start, content: [] };
console.log(`Preparing to add content at line ${parts[1]} in ${file}`);
}
} else if (currentChange && currentChange.type === 'add') {
currentChange.content.push(line);
}
});
rl.on('close', () => {
if (currentChange) {
changes.push(currentChange);
}
try {
applyChanges();
console.log('Patch applied successfully.');
} catch (error) {
console.error('Error applying patch:', error);
}
});