2 # SPDX-License-Identifier: GPL-2.0
4 # Treewide grep for references to files under Documentation, and report
5 # non-existing files in stderr.
9 use Getopt
::Long
qw(:config no_auto_abbrev);
12 $scriptname =~ s
,.*/([^/]+/),$1,;
20 'h|help|usage' => \
$help,
24 print "$scriptname [--help] [--fix]\n";
28 # Step 1: find broken references
29 print "Finding broken references. This may take a while... " if ($fix);
35 open IN
, "git grep ':doc:\`' Documentation/|"
36 or die "Failed to run git grep";
38 next if (!m
,^([^:]+):.*\
:doc\
:\
`([^\`]+)\
`,);
46 $f =~ s,.*\<([^\>]+)\>,$1,;
50 next if (grep -e, glob("$f"));
52 if ($fix && !$doc_fix) {
53 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
57 print STDERR "$f: :doc:`$doc_ref`\n";
61 open IN, "git
grep 'Documentation/'|"
62 or die "Failed to run git
grep";
64 next if (!m/^([^:]+):(.*)/);
69 # On linux-next, discard the Next/ directory
70 next if ($f =~ m,^Next/,);
72 # Makefiles and scripts contain nasty expressions to parse docs
73 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
76 next if ($f eq $scriptname);
78 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
84 # some file references are like:
85 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
86 # For now, ignore them
87 next if ($extra =~ m/^{/);
89 # Remove footnotes at the end like:
90 # Documentation/devicetree/dt-object-internal.txt[1]
91 $ref =~ s/(txt|rst)\[\d+]$/$1/;
93 # Remove ending ']' without any '['
94 $ref =~ s/\].*// if (!($ref =~ m/\[/));
96 # Remove puntuation marks at the end
99 my $fulref = "$prefix$ref";
101 $fulref =~ s/^(\<file|ref)://;
102 $fulref =~ s/^[\'\`]+//;
103 $fulref =~ s,^\$\(.*\)/,,;
106 # Remove URL false-positives
107 next if ($fulref =~ m/^http/);
109 # Remove sched-pelt false-positive
110 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
112 # 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,);
115 # Check if exists, evaluating wildcards
116 next if (grep -e, glob("$ref $fulref"));
118 # Accept relative Documentation patches for tools/
119 if ($f =~ m/tools/) {
121 $path =~ s,(.*)/.*,$1,;
122 next if (grep -e, glob("$path/$ref $path/$fulref"));
126 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
130 print STDERR "$f: $fulref\n";
138 # Step 2: Seek for file name alternatives
139 print "Auto-fixing broken references
. Please double-check the results
\n";
141 foreach my $ref (keys %broken_ref) {
144 # get just the basename
149 # usual reason for breakage: DT file moved around
150 if ($ref =~ /devicetree/) {
153 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
155 # Manufacturer name may have changed
157 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
161 # usual reason for breakage: file renamed to .rst
163 $new =~ s/\.txt$/.rst/;
164 $f=qx(find . -iname $new) if ($new);
167 # usual reason for breakage: use dash or underline
169 $new =~ s/[-_]/[-_]/g;
170 $f=qx(find . -iname $new) if ($new);
173 # Wild guess: seek for the same name on another place
175 $f = qx(find . -iname $new) if ($new);
178 my @find = split /\s+/, $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) {
186 print STDERR
" $j\n";
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);