blob: 63e9542656f1d93d8e01a74656ca9ff06efdda1e [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 Chehabd2656092018-05-09 10:18:49 -030011my $scriptname = $0;
12$scriptname =~ s,.*/([^/]+/),$1,;
13
14# Parse arguments
15my $help = 0;
16my $fix = 0;
17
18GetOptions(
19 'fix' => \$fix,
20 'h|help|usage' => \$help,
21);
22
23if ($help != 0) {
Mauro Carvalho Chehab40fc3eb2018-06-14 07:11:02 -030024 print "$scriptname [--help] [--fix]\n";
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030025 exit -1;
26}
27
28# Step 1: find broken references
29print "Finding broken references. This may take a while... " if ($fix);
30
31my %broken_ref;
32
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030033my $doc_fix = 0;
34
35open IN, "git grep ':doc:\`' Documentation/|"
36 or die "Failed to run git grep";
37while (<IN>) {
38 next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
39
40 my $d = $1;
41 my $doc_ref = $2;
42
43 my $f = $doc_ref;
44
45 $d =~ s,(.*/).*,$1,;
46 $f =~ s,.*\<([^\>]+)\>,$1,;
47
48 $f ="$d$f.rst";
49
50 next if (grep -e, glob("$f"));
51
52 if ($fix && !$doc_fix) {
53 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
54 }
55 $doc_fix++;
56
57 print STDERR "$f: :doc:`$doc_ref`\n";
58}
59close IN;
60
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030061open IN, "git grep 'Documentation/'|"
62 or die "Failed to run git grep";
63while (<IN>) {
64 next if (!m/^([^:]+):(.*)/);
65
66 my $f = $1;
67 my $ln = $2;
68
Mauro Carvalho Chehabfe3e4b92019-04-22 08:42:02 -030069 # On linux-next, discard the Next/ directory
70 next if ($f =~ m,^Next/,);
71
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030072 # Makefiles and scripts contain nasty expressions to parse docs
73 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
74
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030075 # Skip this script
76 next if ($f eq $scriptname);
77
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030078 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030079 my $prefix = $1;
80 my $ref = $2;
81 my $base = $2;
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030082 my $extra = $3;
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030083
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030084 # some file references are like:
85 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
86 # For now, ignore them
87 next if ($extra =~ m/^{/);
88
89 # Remove footnotes at the end like:
90 # Documentation/devicetree/dt-object-internal.txt[1]
91 $ref =~ s/(txt|rst)\[\d+]$/$1/;
92
93 # Remove ending ']' without any '['
94 $ref =~ s/\].*// if (!($ref =~ m/\[/));
95
96 # Remove puntuation marks at the end
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030097 $ref =~ s/[\,\.]+$//;
98
99 my $fulref = "$prefix$ref";
100
101 $fulref =~ s/^(\<file|ref)://;
102 $fulref =~ s/^[\'\`]+//;
103 $fulref =~ s,^\$\(.*\)/,,;
104 $base =~ s,.*/,,;
105
106 # Remove URL false-positives
107 next if ($fulref =~ m/^http/);
108
Mauro Carvalho Chehab5d395fa2018-06-26 06:49:04 -0300109 # Remove sched-pelt false-positive
110 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
111
Mauro Carvalho Chehabd25c0632018-06-26 06:49:03 -0300112 # Discard some build examples from Documentation/target/tcm_mod_builder.txt
113 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
114
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300115 # Check if exists, evaluating wildcards
116 next if (grep -e, glob("$ref $fulref"));
117
Mauro Carvalho Chehaba78513c2018-06-14 11:06:08 -0300118 # Accept relative Documentation patches for tools/
119 if ($f =~ m/tools/) {
120 my $path = $f;
121 $path =~ s,(.*)/.*,$1,;
122 next if (grep -e, glob("$path/$ref $path/$fulref"));
123 }
124
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300125 if ($fix) {
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300126 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300127 $broken_ref{$ref}++;
128 }
129 } else {
130 print STDERR "$f: $fulref\n";
131 }
132 }
133}
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -0300134close IN;
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300135
136exit 0 if (!$fix);
137
138# Step 2: Seek for file name alternatives
139print "Auto-fixing broken references. Please double-check the results\n";
140
141foreach my $ref (keys %broken_ref) {
142 my $new =$ref;
143
144 # get just the basename
145 $new =~ s,.*/,,;
146
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300147 my $f="";
148
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300149 # usual reason for breakage: DT file moved around
150 if ($ref =~ /devicetree/) {
151 my $search = $new;
152 $search =~ s,^.*/,,;
153 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
154 if (!$f) {
155 # Manufacturer name may have changed
156 $search =~ s/^.*,//;
157 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
158 }
159 }
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300160
161 # usual reason for breakage: file renamed to .rst
162 if (!$f) {
163 $new =~ s/\.txt$/.rst/;
164 $f=qx(find . -iname $new) if ($new);
165 }
166
Mauro Carvalho Chehabe1f319f2018-06-14 10:14:54 -0300167 # usual reason for breakage: use dash or underline
168 if (!$f) {
169 $new =~ s/[-_]/[-_]/g;
170 $f=qx(find . -iname $new) if ($new);
171 }
172
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300173 # Wild guess: seek for the same name on another place
174 if (!$f) {
175 $f = qx(find . -iname $new) if ($new);
176 }
177
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300178 my @find = split /\s+/, $f;
179
180 if (!$f) {
181 print STDERR "ERROR: Didn't find a replacement for $ref\n";
182 } elsif (scalar(@find) > 1) {
183 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
184 foreach my $j (@find) {
185 $j =~ s,^./,,;
186 print STDERR " $j\n";
187 }
188 } else {
189 $f = $find[0];
190 $f =~ s,^./,,;
191 print "INFO: Replacing $ref to $f\n";
192 foreach my $j (qx(git grep -l $ref)) {
193 qx(sed "s\@$ref\@$f\@g" -i $j);
194 }
195 }
196}