3 final class ReleephCommitFinder
extends Phobject
{
5 private $releephProject;
9 public function setUser(PhabricatorUser
$user) {
13 public function getUser() {
17 public function setReleephProject(ReleephProject
$rp) {
18 $this->releephProject
= $rp;
22 public function getRequestedObjectPHID() {
23 return $this->objectPHID
;
26 public function fromPartial($partial_string) {
27 $this->objectPHID
= null;
31 if (preg_match('/^D([1-9]\d*)$/', $partial_string, $matches)) {
32 $diff_id = $matches[1];
33 $diff_rev = id(new DifferentialRevisionQuery())
34 ->setViewer($this->getUser())
35 ->withIDs(array($diff_id))
36 ->needCommitPHIDs(true)
39 throw new ReleephCommitFinderException(
41 '%s does not refer to an existing diff.',
44 $commit_phids = $diff_rev->getCommitPHIDs();
47 throw new ReleephCommitFinderException(
49 '%s has no commits associated with it yet.',
53 $this->objectPHID
= $diff_rev->getPHID();
55 $commits = id(new DiffusionCommitQuery())
56 ->setViewer($this->getUser())
57 ->withPHIDs($commit_phids)
59 $commits = msort($commits, 'getEpoch');
60 return head($commits);
63 // Look for a raw commit number, or r<callsign><commit-number>.
64 $repository = $this->releephProject
->getRepository();
67 if (preg_match('/^r(?P<callsign>[A-Z]+)(?P<commit>\w+)$/',
68 $partial_string, $matches)) {
69 $callsign = $matches['callsign'];
70 if ($callsign != $repository->getCallsign()) {
71 throw new ReleephCommitFinderException(
73 '%s is in a different repository to this Releeph project (%s).',
75 $repository->getCallsign()));
81 'callsign' => $repository->getCallsign(),
82 'commit' => $partial_string,
87 $dr_data['user'] = $this->getUser();
88 $dr = DiffusionRequest
::newFromDictionary($dr_data);
89 } catch (Exception
$ex) {
91 'No commit matches %s: %s',
94 throw new ReleephCommitFinderException($message);
97 $phabricator_repository_commit = $dr->loadCommit();
99 if (!$phabricator_repository_commit) {
100 throw new ReleephCommitFinderException(
102 "The commit %s doesn't exist in this repository.",
106 // When requesting a single commit, if it has an associated review we
107 // imply the review was requested instead. This is always correct for now
108 // and consistent with the older behavior, although it might not be the
109 // right rule in the future.
110 $phids = PhabricatorEdgeQuery
::loadDestinationPHIDs(
111 $phabricator_repository_commit->getPHID(),
112 DiffusionCommitHasRevisionEdgeType
::EDGECONST
);
114 $this->objectPHID
= head($phids);
117 return $phabricator_repository_commit;