2 # SPDX-License-Identifier: GPL-2.0
4 # Treewide grep for references to files under doc, and report
5 # non-existing files in stderr.
9 use Getopt::Long qw(:config no_auto_abbrev);
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
14 my %false_positives = (
18 $scriptname =~ s,.*/([^/]+/),$1,;
26 printf "Warning: can't check if file exists, as this is not a git tree";
33 'h|help|usage' => \$help,
37 print "$scriptname [--help] [--fix]\n";
41 # Step 1: find broken references
42 print "Finding broken references. This may take a while... " if ($fix);
48 open IN, "git grep ':doc:\`' doc/|"
49 or die "Failed to run git grep";
51 next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
59 $f =~ s,.*\<([^\>]+)\>,$1,;
63 next if (grep -e, glob("$f"));
65 if ($fix && !$doc_fix) {
66 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
70 print STDERR "$f: :doc:`$doc_ref`\n";
74 open IN, "git grep 'doc/'|"
75 or die "Failed to run git grep";
77 next if (!m/^([^:]+):(.*)/);
82 # On linux-next, discard the Next/ directory
83 next if ($f =~ m,^Next/,);
85 # Makefiles and scripts contain nasty expressions to parse docs
86 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
89 next if ($f eq $scriptname);
91 # Ignore the dir where documentation will be built
92 next if ($ln =~ m,\b(\S*)doc/output,);
94 if ($ln =~ m,\b(\S*)(doc/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
100 # some file references are like:
101 # /usr/src/linux/doc/DMA-{API,mapping}.txt
102 # For now, ignore them
103 next if ($extra =~ m/^{/);
105 # Remove footnotes at the end like:
106 # doc/devicetree/dt-object-internal.txt[1]
107 $ref =~ s/(txt|rst)\[\d+]$/$1/;
109 # Remove ending ']' without any '['
110 $ref =~ s/\].*// if (!($ref =~ m/\[/));
112 # Remove puntuation marks at the end
113 $ref =~ s/[\,\.]+$//;
115 my $fulref = "$prefix$ref";
117 $fulref =~ s/^(\<file|ref)://;
118 $fulref =~ s/^[\'\`]+//;
119 $fulref =~ s,^\$\(.*\)/,,;
122 # Remove URL false-positives
123 next if ($fulref =~ m/^http/);
125 # Check if exists, evaluating wildcards
126 next if (grep -e, glob("$ref $fulref"));
128 # Accept relative doc patches for tools/
129 if ($f =~ m/tools/) {
131 $path =~ s,(.*)/.*,$1,;
132 next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
135 # Discard known false-positives
136 if (defined($false_positives{$f})) {
137 next if ($false_positives{$f} eq $fulref);
141 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
145 print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
147 print STDERR "$f: $fulref\n";
155 # Step 2: Seek for file name alternatives
156 print "Auto-fixing broken references. Please double-check the results\n";
158 foreach my $ref (keys %broken_ref) {
162 # On translations, only seek inside the translations directory
163 $basedir = $1 if ($ref =~ m,(doc/translations/[^/]+),);
165 # get just the basename
170 # usual reason for breakage: DT file moved around
171 if ($ref =~ /devicetree/) {
172 # usual reason for breakage: DT file renamed to .yaml
175 $new_ref =~ s/\.txt$/.yaml/;
176 $f=$new_ref if (-f $new_ref);
182 $f = qx(find doc/device-tree-bindings/ -iname "*$search*") if ($search);
184 # Manufacturer name may have changed
186 $f = qx(find doc/device-tree-bindings/ -iname "*$search*") if ($search);
191 # usual reason for breakage: file renamed to .rst
193 $new =~ s/\.txt$/.rst/;
194 $f=qx(find $basedir -iname $new) if ($new);
197 # usual reason for breakage: use dash or underline
199 $new =~ s/[-_]/[-_]/g;
200 $f=qx(find $basedir -iname $new) if ($new);
203 # Wild guess: seek for the same name on another place
205 $f = qx(find $basedir -iname $new) if ($new);
208 my @find = split /\s+/, $f;
211 print STDERR "ERROR: Didn't find a replacement for $ref\n";
212 } elsif (scalar(@find) > 1) {
213 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
214 foreach my $j (@find) {
216 print STDERR " $j\n";
221 print "INFO: Replacing $ref to $f\n";
222 foreach my $j (qx(git grep -l $ref)) {
223 qx(sed "s\@$ref\@$f\@g" -i $j);