Skip to content

Commit

Permalink
Split committable into reusable subs
Browse files Browse the repository at this point in the history
This will come handy when improving bisectable.
  • Loading branch information
AlexDaniel committed Jun 15, 2020
1 parent 363dd9c commit 90bf8f0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
54 changes: 54 additions & 0 deletions lib/Whateverable/Processing.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,60 @@ sub subprocess-commit($commit, $filename, $full-commit, :%ENV) is export {
$output
}

#| Transform a revision into `output => short SHA` pair
sub process-commit($commit, $filename, :%ENV) is export {
# convert to real ids so we can look up the builds
my $full-commit = to-full-commit $commit;
my $short-commit = get-short-commit $commit;
$short-commit ~= ({get-short-commit $full-commit}) if $commit eq HEAD;

without $full-commit {
return $short-commit R=> Cannot find this revision (did you mean “ ~
get-short-commit(get-similar $commit, <HEAD v6.c releases all>) ~
”?)
}
$short-commit R=> subprocess-commit $commit, $filename, $full-commit, :%ENV;
}

#| Runs process-commit on each commit and saves the
#| results in a given array and hash
sub proccess-and-group-commits(@outputs, # unlike %shas this is ordered
%shas, # { output => [sha, sha, …], … }
$file,
*@commits,
:$intermingle=True, :$prepend=False,
:$start-time, :$time-limit,
:%ENV=%*ENV) is export {
for @commits.map: { process-commit $_, $file, :%ENV } {
if $start-time && $time-limit && now - $start-time > $time-limit { # bail out if needed
grumble «hit the total time limit of $time-limit seconds»
}
my $push-needed = $intermingle
?? (%shas{.key}:!exists)
!! !@outputs || @outputs.tail ne .key;
@outputs.push: .key if $push-needed;
if $prepend {
%shas{.key}.prepend: .value;
} else {
%shas{.key}.append: .value;
}
}
}

#| Takes the array and hash produced by `proccess-and-group-commits`
#| and turns it into a beautiful gist (or a short reply).
#| Note that it can list the same commit set more than once if you're
#| not using intermingle feature in proccess-and-group-commits.
#| Arguably it's a feature, but please judge yourself.
sub commit-groups-to-gisted-reply(@outputs, %shas, $config) is export {
my $short-str = @outputs == 1 && %shas{@outputs[0]} > 3 && $config.chars < 20
?? ¦{$config} ({+%shas{@outputs[0]}} commits): «{@outputs[0]}»
!! ¦ ~ @outputs.map({ {%shas{$_}.join: ,}: «$_» }).join: ¦;

my $long-str = ¦ ~ @outputs.map({ «{limited-join %shas{$_}}»:\n$_ }).join: \n¦;
$short-str but ProperStr($long-str);
}

#↓ Substitutes some characters in $code if it looks like code, or
#↓ fetches code from a url if $code looks like one.
sub process-code($code is copy, $msg) is export {
Expand Down
40 changes: 9 additions & 31 deletions xbin/Committable.p6
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ multi method irc-to-me($msg where .args[1] ~~ ?(my $prefix = m/^ $<shortcut>=@(s
&& .text ~~ /^ \s* $<code>=.+ /) is default {
my $code = ~$<code>;
my $shortcut = shortcuts{$prefix<shortcut>};
start self.process: $msg, $shortcut, $code
start process $msg, $shortcut, $code
}

multi method irc-to-me($msg where /^ \s* [ @<envs>=((<[\w-]>+)=(\S*)) ]* %% \s+
Expand All @@ -64,25 +64,11 @@ multi method irc-to-me($msg where /^ \s* [ @<envs>=((<[\w-]>+)‘=’(\S*)) ]* %
%ENV ,= %*ENV;
my $config = ~$<config>;
my $code = ~$<code>;
start self.process: $msg, $config, $code, :%ENV
start process $msg, $config, $code, :%ENV

}

method process-commit($commit, $filename, :%ENV) {
# convert to real ids so we can look up the builds
my $full-commit = to-full-commit $commit;
my $short-commit = get-short-commit $commit;
$short-commit ~= ({get-short-commit $full-commit}) if $commit eq HEAD;

without $full-commit {
return $short-commit R=> Cannot find this revision (did you mean “ ~
get-short-commit(get-similar $commit, <HEAD v6.c releases all>) ~
”?)
}
$short-commit R=> subprocess-commit $commit, $filename, $full-commit, :%ENV;
}

method process($msg, $config is copy, $code is copy, :%ENV) {
sub process($msg, $config is copy, $code is copy, :%ENV) {
my $start-time = now;
if $config ~~ /^ [say|sub] $/ {
$msg.reply: Seems like you forgot to specify a revision (will use “v6.c” instead of “$config”);
Expand All @@ -95,20 +81,12 @@ method process($msg, $config is copy, $code is copy, :%ENV) {

my @outputs; # unlike %shas this is ordered
my %shas; # { output => [sha, sha, …], … }
%shas.categorize-list: as => *.value, {
if now - $start-time > TOTAL-TIME { # bail out if needed
grumble «hit the total time limit of {TOTAL-TIME} seconds»
}
@outputs.push: .key if %shas{.key}:!exists;
.key
}, @commits.map: { self.process-commit: $_, $file, :%ENV };

my $short-str = @outputs == 1 && %shas{@outputs[0]} > 3 && $config.chars < 20
?? ¦{$config} ({+%shas{@outputs[0]}} commits): «{@outputs[0]}»
!! ¦ ~ @outputs.map({ {%shas{$_}.join: ,}: «$_» }).join: ¦;

my $long-str = ¦ ~ @outputs.map({ «{limited-join %shas{$_}}»:\n$_ }).join: \n¦;
$short-str but ProperStr($long-str);

proccess-and-group-commits @outputs, %shas, $file, @commits,
:intermingle, :!prepend,
:$start-time, time-limit => TOTAL-TIME;

commit-groups-to-gisted-reply @outputs, %shas, $config;
}


Expand Down

0 comments on commit 90bf8f0

Please sign in to comment.