2011-01-10 00:22:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-05 00:22:17 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-01-10 00:22:25 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-02-19 20:36:08 +01:00
|
|
|
/**
|
|
|
|
* Covers your professional reputation by blaming changes to locate reviewers.
|
|
|
|
*
|
|
|
|
* @group workflow
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistCoverWorkflow extends ArcanistBaseWorkflow {
|
2011-01-10 00:22:25 +01:00
|
|
|
|
2012-03-05 19:02:37 +01:00
|
|
|
public function getCommandSynopses() {
|
2011-01-10 00:22:25 +01:00
|
|
|
return phutil_console_format(<<<EOTEXT
|
|
|
|
**cover**
|
2012-03-05 19:02:37 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2011-01-10 00:22:25 +01:00
|
|
|
Supports: svn, git
|
|
|
|
Cover your... professional reputation. Show blame for the lines you
|
|
|
|
changed in your working copy. This will take a minute because blame
|
|
|
|
takes a minute, especially under SVN.
|
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresWorkingCopy() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresConduit() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresAuthentication() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresRepositoryAPI() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
|
|
|
|
|
|
|
$repository_api = $this->getRepositoryAPI();
|
|
|
|
|
|
|
|
$paths = $repository_api->getWorkingCopyStatus();
|
|
|
|
|
|
|
|
foreach ($paths as $path => $status) {
|
|
|
|
if (is_dir($path)) {
|
|
|
|
unset($paths[$path]);
|
|
|
|
}
|
|
|
|
if ($status & ArcanistRepositoryAPI::FLAG_UNTRACKED) {
|
|
|
|
unset($paths[$path]);
|
|
|
|
}
|
|
|
|
if ($status & ArcanistRepositoryAPI::FLAG_ADDED) {
|
|
|
|
unset($paths[$path]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$paths = array_keys($paths);
|
|
|
|
|
|
|
|
if (!$paths) {
|
|
|
|
throw new ArcanistNoEffectException(
|
|
|
|
"You're covered, you didn't change anything.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$covers = array();
|
|
|
|
foreach ($paths as $path) {
|
2012-01-05 00:22:17 +01:00
|
|
|
$lines = $this->getChangedLines($path, 'cover');
|
|
|
|
if (!$lines) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
$blame = $repository_api->getBlame($path);
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
list($author, $revision) = idx($blame, $line, array(null, null));
|
|
|
|
if (!$author) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isset($covers[$author])) {
|
|
|
|
$covers[$author] = array();
|
|
|
|
}
|
|
|
|
if (!isset($covers[$author][$path])) {
|
|
|
|
$covers[$author][$path] = array(
|
|
|
|
'lines' => array(),
|
|
|
|
'revisions' => array(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$covers[$author][$path]['lines'][] = $line;
|
|
|
|
$covers[$author][$path]['revisions'][] = $revision;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($covers)) {
|
|
|
|
foreach ($covers as $author => $files) {
|
|
|
|
echo phutil_console_format(
|
|
|
|
"**%s**\n",
|
|
|
|
$author);
|
|
|
|
foreach ($files as $file => $info) {
|
|
|
|
$line_noun = count($info['lines']) == 1 ? 'line' : 'lines';
|
|
|
|
$lines = $this->readableSequenceFromLineNumbers($info['lines']);
|
|
|
|
echo " {$file}: {$line_noun} {$lines}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo "You're covered, your changes didn't touch anyone else's code.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function readableSequenceFromLineNumbers(array $array) {
|
|
|
|
$sequence = array();
|
|
|
|
$last = null;
|
|
|
|
$seq = null;
|
|
|
|
$array = array_unique(array_map('intval', $array));
|
|
|
|
sort($array);
|
|
|
|
foreach ($array as $element) {
|
|
|
|
if ($seq !== null && $element == ($seq + 1)) {
|
|
|
|
$seq++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($seq === null) {
|
|
|
|
$last = $element;
|
|
|
|
$seq = $element;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($seq > $last) {
|
|
|
|
$sequence[] = $last.'-'.$seq;
|
|
|
|
} else {
|
|
|
|
$sequence[] = $last;
|
|
|
|
}
|
|
|
|
|
|
|
|
$last = $element;
|
|
|
|
$seq = $element;
|
|
|
|
}
|
|
|
|
if ($last !== null && $seq > $last) {
|
|
|
|
$sequence[] = $last.'-'.$seq;
|
|
|
|
} else if ($last !== null) {
|
|
|
|
$sequence[] = $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|