documentation-file-ref-check 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Treewide grep for references to files under Documentation, and report
  5. # non-existing files in stderr.
  6. use warnings;
  7. use strict;
  8. use Getopt::Long qw(:config no_auto_abbrev);
  9. # NOTE: only add things here when the file was gone, but the text wants
  10. # to mention a past documentation file, for example, to give credits for
  11. # the original work.
  12. my %false_positives = (
  13. "Documentation/scsi/scsi_mid_low_api.txt" => "Documentation/Configure.help",
  14. "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c",
  15. );
  16. my $scriptname = $0;
  17. $scriptname =~ s,.*/([^/]+/),$1,;
  18. # Parse arguments
  19. my $help = 0;
  20. my $fix = 0;
  21. my $warn = 0;
  22. if (! -d ".git") {
  23. printf "Warning: can't check if file exists, as this is not a git tree";
  24. exit 0;
  25. }
  26. GetOptions(
  27. 'fix' => \$fix,
  28. 'warn' => \$warn,
  29. 'h|help|usage' => \$help,
  30. );
  31. if ($help != 0) {
  32. print "$scriptname [--help] [--fix]\n";
  33. exit -1;
  34. }
  35. # Step 1: find broken references
  36. print "Finding broken references. This may take a while... " if ($fix);
  37. my %broken_ref;
  38. my $doc_fix = 0;
  39. open IN, "git grep ':doc:\`' Documentation/|"
  40. or die "Failed to run git grep";
  41. while (<IN>) {
  42. next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
  43. my $d = $1;
  44. my $doc_ref = $2;
  45. my $f = $doc_ref;
  46. $d =~ s,(.*/).*,$1,;
  47. $f =~ s,.*\<([^\>]+)\>,$1,;
  48. $f ="$d$f.rst";
  49. next if (grep -e, glob("$f"));
  50. if ($fix && !$doc_fix) {
  51. print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
  52. }
  53. $doc_fix++;
  54. print STDERR "$f: :doc:`$doc_ref`\n";
  55. }
  56. close IN;
  57. open IN, "git grep 'Documentation/'|"
  58. or die "Failed to run git grep";
  59. while (<IN>) {
  60. next if (!m/^([^:]+):(.*)/);
  61. my $f = $1;
  62. my $ln = $2;
  63. # On linux-next, discard the Next/ directory
  64. next if ($f =~ m,^Next/,);
  65. # Makefiles and scripts contain nasty expressions to parse docs
  66. next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
  67. # Skip this script
  68. next if ($f eq $scriptname);
  69. # Ignore the dir where documentation will be built
  70. next if ($ln =~ m,\b(\S*)Documentation/output,);
  71. if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
  72. my $prefix = $1;
  73. my $ref = $2;
  74. my $base = $2;
  75. my $extra = $3;
  76. # some file references are like:
  77. # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
  78. # For now, ignore them
  79. next if ($extra =~ m/^{/);
  80. # Remove footnotes at the end like:
  81. # Documentation/devicetree/dt-object-internal.txt[1]
  82. $ref =~ s/(txt|rst)\[\d+]$/$1/;
  83. # Remove ending ']' without any '['
  84. $ref =~ s/\].*// if (!($ref =~ m/\[/));
  85. # Remove puntuation marks at the end
  86. $ref =~ s/[\,\.]+$//;
  87. my $fulref = "$prefix$ref";
  88. $fulref =~ s/^(\<file|ref)://;
  89. $fulref =~ s/^[\'\`]+//;
  90. $fulref =~ s,^\$\(.*\)/,,;
  91. $base =~ s,.*/,,;
  92. # Remove URL false-positives
  93. next if ($fulref =~ m/^http/);
  94. # Remove sched-pelt false-positive
  95. next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
  96. # Discard some build examples from Documentation/target/tcm_mod_builder.rst
  97. next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
  98. # Check if exists, evaluating wildcards
  99. next if (grep -e, glob("$ref $fulref"));
  100. # Accept relative Documentation patches for tools/
  101. if ($f =~ m/tools/) {
  102. my $path = $f;
  103. $path =~ s,(.*)/.*,$1,;
  104. next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
  105. }
  106. # Discard known false-positives
  107. if (defined($false_positives{$f})) {
  108. next if ($false_positives{$f} eq $fulref);
  109. }
  110. if ($fix) {
  111. if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
  112. $broken_ref{$ref}++;
  113. }
  114. } elsif ($warn) {
  115. print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
  116. } else {
  117. print STDERR "$f: $fulref\n";
  118. }
  119. }
  120. }
  121. close IN;
  122. exit 0 if (!$fix);
  123. # Step 2: Seek for file name alternatives
  124. print "Auto-fixing broken references. Please double-check the results\n";
  125. foreach my $ref (keys %broken_ref) {
  126. my $new =$ref;
  127. my $basedir = ".";
  128. # On translations, only seek inside the translations directory
  129. $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
  130. # get just the basename
  131. $new =~ s,.*/,,;
  132. my $f="";
  133. # usual reason for breakage: DT file moved around
  134. if ($ref =~ /devicetree/) {
  135. # usual reason for breakage: DT file renamed to .yaml
  136. if (!$f) {
  137. my $new_ref = $ref;
  138. $new_ref =~ s/\.txt$/.yaml/;
  139. $f=$new_ref if (-f $new_ref);
  140. }
  141. if (!$f) {
  142. my $search = $new;
  143. $search =~ s,^.*/,,;
  144. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  145. if (!$f) {
  146. # Manufacturer name may have changed
  147. $search =~ s/^.*,//;
  148. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  149. }
  150. }
  151. }
  152. # usual reason for breakage: file renamed to .rst
  153. if (!$f) {
  154. $new =~ s/\.txt$/.rst/;
  155. $f=qx(find $basedir -iname $new) if ($new);
  156. }
  157. # usual reason for breakage: use dash or underline
  158. if (!$f) {
  159. $new =~ s/[-_]/[-_]/g;
  160. $f=qx(find $basedir -iname $new) if ($new);
  161. }
  162. # Wild guess: seek for the same name on another place
  163. if (!$f) {
  164. $f = qx(find $basedir -iname $new) if ($new);
  165. }
  166. my @find = split /\s+/, $f;
  167. if (!$f) {
  168. print STDERR "ERROR: Didn't find a replacement for $ref\n";
  169. } elsif (scalar(@find) > 1) {
  170. print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
  171. foreach my $j (@find) {
  172. $j =~ s,^./,,;
  173. print STDERR " $j\n";
  174. }
  175. } else {
  176. $f = $find[0];
  177. $f =~ s,^./,,;
  178. print "INFO: Replacing $ref to $f\n";
  179. foreach my $j (qx(git grep -l $ref)) {
  180. qx(sed "s\@$ref\@$f\@g" -i $j);
  181. }
  182. }
  183. }