2013-01-30 17:33:32 -08:00
|
|
|
<?php
|
|
|
|
|
2020-04-08 10:24:46 -07:00
|
|
|
class ArcanistFeatureWorkflow
|
|
|
|
extends ArcanistArcWorkflow {
|
2013-01-30 17:33:32 -08:00
|
|
|
|
|
|
|
private $branches;
|
|
|
|
|
|
|
|
public function getWorkflowName() {
|
|
|
|
return 'feature';
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:24:46 -07:00
|
|
|
public function getWorkflowArguments() {
|
|
|
|
return array(
|
|
|
|
$this->newWorkflowArgument('view-all')
|
|
|
|
->setHelp(pht('Include closed and abandoned revisions.')),
|
|
|
|
$this->newWorkflowArgument('by-status')
|
|
|
|
->setParameter('status')
|
|
|
|
->setHelp(pht('Sort branches by status instead of time.')),
|
|
|
|
$this->newWorkflowArgument('output')
|
|
|
|
->setParameter('format')
|
|
|
|
->setHelp(
|
|
|
|
pht(
|
|
|
|
'With "json", show features in machine-readable JSON format.')),
|
|
|
|
$this->newWorkflowArgument('branch')
|
|
|
|
->setWildcard(true),
|
|
|
|
);
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:24:46 -07:00
|
|
|
public function getWorkflowInformation() {
|
|
|
|
return $this->newWorkflowInformation()
|
|
|
|
->setSynopsis(pht('Wrapper on "git branch" or "hg bookmark".'))
|
|
|
|
->addExample(pht('**feature** [__options__]'))
|
|
|
|
->addExample(pht('**feature** __name__ [__start__]'))
|
|
|
|
->setHelp(
|
|
|
|
pht(<<<EOHELP
|
|
|
|
A wrapper on 'git branch' or 'hg bookmark'.
|
2013-01-30 17:33:32 -08:00
|
|
|
|
2020-04-08 10:24:46 -07:00
|
|
|
Without __name__, it lists the available branches and their revision status.
|
2013-01-30 17:33:32 -08:00
|
|
|
|
2020-04-08 10:24:46 -07:00
|
|
|
With __name__, it creates or checks out a branch. If the branch __name__
|
|
|
|
doesn't exist and is in format D123 then the branch of revision D123 is
|
|
|
|
checked out. Use __start__ to specify where the new branch will start. Use
|
|
|
|
'arc.feature.start.default' to set the default feature start location.
|
|
|
|
EOHELP
|
|
|
|
));
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:24:46 -07:00
|
|
|
public function runWorkflow() {
|
2013-01-30 17:33:32 -08:00
|
|
|
$repository_api = $this->getRepositoryAPI();
|
2020-04-08 10:24:46 -07:00
|
|
|
if (!$repository_api) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
|
|
|
'This command must be run in a Git or Mercurial working copy.'));
|
|
|
|
}
|
2013-01-30 17:33:32 -08:00
|
|
|
|
2013-10-21 16:29:08 -07:00
|
|
|
$names = $this->getArgument('branch');
|
2013-01-30 17:33:32 -08:00
|
|
|
if ($names) {
|
|
|
|
if (count($names) > 2) {
|
2015-05-13 18:05:15 +10:00
|
|
|
throw new ArcanistUsageException(pht('Specify only one branch.'));
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
return $this->checkoutBranch($names);
|
|
|
|
}
|
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
// TODO: Everything in this whole workflow that says "branch" means
|
|
|
|
// "bookmark" in Mercurial.
|
|
|
|
|
|
|
|
$branches = $repository_api->getAllBranchRefs();
|
2013-01-30 17:33:32 -08:00
|
|
|
if (!$branches) {
|
2015-05-13 18:05:15 +10:00
|
|
|
throw new ArcanistUsageException(
|
|
|
|
pht('No branches in this working copy.'));
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
$states = array();
|
|
|
|
foreach ($branches as $branch) {
|
|
|
|
$states[] = $this->newWorkingCopyStateRef()
|
|
|
|
->attachBranchRef($branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->newRefQuery($states)
|
|
|
|
->needHardpoints(
|
|
|
|
array(
|
|
|
|
'revisionRefs',
|
|
|
|
))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->printBranches($states);
|
2013-01-30 17:33:32 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkoutBranch(array $names) {
|
|
|
|
$api = $this->getRepositoryAPI();
|
|
|
|
|
|
|
|
if ($api instanceof ArcanistMercurialAPI) {
|
|
|
|
$command = 'update %s';
|
|
|
|
} else {
|
|
|
|
$command = 'checkout %s';
|
|
|
|
}
|
|
|
|
|
2013-02-20 20:06:16 -08:00
|
|
|
$err = 1;
|
|
|
|
|
2013-03-05 12:48:53 -08:00
|
|
|
$name = $names[0];
|
|
|
|
if (isset($names[1])) {
|
|
|
|
$start = $names[1];
|
|
|
|
} else {
|
2014-06-18 03:41:21 +10:00
|
|
|
$start = $this->getConfigFromAnySource('arc.feature.start.default');
|
2013-03-05 12:48:53 -08:00
|
|
|
}
|
|
|
|
|
2013-02-20 20:06:16 -08:00
|
|
|
$branches = $api->getAllBranches();
|
2013-03-05 12:48:53 -08:00
|
|
|
if (in_array($name, ipull($branches, 'name'))) {
|
2014-06-18 03:41:21 +10:00
|
|
|
list($err, $stdout, $stderr) = $api->execManualLocal($command, $name);
|
2013-02-20 20:06:16 -08:00
|
|
|
}
|
2013-01-30 17:33:32 -08:00
|
|
|
|
|
|
|
if ($err) {
|
|
|
|
$match = null;
|
2013-03-05 12:48:53 -08:00
|
|
|
if (preg_match('/^D(\d+)$/', $name, $match)) {
|
2020-02-13 06:01:26 -08:00
|
|
|
$diff = $this->getConduitEngine()->resolveCall(
|
|
|
|
'differential.querydiffs',
|
|
|
|
array(
|
|
|
|
'revisionIDs' => array($match[1]),
|
|
|
|
));
|
|
|
|
$diff = head($diff);
|
|
|
|
|
|
|
|
if ($diff['branch'] != '') {
|
|
|
|
$name = $diff['branch'];
|
|
|
|
list($err, $stdout, $stderr) = $api->execManualLocal(
|
|
|
|
$command,
|
|
|
|
$name);
|
|
|
|
}
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($err) {
|
|
|
|
if ($api instanceof ArcanistMercurialAPI) {
|
|
|
|
$rev = '';
|
2013-03-05 12:48:53 -08:00
|
|
|
if ($start) {
|
|
|
|
$rev = csprintf('-r %s', $start);
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
2013-02-20 20:06:16 -08:00
|
|
|
|
2014-06-18 03:41:21 +10:00
|
|
|
$exec = $api->execManualLocal('bookmark %C %s', $rev, $name);
|
2013-01-30 17:33:32 -08:00
|
|
|
|
2013-03-05 12:48:53 -08:00
|
|
|
if (!$exec[0] && $start) {
|
|
|
|
$api->execxLocal('update %s', $name);
|
2013-02-20 20:06:16 -08:00
|
|
|
}
|
2013-01-30 17:33:32 -08:00
|
|
|
} else {
|
2013-03-05 12:48:53 -08:00
|
|
|
$startarg = $start ? csprintf('%s', $start) : '';
|
2013-01-30 17:33:32 -08:00
|
|
|
$exec = $api->execManualLocal(
|
2013-03-05 12:48:53 -08:00
|
|
|
'checkout --track -b %s %C',
|
|
|
|
$name,
|
|
|
|
$startarg);
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
list($err, $stdout, $stderr) = $exec;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $stdout;
|
2018-03-26 10:36:04 -07:00
|
|
|
fprintf(STDERR, '%s', $stderr);
|
2013-01-30 17:33:32 -08:00
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
private function printBranches(array $states) {
|
2013-01-30 17:33:32 -08:00
|
|
|
static $color_map = array(
|
|
|
|
'Closed' => 'cyan',
|
|
|
|
'Needs Review' => 'magenta',
|
|
|
|
'Needs Revision' => 'red',
|
|
|
|
'Accepted' => 'green',
|
|
|
|
'No Revision' => 'blue',
|
|
|
|
'Abandoned' => 'default',
|
|
|
|
);
|
|
|
|
|
|
|
|
static $ssort_map = array(
|
|
|
|
'Closed' => 1,
|
|
|
|
'No Revision' => 2,
|
|
|
|
'Needs Review' => 3,
|
|
|
|
'Needs Revision' => 4,
|
|
|
|
'Accepted' => 5,
|
|
|
|
);
|
|
|
|
|
|
|
|
$out = array();
|
2020-02-13 06:01:26 -08:00
|
|
|
foreach ($states as $state) {
|
|
|
|
$branch = $state->getBranchRef();
|
2013-01-30 17:33:32 -08:00
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
$revision = null;
|
|
|
|
if ($state->hasAmbiguousRevisionRefs()) {
|
|
|
|
$status = pht('Ambiguous Revision');
|
2013-01-30 17:33:32 -08:00
|
|
|
} else {
|
2020-02-13 06:01:26 -08:00
|
|
|
$revision = $state->getRevisionRef();
|
|
|
|
if ($revision) {
|
|
|
|
$status = $revision->getStatusDisplayName();
|
|
|
|
} else {
|
|
|
|
$status = pht('No Revision');
|
|
|
|
}
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
if (!$this->getArgument('view-all') && !$branch->getIsCurrentBranch()) {
|
2013-01-30 17:33:32 -08:00
|
|
|
if ($status == 'Closed' || $status == 'Abandoned') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
$commit = $branch->getCommitRef();
|
|
|
|
$epoch = $commit->getCommitEpoch();
|
2013-01-30 17:33:32 -08:00
|
|
|
|
|
|
|
$color = idx($color_map, $status, 'default');
|
|
|
|
$ssort = sprintf('%d%012d', idx($ssort_map, $status, 0), $epoch);
|
|
|
|
|
2020-02-13 06:01:26 -08:00
|
|
|
if ($revision) {
|
|
|
|
$desc = $revision->getFullName();
|
|
|
|
} else {
|
|
|
|
$desc = $commit->getSummary();
|
|
|
|
}
|
|
|
|
|
2013-01-30 17:33:32 -08:00
|
|
|
$out[] = array(
|
2020-02-13 06:01:26 -08:00
|
|
|
'name' => $branch->getBranchName(),
|
|
|
|
'current' => $branch->getIsCurrentBranch(),
|
2013-01-30 17:33:32 -08:00
|
|
|
'status' => $status,
|
|
|
|
'desc' => $desc,
|
2020-02-13 06:01:26 -08:00
|
|
|
'revision' => $revision ? $revision->getID() : null,
|
2013-01-30 17:33:32 -08:00
|
|
|
'color' => $color,
|
|
|
|
'esort' => $epoch,
|
2013-11-25 11:50:11 -08:00
|
|
|
'epoch' => $epoch,
|
2013-01-30 17:33:32 -08:00
|
|
|
'ssort' => $ssort,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-16 09:38:17 -07:00
|
|
|
if (!$out) {
|
|
|
|
// All of the revisions are closed or abandoned.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-30 17:33:32 -08:00
|
|
|
$len_name = max(array_map('strlen', ipull($out, 'name'))) + 2;
|
|
|
|
$len_status = max(array_map('strlen', ipull($out, 'status'))) + 2;
|
|
|
|
|
|
|
|
if ($this->getArgument('by-status')) {
|
|
|
|
$out = isort($out, 'ssort');
|
|
|
|
} else {
|
|
|
|
$out = isort($out, 'esort');
|
|
|
|
}
|
2013-11-25 11:50:11 -08:00
|
|
|
if ($this->getArgument('output') == 'json') {
|
|
|
|
foreach ($out as &$feature) {
|
|
|
|
unset($feature['color'], $feature['ssort'], $feature['esort']);
|
|
|
|
}
|
2014-06-10 11:02:42 -07:00
|
|
|
echo json_encode(ipull($out, null, 'name'))."\n";
|
2013-11-25 11:50:11 -08:00
|
|
|
} else {
|
2014-06-18 03:41:21 +10:00
|
|
|
$table = id(new PhutilConsoleTable())
|
|
|
|
->setShowHeader(false)
|
|
|
|
->addColumn('current', array('title' => ''))
|
2015-06-09 07:17:39 +10:00
|
|
|
->addColumn('name', array('title' => pht('Name')))
|
|
|
|
->addColumn('status', array('title' => pht('Status')))
|
|
|
|
->addColumn('descr', array('title' => pht('Description')));
|
2014-06-18 03:41:21 +10:00
|
|
|
|
2013-11-25 11:50:11 -08:00
|
|
|
foreach ($out as $line) {
|
2014-06-18 03:41:21 +10:00
|
|
|
$table->addRow(array(
|
|
|
|
'current' => $line['current'] ? '*' : '',
|
2015-09-21 12:40:06 -07:00
|
|
|
'name' => tsprintf('**%s**', $line['name']),
|
|
|
|
'status' => tsprintf(
|
2014-06-18 03:41:21 +10:00
|
|
|
"<fg:{$line['color']}>%s</fg>", $line['status']),
|
|
|
|
'descr' => $line['desc'],
|
|
|
|
));
|
2013-11-25 11:50:11 -08:00
|
|
|
}
|
2014-06-18 03:41:21 +10:00
|
|
|
|
|
|
|
$table->draw();
|
2013-01-30 17:33:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|