Revert "ovl: fix relatime for directories"
[linux/fpc-iii.git] / scripts / documentation-file-ref-check
blob078999a3fdff7542ff52db288b96d9f0848e1c49
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: GPL-2.0
4 # Treewide grep for references to files under Documentation, and report
5 # non-existing files in stderr.
7 use warnings;
8 use strict;
9 use Getopt::Long qw(:config no_auto_abbrev);
11 my $scriptname = $0;
12 $scriptname =~ s,.*/([^/]+/),$1,;
14 # Parse arguments
15 my $help = 0;
16 my $fix = 0;
18 GetOptions(
19 'fix' => \$fix,
20 'h|help|usage' => \$help,
23 if ($help != 0) {
24 print "$scriptname [--help] [--fix]\n";
25 exit -1;
28 # Step 1: find broken references
29 print "Finding broken references. This may take a while... " if ($fix);
31 my %broken_ref;
33 open IN, "git grep 'Documentation/'|"
34 or die "Failed to run git grep";
35 while (<IN>) {
36 next if (!m/^([^:]+):(.*)/);
38 my $f = $1;
39 my $ln = $2;
41 # Makefiles and scripts contain nasty expressions to parse docs
42 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
44 # Skip this script
45 next if ($f eq $scriptname);
47 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
48 my $prefix = $1;
49 my $ref = $2;
50 my $base = $2;
51 my $extra = $3;
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/^{/);
58 # Remove footnotes at the end like:
59 # Documentation/devicetree/dt-object-internal.txt[1]
60 $ref =~ s/(txt|rst)\[\d+]$/$1/;
62 # Remove ending ']' without any '['
63 $ref =~ s/\].*// if (!($ref =~ m/\[/));
65 # Remove puntuation marks at the end
66 $ref =~ s/[\,\.]+$//;
68 my $fulref = "$prefix$ref";
70 $fulref =~ s/^(\<file|ref)://;
71 $fulref =~ s/^[\'\`]+//;
72 $fulref =~ s,^\$\(.*\)/,,;
73 $base =~ s,.*/,,;
75 # Remove URL false-positives
76 next if ($fulref =~ m/^http/);
78 # Check if exists, evaluating wildcards
79 next if (grep -e, glob("$ref $fulref"));
81 # Accept relative Documentation patches for tools/
82 if ($f =~ m/tools/) {
83 my $path = $f;
84 $path =~ s,(.*)/.*,$1,;
85 next if (grep -e, glob("$path/$ref $path/$fulref"));
88 if ($fix) {
89 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
90 $broken_ref{$ref}++;
92 } else {
93 print STDERR "$f: $fulref\n";
98 exit 0 if (!$fix);
100 # Step 2: Seek for file name alternatives
101 print "Auto-fixing broken references. Please double-check the results\n";
103 foreach my $ref (keys %broken_ref) {
104 my $new =$ref;
106 # get just the basename
107 $new =~ s,.*/,,;
109 my $f="";
111 # usual reason for breakage: DT file moved around
112 if ($ref =~ /devicetree/) {
113 my $search = $new;
114 $search =~ s,^.*/,,;
115 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
116 if (!$f) {
117 # Manufacturer name may have changed
118 $search =~ s/^.*,//;
119 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
123 # usual reason for breakage: file renamed to .rst
124 if (!$f) {
125 $new =~ s/\.txt$/.rst/;
126 $f=qx(find . -iname $new) if ($new);
129 # usual reason for breakage: use dash or underline
130 if (!$f) {
131 $new =~ s/[-_]/[-_]/g;
132 $f=qx(find . -iname $new) if ($new);
135 # Wild guess: seek for the same name on another place
136 if (!$f) {
137 $f = qx(find . -iname $new) if ($new);
140 my @find = split /\s+/, $f;
142 if (!$f) {
143 print STDERR "ERROR: Didn't find a replacement for $ref\n";
144 } elsif (scalar(@find) > 1) {
145 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
146 foreach my $j (@find) {
147 $j =~ s,^./,,;
148 print STDERR " $j\n";
150 } else {
151 $f = $find[0];
152 $f =~ s,^./,,;
153 print "INFO: Replacing $ref to $f\n";
154 foreach my $j (qx(git grep -l $ref)) {
155 qx(sed "s\@$ref\@$f\@g" -i $j);