1 #!/usr/bin/env perl 1 #!/usr/bin/env perl 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 # 3 # 4 # Treewide grep for references to files under 4 # Treewide grep for references to files under Documentation, and report 5 # non-existing files in stderr. 5 # non-existing files in stderr. 6 6 7 use warnings; 7 use warnings; 8 use strict; 8 use strict; 9 use Getopt::Long qw(:config no_auto_abbrev); 9 use Getopt::Long qw(:config no_auto_abbrev); 10 10 11 # NOTE: only add things here when the file was << 12 # to mention a past documentation file, for ex << 13 # the original work. << 14 my %false_positives = ( << 15 "Documentation/scsi/scsi_mid_low_api.r << 16 "drivers/vhost/vhost.c" => "Documentat << 17 ); << 18 << 19 my $scriptname = $0; 11 my $scriptname = $0; 20 $scriptname =~ s,.*/([^/]+/),$1,; 12 $scriptname =~ s,.*/([^/]+/),$1,; 21 13 22 # Parse arguments 14 # Parse arguments 23 my $help = 0; 15 my $help = 0; 24 my $fix = 0; 16 my $fix = 0; 25 my $warn = 0; << 26 << 27 if (! -e ".git") { << 28 printf "Warning: can't check if file e << 29 exit 0; << 30 } << 31 17 32 GetOptions( 18 GetOptions( 33 'fix' => \$fix, 19 'fix' => \$fix, 34 'warn' => \$warn, << 35 'h|help|usage' => \$help, 20 'h|help|usage' => \$help, 36 ); 21 ); 37 22 38 if ($help != 0) { 23 if ($help != 0) { 39 print "$scriptname [--help] [--fix]\n"; 24 print "$scriptname [--help] [--fix]\n"; 40 exit -1; 25 exit -1; 41 } 26 } 42 27 43 # Step 1: find broken references 28 # Step 1: find broken references 44 print "Finding broken references. This may tak 29 print "Finding broken references. This may take a while... " if ($fix); 45 30 46 my %broken_ref; 31 my %broken_ref; 47 32 48 my $doc_fix = 0; 33 my $doc_fix = 0; 49 34 50 open IN, "git grep ':doc:\`' Documentation/|" 35 open IN, "git grep ':doc:\`' Documentation/|" 51 or die "Failed to run git grep"; 36 or die "Failed to run git grep"; 52 while (<IN>) { 37 while (<IN>) { 53 next if (!m,^([^:]+):.*\:doc\:\`([^\`] 38 next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,); 54 next if (m,sphinx/,); << 55 39 56 my $file = $1; << 57 my $d = $1; 40 my $d = $1; 58 my $doc_ref = $2; 41 my $doc_ref = $2; 59 42 60 my $f = $doc_ref; 43 my $f = $doc_ref; 61 44 62 $d =~ s,(.*/).*,$1,; 45 $d =~ s,(.*/).*,$1,; 63 $f =~ s,.*\<([^\>]+)\>,$1,; 46 $f =~ s,.*\<([^\>]+)\>,$1,; 64 47 65 if ($f =~ m,^/,) { !! 48 $f ="$d$f.rst"; 66 $f = "$f.rst"; << 67 $f =~ s,^/,Documentation/,; << 68 } else { << 69 $f = "$d$f.rst"; << 70 } << 71 49 72 next if (grep -e, glob("$f")); 50 next if (grep -e, glob("$f")); 73 51 74 if ($fix && !$doc_fix) { 52 if ($fix && !$doc_fix) { 75 print STDERR "\nWARNING: Curre 53 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n"; 76 } 54 } 77 $doc_fix++; 55 $doc_fix++; 78 56 79 print STDERR "$file: :doc:`$doc_ref`\n !! 57 print STDERR "$f: :doc:`$doc_ref`\n"; 80 } 58 } 81 close IN; 59 close IN; 82 60 83 open IN, "git grep 'Documentation/'|" 61 open IN, "git grep 'Documentation/'|" 84 or die "Failed to run git grep"; 62 or die "Failed to run git grep"; 85 while (<IN>) { 63 while (<IN>) { 86 next if (!m/^([^:]+):(.*)/); 64 next if (!m/^([^:]+):(.*)/); 87 65 88 my $f = $1; 66 my $f = $1; 89 my $ln = $2; 67 my $ln = $2; 90 68 91 # On linux-next, discard the Next/ dir 69 # On linux-next, discard the Next/ directory 92 next if ($f =~ m,^Next/,); 70 next if ($f =~ m,^Next/,); 93 71 94 # Makefiles and scripts contain nasty 72 # Makefiles and scripts contain nasty expressions to parse docs 95 next if ($f =~ m/Makefile/ || $f =~ m/ 73 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/); 96 74 97 # It doesn't make sense to parse hidde << 98 next if ($f =~ m#/\.#); << 99 << 100 # Skip this script 75 # Skip this script 101 next if ($f eq $scriptname); 76 next if ($f eq $scriptname); 102 77 103 # Ignore the dir where documentation w << 104 next if ($ln =~ m,\b(\S*)Documentation << 105 << 106 if ($ln =~ m,\b(\S*)(Documentation/[A- 78 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) { 107 my $prefix = $1; 79 my $prefix = $1; 108 my $ref = $2; 80 my $ref = $2; 109 my $base = $2; 81 my $base = $2; 110 my $extra = $3; 82 my $extra = $3; 111 83 112 # some file references are lik 84 # some file references are like: 113 # /usr/src/linux/Documentation 85 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt 114 # For now, ignore them 86 # For now, ignore them 115 next if ($extra =~ m/^{/); 87 next if ($extra =~ m/^{/); 116 88 117 # Remove footnotes at the end 89 # Remove footnotes at the end like: 118 # Documentation/devicetree/dt- 90 # Documentation/devicetree/dt-object-internal.txt[1] 119 $ref =~ s/(txt|rst)\[\d+]$/$1/ 91 $ref =~ s/(txt|rst)\[\d+]$/$1/; 120 92 121 # Remove ending ']' without an 93 # Remove ending ']' without any '[' 122 $ref =~ s/\].*// if (!($ref =~ 94 $ref =~ s/\].*// if (!($ref =~ m/\[/)); 123 95 124 # Remove puntuation marks at t 96 # Remove puntuation marks at the end 125 $ref =~ s/[\,\.]+$//; 97 $ref =~ s/[\,\.]+$//; 126 98 127 my $fulref = "$prefix$ref"; 99 my $fulref = "$prefix$ref"; 128 100 129 $fulref =~ s/^(\<file|ref)://; 101 $fulref =~ s/^(\<file|ref)://; 130 $fulref =~ s/^[\'\`]+//; 102 $fulref =~ s/^[\'\`]+//; 131 $fulref =~ s,^\$\(.*\)/,,; 103 $fulref =~ s,^\$\(.*\)/,,; 132 $base =~ s,.*/,,; 104 $base =~ s,.*/,,; 133 105 134 # Remove URL false-positives 106 # Remove URL false-positives 135 next if ($fulref =~ m/^http/); 107 next if ($fulref =~ m/^http/); 136 108 137 # Remove sched-pelt false-posi 109 # Remove sched-pelt false-positive 138 next if ($fulref =~ m,^Documen 110 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,); 139 111 140 # Discard some build examples !! 112 # Discard some build examples from Documentation/target/tcm_mod_builder.txt 141 next if ($fulref =~ m,mnt/sdb/ 113 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,); 142 114 143 # Check if exists, evaluating 115 # Check if exists, evaluating wildcards 144 next if (grep -e, glob("$ref $ 116 next if (grep -e, glob("$ref $fulref")); 145 117 146 # Accept relative Documentatio 118 # Accept relative Documentation patches for tools/ 147 if ($f =~ m/tools/) { 119 if ($f =~ m/tools/) { 148 my $path = $f; 120 my $path = $f; 149 $path =~ s,(.*)/.*,$1, 121 $path =~ s,(.*)/.*,$1,; 150 $path =~ s,testing/sel !! 122 next if (grep -e, glob("$path/$ref $path/$fulref")); 151 next if (grep -e, glob << 152 } << 153 << 154 # Discard known false-positive << 155 if (defined($false_positives{$ << 156 next if ($false_positi << 157 } 123 } 158 124 159 if ($fix) { 125 if ($fix) { 160 if (!($ref =~ m/(scrip 126 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) { 161 $broken_ref{$r 127 $broken_ref{$ref}++; 162 } 128 } 163 } elsif ($warn) { << 164 print STDERR "Warning: << 165 } else { 129 } else { 166 print STDERR "$f: $ful 130 print STDERR "$f: $fulref\n"; 167 } 131 } 168 } 132 } 169 } 133 } 170 close IN; 134 close IN; 171 135 172 exit 0 if (!$fix); 136 exit 0 if (!$fix); 173 137 174 # Step 2: Seek for file name alternatives 138 # Step 2: Seek for file name alternatives 175 print "Auto-fixing broken references. Please d 139 print "Auto-fixing broken references. Please double-check the results\n"; 176 140 177 foreach my $ref (keys %broken_ref) { 141 foreach my $ref (keys %broken_ref) { 178 my $new =$ref; 142 my $new =$ref; 179 143 180 my $basedir = "."; << 181 # On translations, only seek inside th << 182 $basedir = $1 if ($ref =~ m,(Document << 183 << 184 # get just the basename 144 # get just the basename 185 $new =~ s,.*/,,; 145 $new =~ s,.*/,,; 186 146 187 my $f=""; 147 my $f=""; 188 148 189 # usual reason for breakage: DT file m 149 # usual reason for breakage: DT file moved around 190 if ($ref =~ /devicetree/) { 150 if ($ref =~ /devicetree/) { 191 # usual reason for breakage: D !! 151 my $search = $new; >> 152 $search =~ s,^.*/,,; >> 153 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); 192 if (!$f) { 154 if (!$f) { 193 my $new_ref = $ref; !! 155 # Manufacturer name may have changed 194 $new_ref =~ s/\.txt$/. !! 156 $search =~ s/^.*,//; 195 $f=$new_ref if (-f $ne << 196 } << 197 << 198 if (!$f) { << 199 my $search = $new; << 200 $search =~ s,^.*/,,; << 201 $f = qx(find Documenta 157 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); 202 if (!$f) { << 203 # Manufacturer << 204 $search =~ s/^ << 205 $f = qx(find D << 206 } << 207 } 158 } 208 } 159 } 209 160 210 # usual reason for breakage: file rena 161 # usual reason for breakage: file renamed to .rst 211 if (!$f) { 162 if (!$f) { 212 $new =~ s/\.txt$/.rst/; 163 $new =~ s/\.txt$/.rst/; 213 $f=qx(find $basedir -iname $ne !! 164 $f=qx(find . -iname $new) if ($new); 214 } 165 } 215 166 216 # usual reason for breakage: use dash 167 # usual reason for breakage: use dash or underline 217 if (!$f) { 168 if (!$f) { 218 $new =~ s/[-_]/[-_]/g; 169 $new =~ s/[-_]/[-_]/g; 219 $f=qx(find $basedir -iname $ne !! 170 $f=qx(find . -iname $new) if ($new); 220 } 171 } 221 172 222 # Wild guess: seek for the same name o 173 # Wild guess: seek for the same name on another place 223 if (!$f) { 174 if (!$f) { 224 $f = qx(find $basedir -iname $ !! 175 $f = qx(find . -iname $new) if ($new); 225 } 176 } 226 177 227 my @find = split /\s+/, $f; 178 my @find = split /\s+/, $f; 228 179 229 if (!$f) { 180 if (!$f) { 230 print STDERR "ERROR: Didn't fi 181 print STDERR "ERROR: Didn't find a replacement for $ref\n"; 231 } elsif (scalar(@find) > 1) { 182 } elsif (scalar(@find) > 1) { 232 print STDERR "WARNING: Won't a 183 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n"; 233 foreach my $j (@find) { 184 foreach my $j (@find) { 234 $j =~ s,^./,,; 185 $j =~ s,^./,,; 235 print STDERR " $j\n 186 print STDERR " $j\n"; 236 } 187 } 237 } else { 188 } else { 238 $f = $find[0]; 189 $f = $find[0]; 239 $f =~ s,^./,,; 190 $f =~ s,^./,,; 240 print "INFO: Replacing $ref to 191 print "INFO: Replacing $ref to $f\n"; 241 foreach my $j (qx(git grep -l 192 foreach my $j (qx(git grep -l $ref)) { 242 qx(sed "s\@$ref\@$f\@g 193 qx(sed "s\@$ref\@$f\@g" -i $j); 243 } 194 } 244 } 195 } 245 } 196 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.