3
(our $ME = $0) =~ s|^.*/||;
7
our $DSM = 'docs/source/markdown';
9
my ($oldname, $newname);
12
open my $git_diff, '-|', 'git', 'log', '-1', '-p'
13
or die "$ME: Cannot fork: $!\n";
14
while (my $line = <$git_diff>) {
17
if ($line =~ m!^\-\-\-\s+a/$DSM/(podman-\S+\.md(\.in)?)!) {
21
elsif ($line =~ m!^\+\+\+\s+b/$DSM/(podman-\S+\.md(\.in)?)!) {
23
$oldname{$newname} = $oldname;
26
if ($line =~ s/^-####\s+//) {
27
$line =~ /^\*\*--(\S+?)\*\*/
28
or die "$ME: in $newname: weird '$line'";
29
$changed{$newname}{$1}{name} //= $1;
31
# Usually the same, but not for host.container and host.pod.md
32
elsif ($line =~ /^\+\@\@option\s+(\S+)/) {
34
if ($optfile =~ /^(.*)\.\S+$/) {
35
$changed{$newname}{$1}{name} = $optfile;
42
# Pass 2: read each oldfile, parse changed options
43
for my $f (sort keys %changed) {
44
my $oldfile = $oldname{$f};
45
open my $git_fh, '-|', 'git', 'show', "HEAD^:$DSM/$oldfile"
46
or die "$ME: Cannot fork: $!\n";
48
while (my $line = <$git_fh>) {
49
if ($line =~ /^####\s+\*\*--(\S+?)\*\*/) {
51
if ($changed{$f}{$opt}) {
52
$changed{$f}{$opt}{text} = $line;
58
elsif ($line =~ /^#/ || $line =~ /^\@\@option\s/) {
62
$changed{$f}{$opt}{text} .= $line;
66
or die "$ME: Error running git on $oldfile\n";
69
# Pass 3: write out files
70
my $tempdir = "/tmp/$ME.diffs";
71
system('rm', '-rf', $tempdir);
74
for my $md_file (sort keys %changed) {
75
for my $opt (sort keys %{$changed{$md_file}}) {
76
my $d = "$tempdir/$changed{$md_file}{$opt}{name}";
79
my $outfile = "$d/$md_file";
80
open my $fh, '>', $outfile
81
or die "$ME: Cannot create $outfile: $!\n";
82
# strip all trailing newlines
83
(my $text = $changed{$md_file}{$opt}{text}) =~ s/\n+$/\n/s;
86
or die "$ME: Error writing $outfile: $!\n";
88
my $new_text = "$DSM/options/$changed{$md_file}{$opt}{name}.md";
89
die "$ME: $md_file: File does not exist: $new_text\n" if ! -e $new_text;
90
system('cp', $new_text, "$d/zzz-chosen.md");
96
my @all_opts = glob("*");
97
for my $i (0..$#all_opts) {
98
my $opt = $all_opts[$i];
100
or die "??? Internal error, cannot cd $tempdir/$opt: $!";
102
$| = 1; printf "--%s (%d/%d) ", $opt, $i+1, scalar(@all_opts);
104
my @all_files = glob("*");
105
if (all_files_identical(@all_files)) {
107
print "[identical between @all_files]\n";
114
next if $ans =~ /^n/i;
115
exit 0 if $ans =~ /^q/i;
117
# Try to cull the files (remove identical ones)
118
my @files = glob("*");
119
my $winner = pop @files;
122
system('cmp', '-s', $f, $winner);
124
print "[ $f is the one we went with; removing from list ]\n";
129
system('wdiff', '-1', '-2', '-3', $f, $winner);
131
print "[ $f is whitespace-identical with what we went with ]\n";
137
# Recompute @files, in case some were deleted above
138
@files = glob("*"); pop @files;
140
for (my $i=0; $i < $#files; $i++) {
144
for (my $j=$i+1; $j <= $#files; $j++) {
148
system('wdiff', '-1', '-2', '-3', $f1, $f2);
150
print "[ $f2 : removing, it =~ $f1 ]\n";
156
# Recompute @files, in case some were deleted above
159
# diffuse works great for 3-4 files, passable for 5, not at all for >5
161
system("diffuse", "-w", @files) == 0
162
or die "Diffuse failed\n";
165
# Too many files. Go by threes.
166
my $winner = pop @files;
167
for (my $i=0; $i < @files; $i += 3) {
168
system("diffuse", "-w", @files[$i..$i+2], $winner);
174
sub all_files_identical {
177
my $result = qx{sha256sum $f};
178
$result =~ /^([0-9a-f]+)\s/
179
or die "Internal error: unexpected result from sha256sum $f: $result";
183
return (keys(%sha) == 1);