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 contain nasty expressions to parse docs
42 next if ($f =~ m/Makefile/);
44 next if ($f eq $scriptname);
46 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*),) {
53 my $fulref = "$prefix$ref";
55 $fulref =~ s/^(\<file|ref)://;
56 $fulref =~ s/^[\'\`]+//;
57 $fulref =~ s,^\$\(.*\)/,,;
60 # Remove URL false-positives
61 next if ($fulref =~ m/^http/);
63 # Check if exists, evaluating wildcards
64 next if (grep -e, glob("$ref $fulref"));
67 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
71 print STDERR "$f: $fulref\n";
78 # Step 2: Seek for file name alternatives
79 print "Auto-fixing broken references. Please double-check the results\n";
81 foreach my $ref (keys %broken_ref) {
84 # get just the basename
89 # usual reason for breakage: DT file moved around
90 if ($ref =~ /devicetree/) {
93 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
95 # Manufacturer name may have changed
97 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
101 # usual reason for breakage: file renamed to .rst
103 $new =~ s/\.txt$/.rst/;
104 $f=qx(find . -iname $new) if ($new);
107 # Wild guess: seek for the same name on another place
109 $f = qx(find . -iname $new) if ($new);
112 my @find = split /\s+/, $f;
115 print STDERR "ERROR: Didn't find a replacement for $ref\n";
116 } elsif (scalar(@find) > 1) {
117 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
118 foreach my $j (@find) {
120 print STDERR " $j\n";
125 print "INFO: Replacing $ref to $f\n";
126 foreach my $j (qx(git grep -l $ref)) {
127 qx(sed "s\@$ref\@$f\@g" -i $j);