Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
Jani Nikula | e893922 | 2017-10-09 18:26:15 +0300 | [diff] [blame] | 4 | # Treewide grep for references to files under Documentation, and report |
| 5 | # non-existing files in stderr. |
| 6 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 7 | use warnings; |
| 8 | use strict; |
| 9 | use Getopt::Long qw(:config no_auto_abbrev); |
Jani Nikula | e893922 | 2017-10-09 18:26:15 +0300 | [diff] [blame] | 10 | |
Mauro Carvalho Chehab | aeaacbf | 2019-05-29 20:09:28 -0300 | [diff] [blame^] | 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 |
| 13 | # the original work. |
| 14 | my %false_positives = ( |
| 15 | "Documentation/scsi/scsi_mid_low_api.txt" => "Documentation/Configure.help", |
| 16 | "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c", |
| 17 | ); |
| 18 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 19 | my $scriptname = $0; |
| 20 | $scriptname =~ s,.*/([^/]+/),$1,; |
| 21 | |
| 22 | # Parse arguments |
| 23 | my $help = 0; |
| 24 | my $fix = 0; |
| 25 | |
| 26 | GetOptions( |
| 27 | 'fix' => \$fix, |
| 28 | 'h|help|usage' => \$help, |
| 29 | ); |
| 30 | |
| 31 | if ($help != 0) { |
Mauro Carvalho Chehab | 40fc3eb | 2018-06-14 07:11:02 -0300 | [diff] [blame] | 32 | print "$scriptname [--help] [--fix]\n"; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 33 | exit -1; |
| 34 | } |
| 35 | |
| 36 | # Step 1: find broken references |
| 37 | print "Finding broken references. This may take a while... " if ($fix); |
| 38 | |
| 39 | my %broken_ref; |
| 40 | |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 41 | my $doc_fix = 0; |
| 42 | |
| 43 | open IN, "git grep ':doc:\`' Documentation/|" |
| 44 | or die "Failed to run git grep"; |
| 45 | while (<IN>) { |
| 46 | next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,); |
| 47 | |
| 48 | my $d = $1; |
| 49 | my $doc_ref = $2; |
| 50 | |
| 51 | my $f = $doc_ref; |
| 52 | |
| 53 | $d =~ s,(.*/).*,$1,; |
| 54 | $f =~ s,.*\<([^\>]+)\>,$1,; |
| 55 | |
| 56 | $f ="$d$f.rst"; |
| 57 | |
| 58 | next if (grep -e, glob("$f")); |
| 59 | |
| 60 | if ($fix && !$doc_fix) { |
| 61 | print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n"; |
| 62 | } |
| 63 | $doc_fix++; |
| 64 | |
| 65 | print STDERR "$f: :doc:`$doc_ref`\n"; |
| 66 | } |
| 67 | close IN; |
| 68 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 69 | open IN, "git grep 'Documentation/'|" |
| 70 | or die "Failed to run git grep"; |
| 71 | while (<IN>) { |
| 72 | next if (!m/^([^:]+):(.*)/); |
| 73 | |
| 74 | my $f = $1; |
| 75 | my $ln = $2; |
| 76 | |
Mauro Carvalho Chehab | fe3e4b9 | 2019-04-22 08:42:02 -0300 | [diff] [blame] | 77 | # On linux-next, discard the Next/ directory |
| 78 | next if ($f =~ m,^Next/,); |
| 79 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 80 | # Makefiles and scripts contain nasty expressions to parse docs |
| 81 | next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/); |
| 82 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 83 | # Skip this script |
| 84 | next if ($f eq $scriptname); |
| 85 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 86 | if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 87 | my $prefix = $1; |
| 88 | my $ref = $2; |
| 89 | my $base = $2; |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 90 | my $extra = $3; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 91 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 92 | # some file references are like: |
| 93 | # /usr/src/linux/Documentation/DMA-{API,mapping}.txt |
| 94 | # For now, ignore them |
| 95 | next if ($extra =~ m/^{/); |
| 96 | |
| 97 | # Remove footnotes at the end like: |
| 98 | # Documentation/devicetree/dt-object-internal.txt[1] |
| 99 | $ref =~ s/(txt|rst)\[\d+]$/$1/; |
| 100 | |
| 101 | # Remove ending ']' without any '[' |
| 102 | $ref =~ s/\].*// if (!($ref =~ m/\[/)); |
| 103 | |
| 104 | # Remove puntuation marks at the end |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 105 | $ref =~ s/[\,\.]+$//; |
| 106 | |
| 107 | my $fulref = "$prefix$ref"; |
| 108 | |
| 109 | $fulref =~ s/^(\<file|ref)://; |
| 110 | $fulref =~ s/^[\'\`]+//; |
| 111 | $fulref =~ s,^\$\(.*\)/,,; |
| 112 | $base =~ s,.*/,,; |
| 113 | |
| 114 | # Remove URL false-positives |
| 115 | next if ($fulref =~ m/^http/); |
| 116 | |
Mauro Carvalho Chehab | 5d395fa | 2018-06-26 06:49:04 -0300 | [diff] [blame] | 117 | # Remove sched-pelt false-positive |
| 118 | next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,); |
| 119 | |
Mauro Carvalho Chehab | d25c063 | 2018-06-26 06:49:03 -0300 | [diff] [blame] | 120 | # Discard some build examples from Documentation/target/tcm_mod_builder.txt |
| 121 | next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,); |
| 122 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 123 | # Check if exists, evaluating wildcards |
| 124 | next if (grep -e, glob("$ref $fulref")); |
| 125 | |
Mauro Carvalho Chehab | a78513c | 2018-06-14 11:06:08 -0300 | [diff] [blame] | 126 | # Accept relative Documentation patches for tools/ |
| 127 | if ($f =~ m/tools/) { |
| 128 | my $path = $f; |
| 129 | $path =~ s,(.*)/.*,$1,; |
| 130 | next if (grep -e, glob("$path/$ref $path/$fulref")); |
| 131 | } |
| 132 | |
Mauro Carvalho Chehab | aeaacbf | 2019-05-29 20:09:28 -0300 | [diff] [blame^] | 133 | # Discard known false-positives |
| 134 | if (defined($false_positives{$f})) { |
| 135 | next if ($false_positives{$f} eq $fulref); |
| 136 | } |
| 137 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 138 | if ($fix) { |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 139 | if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 140 | $broken_ref{$ref}++; |
| 141 | } |
| 142 | } else { |
| 143 | print STDERR "$f: $fulref\n"; |
| 144 | } |
| 145 | } |
| 146 | } |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 147 | close IN; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 148 | |
| 149 | exit 0 if (!$fix); |
| 150 | |
| 151 | # Step 2: Seek for file name alternatives |
| 152 | print "Auto-fixing broken references. Please double-check the results\n"; |
| 153 | |
| 154 | foreach my $ref (keys %broken_ref) { |
| 155 | my $new =$ref; |
| 156 | |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 157 | my $basedir = "."; |
| 158 | # On translations, only seek inside the translations directory |
| 159 | $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),); |
| 160 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 161 | # get just the basename |
| 162 | $new =~ s,.*/,,; |
| 163 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 164 | my $f=""; |
| 165 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 166 | # usual reason for breakage: DT file moved around |
| 167 | if ($ref =~ /devicetree/) { |
| 168 | my $search = $new; |
| 169 | $search =~ s,^.*/,,; |
| 170 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 171 | if (!$f) { |
| 172 | # Manufacturer name may have changed |
| 173 | $search =~ s/^.*,//; |
| 174 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 175 | } |
| 176 | } |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 177 | |
| 178 | # usual reason for breakage: file renamed to .rst |
| 179 | if (!$f) { |
| 180 | $new =~ s/\.txt$/.rst/; |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 181 | $f=qx(find $basedir -iname $new) if ($new); |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 182 | } |
| 183 | |
Mauro Carvalho Chehab | e1f319f | 2018-06-14 10:14:54 -0300 | [diff] [blame] | 184 | # usual reason for breakage: use dash or underline |
| 185 | if (!$f) { |
| 186 | $new =~ s/[-_]/[-_]/g; |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 187 | $f=qx(find $basedir -iname $new) if ($new); |
Mauro Carvalho Chehab | e1f319f | 2018-06-14 10:14:54 -0300 | [diff] [blame] | 188 | } |
| 189 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 190 | # Wild guess: seek for the same name on another place |
| 191 | if (!$f) { |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 192 | $f = qx(find $basedir -iname $new) if ($new); |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 193 | } |
| 194 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 195 | my @find = split /\s+/, $f; |
| 196 | |
| 197 | if (!$f) { |
| 198 | print STDERR "ERROR: Didn't find a replacement for $ref\n"; |
| 199 | } elsif (scalar(@find) > 1) { |
| 200 | print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n"; |
| 201 | foreach my $j (@find) { |
| 202 | $j =~ s,^./,,; |
| 203 | print STDERR " $j\n"; |
| 204 | } |
| 205 | } else { |
| 206 | $f = $find[0]; |
| 207 | $f =~ s,^./,,; |
| 208 | print "INFO: Replacing $ref to $f\n"; |
| 209 | foreach my $j (qx(git grep -l $ref)) { |
| 210 | qx(sed "s\@$ref\@$f\@g" -i $j); |
| 211 | } |
| 212 | } |
| 213 | } |