-
Notifications
You must be signed in to change notification settings - Fork 214
/
diff-to-changed-lines.awk
executable file
·147 lines (130 loc) · 3.38 KB
/
diff-to-changed-lines.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
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
#!/usr/bin/awk -f
# Copyright 2020 The Verible Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############################
######## DEPRECATED ########
############################
#
# This script has been replaced with the executable target:
# .../common/tools:verible-patch-tool
# and will no longer be installed.
#
# usage:
# verible-patch-tool changed-lines patchfile
#
# or pipe through:
#
# verible-patch-tool changed-lines -
#
############################
############################
# Converts a unified-diff into file names and their modified line ranges.
# This supports reading the output of 'git diff', but should handle most
# variants of diffs.
# Note, however, that git diffs shows files as being prefixed with "a/" and "b/",
# so the caller of this script will need to adjust filenames accordingly.
# The output style of line ranges is suitable for 'verilog_format --lines=...'
#
# usage: awk -f (this_script) < file.diff
#
# Changed lines are those that start with '+' in unified diffs.
# Ranges can be compact: N,P-Q
# Prints one file per line, e.g.:
# abc.sv 5,10-12,18,21-22
# xyz.sv 232-245
# New files (diffed vs. /dev/null) will appear without line ranges:
# new_file.sv
BEGIN {
expect_filename = 0;
active_range = 0;
seen_file = 0;
}
# Detect whether this entry is a new file.
/^---/ {
is_new_file = ($2 == "/dev/null");
}
# contains filename at $2
/^+++/ {
if (expect_filename) {
filename = $2;
if (seen_file) print "";
printf(filename " ");
expect_filename = 0;
seen_range = 0;
seen_file = 1;
}
}
# given:
# @@ -L,J +R,K
# R is the next starting line number in the next chunk
/^@@ / {
split($3, a, ",");
left_current_line = substr(a[1], 2) - 1;
right_current_line = left_current_line;
active_range = 0;
}
# seen in 'git diff'
# diff before-file after-file
/^diff/ {
auto_flush_range();
expect_filename = 1;
}
# seen in 'p4 diff'
# ==== //depot/...#N - /local/path/to/file ====
/^==== .* ====$/ {
auto_flush_range();
expect_filename = 1;
}
# + lines are after change only
/^+/ {
if (!expect_filename) {
++right_current_line;
if (!active_range) {
modified_from = right_current_line;
}
active_range = 1;
}
}
# print the most recent completed range
function flush_range() {
if (!is_new_file) {
if (seen_range) printf(",");
modified_to = right_current_line;
if (modified_from == modified_to) printf(modified_from);
else printf(modified_from "-" modified_to);
}
seen_range = 1;
active_range = 0;
}
function auto_flush_range() {
if (active_range) {
flush_range();
}
}
# <space> lines in unified diffs are common to both before/after
/^ / {
auto_flush_range();
++left_current_line;
++right_current_line;
}
# - lines are before change only
/^-/ {
auto_flush_range();
++left_current_line;
}
END {
# if diff ends with an active range, print it.
auto_flush_range();
print "";
}