documentation-file-ref-check 3.7 KB

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