Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 254 lines (230 sloc) 7.256 kb
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
package cherrymaint;
use Dancer;
use HTML::Entities;
use Socket qw/inet_aton/;
use List::Util qw/min max/;
use Fcntl qw/LOCK_EX LOCK_UN/;

my $BLEADGITHOME = config->{gitroot};
my $TESTING = config->{testing}; # single-user mode, for testing
my $GIT = config->{gitpath};
my $DATAFILE = config->{datafile};
my $LOCK = config->{lock};
my $STARTPOINT = config->{startpoint};
my $ENDPOINT = config->{endpoint};

$_ = (glob)[0] for $GIT, $BLEADGITHOME, $DATAFILE, $LOCK;

chdir $BLEADGITHOME or die "Can't chdir to $BLEADGITHOME: $!\n";

sub any_eq {
    my $str = shift;
    $str eq $_ and return 1 for @_;
    0;
}

sub load_datafile {
    my $data = {};
    open my $fh, '<', $DATAFILE or die "Can't open $DATAFILE: $!";
    while (<$fh>) {
        chomp;
        my ($commit, $value, @votes) = split ' ';
        $data->{$commit} = [
            0 + $value,
            \@votes,
        ];
    }
    close $fh;
    return $data;
}

sub save_datafile {
    my ($data) = @_;
    open my $fh, '>', $DATAFILE or die "Can't open $DATAFILE: $!";
    for my $k (keys %$data) {
        next unless $data->{$k};
        my ($value, $votes) = @{ $data->{$k} };
        my @votes = @{ $votes || [] };
        print $fh "$k $value @votes\n";
    }
    close $fh;
}

sub lock_datafile {
    my ($id) = @_;
    open my $fh, '>', $LOCK or die $!;
    flock $fh, LOCK_EX or die $!;
    print $fh "$id\n";
    return bless { fh => $fh }, 'cherrymaint::lock';
}

sub cherrymaint::lock::DESTROY {
    my ($lock) = @_;
    my $fh = $lock->{fh};
    flock $fh, LOCK_UN or die $!;
    close $fh;
}

sub unlock_datafile {
    $_[0]->DESTROY;
}

sub get_user {
    if ($TESTING) {
        my ($user) = getpwuid $<;
        return $user;
    }
    my ($addr, $port) = @_;
    $addr = sprintf '%08X', unpack 'L', inet_aton $addr;
    $port = sprintf '%04X', $port;
    my $remote = join ':', $addr, $port;
    open my $tcp, '<', '/proc/net/tcp' or die $!;
    while (<$tcp>) {
        next unless /^\s*\d+:/;
        my @parts = split ' ';
        next unless $#parts >= 7 and $parts[1] eq $remote;
        my $user = getpwuid $parts[7];
        die 'Invalid user' unless defined $user and $user !~ /\s/;
        return $user;
    }
    die "Couldn't find the current user";
    return;
}

my %cache;

sub get_log {
    my $from = qx($GIT log --pretty=format:%H -1 $STARTPOINT);
    my $to = qx($GIT log --pretty=format:%H -1 $ENDPOINT);
    my $cached = $cache{$from}{$to};
    return @$cached if defined $cached;

    local $ENV{PAGER} = '';
    open my $fh, '-|', $GIT, qw(log --no-color --oneline --no-merges),
                             "$from..$to"
         or die $!;
    my @log;
    while (<$fh>) {
        chomp;
        my ($commit, $message) = split / /, $_, 2;
        $commit =~ /^[0-9a-f]+$/ or die;
        utf8::decode $message;
        $message = encode_entities($message);
        push @log, [ $commit, $message ];
    }
    %cache = (); # Keep only one entry in the cache
    $cache{$from}{$to} = \@log;
    return @log;
}

get '/' => sub {
    my $page = params->{page};
    $page = 0 unless defined $page;
    $page =~ /^[0-9]+$/ or die 'Invalid page number';

    my $limit = params->{limit};
    $limit = 0 unless defined $limit;
    $limit =~ /^[0-9]+$/ or die 'Invalid limit';

    my $user = get_user(@ENV{qw/REMOTE_ADDR REMOTE_PORT/});
    my @log = get_log;
    my $data = do {
        my $lock = lock_datafile("$$-$user");
        load_datafile;
    };

    my (@pages, $current_page);
    if ($limit) {
        my $num = 0;
        for (my $start = 0; $start <= $#log; $start += $limit) {
            my $end = $start + $limit - 1;
            $end = $#log if $end > $#log;
            if ($num == $page) {
                $current_page = [ $start => $end ];
            }
            push @pages, [ $num, $num == $page ? 1 : 0 ];
            ++$num;
        }
        unless ($current_page) { # Page was out of bounds
            $page = 0;
            $current_page = [ 0 => -1 ];
        }
    } else {
        $page = 0;
        $current_page = [ 0 => $#log ];
        @pages = [ 0, 1 ];
    }

    my ($start, $end) = @$current_page;
    my @commits;
    for my $i ($start .. $end) {
        next if $i > $#log;
        my $log = $log[$i];
        my $commit = $log->[0];
        my $status = $data->{$commit}->[0] || 0;
        my $votes = $data->{$commit}->[1];
        push @commits, {
            sha1 => $commit,
            msg => $log->[1],
            status => $status,
            votes => $votes,
        };
    }
    template 'index', {
        commits => \@commits,
        user => $user,
        limit => $limit,
        cur_page => $page,
        last_page => $#pages,
        pages => \@pages,
    };
};

get '/mark' => sub {
    my $commit = params->{commit};
    $commit =~ /^[0-9a-f]+$/ or die 'Invalid commit sha1';

    my $value = params->{value};
    $value =~ /^[0-6]$/ or die 'Invalid grade value';

    my $user = get_user(@ENV{qw/REMOTE_ADDR REMOTE_PORT/});
    my $lock = lock_datafile("$$-$user-mark");
    my $data = load_datafile;

    my $state = $data->{$commit};
    if ($value == 0) { # Unexamined
        $state = [
            $value,
            [ ],
        ];
    } elsif ($value == 1 or $value == 6) { # Rejected or To be discussed
        $state = [
            $value,
            [ $user ],
        ];
    } elsif ($value == 5) { # Cherry-picked
        $state->[0] = $value;
    } else { # Vote
        my $old_value = $state->[0];
        if (not defined $old_value or $old_value < 2 or $old_value == 6) {
            # Voting from unexamined / rejected / to be discussed
            $state = [
                2,
                [ $user ],
            ];
        } elsif ($old_value == 5) {
            # Downvoting from cherry-picked : revert to the state corresponding
            # to the number of stored voters
            my @votes = @{ $state->[1] || [] };
            my $votes = @votes;
            unless (any_eq $user => @votes) {
                # The current user hasn't voted yet, revert to one grade above
                push @{ $state->[1] }, $user;
                ++$votes;
            }
            $state->[0] = ($votes <= 3) ? (1 + $votes) : 4;
        } elsif ($old_value < 5) {
            my @votes = @{ $state->[1] || [] };
            if ($old_value < $value) {
                # Upvoting, only bump the vote by 1 if the user hasn't voted yet
                unless (any_eq $user => @votes) {
                    $state->[0] = $old_value + 1;
                    push @{ $state->[1] }, $user;
                }
            } elsif ($old_value > $value) {
                # Downvoting, only drop the vote by 1 if the user has voted
                my $idx = 0;
                for (@votes) {
                    last if $user eq $_;
                    $idx++;
                }
                if ($idx < @votes) {
                    $state->[0] = $old_value - 1;
                    splice @{ $state->[1] }, $idx, 1;
                }
            }
        }
    }
    $data->{$commit} = $state;
    save_datafile($data);
    return join ' ', $state->[0], @{ $state->[1] || [] };
};

true;
Something went wrong with that request. Please try again.