162306a36Sopenharmony_ci#!/usr/bin/env perl
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
362306a36Sopenharmony_ci#
462306a36Sopenharmony_ci# Treewide grep for references to files under Documentation, and report
562306a36Sopenharmony_ci# non-existing files in stderr.
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciuse warnings;
862306a36Sopenharmony_ciuse strict;
962306a36Sopenharmony_ciuse Getopt::Long qw(:config no_auto_abbrev);
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci# NOTE: only add things here when the file was gone, but the text wants
1262306a36Sopenharmony_ci# to mention a past documentation file, for example, to give credits for
1362306a36Sopenharmony_ci# the original work.
1462306a36Sopenharmony_cimy %false_positives = (
1562306a36Sopenharmony_ci	"Documentation/scsi/scsi_mid_low_api.rst" => "Documentation/Configure.help",
1662306a36Sopenharmony_ci	"drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c",
1762306a36Sopenharmony_ci);
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cimy $scriptname = $0;
2062306a36Sopenharmony_ci$scriptname =~ s,.*/([^/]+/),$1,;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci# Parse arguments
2362306a36Sopenharmony_cimy $help = 0;
2462306a36Sopenharmony_cimy $fix = 0;
2562306a36Sopenharmony_cimy $warn = 0;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ciif (! -e ".git") {
2862306a36Sopenharmony_ci	printf "Warning: can't check if file exists, as this is not a git tree\n";
2962306a36Sopenharmony_ci	exit 0;
3062306a36Sopenharmony_ci}
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciGetOptions(
3362306a36Sopenharmony_ci	'fix' => \$fix,
3462306a36Sopenharmony_ci	'warn' => \$warn,
3562306a36Sopenharmony_ci	'h|help|usage' => \$help,
3662306a36Sopenharmony_ci);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciif ($help != 0) {
3962306a36Sopenharmony_ci    print "$scriptname [--help] [--fix]\n";
4062306a36Sopenharmony_ci    exit -1;
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci# Step 1: find broken references
4462306a36Sopenharmony_ciprint "Finding broken references. This may take a while...  " if ($fix);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cimy %broken_ref;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cimy $doc_fix = 0;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciopen IN, "git grep ':doc:\`' Documentation/|"
5162306a36Sopenharmony_ci     or die "Failed to run git grep";
5262306a36Sopenharmony_ciwhile (<IN>) {
5362306a36Sopenharmony_ci	next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
5462306a36Sopenharmony_ci	next if (m,sphinx/,);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	my $file = $1;
5762306a36Sopenharmony_ci	my $d = $1;
5862306a36Sopenharmony_ci	my $doc_ref = $2;
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	my $f = $doc_ref;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	$d =~ s,(.*/).*,$1,;
6362306a36Sopenharmony_ci	$f =~ s,.*\<([^\>]+)\>,$1,;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	if ($f =~ m,^/,) {
6662306a36Sopenharmony_ci		$f = "$f.rst";
6762306a36Sopenharmony_ci		$f =~ s,^/,Documentation/,;
6862306a36Sopenharmony_ci	} else {
6962306a36Sopenharmony_ci		$f = "$d$f.rst";
7062306a36Sopenharmony_ci	}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	next if (grep -e, glob("$f"));
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if ($fix && !$doc_fix) {
7562306a36Sopenharmony_ci		print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
7662306a36Sopenharmony_ci	}
7762306a36Sopenharmony_ci	$doc_fix++;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	print STDERR "$file: :doc:`$doc_ref`\n";
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ciclose IN;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciopen IN, "git grep 'Documentation/'|"
8462306a36Sopenharmony_ci     or die "Failed to run git grep";
8562306a36Sopenharmony_ciwhile (<IN>) {
8662306a36Sopenharmony_ci	next if (!m/^([^:]+):(.*)/);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	my $f = $1;
8962306a36Sopenharmony_ci	my $ln = $2;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	# On linux-next, discard the Next/ directory
9262306a36Sopenharmony_ci	next if ($f =~ m,^Next/,);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	# Makefiles and scripts contain nasty expressions to parse docs
9562306a36Sopenharmony_ci	next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	# It doesn't make sense to parse hidden files
9862306a36Sopenharmony_ci	next if ($f =~ m#/\.#);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	# Skip this script
10162306a36Sopenharmony_ci	next if ($f eq $scriptname);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	# Ignore the dir where documentation will be built
10462306a36Sopenharmony_ci	next if ($ln =~ m,\b(\S*)Documentation/output,);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
10762306a36Sopenharmony_ci		my $prefix = $1;
10862306a36Sopenharmony_ci		my $ref = $2;
10962306a36Sopenharmony_ci		my $base = $2;
11062306a36Sopenharmony_ci		my $extra = $3;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci		# some file references are like:
11362306a36Sopenharmony_ci		# /usr/src/linux/Documentation/DMA-{API,mapping}.txt
11462306a36Sopenharmony_ci		# For now, ignore them
11562306a36Sopenharmony_ci		next if ($extra =~ m/^{/);
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci		# Remove footnotes at the end like:
11862306a36Sopenharmony_ci		# Documentation/devicetree/dt-object-internal.txt[1]
11962306a36Sopenharmony_ci		$ref =~ s/(txt|rst)\[\d+]$/$1/;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci		# Remove ending ']' without any '['
12262306a36Sopenharmony_ci		$ref =~ s/\].*// if (!($ref =~ m/\[/));
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci		# Remove puntuation marks at the end
12562306a36Sopenharmony_ci		$ref =~ s/[\,\.]+$//;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci		my $fulref = "$prefix$ref";
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci		$fulref =~ s/^(\<file|ref)://;
13062306a36Sopenharmony_ci		$fulref =~ s/^[\'\`]+//;
13162306a36Sopenharmony_ci		$fulref =~ s,^\$\(.*\)/,,;
13262306a36Sopenharmony_ci		$base =~ s,.*/,,;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci		# Remove URL false-positives
13562306a36Sopenharmony_ci		next if ($fulref =~ m/^http/);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci		# Remove sched-pelt false-positive
13862306a36Sopenharmony_ci		next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		# Discard some build examples from Documentation/target/tcm_mod_builder.rst
14162306a36Sopenharmony_ci		next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci		# Check if exists, evaluating wildcards
14462306a36Sopenharmony_ci		next if (grep -e, glob("$ref $fulref"));
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci		# Accept relative Documentation patches for tools/
14762306a36Sopenharmony_ci		if ($f =~ m/tools/) {
14862306a36Sopenharmony_ci			my $path = $f;
14962306a36Sopenharmony_ci			$path =~ s,(.*)/.*,$1,;
15062306a36Sopenharmony_ci			$path =~ s,testing/selftests/bpf,bpf/bpftool,;
15162306a36Sopenharmony_ci			next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
15262306a36Sopenharmony_ci		}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci		# Discard known false-positives
15562306a36Sopenharmony_ci		if (defined($false_positives{$f})) {
15662306a36Sopenharmony_ci			next if ($false_positives{$f} eq $fulref);
15762306a36Sopenharmony_ci		}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci		if ($fix) {
16062306a36Sopenharmony_ci			if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
16162306a36Sopenharmony_ci				$broken_ref{$ref}++;
16262306a36Sopenharmony_ci			}
16362306a36Sopenharmony_ci		} elsif ($warn) {
16462306a36Sopenharmony_ci			print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
16562306a36Sopenharmony_ci		} else {
16662306a36Sopenharmony_ci			print STDERR "$f: $fulref\n";
16762306a36Sopenharmony_ci		}
16862306a36Sopenharmony_ci	}
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ciclose IN;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciexit 0 if (!$fix);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci# Step 2: Seek for file name alternatives
17562306a36Sopenharmony_ciprint "Auto-fixing broken references. Please double-check the results\n";
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciforeach my $ref (keys %broken_ref) {
17862306a36Sopenharmony_ci	my $new =$ref;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	my $basedir = ".";
18162306a36Sopenharmony_ci	# On translations, only seek inside the translations directory
18262306a36Sopenharmony_ci	$basedir  = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	# get just the basename
18562306a36Sopenharmony_ci	$new =~ s,.*/,,;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	my $f="";
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	# usual reason for breakage: DT file moved around
19062306a36Sopenharmony_ci	if ($ref =~ /devicetree/) {
19162306a36Sopenharmony_ci		# usual reason for breakage: DT file renamed to .yaml
19262306a36Sopenharmony_ci		if (!$f) {
19362306a36Sopenharmony_ci			my $new_ref = $ref;
19462306a36Sopenharmony_ci			$new_ref =~ s/\.txt$/.yaml/;
19562306a36Sopenharmony_ci			$f=$new_ref if (-f $new_ref);
19662306a36Sopenharmony_ci		}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci		if (!$f) {
19962306a36Sopenharmony_ci			my $search = $new;
20062306a36Sopenharmony_ci			$search =~ s,^.*/,,;
20162306a36Sopenharmony_ci			$f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
20262306a36Sopenharmony_ci			if (!$f) {
20362306a36Sopenharmony_ci				# Manufacturer name may have changed
20462306a36Sopenharmony_ci				$search =~ s/^.*,//;
20562306a36Sopenharmony_ci				$f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
20662306a36Sopenharmony_ci			}
20762306a36Sopenharmony_ci		}
20862306a36Sopenharmony_ci	}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	# usual reason for breakage: file renamed to .rst
21162306a36Sopenharmony_ci	if (!$f) {
21262306a36Sopenharmony_ci		$new =~ s/\.txt$/.rst/;
21362306a36Sopenharmony_ci		$f=qx(find $basedir -iname $new) if ($new);
21462306a36Sopenharmony_ci	}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	# usual reason for breakage: use dash or underline
21762306a36Sopenharmony_ci	if (!$f) {
21862306a36Sopenharmony_ci		$new =~ s/[-_]/[-_]/g;
21962306a36Sopenharmony_ci		$f=qx(find $basedir -iname $new) if ($new);
22062306a36Sopenharmony_ci	}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	# Wild guess: seek for the same name on another place
22362306a36Sopenharmony_ci	if (!$f) {
22462306a36Sopenharmony_ci		$f = qx(find $basedir -iname $new) if ($new);
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	my @find = split /\s+/, $f;
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	if (!$f) {
23062306a36Sopenharmony_ci		print STDERR "ERROR: Didn't find a replacement for $ref\n";
23162306a36Sopenharmony_ci	} elsif (scalar(@find) > 1) {
23262306a36Sopenharmony_ci		print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
23362306a36Sopenharmony_ci		foreach my $j (@find) {
23462306a36Sopenharmony_ci			$j =~ s,^./,,;
23562306a36Sopenharmony_ci			print STDERR "    $j\n";
23662306a36Sopenharmony_ci		}
23762306a36Sopenharmony_ci	} else {
23862306a36Sopenharmony_ci		$f = $find[0];
23962306a36Sopenharmony_ci		$f =~ s,^./,,;
24062306a36Sopenharmony_ci		print "INFO: Replacing $ref to $f\n";
24162306a36Sopenharmony_ci		foreach my $j (qx(git grep -l $ref)) {
24262306a36Sopenharmony_ci			qx(sed "s\@$ref\@$f\@g" -i $j);
24362306a36Sopenharmony_ci		}
24462306a36Sopenharmony_ci	}
24562306a36Sopenharmony_ci}
246