scripts/documentation-file-ref-check: hint: dash or underline
[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
41 # Makefiles contain nasty expressions to parse docs
42 next if ($f =~ m/Makefile/);
43 # Skip this script
44 next if ($f eq $scriptname);
45
50440240 46 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*),) {
d2656095
MCC
47 my $prefix = $1;
48 my $ref = $2;
49 my $base = $2;
50
51 $ref =~ s/[\,\.]+$//;
52
53 my $fulref = "$prefix$ref";
54
55 $fulref =~ s/^(\<file|ref)://;
56 $fulref =~ s/^[\'\`]+//;
57 $fulref =~ s,^\$\(.*\)/,,;
58 $base =~ s,.*/,,;
59
60 # Remove URL false-positives
61 next if ($fulref =~ m/^http/);
62
63 # Check if exists, evaluating wildcards
64 next if (grep -e, glob("$ref $fulref"));
65
66 if ($fix) {
be600e5a 67 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
d2656095
MCC
68 $broken_ref{$ref}++;
69 }
70 } else {
71 print STDERR "$f: $fulref\n";
72 }
73 }
74}
75
76exit 0 if (!$fix);
77
78# Step 2: Seek for file name alternatives
79print "Auto-fixing broken references. Please double-check the results\n";
80
81foreach my $ref (keys %broken_ref) {
82 my $new =$ref;
83
84 # get just the basename
85 $new =~ s,.*/,,;
86
d2656095
MCC
87 my $f="";
88
be600e5a
MCC
89 # usual reason for breakage: DT file moved around
90 if ($ref =~ /devicetree/) {
91 my $search = $new;
92 $search =~ s,^.*/,,;
93 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
94 if (!$f) {
95 # Manufacturer name may have changed
96 $search =~ s/^.*,//;
97 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
98 }
99 }
d2656095
MCC
100
101 # usual reason for breakage: file renamed to .rst
102 if (!$f) {
103 $new =~ s/\.txt$/.rst/;
104 $f=qx(find . -iname $new) if ($new);
105 }
106
e1f319fe
MCC
107 # usual reason for breakage: use dash or underline
108 if (!$f) {
109 $new =~ s/[-_]/[-_]/g;
110 $f=qx(find . -iname $new) if ($new);
111 }
112
be600e5a
MCC
113 # Wild guess: seek for the same name on another place
114 if (!$f) {
115 $f = qx(find . -iname $new) if ($new);
116 }
117
d2656095
MCC
118 my @find = split /\s+/, $f;
119
120 if (!$f) {
121 print STDERR "ERROR: Didn't find a replacement for $ref\n";
122 } elsif (scalar(@find) > 1) {
123 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
124 foreach my $j (@find) {
125 $j =~ s,^./,,;
126 print STDERR " $j\n";
127 }
128 } else {
129 $f = $find[0];
130 $f =~ s,^./,,;
131 print "INFO: Replacing $ref to $f\n";
132 foreach my $j (qx(git grep -l $ref)) {
133 qx(sed "s\@$ref\@$f\@g" -i $j);
134 }
135 }
136}