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 | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 11 | my $scriptname = $0; |
| 12 | $scriptname =~ s,.*/([^/]+/),$1,; |
| 13 | |
| 14 | # Parse arguments |
| 15 | my $help = 0; |
| 16 | my $fix = 0; |
| 17 | |
| 18 | GetOptions( |
| 19 | 'fix' => \$fix, |
| 20 | 'h|help|usage' => \$help, |
| 21 | ); |
| 22 | |
| 23 | if ($help != 0) { |
Mauro Carvalho Chehab | 40fc3eb | 2018-06-14 07:11:02 -0300 | [diff] [blame] | 24 | print "$scriptname [--help] [--fix]\n"; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 25 | exit -1; |
| 26 | } |
| 27 | |
| 28 | # Step 1: find broken references |
| 29 | print "Finding broken references. This may take a while... " if ($fix); |
| 30 | |
| 31 | my %broken_ref; |
| 32 | |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame^] | 33 | my $doc_fix = 0; |
| 34 | |
| 35 | open IN, "git grep ':doc:\`' Documentation/|" |
| 36 | or die "Failed to run git grep"; |
| 37 | while (<IN>) { |
| 38 | next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,); |
| 39 | |
| 40 | my $d = $1; |
| 41 | my $doc_ref = $2; |
| 42 | |
| 43 | my $f = $doc_ref; |
| 44 | |
| 45 | $d =~ s,(.*/).*,$1,; |
| 46 | $f =~ s,.*\<([^\>]+)\>,$1,; |
| 47 | |
| 48 | $f ="$d$f.rst"; |
| 49 | |
| 50 | next if (grep -e, glob("$f")); |
| 51 | |
| 52 | if ($fix && !$doc_fix) { |
| 53 | print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n"; |
| 54 | } |
| 55 | $doc_fix++; |
| 56 | |
| 57 | print STDERR "$f: :doc:`$doc_ref`\n"; |
| 58 | } |
| 59 | close IN; |
| 60 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 61 | open IN, "git grep 'Documentation/'|" |
| 62 | or die "Failed to run git grep"; |
| 63 | while (<IN>) { |
| 64 | next if (!m/^([^:]+):(.*)/); |
| 65 | |
| 66 | my $f = $1; |
| 67 | my $ln = $2; |
| 68 | |
Mauro Carvalho Chehab | fe3e4b9 | 2019-04-22 08:42:02 -0300 | [diff] [blame] | 69 | # On linux-next, discard the Next/ directory |
| 70 | next if ($f =~ m,^Next/,); |
| 71 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 72 | # Makefiles and scripts contain nasty expressions to parse docs |
| 73 | next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/); |
| 74 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 75 | # Skip this script |
| 76 | next if ($f eq $scriptname); |
| 77 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 78 | if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 79 | my $prefix = $1; |
| 80 | my $ref = $2; |
| 81 | my $base = $2; |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 82 | my $extra = $3; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 83 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 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/^{/); |
| 88 | |
| 89 | # Remove footnotes at the end like: |
| 90 | # Documentation/devicetree/dt-object-internal.txt[1] |
| 91 | $ref =~ s/(txt|rst)\[\d+]$/$1/; |
| 92 | |
| 93 | # Remove ending ']' without any '[' |
| 94 | $ref =~ s/\].*// if (!($ref =~ m/\[/)); |
| 95 | |
| 96 | # Remove puntuation marks at the end |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 97 | $ref =~ s/[\,\.]+$//; |
| 98 | |
| 99 | my $fulref = "$prefix$ref"; |
| 100 | |
| 101 | $fulref =~ s/^(\<file|ref)://; |
| 102 | $fulref =~ s/^[\'\`]+//; |
| 103 | $fulref =~ s,^\$\(.*\)/,,; |
| 104 | $base =~ s,.*/,,; |
| 105 | |
| 106 | # Remove URL false-positives |
| 107 | next if ($fulref =~ m/^http/); |
| 108 | |
Mauro Carvalho Chehab | 5d395fa | 2018-06-26 06:49:04 -0300 | [diff] [blame] | 109 | # Remove sched-pelt false-positive |
| 110 | next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,); |
| 111 | |
Mauro Carvalho Chehab | d25c063 | 2018-06-26 06:49:03 -0300 | [diff] [blame] | 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,); |
| 114 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 115 | # Check if exists, evaluating wildcards |
| 116 | next if (grep -e, glob("$ref $fulref")); |
| 117 | |
Mauro Carvalho Chehab | a78513c | 2018-06-14 11:06:08 -0300 | [diff] [blame] | 118 | # Accept relative Documentation patches for tools/ |
| 119 | if ($f =~ m/tools/) { |
| 120 | my $path = $f; |
| 121 | $path =~ s,(.*)/.*,$1,; |
| 122 | next if (grep -e, glob("$path/$ref $path/$fulref")); |
| 123 | } |
| 124 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 125 | if ($fix) { |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 126 | if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 127 | $broken_ref{$ref}++; |
| 128 | } |
| 129 | } else { |
| 130 | print STDERR "$f: $fulref\n"; |
| 131 | } |
| 132 | } |
| 133 | } |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame^] | 134 | close IN; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 135 | |
| 136 | exit 0 if (!$fix); |
| 137 | |
| 138 | # Step 2: Seek for file name alternatives |
| 139 | print "Auto-fixing broken references. Please double-check the results\n"; |
| 140 | |
| 141 | foreach my $ref (keys %broken_ref) { |
| 142 | my $new =$ref; |
| 143 | |
| 144 | # get just the basename |
| 145 | $new =~ s,.*/,,; |
| 146 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 147 | my $f=""; |
| 148 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 149 | # usual reason for breakage: DT file moved around |
| 150 | if ($ref =~ /devicetree/) { |
| 151 | my $search = $new; |
| 152 | $search =~ s,^.*/,,; |
| 153 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 154 | if (!$f) { |
| 155 | # Manufacturer name may have changed |
| 156 | $search =~ s/^.*,//; |
| 157 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 158 | } |
| 159 | } |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 160 | |
| 161 | # usual reason for breakage: file renamed to .rst |
| 162 | if (!$f) { |
| 163 | $new =~ s/\.txt$/.rst/; |
| 164 | $f=qx(find . -iname $new) if ($new); |
| 165 | } |
| 166 | |
Mauro Carvalho Chehab | e1f319f | 2018-06-14 10:14:54 -0300 | [diff] [blame] | 167 | # usual reason for breakage: use dash or underline |
| 168 | if (!$f) { |
| 169 | $new =~ s/[-_]/[-_]/g; |
| 170 | $f=qx(find . -iname $new) if ($new); |
| 171 | } |
| 172 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 173 | # Wild guess: seek for the same name on another place |
| 174 | if (!$f) { |
| 175 | $f = qx(find . -iname $new) if ($new); |
| 176 | } |
| 177 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 178 | my @find = split /\s+/, $f; |
| 179 | |
| 180 | if (!$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) { |
| 185 | $j =~ s,^./,,; |
| 186 | print STDERR " $j\n"; |
| 187 | } |
| 188 | } else { |
| 189 | $f = $find[0]; |
| 190 | $f =~ s,^./,,; |
| 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); |
| 194 | } |
| 195 | } |
| 196 | } |