aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/documentation-file-ref-check
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab+samsung@kernel.org>2018-05-09 09:18:49 -0400
committerJonathan Corbet <corbet@lwn.net>2018-05-10 17:42:43 -0400
commitd26560950b6ba6454c11cd978d3e6bb4d38430e8 (patch)
tree42c6462dd3710e16f07b6f3a05c62cbc1a8e2832 /scripts/documentation-file-ref-check
parent02a43659e15893a6611cfc10dc7aae1746eb0cdc (diff)
scripts/documentation-file-ref-check: rewrite it in perl with auto-fix mode
The original shell script works, but: 1) it is too slow; 2) it is hard to exclude rejex patterns Convert it to perl. Here, the new version is able to check the entire tree in less than a second (after cached): real 0m0,284s user 0m0,668s sys 0m0,778s The old version takes more than a minute to complete (also after cached): real 1m17,905s user 0m25,583s sys 0m55,334s It also produce less false-positives (if any). The new script also contains an auto-fix mode. Usually, file references get lost when they're moved to some other place and/or renamed to .rst. Add an experimental mode to auto-fix those. Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'scripts/documentation-file-ref-check')
-rwxr-xr-xscripts/documentation-file-ref-check125
1 files changed, 113 insertions, 12 deletions
diff --git a/scripts/documentation-file-ref-check b/scripts/documentation-file-ref-check
index bc1659900e89..2520bc14ffac 100755
--- a/scripts/documentation-file-ref-check
+++ b/scripts/documentation-file-ref-check
@@ -1,15 +1,116 @@
1#!/bin/sh 1#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3#
2# Treewide grep for references to files under Documentation, and report 4# Treewide grep for references to files under Documentation, and report
3# non-existing files in stderr. 5# non-existing files in stderr.
4 6
5for f in $(git ls-files); do 7use warnings;
6 for ref in $(grep -ho "Documentation/[A-Za-z0-9_.,~/*+-]*" "$f"); do 8use strict;
7 # presume trailing . and , are not part of the name 9use Getopt::Long qw(:config no_auto_abbrev);
8 ref=${ref%%[.,]} 10
9 11my $scriptname = $0;
10 # use ls to handle wildcards 12$scriptname =~ s,.*/([^/]+/),$1,;
11 if ! ls $ref >/dev/null 2>&1; then 13
12 echo "$f: $ref" >&2 14# Parse arguments
13 fi 15my $help = 0;
14 done 16my $fix = 0;
15done 17
18GetOptions(
19 'fix' => \$fix,
20 'h|help|usage' => \$help,
21);
22
23if ($help != 0) {
24 print "$scriptname [--help] [--fix-rst]\n";
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
46 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*+-]*),) {
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) {
67 if (!($ref =~ m/(devicetree|scripts|Kconfig|Kbuild)/)) {
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
87 # Seek for the same name on another place, as it may have been moved
88 my $f="";
89
90 $f = qx(find . -iname $new) if ($new);
91
92 # usual reason for breakage: file renamed to .rst
93 if (!$f) {
94 $new =~ s/\.txt$/.rst/;
95 $f=qx(find . -iname $new) if ($new);
96 }
97
98 my @find = split /\s+/, $f;
99
100 if (!$f) {
101 print STDERR "ERROR: Didn't find a replacement for $ref\n";
102 } elsif (scalar(@find) > 1) {
103 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
104 foreach my $j (@find) {
105 $j =~ s,^./,,;
106 print STDERR " $j\n";
107 }
108 } else {
109 $f = $find[0];
110 $f =~ s,^./,,;
111 print "INFO: Replacing $ref to $f\n";
112 foreach my $j (qx(git grep -l $ref)) {
113 qx(sed "s\@$ref\@$f\@g" -i $j);
114 }
115 }
116}