]>
Commit | Line | Data |
---|---|---|
d2656095 MCC |
1 | #!/usr/bin/env perl |
2 | # SPDX-License-Identifier: GPL-2.0 | |
3 | # | |
e8939222 JN |
4 | # Treewide grep for references to files under Documentation, and report |
5 | # non-existing files in stderr. | |
6 | ||
d2656095 MCC |
7 | use warnings; |
8 | use strict; | |
9 | use Getopt::Long qw(:config no_auto_abbrev); | |
10 | ||
aeaacbfe MCC |
11 | # NOTE: only add things here when the file was gone, but the text wants |
12 | # to mention a past documentation file, for example, to give credits for | |
13 | # the original work. | |
14 | my %false_positives = ( | |
15 | "Documentation/scsi/scsi_mid_low_api.txt" => "Documentation/Configure.help", | |
16 | "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c", | |
17 | ); | |
18 | ||
d2656095 MCC |
19 | my $scriptname = $0; |
20 | $scriptname =~ s,.*/([^/]+/),$1,; | |
21 | ||
22 | # Parse arguments | |
23 | my $help = 0; | |
24 | my $fix = 0; | |
b1663d7e MCC |
25 | my $warn = 0; |
26 | ||
27 | if (! -d ".git") { | |
28 | printf "Warning: can't check if file exists, as this is not a git tree"; | |
29 | exit 0; | |
30 | } | |
d2656095 MCC |
31 | |
32 | GetOptions( | |
33 | 'fix' => \$fix, | |
b1663d7e | 34 | 'warn' => \$warn, |
d2656095 MCC |
35 | 'h|help|usage' => \$help, |
36 | ); | |
37 | ||
38 | if ($help != 0) { | |
40fc3eb0 | 39 | print "$scriptname [--help] [--fix]\n"; |
d2656095 MCC |
40 | exit -1; |
41 | } | |
42 | ||
43 | # Step 1: find broken references | |
44 | print "Finding broken references. This may take a while... " if ($fix); | |
45 | ||
46 | my %broken_ref; | |
47 | ||
894ee5ff MCC |
48 | my $doc_fix = 0; |
49 | ||
50 | open IN, "git grep ':doc:\`' Documentation/|" | |
51 | or die "Failed to run git grep"; | |
52 | while (<IN>) { | |
53 | next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,); | |
54 | ||
55 | my $d = $1; | |
56 | my $doc_ref = $2; | |
57 | ||
58 | my $f = $doc_ref; | |
59 | ||
60 | $d =~ s,(.*/).*,$1,; | |
61 | $f =~ s,.*\<([^\>]+)\>,$1,; | |
62 | ||
63 | $f ="$d$f.rst"; | |
64 | ||
65 | next if (grep -e, glob("$f")); | |
66 | ||
67 | if ($fix && !$doc_fix) { | |
68 | print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n"; | |
69 | } | |
70 | $doc_fix++; | |
71 | ||
72 | print STDERR "$f: :doc:`$doc_ref`\n"; | |
73 | } | |
74 | close IN; | |
75 | ||
d2656095 MCC |
76 | open IN, "git grep 'Documentation/'|" |
77 | or die "Failed to run git grep"; | |
78 | while (<IN>) { | |
79 | next if (!m/^([^:]+):(.*)/); | |
80 | ||
81 | my $f = $1; | |
82 | my $ln = $2; | |
83 | ||
fe3e4b9c MCC |
84 | # On linux-next, discard the Next/ directory |
85 | next if ($f =~ m,^Next/,); | |
86 | ||
2d69708f MCC |
87 | # Makefiles and scripts contain nasty expressions to parse docs |
88 | next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/); | |
89 | ||
d2656095 MCC |
90 | # Skip this script |
91 | next if ($f eq $scriptname); | |
92 | ||
407b584d MCC |
93 | # Ignore the dir where documentation will be built |
94 | next if ($ln =~ m,\b(\S*)Documentation/output,); | |
95 | ||
2d69708f | 96 | if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) { |
d2656095 MCC |
97 | my $prefix = $1; |
98 | my $ref = $2; | |
99 | my $base = $2; | |
2d69708f MCC |
100 | my $extra = $3; |
101 | ||
102 | # some file references are like: | |
103 | # /usr/src/linux/Documentation/DMA-{API,mapping}.txt | |
104 | # For now, ignore them | |
105 | next if ($extra =~ m/^{/); | |
106 | ||
107 | # Remove footnotes at the end like: | |
108 | # Documentation/devicetree/dt-object-internal.txt[1] | |
109 | $ref =~ s/(txt|rst)\[\d+]$/$1/; | |
110 | ||
111 | # Remove ending ']' without any '[' | |
112 | $ref =~ s/\].*// if (!($ref =~ m/\[/)); | |
d2656095 | 113 | |
2d69708f | 114 | # Remove puntuation marks at the end |
d2656095 MCC |
115 | $ref =~ s/[\,\.]+$//; |
116 | ||
117 | my $fulref = "$prefix$ref"; | |
118 | ||
119 | $fulref =~ s/^(\<file|ref)://; | |
120 | $fulref =~ s/^[\'\`]+//; | |
121 | $fulref =~ s,^\$\(.*\)/,,; | |
122 | $base =~ s,.*/,,; | |
123 | ||
124 | # Remove URL false-positives | |
125 | next if ($fulref =~ m/^http/); | |
126 | ||
5d395fa6 MCC |
127 | # Remove sched-pelt false-positive |
128 | next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,); | |
129 | ||
4ca9bc22 | 130 | # Discard some build examples from Documentation/target/tcm_mod_builder.rst |
d25c0634 MCC |
131 | next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,); |
132 | ||
d2656095 MCC |
133 | # Check if exists, evaluating wildcards |
134 | next if (grep -e, glob("$ref $fulref")); | |
135 | ||
a78513c6 MCC |
136 | # Accept relative Documentation patches for tools/ |
137 | if ($f =~ m/tools/) { | |
138 | my $path = $f; | |
139 | $path =~ s,(.*)/.*,$1,; | |
4904aeed | 140 | next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref")); |
a78513c6 MCC |
141 | } |
142 | ||
aeaacbfe MCC |
143 | # Discard known false-positives |
144 | if (defined($false_positives{$f})) { | |
145 | next if ($false_positives{$f} eq $fulref); | |
146 | } | |
147 | ||
d2656095 | 148 | if ($fix) { |
be600e5a | 149 | if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) { |
d2656095 MCC |
150 | $broken_ref{$ref}++; |
151 | } | |
b1663d7e MCC |
152 | } elsif ($warn) { |
153 | print STDERR "Warning: $f references a file that doesn't exist: $fulref\n"; | |
d2656095 MCC |
154 | } else { |
155 | print STDERR "$f: $fulref\n"; | |
156 | } | |
157 | } | |
158 | } | |
894ee5ff | 159 | close IN; |
d2656095 MCC |
160 | |
161 | exit 0 if (!$fix); | |
162 | ||
163 | # Step 2: Seek for file name alternatives | |
164 | print "Auto-fixing broken references. Please double-check the results\n"; | |
165 | ||
166 | foreach my $ref (keys %broken_ref) { | |
167 | my $new =$ref; | |
168 | ||
9e78e7fc MCC |
169 | my $basedir = "."; |
170 | # On translations, only seek inside the translations directory | |
171 | $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),); | |
172 | ||
d2656095 MCC |
173 | # get just the basename |
174 | $new =~ s,.*/,,; | |
175 | ||
d2656095 MCC |
176 | my $f=""; |
177 | ||
be600e5a MCC |
178 | # usual reason for breakage: DT file moved around |
179 | if ($ref =~ /devicetree/) { | |
0ca862e6 | 180 | # usual reason for breakage: DT file renamed to .yaml |
be600e5a | 181 | if (!$f) { |
0ca862e6 MCC |
182 | my $new_ref = $ref; |
183 | $new_ref =~ s/\.txt$/.yaml/; | |
184 | $f=$new_ref if (-f $new_ref); | |
185 | } | |
186 | ||
187 | if (!$f) { | |
188 | my $search = $new; | |
189 | $search =~ s,^.*/,,; | |
be600e5a | 190 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
0ca862e6 MCC |
191 | if (!$f) { |
192 | # Manufacturer name may have changed | |
193 | $search =~ s/^.*,//; | |
194 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); | |
195 | } | |
be600e5a MCC |
196 | } |
197 | } | |
d2656095 MCC |
198 | |
199 | # usual reason for breakage: file renamed to .rst | |
200 | if (!$f) { | |
201 | $new =~ s/\.txt$/.rst/; | |
9e78e7fc | 202 | $f=qx(find $basedir -iname $new) if ($new); |
d2656095 MCC |
203 | } |
204 | ||
e1f319fe MCC |
205 | # usual reason for breakage: use dash or underline |
206 | if (!$f) { | |
207 | $new =~ s/[-_]/[-_]/g; | |
9e78e7fc | 208 | $f=qx(find $basedir -iname $new) if ($new); |
e1f319fe MCC |
209 | } |
210 | ||
be600e5a MCC |
211 | # Wild guess: seek for the same name on another place |
212 | if (!$f) { | |
9e78e7fc | 213 | $f = qx(find $basedir -iname $new) if ($new); |
be600e5a MCC |
214 | } |
215 | ||
d2656095 MCC |
216 | my @find = split /\s+/, $f; |
217 | ||
218 | if (!$f) { | |
219 | print STDERR "ERROR: Didn't find a replacement for $ref\n"; | |
220 | } elsif (scalar(@find) > 1) { | |
221 | print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n"; | |
222 | foreach my $j (@find) { | |
223 | $j =~ s,^./,,; | |
224 | print STDERR " $j\n"; | |
225 | } | |
226 | } else { | |
227 | $f = $find[0]; | |
228 | $f =~ s,^./,,; | |
229 | print "INFO: Replacing $ref to $f\n"; | |
230 | foreach my $j (qx(git grep -l $ref)) { | |
231 | qx(sed "s\@$ref\@$f\@g" -i $j); | |
232 | } | |
233 | } | |
234 | } |