documentation-file-ref-check 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-rst]\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 contain nasty expressions to parse docs
  32. next if ($f =~ m/Makefile/);
  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. $ref =~ s/[\,\.]+$//;
  40. my $fulref = "$prefix$ref";
  41. $fulref =~ s/^(\<file|ref)://;
  42. $fulref =~ s/^[\'\`]+//;
  43. $fulref =~ s,^\$\(.*\)/,,;
  44. $base =~ s,.*/,,;
  45. # Remove URL false-positives
  46. next if ($fulref =~ m/^http/);
  47. # Check if exists, evaluating wildcards
  48. next if (grep -e, glob("$ref $fulref"));
  49. if ($fix) {
  50. if (!($ref =~ m/(devicetree|scripts|Kconfig|Kbuild)/)) {
  51. $broken_ref{$ref}++;
  52. }
  53. } else {
  54. print STDERR "$f: $fulref\n";
  55. }
  56. }
  57. }
  58. exit 0 if (!$fix);
  59. # Step 2: Seek for file name alternatives
  60. print "Auto-fixing broken references. Please double-check the results\n";
  61. foreach my $ref (keys %broken_ref) {
  62. my $new =$ref;
  63. # get just the basename
  64. $new =~ s,.*/,,;
  65. # Seek for the same name on another place, as it may have been moved
  66. my $f="";
  67. $f = qx(find . -iname $new) if ($new);
  68. # usual reason for breakage: file renamed to .rst
  69. if (!$f) {
  70. $new =~ s/\.txt$/.rst/;
  71. $f=qx(find . -iname $new) if ($new);
  72. }
  73. my @find = split /\s+/, $f;
  74. if (!$f) {
  75. print STDERR "ERROR: Didn't find a replacement for $ref\n";
  76. } elsif (scalar(@find) > 1) {
  77. print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
  78. foreach my $j (@find) {
  79. $j =~ s,^./,,;
  80. print STDERR " $j\n";
  81. }
  82. } else {
  83. $f = $find[0];
  84. $f =~ s,^./,,;
  85. print "INFO: Replacing $ref to $f\n";
  86. foreach my $j (qx(git grep -l $ref)) {
  87. qx(sed "s\@$ref\@$f\@g" -i $j);
  88. }
  89. }
  90. }