summaryrefslogtreecommitdiff
path: root/hack/markdown-preprocess-review
blob: a487265ad7821e0d3eb241f3b3bf42ec2021b417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl

(our $ME = $0) =~ s|^.*/||;

use v5.20;

our $DSM = 'docs/source/markdown';

my ($oldname, $newname);
my %oldname;
my %changed;
open my $git_diff, '-|', 'git', 'log', '-1', '-p'
    or die "$ME: Cannot fork: $!\n";
while (my $line = <$git_diff>) {
    chomp $line;

    if ($line =~ m!^\-\-\-\s+a/$DSM/(podman-\S+\.md(\.in)?)!) {
        $oldname = $1;
        $newname = undef;
    }
    elsif ($line =~ m!^\+\+\+\s+b/$DSM/(podman-\S+\.md(\.in)?)!) {
        $newname = $1;
        $oldname{$newname} = $oldname;
    }
    elsif ($newname) {
        if ($line =~ s/^-####\s+//) {
            $line =~ /^\*\*--(\S+?)\*\*/
                or die "$ME: in $newname: weird '$line'";
            $changed{$newname}{$1}{name} //= $1;
        }
        # Usually the same, but not for host.container and host.pod.md
        elsif ($line =~ /^\+\@\@option\s+(\S+)/) {
            my $optfile = $1;
            if ($optfile =~ /^(.*)\.\S+$/) {
                $changed{$newname}{$1}{name} = $optfile;
            }
        }
    }
}
close $git_diff;

# Pass 2: read each oldfile, parse changed options
for my $f (sort keys %changed) {
    my $oldfile = $oldname{$f};
    open my $git_fh, '-|', 'git', 'show', "HEAD^:$DSM/$oldfile"
        or die "$ME: Cannot fork: $!\n";
    my $opt;
    while (my $line = <$git_fh>) {
        if ($line =~ /^####\s+\*\*--(\S+?)\*\*/) {
            $opt = $1;
            if ($changed{$f}{$opt}) {
                $changed{$f}{$opt}{text} = $line;
            }
            else {
                undef $opt;
            }
        }
        elsif ($line =~ /^#/ || $line =~ /^\@\@option\s/) {
            undef $opt;
        }
        elsif ($opt) {
            $changed{$f}{$opt}{text} .= $line;
        }
    }
    close $git_fh
        or die "$ME: Error running git on $oldfile\n";
}

# Pass 3: write out files
my $tempdir = "/tmp/$ME.diffs";
system('rm', '-rf', $tempdir);
mkdir $tempdir, 0755;

for my $md_file (sort keys %changed) {
    for my $opt (sort keys %{$changed{$md_file}}) {
        my $d = "$tempdir/$changed{$md_file}{$opt}{name}";
        mkdir $d, 0755;

        my $outfile = "$d/$md_file";
        open my $fh, '>', $outfile
            or die "$ME: Cannot create $outfile: $!\n";
        # strip all trailing newlines
        (my $text = $changed{$md_file}{$opt}{text}) =~ s/\n+$/\n/s;
        print { $fh } $text;
        close $fh
            or die "$ME: Error writing $outfile: $!\n";

        my $new_text = "$DSM/options/$changed{$md_file}{$opt}{name}.md";
        die "$ME: $md_file: File does not exist: $new_text\n" if ! -e $new_text;
        system('cp', $new_text, "$d/zzz-chosen.md");
    }
}

# Now run diffuse
chdir $tempdir or die;
my @all_opts = glob("*");
for my $i (0..$#all_opts) {
    my $opt = $all_opts[$i];
    chdir "$tempdir/$opt"
        or die "??? Internal error, cannot cd $tempdir/$opt: $!";

    $| = 1; printf "--%s (%d/%d) ", $opt, $i+1, scalar(@all_opts);

    my @all_files = glob("*");
    if (all_files_identical(@all_files)) {
        pop @all_files;
        print "[identical between @all_files]\n";
        next;
    }

    # Prompt
    print "[Y/n/q] ";
    my $ans = <STDIN>;
    next if $ans =~ /^n/i;
    exit 0 if $ans =~ /^q/i;

    system("diffuse", "-w", glob("*")) == 0
        or die "Diffuse failed\n";
}


sub all_files_identical {
    my %sha;
    for my $f (@_) {
        my $result = qx{sha256sum $f};
        $result =~ /^([0-9a-f]+)\s/
            or die "Internal error: unexpected result from sha256sum $f: $result";
        $sha{$1}++;
    }

    return (keys(%sha) == 1);
}