Differential D10246 Diff 24654 src/applications/repository/management/PhabricatorRepositoryManagementPruneWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/repository/management/PhabricatorRepositoryManagementPruneWorkflow.php
- This file was added.
<?php | |||||
final class PhabricatorRepositoryManagementPruneWorkflow | |||||
extends PhabricatorRepositoryManagementWorkflow { | |||||
public function didConstruct() { | |||||
$this | |||||
->setName('prune') | |||||
->setExamples('**prune** __repository__ ...') | |||||
->setSynopsis('Prune __repository__, removing orphaned commits from repository named by callsign.') | |||||
->setArguments( | |||||
array( | |||||
array( | |||||
'name' => 'verbose', | |||||
'help' => 'Show additional debugging information.', | |||||
), | |||||
array( | |||||
'name' => 'repos', | |||||
'wildcard' => true, | |||||
), | |||||
)); | |||||
} | |||||
public function execute(PhutilArgumentParser $args) { | |||||
$repos = $this->loadRepositories($args, 'repos'); | |||||
if (!$repos) { | |||||
throw new PhutilArgumentUsageException( | |||||
'Specify one or more repositories to prune, by callsign.'); | |||||
} | |||||
$console = PhutilConsole::getConsole(); | |||||
foreach ($repos as $repo) { | |||||
$console->writeOut("Pruning '%s'...\n", $repo->getCallsign()); | |||||
$commits = id(new DiffusionCommitQuery) | |||||
->withRepository($repo) | |||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | |||||
->execute(); | |||||
$bad_commits = array(); | |||||
$console->writeOut("For each commit we have record of in phabricator, " | |||||
."we will try to resolve it in the VCS. This will show errors for any " | |||||
."that are missing.\n"); | |||||
foreach ($commits as $commit) { | |||||
$out = $repo->execLocalCommand( | |||||
'show %s', | |||||
$commit->getCommitIdentifier()); | |||||
if (array_shift($out) == 128) { | |||||
$console->writeOut(trim(implode($out,"\n"))."\n"); | |||||
$bad_commits[] = $commit; | |||||
} | |||||
} | |||||
if (count($bad_commits) == 0) { | |||||
$console->writeOut("No orphaned commits.\n"); | |||||
continue; | |||||
} | |||||
$ok = $console->confirm( | |||||
pht( | |||||
'Found %d orphaned commits out of %d total commits. Are you ' | |||||
.'absolutely certain you want to destroy the commits listed above?', | |||||
new PhutilNumber(count($bad_commits)), | |||||
new PhutilNumber(count($commits)) | |||||
)); | |||||
if (!$ok) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht('Aborted, your commits are safe.')); | |||||
} | |||||
foreach ($bad_commits as $commit) { | |||||
$commit->delete(); | |||||
} | |||||
} | |||||
return 0; | |||||
} | |||||
} |