documentation-file-ref-check 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. # Check if exists, evaluating wildcards
  59. next if (grep -e, glob("$ref $fulref"));
  60. # Accept relative Documentation patches for tools/
  61. if ($f =~ m/tools/) {
  62. my $path = $f;
  63. $path =~ s,(.*)/.*,$1,;
  64. next if (grep -e, glob("$path/$ref $path/$fulref"));
  65. }
  66. if ($fix) {
  67. if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
  68. $broken_ref{$ref}++;
  69. }
  70. } else {
  71. print STDERR "$f: $fulref\n";
  72. }
  73. }
  74. }
  75. exit 0 if (!$fix);
  76. # Step 2: Seek for file name alternatives
  77. print "Auto-fixing broken references. Please double-check the results\n";
  78. foreach my $ref (keys %broken_ref) {
  79. my $new =$ref;
  80. # get just the basename
  81. $new =~ s,.*/,,;
  82. my $f="";
  83. # usual reason for breakage: DT file moved around
  84. if ($ref =~ /devicetree/) {
  85. my $search = $new;
  86. $search =~ s,^.*/,,;
  87. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  88. if (!$f) {
  89. # Manufacturer name may have changed
  90. $search =~ s/^.*,//;
  91. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  92. }
  93. }
  94. # usual reason for breakage: file renamed to .rst
  95. if (!$f) {
  96. $new =~ s/\.txt$/.rst/;
  97. $f=qx(find . -iname $new) if ($new);
  98. }
  99. # usual reason for breakage: use dash or underline
  100. if (!$f) {
  101. $new =~ s/[-_]/[-_]/g;
  102. $f=qx(find . -iname $new) if ($new);
  103. }
  104. # Wild guess: seek for the same name on another place
  105. if (!$f) {
  106. $f = qx(find . -iname $new) if ($new);
  107. }
  108. my @find = split /\s+/, $f;
  109. if (!$f) {
  110. print STDERR "ERROR: Didn't find a replacement for $ref\n";
  111. } elsif (scalar(@find) > 1) {
  112. print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
  113. foreach my $j (@find) {
  114. $j =~ s,^./,,;
  115. print STDERR " $j\n";
  116. }
  117. } else {
  118. $f = $find[0];
  119. $f =~ s,^./,,;
  120. print "INFO: Replacing $ref to $f\n";
  121. foreach my $j (qx(git grep -l $ref)) {
  122. qx(sed "s\@$ref\@$f\@g" -i $j);
  123. }
  124. }
  125. }