blob: 440227bb55a943d71ec177a3f5f027bdc143b15a [file] [log] [blame]
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -03001#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3#
Jani Nikulae8939222017-10-09 18:26:15 +03004# Treewide grep for references to files under Documentation, and report
5# non-existing files in stderr.
6
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -03007use warnings;
8use strict;
9use Getopt::Long qw(:config no_auto_abbrev);
Jani Nikulae8939222017-10-09 18:26:15 +030010
Mauro Carvalho Chehabaeaacbf2019-05-29 20:09:28 -030011# 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.
14my %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
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030019my $scriptname = $0;
20$scriptname =~ s,.*/([^/]+/),$1,;
21
22# Parse arguments
23my $help = 0;
24my $fix = 0;
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -030025my $warn = 0;
26
27if (! -d ".git") {
28 printf "Warning: can't check if file exists, as this is not a git tree";
29 exit 0;
30}
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030031
32GetOptions(
33 'fix' => \$fix,
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -030034 'warn' => \$warn,
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030035 'h|help|usage' => \$help,
36);
37
38if ($help != 0) {
Mauro Carvalho Chehab40fc3eb2018-06-14 07:11:02 -030039 print "$scriptname [--help] [--fix]\n";
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030040 exit -1;
41}
42
43# Step 1: find broken references
44print "Finding broken references. This may take a while... " if ($fix);
45
46my %broken_ref;
47
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030048my $doc_fix = 0;
49
50open IN, "git grep ':doc:\`' Documentation/|"
51 or die "Failed to run git grep";
52while (<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}
74close IN;
75
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030076open IN, "git grep 'Documentation/'|"
77 or die "Failed to run git grep";
78while (<IN>) {
79 next if (!m/^([^:]+):(.*)/);
80
81 my $f = $1;
82 my $ln = $2;
83
Mauro Carvalho Chehabfe3e4b92019-04-22 08:42:02 -030084 # On linux-next, discard the Next/ directory
85 next if ($f =~ m,^Next/,);
86
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030087 # Makefiles and scripts contain nasty expressions to parse docs
88 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
89
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030090 # Skip this script
91 next if ($f eq $scriptname);
92
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030093 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030094 my $prefix = $1;
95 my $ref = $2;
96 my $base = $2;
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030097 my $extra = $3;
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030098
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030099 # some file references are like:
100 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
101 # For now, ignore them
102 next if ($extra =~ m/^{/);
103
104 # Remove footnotes at the end like:
105 # Documentation/devicetree/dt-object-internal.txt[1]
106 $ref =~ s/(txt|rst)\[\d+]$/$1/;
107
108 # Remove ending ']' without any '['
109 $ref =~ s/\].*// if (!($ref =~ m/\[/));
110
111 # Remove puntuation marks at the end
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300112 $ref =~ s/[\,\.]+$//;
113
114 my $fulref = "$prefix$ref";
115
116 $fulref =~ s/^(\<file|ref)://;
117 $fulref =~ s/^[\'\`]+//;
118 $fulref =~ s,^\$\(.*\)/,,;
119 $base =~ s,.*/,,;
120
121 # Remove URL false-positives
122 next if ($fulref =~ m/^http/);
123
Mauro Carvalho Chehab5d395fa2018-06-26 06:49:04 -0300124 # Remove sched-pelt false-positive
125 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
126
Mauro Carvalho Chehabd25c0632018-06-26 06:49:03 -0300127 # Discard some build examples from Documentation/target/tcm_mod_builder.txt
128 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
129
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300130 # Check if exists, evaluating wildcards
131 next if (grep -e, glob("$ref $fulref"));
132
Mauro Carvalho Chehaba78513c2018-06-14 11:06:08 -0300133 # Accept relative Documentation patches for tools/
134 if ($f =~ m/tools/) {
135 my $path = $f;
136 $path =~ s,(.*)/.*,$1,;
Mauro Carvalho Chehab4904aee2019-05-29 20:09:29 -0300137 next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
Mauro Carvalho Chehaba78513c2018-06-14 11:06:08 -0300138 }
139
Mauro Carvalho Chehabaeaacbf2019-05-29 20:09:28 -0300140 # Discard known false-positives
141 if (defined($false_positives{$f})) {
142 next if ($false_positives{$f} eq $fulref);
143 }
144
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300145 if ($fix) {
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300146 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300147 $broken_ref{$ref}++;
148 }
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -0300149 } elsif ($warn) {
150 print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300151 } else {
152 print STDERR "$f: $fulref\n";
153 }
154 }
155}
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -0300156close IN;
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300157
158exit 0 if (!$fix);
159
160# Step 2: Seek for file name alternatives
161print "Auto-fixing broken references. Please double-check the results\n";
162
163foreach my $ref (keys %broken_ref) {
164 my $new =$ref;
165
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300166 my $basedir = ".";
167 # On translations, only seek inside the translations directory
168 $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
169
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300170 # get just the basename
171 $new =~ s,.*/,,;
172
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300173 my $f="";
174
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300175 # usual reason for breakage: DT file moved around
176 if ($ref =~ /devicetree/) {
Mauro Carvalho Chehab0ca862e2019-05-29 20:09:30 -0300177 # usual reason for breakage: DT file renamed to .yaml
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300178 if (!$f) {
Mauro Carvalho Chehab0ca862e2019-05-29 20:09:30 -0300179 my $new_ref = $ref;
180 $new_ref =~ s/\.txt$/.yaml/;
181 $f=$new_ref if (-f $new_ref);
182 }
183
184 if (!$f) {
185 my $search = $new;
186 $search =~ s,^.*/,,;
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300187 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
Mauro Carvalho Chehab0ca862e2019-05-29 20:09:30 -0300188 if (!$f) {
189 # Manufacturer name may have changed
190 $search =~ s/^.*,//;
191 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
192 }
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300193 }
194 }
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300195
196 # usual reason for breakage: file renamed to .rst
197 if (!$f) {
198 $new =~ s/\.txt$/.rst/;
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300199 $f=qx(find $basedir -iname $new) if ($new);
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300200 }
201
Mauro Carvalho Chehabe1f319f2018-06-14 10:14:54 -0300202 # usual reason for breakage: use dash or underline
203 if (!$f) {
204 $new =~ s/[-_]/[-_]/g;
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300205 $f=qx(find $basedir -iname $new) if ($new);
Mauro Carvalho Chehabe1f319f2018-06-14 10:14:54 -0300206 }
207
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300208 # Wild guess: seek for the same name on another place
209 if (!$f) {
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300210 $f = qx(find $basedir -iname $new) if ($new);
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300211 }
212
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300213 my @find = split /\s+/, $f;
214
215 if (!$f) {
216 print STDERR "ERROR: Didn't find a replacement for $ref\n";
217 } elsif (scalar(@find) > 1) {
218 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
219 foreach my $j (@find) {
220 $j =~ s,^./,,;
221 print STDERR " $j\n";
222 }
223 } else {
224 $f = $find[0];
225 $f =~ s,^./,,;
226 print "INFO: Replacing $ref to $f\n";
227 foreach my $j (qx(git grep -l $ref)) {
228 qx(sed "s\@$ref\@$f\@g" -i $j);
229 }
230 }
231}