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);
33 open IN, "git grep 'Documentation/'|"
34 or die "Failed to run git grep";
36 next if (!m/^([^:]+):(.*)/);
41 # Makefiles and scripts contain nasty expressions to parse docs
42 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
45 next if ($f eq $scriptname);
47 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
53 # some file references are like:
54 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
55 # For now, ignore them
56 next if ($extra =~ m/^{/);
58 # Remove footnotes at the end like:
59 # Documentation/devicetree/dt-object-internal.txt[1]
60 $ref =~ s/(txt|rst)\[\d+]$/$1/;
62 # Remove ending ']' without any '['
63 $ref =~ s/\].*// if (!($ref =~ m/\[/));
65 # Remove puntuation marks at the end
68 my $fulref = "$prefix$ref";
70 $fulref =~ s/^(\<file|ref)://;
71 $fulref =~ s/^[\'\`]+//;
72 $fulref =~ s,^\$\(.*\)/,,;
75 # Remove URL false-positives
76 next if ($fulref =~ m/^http/);
78 # Remove sched-pelt false-positive
79 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
81 # Discard some build examples from Documentation/target/tcm_mod_builder.txt
82 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
84 # Check if exists, evaluating wildcards
85 next if (grep -e, glob("$ref $fulref"));
87 # Accept relative Documentation patches for tools/
90 $path =~ s,(.*)/.*,$1,;
91 next if (grep -e, glob("$path/$ref $path/$fulref"));
95 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
99 print STDERR "$f: $fulref\n";
106 # Step 2: Seek for file name alternatives
107 print "Auto-fixing broken references. Please double-check the results\n";
109 foreach my $ref (keys %broken_ref) {
112 # get just the basename
117 # usual reason for breakage: DT file moved around
118 if ($ref =~ /devicetree/) {
121 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
123 # Manufacturer name may have changed
125 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
129 # usual reason for breakage: file renamed to .rst
131 $new =~ s/\.txt$/.rst/;
132 $f=qx(find . -iname $new) if ($new);
135 # usual reason for breakage: use dash or underline
137 $new =~ s/[-_]/[-_]/g;
138 $f=qx(find . -iname $new) if ($new);
141 # Wild guess: seek for the same name on another place
143 $f = qx(find . -iname $new) if ($new);
146 my @find = split /\s+/, $f;
149 print STDERR "ERROR: Didn't find a replacement for $ref\n";
150 } elsif (scalar(@find) > 1) {
151 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
152 foreach my $j (@find) {
154 print STDERR " $j\n";
159 print "INFO: Replacing $ref to $f\n";
160 foreach my $j (qx(git grep -l $ref)) {
161 qx(sed "s\@$ref\@$f\@g" -i $j);