-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpatch-selector.awk
69 lines (56 loc) · 956 Bytes
/
patch-selector.awk
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
#!/usr/bin/awk -f
# Array utilities
function push(A,B) {
idx = length(A);
A[idx] = B;
}
function new_array(A) {
split("", A);
}
# hunk management
function new_hunk() {
if (length(hunk) > 0) {
if (hunk_number == desired_hunk_number) {
for (preamble_index = 0; preamble_index < length(preamble); preamble_index ++) {
print preamble[preamble_index];
}
for (hunk_index = 0; hunk_index < length(hunk); hunk_index ++) {
print hunk[hunk_index];
}
}
hunk_number++;
}
new_array(hunk);
}
BEGIN {
hunk_number = 0;
}
# diff marker detection
/^diff --git/ {
new_hunk()
in_hunk = 0;
new_array(preamble);
push(preamble, $0);
}
/^---/ {
in_hunk = 0
push(preamble, $0);
}
/^\+\+\+/ {
in_hunk = 0
push(preamble, $0);
}
/^@@/ {
new_hunk()
in_hunk = 1;
}
# treatment for every line
{
if (in_hunk) {
push(hunk, $0);
}
}
# ensure we print last hunk
END {
new_hunk();
}