-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdiff_extract_changes.php
383 lines (333 loc) · 13.1 KB
/
diff_extract_changes.php
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Extract all modified lines from an unified diff file
*
* The script, given the path to one unified diff file will return
* all the changes into one format suitable to be used later by
* other tools.
*
* Basically, it returns both files in the diff plus the changed lines
* on each one. Such information will be used later for a lot of static
* code analyzers to determine if the changes are introducing new errors.
*
* @category ci
* @package local_ci
* @subpackage diff_extract_changes
* @copyright 2012 Eloy Lafuente (http://stronk7.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../phplib/clilib.php');
// now get cli options
list($options, $unrecognized) = cli_get_params(
array('help' => false, 'diff' => 'example.diff', 'output' => 'txt'),
array('h' => 'help', 'd' => 'diff', 'o' => 'output'));
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error("Unrecognised options:\n{$unrecognized}\n Please use --help option.");
}
if (empty($options['diff'])) {
cli_error('Missing diff file. Use the --diff option to specify one diff file.');
}
if (empty($options['output'])) {
cli_error('Missing output format. Use the --output option to specify one format (txt|xml).');
}
if (!file_exists($options['diff']) || !is_readable($options['diff'])) {
cli_error('Diff file not available or unreadable (' . $options['diff'] . ').');
}
if ($options['output'] !== 'txt' && $options['output'] !== 'xml') { // Only supported for now
cli_error('Unsupported output format (' . $options['output'] . ').');
}
if ($options['help']) {
$help =
"Extract all the changes performed by one unified diff file
Options:
-h, --help Print out this help
-d, --diff Unified diff file to process
-o, --output Output format (txt or xml)
Example:
\$sudo -u www-data /usr/bin/php local/ci/diff_extract_changes/diff_extract_changes.php --file=example.diff --output=txt
";
echo $help;
exit(0);
}
$dec = new diff_changes_extractor($options['diff'], $options['output']);
$dec->process();
/**
* Unified diff changes extractor
*
* Worker class that given one unified diff file and one output format
* will extract all the existing changes, annotating each file and lines
* modified. Suitable to select interesting information from any static
* code analyzer by intersecting results.
*
* @copyright 2012 Eloy Lafuente (http://stronk7.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class diff_changes_extractor {
/** version of the extractor */
const VERSION = '20240323';
/** @var string The unified diff file to process */
protected $file;
/** @var string The output format to utilize (txt, xml) */
protected $output;
/**
* Create one instance of the diff extractor
*
* @param string $file path to the unified diff file
* @param string $output format to output the information (txt|xml)
*
* @return diff_changes_extractor
*/
public function __construct($file, $output) {
// TODO: apply the checks and defaults already available in the CLI
$this->file = $file;
$this->output = $output;
}
/**
* Process one unified diff file, and output all the files/lines changed
*
* This function processes any unified diff file, outputing the changes
* in txt or xml format, suitable for integration with other tools
*/
public function process() {
// Init some vars
$cfile = ''; // Current file
$clineinfile = 0; // Current line in current file
$clineint = array(); // Current line interval being modfied
$inchunk = false; // To determine if we are in a chunk or no
$deletefile = false; // To detect delete operation and skip those files
$binaryfile = false; // To detect binary files and handle them specially
$afterminus = false; // To detect if we are after a minus (-) line
$isnewfile = false; // To detect if we are in a new file
// Skip always these lines
$skiplines = array('diff', 'inde');
// Let's read the diff file, line by line.
$fh = fopen($this->file, 'r');
if ($fh) {
// Start, output begin
$this->output_begin();
while (($line = fgets($fh, 4096)) !== false) {
// Get very 4 first chars, that's enough to analyze the diff
$lineheader = substr($line, 0, 4);
// We can safely ignore some lines always
if (in_array($lineheader, $skiplines)) {
continue;
}
// If it's one deleted file, we mark it to ignore
if ($lineheader === 'dele') {
$deletefile = true;
continue;
}
// If it's the start of a new file (--- ), we mark it for next line.
if ($lineheader === '--- ') {
// Only if the line has something looking like a path.
if (preg_match('~^--- a?(/)(.+)$~', $line, $match)) {
if (!empty($match[2]) && !empty($match[1]) && $match[1] === '/') {
$afterminus = true;
}
}
}
// If it's the start of a new file (+++ ), and we are after a minus (--- )
// we raise the new file flag.
if ($lineheader === '+++ ' && $afterminus) {
// Only if the line has something looking like a path.
if (preg_match('~^\+\+\+ b?(/)(.+)$~', $line, $match)) {
if (!empty($match[2]) && !empty($match[1]) && $match[1] === '/') {
$isnewfile = true;
}
}
}
// If it's one Binary file, we mark it
if ($lineheader === 'Bina') {
$binaryfile = true;
}
// Detect if we are changing of file
if ($isnewfile || $binaryfile) {
$clineinfile = 0;
$inchunk = false;
$afterminus = false;
$isnewfile = false;
// Output interval end
if (!empty($clineint)) {
$this->output_interval_end($clineint);
$clineint = array();
}
// Output file end
if ($cfile) {
$this->output_file_end($cfile);
$cfile = '';
}
// Skip new file and clean all flags if deleting
if ($deletefile) {
$deletefile = false;
$binaryfile = false;
continue;
}
// Calculate new file being processed.
if ($binaryfile) {
if (!preg_match('/^Binary files .* and (b\/)?(.*?)\s*differ$/', $line, $match)) {
print_error('Error: Something went wrong matching file. Line: ' . $line);
}
$binaryfile = false;
} else {
if (!preg_match('/^\+\+\+ (b\/)?(.*?)\s*$/', $line, $match)) {
print_error('Error: Something went wrong matching file. Line: ' . $line);
}
}
$cfile = $match[2];
// Output file begin
$this->output_file_begin($cfile);
continue;
}
// Detect if we are changing of chunk
if ($lineheader === '@@ -') {
// Output interval end
if (!empty($clineint)) {
$this->output_interval_end($clineint);
$clineint = array();
}
// Change variables for new chunk
if (!preg_match('/^@@ .*\+(\d*).*@@/', $line, $match)) {
print_error('Error: Something went wrong matching chunk. Line: ' . $line);
}
// If the line matched is < 0 (delete all), skip the chunk
if ($match[1] < 1) {
continue;
}
$clineinfile = $match[1] - 1; // Position to line before chunk begins
$clineint = array();
$inchunk = true;
continue;
}
// Skip any further processing if we are not $inchunk
if (!$inchunk) {
continue;
}
// Arrived here, we only need the 1st char
$linefirst = substr($lineheader, 0, 1);
// minus (-) found, deleted line, do nothing
if ($linefirst === '-') {
continue;
}
// space ( ) found, increment $clineinfile and finish interval
if ($linefirst === ' ') {
$clineinfile++;
// Output interval end
if (!empty($clineint)) {
$this->output_interval_end($clineint);
$clineint = array();
}
continue;
}
// plus (+) found, increment $clineinfile and start/continue interval
if ($linefirst === '+') {
$clineinfile++;
if (empty($clineint)) {
$clineint = array($clineinfile, $clineinfile);
// Output interval begin
$this->output_interval_begin($clineint);
} else {
$clineint[1] = $clineinfile;
}
continue;
}
}
if (!feof($fh)) {
print_error('Error: Something went wrong reading ' . $this->file);
}
fclose($fh);
// Output interval end
if (!empty($clineint)) {
$this->output_interval_end($clineint);
$clineint = array();
}
// output file end
if ($cfile) {
$this->output_file_end($cfile);
$cfile = '';
}
// Finished, output end
$this->output_end();
}
}
// Helper functions used to output information in the desired output
/**
* Output begin of the changes
*/
private function output_begin() {
if ($this->output == 'xml') {
echo '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
echo '<diffchanges version="' . self::VERSION . '">' . PHP_EOL;
}
}
/**
* Output end of the changes
*/
private function output_end() {
if ($this->output == 'xml') {
echo '</diffchanges>' . PHP_EOL;
}
}
/**
* Output begin of file changes
*
* @param string $file path of the file we are going to show changes
*/
private function output_file_begin($file) {
if ($this->output == 'xml') {
echo ' <file name="' . $file . '">' . PHP_EOL;
} else {
echo $file . ':';
}
}
/**
* Output bend of file changes
*
* @param string $file path of the file we are going to show changes
*/
private function output_file_end($file) {
if ($this->output == 'xml') {
echo ' </file>' . PHP_EOL;
} else {
echo PHP_EOL;
}
}
/**
* Output begin of interval of line changes
*
* @param array $interval of lines changed
*/
private function output_interval_begin($interval) {
if ($this->output == 'xml') {
echo ' <lines from="' . $interval[0] . '" ';
} else {
echo $interval[0] . '-';
}
}
/**
* Output end of interval of line changes
*
* @param array $interval of lines changed
*/
private function output_interval_end($interval) {
if ($this->output == 'xml') {
echo 'to="' . $interval[1] . '"/>' . PHP_EOL;
} else {
echo $interval[1] . ';';
}
}
}