scripts/documentation-file-ref-check: remove some false positives
[linux-block.git] / scripts / documentation-file-ref-check
CommitLineData
d2656095
MCC
1#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3#
e8939222
JN
4# Treewide grep for references to files under Documentation, and report
5# non-existing files in stderr.
6
d2656095
MCC
7use warnings;
8use strict;
9use Getopt::Long qw(:config no_auto_abbrev);
10
11my $scriptname = $0;
12$scriptname =~ s,.*/([^/]+/),$1,;
13
14# Parse arguments
15my $help = 0;
16my $fix = 0;
17
18GetOptions(
19 'fix' => \$fix,
20 'h|help|usage' => \$help,
21);
22
23if ($help != 0) {
40fc3eb0 24 print "$scriptname [--help] [--fix]\n";
d2656095
MCC
25 exit -1;
26}
27
28# Step 1: find broken references
29print "Finding broken references. This may take a while... " if ($fix);
30
31my %broken_ref;
32
33open IN, "git grep 'Documentation/'|"
34 or die "Failed to run git grep";
35while (<IN>) {
36 next if (!m/^([^:]+):(.*)/);
37
38 my $f = $1;
39 my $ln = $2;
40
2d69708f
MCC
41 # Makefiles and scripts contain nasty expressions to parse docs
42 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
43
d2656095
MCC
44 # Skip this script
45 next if ($f eq $scriptname);
46
2d69708f 47 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
d2656095
MCC
48 my $prefix = $1;
49 my $ref = $2;
50 my $base = $2;
2d69708f
MCC
51 my $extra = $3;
52
53 # some file references are like:
54 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
55 # For now, ignore them
56 next if ($extra =~ m/^{/);
57
58 # Remove footnotes at the end like:
59 # Documentation/devicetree/dt-object-internal.txt[1]
60 $ref =~ s/(txt|rst)\[\d+]$/$1/;
61
62 # Remove ending ']' without any '['
63 $ref =~ s/\].*// if (!($ref =~ m/\[/));
d2656095 64
2d69708f 65 # Remove puntuation marks at the end
d2656095
MCC
66 $ref =~ s/[\,\.]+$//;
67
68 my $fulref = "$prefix$ref";
69
70 $fulref =~ s/^(\<file|ref)://;
71 $fulref =~ s/^[\'\`]+//;
72 $fulref =~ s,^\$\(.*\)/,,;
73 $base =~ s,.*/,,;
74
75 # Remove URL false-positives
76 next if ($fulref =~ m/^http/);
77
d25c0634
MCC
78 # Discard some build examples from Documentation/target/tcm_mod_builder.txt
79 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
80
d2656095
MCC
81 # Check if exists, evaluating wildcards
82 next if (grep -e, glob("$ref $fulref"));
83
a78513c6
MCC
84 # Accept relative Documentation patches for tools/
85 if ($f =~ m/tools/) {
86 my $path = $f;
87 $path =~ s,(.*)/.*,$1,;
88 next if (grep -e, glob("$path/$ref $path/$fulref"));
89 }
90
d2656095 91 if ($fix) {
be600e5a 92 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
d2656095
MCC
93 $broken_ref{$ref}++;
94 }
95 } else {
96 print STDERR "$f: $fulref\n";
97 }
98 }
99}
100
101exit 0 if (!$fix);
102
103# Step 2: Seek for file name alternatives
104print "Auto-fixing broken references. Please double-check the results\n";
105
106foreach my $ref (keys %broken_ref) {
107 my $new =$ref;
108
109 # get just the basename
110 $new =~ s,.*/,,;
111
d2656095
MCC
112 my $f="";
113
be600e5a
MCC
114 # usual reason for breakage: DT file moved around
115 if ($ref =~ /devicetree/) {
116 my $search = $new;
117 $search =~ s,^.*/,,;
118 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
119 if (!$f) {
120 # Manufacturer name may have changed
121 $search =~ s/^.*,//;
122 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
123 }
124 }
d2656095
MCC
125
126 # usual reason for breakage: file renamed to .rst
127 if (!$f) {
128 $new =~ s/\.txt$/.rst/;
129 $f=qx(find . -iname $new) if ($new);
130 }
131
e1f319fe
MCC
132 # usual reason for breakage: use dash or underline
133 if (!$f) {
134 $new =~ s/[-_]/[-_]/g;
135 $f=qx(find . -iname $new) if ($new);
136 }
137
be600e5a
MCC
138 # Wild guess: seek for the same name on another place
139 if (!$f) {
140 $f = qx(find . -iname $new) if ($new);
141 }
142
d2656095
MCC
143 my @find = split /\s+/, $f;
144
145 if (!$f) {
146 print STDERR "ERROR: Didn't find a replacement for $ref\n";
147 } elsif (scalar(@find) > 1) {
148 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
149 foreach my $j (@find) {
150 $j =~ s,^./,,;
151 print STDERR " $j\n";
152 }
153 } else {
154 $f = $find[0];
155 $f =~ s,^./,,;
156 print "INFO: Replacing $ref to $f\n";
157 foreach my $j (qx(git grep -l $ref)) {
158 qx(sed "s\@$ref\@$f\@g" -i $j);
159 }
160 }
161}