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 | |
| 33 | open IN, "git grep 'Documentation/'|" |
| 34 | or die "Failed to run git grep"; |
| 35 | while (<IN>) { |
| 36 | next if (!m/^([^:]+):(.*)/); |
| 37 | |
| 38 | my $f = $1; |
| 39 | my $ln = $2; |
| 40 | |
Mauro Carvalho Chehab | fe3e4b9 | 2019-04-22 08:42:02 -0300 | [diff] [blame^] | 41 | # On linux-next, discard the Next/ directory |
| 42 | next if ($f =~ m,^Next/,); |
| 43 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 44 | # Makefiles and scripts contain nasty expressions to parse docs |
| 45 | next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/); |
| 46 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 47 | # Skip this script |
| 48 | next if ($f eq $scriptname); |
| 49 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 50 | if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 51 | my $prefix = $1; |
| 52 | my $ref = $2; |
| 53 | my $base = $2; |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 54 | my $extra = $3; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 55 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 56 | # some file references are like: |
| 57 | # /usr/src/linux/Documentation/DMA-{API,mapping}.txt |
| 58 | # For now, ignore them |
| 59 | next if ($extra =~ m/^{/); |
| 60 | |
| 61 | # Remove footnotes at the end like: |
| 62 | # Documentation/devicetree/dt-object-internal.txt[1] |
| 63 | $ref =~ s/(txt|rst)\[\d+]$/$1/; |
| 64 | |
| 65 | # Remove ending ']' without any '[' |
| 66 | $ref =~ s/\].*// if (!($ref =~ m/\[/)); |
| 67 | |
| 68 | # Remove puntuation marks at the end |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 69 | $ref =~ s/[\,\.]+$//; |
| 70 | |
| 71 | my $fulref = "$prefix$ref"; |
| 72 | |
| 73 | $fulref =~ s/^(\<file|ref)://; |
| 74 | $fulref =~ s/^[\'\`]+//; |
| 75 | $fulref =~ s,^\$\(.*\)/,,; |
| 76 | $base =~ s,.*/,,; |
| 77 | |
| 78 | # Remove URL false-positives |
| 79 | next if ($fulref =~ m/^http/); |
| 80 | |
Mauro Carvalho Chehab | 5d395fa | 2018-06-26 06:49:04 -0300 | [diff] [blame] | 81 | # Remove sched-pelt false-positive |
| 82 | next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,); |
| 83 | |
Mauro Carvalho Chehab | d25c063 | 2018-06-26 06:49:03 -0300 | [diff] [blame] | 84 | # Discard some build examples from Documentation/target/tcm_mod_builder.txt |
| 85 | next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,); |
| 86 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 87 | # Check if exists, evaluating wildcards |
| 88 | next if (grep -e, glob("$ref $fulref")); |
| 89 | |
Mauro Carvalho Chehab | a78513c | 2018-06-14 11:06:08 -0300 | [diff] [blame] | 90 | # Accept relative Documentation patches for tools/ |
| 91 | if ($f =~ m/tools/) { |
| 92 | my $path = $f; |
| 93 | $path =~ s,(.*)/.*,$1,; |
| 94 | next if (grep -e, glob("$path/$ref $path/$fulref")); |
| 95 | } |
| 96 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 97 | if ($fix) { |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 98 | if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 99 | $broken_ref{$ref}++; |
| 100 | } |
| 101 | } else { |
| 102 | print STDERR "$f: $fulref\n"; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | exit 0 if (!$fix); |
| 108 | |
| 109 | # Step 2: Seek for file name alternatives |
| 110 | print "Auto-fixing broken references. Please double-check the results\n"; |
| 111 | |
| 112 | foreach my $ref (keys %broken_ref) { |
| 113 | my $new =$ref; |
| 114 | |
| 115 | # get just the basename |
| 116 | $new =~ s,.*/,,; |
| 117 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 118 | my $f=""; |
| 119 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 120 | # usual reason for breakage: DT file moved around |
| 121 | if ($ref =~ /devicetree/) { |
| 122 | my $search = $new; |
| 123 | $search =~ s,^.*/,,; |
| 124 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 125 | if (!$f) { |
| 126 | # Manufacturer name may have changed |
| 127 | $search =~ s/^.*,//; |
| 128 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 129 | } |
| 130 | } |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 131 | |
| 132 | # usual reason for breakage: file renamed to .rst |
| 133 | if (!$f) { |
| 134 | $new =~ s/\.txt$/.rst/; |
| 135 | $f=qx(find . -iname $new) if ($new); |
| 136 | } |
| 137 | |
Mauro Carvalho Chehab | e1f319f | 2018-06-14 10:14:54 -0300 | [diff] [blame] | 138 | # usual reason for breakage: use dash or underline |
| 139 | if (!$f) { |
| 140 | $new =~ s/[-_]/[-_]/g; |
| 141 | $f=qx(find . -iname $new) if ($new); |
| 142 | } |
| 143 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 144 | # Wild guess: seek for the same name on another place |
| 145 | if (!$f) { |
| 146 | $f = qx(find . -iname $new) if ($new); |
| 147 | } |
| 148 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 149 | my @find = split /\s+/, $f; |
| 150 | |
| 151 | if (!$f) { |
| 152 | print STDERR "ERROR: Didn't find a replacement for $ref\n"; |
| 153 | } elsif (scalar(@find) > 1) { |
| 154 | print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n"; |
| 155 | foreach my $j (@find) { |
| 156 | $j =~ s,^./,,; |
| 157 | print STDERR " $j\n"; |
| 158 | } |
| 159 | } else { |
| 160 | $f = $find[0]; |
| 161 | $f =~ s,^./,,; |
| 162 | print "INFO: Replacing $ref to $f\n"; |
| 163 | foreach my $j (qx(git grep -l $ref)) { |
| 164 | qx(sed "s\@$ref\@$f\@g" -i $j); |
| 165 | } |
| 166 | } |
| 167 | } |