mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-21 11:09:02 +01:00
Summary: Ref T9028. This corrects the reachability of existing commits in a repository. In particular, it can be used to mark deleted commits as unreachable. Test Plan: - Ran it on a bad repository, with bad args, etc. - Ran it on a clean repo, got no changes. - Marked a reachable commit as unreachable, ran script, got it marked reachable. - Started deleting tags and branches from the local working copy while running the script, saw greater parts of the repository get marked unreachable. - Pulled repository again, everything automatically revived. Reviewers: chad Reviewed By: chad Maniphest Tasks: T9028 Differential Revision: https://secure.phabricator.com/D16132
95 lines
2.1 KiB
PHP
95 lines
2.1 KiB
PHP
<?php
|
|
|
|
final class PhabricatorGitGraphStream
|
|
extends PhabricatorRepositoryGraphStream {
|
|
|
|
private $repository;
|
|
private $iterator;
|
|
|
|
private $parents = array();
|
|
private $dates = array();
|
|
|
|
public function __construct(
|
|
PhabricatorRepository $repository,
|
|
$start_commit = null) {
|
|
|
|
$this->repository = $repository;
|
|
|
|
if ($start_commit !== null) {
|
|
$future = $repository->getLocalCommandFuture(
|
|
'log --format=%s %s --',
|
|
'%H%x01%P%x01%ct',
|
|
$start_commit);
|
|
} else {
|
|
$future = $repository->getLocalCommandFuture(
|
|
'log --format=%s --all --',
|
|
'%H%x01%P%x01%ct');
|
|
}
|
|
|
|
$this->iterator = new LinesOfALargeExecFuture($future);
|
|
$this->iterator->setDelimiter("\n");
|
|
$this->iterator->rewind();
|
|
}
|
|
|
|
public function getParents($commit) {
|
|
if (!isset($this->parents[$commit])) {
|
|
$this->parseUntil($commit);
|
|
}
|
|
$parents = $this->parents[$commit];
|
|
|
|
// NOTE: In Git, it is possible for a commit to list the same parent more
|
|
// than once. See T5226. Discard duplicate parents.
|
|
|
|
return array_unique($parents);
|
|
}
|
|
|
|
public function getCommitDate($commit) {
|
|
if (!isset($this->dates[$commit])) {
|
|
$this->parseUntil($commit);
|
|
}
|
|
return $this->dates[$commit];
|
|
}
|
|
|
|
private function parseUntil($commit) {
|
|
if ($this->isParsed($commit)) {
|
|
return;
|
|
}
|
|
|
|
$gitlog = $this->iterator;
|
|
|
|
while ($gitlog->valid()) {
|
|
$line = $gitlog->current();
|
|
$gitlog->next();
|
|
|
|
$line = trim($line);
|
|
if (!strlen($line)) {
|
|
break;
|
|
}
|
|
list($hash, $parents, $epoch) = explode("\1", $line);
|
|
|
|
if ($parents) {
|
|
$parents = explode(' ', $parents);
|
|
} else {
|
|
// First commit.
|
|
$parents = array();
|
|
}
|
|
|
|
$this->dates[$hash] = $epoch;
|
|
$this->parents[$hash] = $parents;
|
|
|
|
if ($this->isParsed($commit)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
throw new Exception(
|
|
pht(
|
|
"No such commit '%s' in repository!",
|
|
$commit));
|
|
}
|
|
|
|
private function isParsed($commit) {
|
|
return isset($this->dates[$commit]);
|
|
}
|
|
|
|
}
|