1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 18:22:41 +01:00

Diffusion - move querying for stable commit name to occur over conduit

Summary: Ref T2784. This is a lower-level one from drequest so it gets the conditional initialization treatment. Consolidated SVN as well even though SVN is issuing database queries; I felt better about the code de-duplication despite the small performance hit when we could just query the DB directly in the SVN case.

Test Plan: browsed around my Phabricator repositories in Mercurial, Git, and SVN flavors. Looked good.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T2784

Differential Revision: https://secure.phabricator.com/D5956
This commit is contained in:
Bob Trahan 2013-05-17 14:12:46 -07:00
parent 63e39cef32
commit 2f44c0fac9
10 changed files with 172 additions and 66 deletions

View file

@ -158,6 +158,7 @@ phutil_register_library_map(array(
'ConduitAPI_diffusion_lastmodifiedquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_lastmodifiedquery_Method.php', 'ConduitAPI_diffusion_lastmodifiedquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_lastmodifiedquery_Method.php',
'ConduitAPI_diffusion_rawdiffquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_rawdiffquery_Method.php', 'ConduitAPI_diffusion_rawdiffquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_rawdiffquery_Method.php',
'ConduitAPI_diffusion_searchquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_searchquery_Method.php', 'ConduitAPI_diffusion_searchquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_searchquery_Method.php',
'ConduitAPI_diffusion_stablecommitnamequery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_stablecommitnamequery_Method.php',
'ConduitAPI_diffusion_tagsquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_tagsquery_Method.php', 'ConduitAPI_diffusion_tagsquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_tagsquery_Method.php',
'ConduitAPI_feed_Method' => 'applications/feed/conduit/ConduitAPI_feed_Method.php', 'ConduitAPI_feed_Method' => 'applications/feed/conduit/ConduitAPI_feed_Method.php',
'ConduitAPI_feed_publish_Method' => 'applications/feed/conduit/ConduitAPI_feed_publish_Method.php', 'ConduitAPI_feed_publish_Method' => 'applications/feed/conduit/ConduitAPI_feed_publish_Method.php',
@ -446,6 +447,7 @@ phutil_register_library_map(array(
'DiffusionGitMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionGitMergedCommitsQuery.php', 'DiffusionGitMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionGitMergedCommitsQuery.php',
'DiffusionGitRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionGitRawDiffQuery.php', 'DiffusionGitRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionGitRawDiffQuery.php',
'DiffusionGitRequest' => 'applications/diffusion/request/DiffusionGitRequest.php', 'DiffusionGitRequest' => 'applications/diffusion/request/DiffusionGitRequest.php',
'DiffusionGitStableCommitNameQuery' => 'applications/diffusion/query/stablecommitname/DiffusionGitStableCommitNameQuery.php',
'DiffusionHistoryController' => 'applications/diffusion/controller/DiffusionHistoryController.php', 'DiffusionHistoryController' => 'applications/diffusion/controller/DiffusionHistoryController.php',
'DiffusionHistoryQuery' => 'applications/diffusion/query/history/DiffusionHistoryQuery.php', 'DiffusionHistoryQuery' => 'applications/diffusion/query/history/DiffusionHistoryQuery.php',
'DiffusionHistoryTableView' => 'applications/diffusion/view/DiffusionHistoryTableView.php', 'DiffusionHistoryTableView' => 'applications/diffusion/view/DiffusionHistoryTableView.php',
@ -465,6 +467,7 @@ phutil_register_library_map(array(
'DiffusionMercurialMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionMercurialMergedCommitsQuery.php', 'DiffusionMercurialMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionMercurialMergedCommitsQuery.php',
'DiffusionMercurialRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionMercurialRawDiffQuery.php', 'DiffusionMercurialRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionMercurialRawDiffQuery.php',
'DiffusionMercurialRequest' => 'applications/diffusion/request/DiffusionMercurialRequest.php', 'DiffusionMercurialRequest' => 'applications/diffusion/request/DiffusionMercurialRequest.php',
'DiffusionMercurialStableCommitNameQuery' => 'applications/diffusion/query/stablecommitname/DiffusionMercurialStableCommitNameQuery.php',
'DiffusionMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionMergedCommitsQuery.php', 'DiffusionMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionMergedCommitsQuery.php',
'DiffusionPathChange' => 'applications/diffusion/data/DiffusionPathChange.php', 'DiffusionPathChange' => 'applications/diffusion/data/DiffusionPathChange.php',
'DiffusionPathChangeQuery' => 'applications/diffusion/query/pathchange/DiffusionPathChangeQuery.php', 'DiffusionPathChangeQuery' => 'applications/diffusion/query/pathchange/DiffusionPathChangeQuery.php',
@ -483,6 +486,7 @@ phutil_register_library_map(array(
'DiffusionRepositoryTag' => 'applications/diffusion/data/DiffusionRepositoryTag.php', 'DiffusionRepositoryTag' => 'applications/diffusion/data/DiffusionRepositoryTag.php',
'DiffusionRequest' => 'applications/diffusion/request/DiffusionRequest.php', 'DiffusionRequest' => 'applications/diffusion/request/DiffusionRequest.php',
'DiffusionSetupException' => 'applications/diffusion/exception/DiffusionSetupException.php', 'DiffusionSetupException' => 'applications/diffusion/exception/DiffusionSetupException.php',
'DiffusionStableCommitNameQuery' => 'applications/diffusion/query/stablecommitname/DiffusionStableCommitNameQuery.php',
'DiffusionSvnCommitParentsQuery' => 'applications/diffusion/query/parents/DiffusionSvnCommitParentsQuery.php', 'DiffusionSvnCommitParentsQuery' => 'applications/diffusion/query/parents/DiffusionSvnCommitParentsQuery.php',
'DiffusionSvnContainsQuery' => 'applications/diffusion/query/contains/DiffusionSvnContainsQuery.php', 'DiffusionSvnContainsQuery' => 'applications/diffusion/query/contains/DiffusionSvnContainsQuery.php',
'DiffusionSvnFileContentQuery' => 'applications/diffusion/query/filecontent/DiffusionSvnFileContentQuery.php', 'DiffusionSvnFileContentQuery' => 'applications/diffusion/query/filecontent/DiffusionSvnFileContentQuery.php',
@ -490,6 +494,7 @@ phutil_register_library_map(array(
'DiffusionSvnMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionSvnMergedCommitsQuery.php', 'DiffusionSvnMergedCommitsQuery' => 'applications/diffusion/query/mergedcommits/DiffusionSvnMergedCommitsQuery.php',
'DiffusionSvnRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionSvnRawDiffQuery.php', 'DiffusionSvnRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionSvnRawDiffQuery.php',
'DiffusionSvnRequest' => 'applications/diffusion/request/DiffusionSvnRequest.php', 'DiffusionSvnRequest' => 'applications/diffusion/request/DiffusionSvnRequest.php',
'DiffusionSvnStableCommitNameQuery' => 'applications/diffusion/query/stablecommitname/DiffusionSvnStableCommitNameQuery.php',
'DiffusionSymbolController' => 'applications/diffusion/controller/DiffusionSymbolController.php', 'DiffusionSymbolController' => 'applications/diffusion/controller/DiffusionSymbolController.php',
'DiffusionSymbolQuery' => 'applications/diffusion/query/DiffusionSymbolQuery.php', 'DiffusionSymbolQuery' => 'applications/diffusion/query/DiffusionSymbolQuery.php',
'DiffusionTagListController' => 'applications/diffusion/controller/DiffusionTagListController.php', 'DiffusionTagListController' => 'applications/diffusion/controller/DiffusionTagListController.php',
@ -1957,6 +1962,7 @@ phutil_register_library_map(array(
'ConduitAPI_diffusion_lastmodifiedquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 'ConduitAPI_diffusion_lastmodifiedquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_rawdiffquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 'ConduitAPI_diffusion_rawdiffquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_searchquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 'ConduitAPI_diffusion_searchquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_stablecommitnamequery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_tagsquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 'ConduitAPI_diffusion_tagsquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_feed_Method' => 'ConduitAPIMethod', 'ConduitAPI_feed_Method' => 'ConduitAPIMethod',
'ConduitAPI_feed_publish_Method' => 'ConduitAPI_feed_Method', 'ConduitAPI_feed_publish_Method' => 'ConduitAPI_feed_Method',
@ -2233,6 +2239,7 @@ phutil_register_library_map(array(
'DiffusionGitMergedCommitsQuery' => 'DiffusionMergedCommitsQuery', 'DiffusionGitMergedCommitsQuery' => 'DiffusionMergedCommitsQuery',
'DiffusionGitRawDiffQuery' => 'DiffusionRawDiffQuery', 'DiffusionGitRawDiffQuery' => 'DiffusionRawDiffQuery',
'DiffusionGitRequest' => 'DiffusionRequest', 'DiffusionGitRequest' => 'DiffusionRequest',
'DiffusionGitStableCommitNameQuery' => 'DiffusionStableCommitNameQuery',
'DiffusionHistoryController' => 'DiffusionController', 'DiffusionHistoryController' => 'DiffusionController',
'DiffusionHistoryQuery' => 'DiffusionQuery', 'DiffusionHistoryQuery' => 'DiffusionQuery',
'DiffusionHistoryTableView' => 'DiffusionView', 'DiffusionHistoryTableView' => 'DiffusionView',
@ -2251,6 +2258,7 @@ phutil_register_library_map(array(
'DiffusionMercurialMergedCommitsQuery' => 'DiffusionMergedCommitsQuery', 'DiffusionMercurialMergedCommitsQuery' => 'DiffusionMergedCommitsQuery',
'DiffusionMercurialRawDiffQuery' => 'DiffusionRawDiffQuery', 'DiffusionMercurialRawDiffQuery' => 'DiffusionRawDiffQuery',
'DiffusionMercurialRequest' => 'DiffusionRequest', 'DiffusionMercurialRequest' => 'DiffusionRequest',
'DiffusionMercurialStableCommitNameQuery' => 'DiffusionStableCommitNameQuery',
'DiffusionMergedCommitsQuery' => 'DiffusionQuery', 'DiffusionMergedCommitsQuery' => 'DiffusionQuery',
'DiffusionPathCompleteController' => 'DiffusionController', 'DiffusionPathCompleteController' => 'DiffusionController',
'DiffusionPathQueryTestCase' => 'PhabricatorTestCase', 'DiffusionPathQueryTestCase' => 'PhabricatorTestCase',
@ -2261,6 +2269,7 @@ phutil_register_library_map(array(
'DiffusionRemarkupRule' => 'PhabricatorRemarkupRuleObject', 'DiffusionRemarkupRule' => 'PhabricatorRemarkupRuleObject',
'DiffusionRepositoryController' => 'DiffusionController', 'DiffusionRepositoryController' => 'DiffusionController',
'DiffusionSetupException' => 'AphrontUsageException', 'DiffusionSetupException' => 'AphrontUsageException',
'DiffusionStableCommitNameQuery' => 'DiffusionQuery',
'DiffusionSvnCommitParentsQuery' => 'DiffusionCommitParentsQuery', 'DiffusionSvnCommitParentsQuery' => 'DiffusionCommitParentsQuery',
'DiffusionSvnContainsQuery' => 'DiffusionContainsQuery', 'DiffusionSvnContainsQuery' => 'DiffusionContainsQuery',
'DiffusionSvnFileContentQuery' => 'DiffusionFileContentQuery', 'DiffusionSvnFileContentQuery' => 'DiffusionFileContentQuery',
@ -2268,6 +2277,7 @@ phutil_register_library_map(array(
'DiffusionSvnMergedCommitsQuery' => 'DiffusionMergedCommitsQuery', 'DiffusionSvnMergedCommitsQuery' => 'DiffusionMergedCommitsQuery',
'DiffusionSvnRawDiffQuery' => 'DiffusionRawDiffQuery', 'DiffusionSvnRawDiffQuery' => 'DiffusionRawDiffQuery',
'DiffusionSvnRequest' => 'DiffusionRequest', 'DiffusionSvnRequest' => 'DiffusionRequest',
'DiffusionSvnStableCommitNameQuery' => 'DiffusionStableCommitNameQuery',
'DiffusionSymbolController' => 'DiffusionController', 'DiffusionSymbolController' => 'DiffusionController',
'DiffusionSymbolQuery' => 'PhabricatorOffsetPagedQuery', 'DiffusionSymbolQuery' => 'PhabricatorOffsetPagedQuery',
'DiffusionTagListController' => 'DiffusionController', 'DiffusionTagListController' => 'DiffusionController',

View file

@ -0,0 +1,35 @@
<?php
/**
* @group conduit
*/
final class ConduitAPI_diffusion_stablecommitnamequery_Method
extends ConduitAPI_diffusion_abstractquery_Method {
public function __construct() {
$this->setShouldCreateDiffusionRequest(false);
}
public function getMethodDescription() {
return
'Identifies the latest commit in a repository. Repositories with '.
'branch support must specify which branch to look at.';
}
public function defineReturnType() {
return 'string';
}
protected function defineCustomParamTypes() {
return array(
'branch' => 'required string',
);
}
protected function getResult(ConduitAPIRequest $request) {
$repository = $this->getRepository($request);
$query = DiffusionStableCommitNameQuery::newFromRepository($repository);
$query->setBranch($request->getValue('branch'));
return $query->load();
}
}

View file

@ -0,0 +1,17 @@
<?php
final class DiffusionGitStableCommitNameQuery
extends DiffusionStableCommitNameQuery {
protected function executeQuery() {
$repository = $this->getRepository();
$branch = $this->getBranch();
list($stdout) = $repository->execxLocalCommand(
'rev-parse --verify %s/%s',
DiffusionBranchInformation::DEFAULT_GIT_REMOTE,
$branch);
$commit = trim($stdout);
return substr($commit, 0, 16);
}
}

View file

@ -0,0 +1,30 @@
<?php
final class DiffusionMercurialStableCommitNameQuery
extends DiffusionStableCommitNameQuery {
protected function executeQuery() {
$repository = $this->getRepository();
// NOTE: For branches with spaces in their name like "a b", this
// does not work properly:
//
// $ hg log --rev 'a b'
//
// We can use revsets instead:
//
// $ hg log --rev branch('a b')
//
// ...but they require a somewhat newer version of Mercurial. Instead,
// use "-b" flag with limit 1 for greatest compatibility across
// versions.
list($stable_commit_name) = $repository->execxLocalCommand(
'log --template=%s -b %s --limit 1',
'{node}',
$this->getBranch());
return $stable_commit_name;
}
}

View file

@ -0,0 +1,35 @@
<?php
abstract class DiffusionStableCommitNameQuery extends DiffusionQuery {
private $branch;
private $repository;
public function setBranch($branch) {
$this->branch = $branch;
return $this;
}
public function getBranch() {
return $this->branch;
}
public function setRepository(PhabricatorRepository $repository) {
$this->repository = $repository;
return $this;
}
public function getRepository() {
return $this->repository;
}
final public static function newFromRepository(
PhabricatorRepository $repository) {
$obj = parent::initQueryObject(__CLASS__, $repository);
$obj->setRepository($repository);
return $obj;
}
final public function load() {
return $this->executeQuery();
}
}

View file

@ -0,0 +1,23 @@
<?php
final class DiffusionSvnStableCommitNameQuery
extends DiffusionStableCommitNameQuery {
protected function executeQuery() {
$repository = $this->getRepository();
$commit = id(new PhabricatorRepositoryCommit())
->loadOneWhere(
'repositoryID = %d ORDER BY epoch DESC LIMIT 1',
$repository->getID());
if ($commit) {
$stable_commit_name = $commit->getCommitIdentifier();
} else {
// For new repositories, we may not have parsed any commits yet. Call
// the stable commit "1" and avoid fataling.
$stable_commit_name = 1;
}
return $stable_commit_name;
}
}

View file

@ -35,20 +35,4 @@ final class DiffusionGitRequest extends DiffusionRequest {
return $remote.'/'.$this->getBranch(); return $remote.'/'.$this->getBranch();
} }
public function getStableCommitName() {
if (!$this->stableCommitName) {
if ($this->commit) {
$this->stableCommitName = $this->commit;
} else {
$branch = $this->getBranch();
list($stdout) = $this->getRepository()->execxLocalCommand(
'rev-parse --verify %s/%s',
DiffusionBranchInformation::DEFAULT_GIT_REMOTE,
$branch);
$this->stableCommitName = trim($stdout);
}
}
return substr($this->stableCommitName, 0, 16);
}
} }

View file

@ -42,32 +42,4 @@ final class DiffusionMercurialRequest extends DiffusionRequest {
return $this->getBranch(); return $this->getBranch();
} }
public function getStableCommitName() {
if (!$this->stableCommitName) {
if ($this->commit) {
$this->stableCommitName = $this->commit;
} else {
// NOTE: For branches with spaces in their name like "a b", this
// does not work properly:
//
// $ hg log --rev 'a b'
//
// We can use revsets instead:
//
// $ hg log --rev branch('a b')
//
// ...but they require a somewhat newer version of Mercurial. Instead,
// use "-b" flag with limit 1 for greatest compatibility across
// versions.
list($this->stableCommitName) = $this->repository->execxLocalCommand(
'log --template=%s -b %s --limit 1',
'{node}',
$this->getBranch());
}
}
return $this->stableCommitName;
}
} }

View file

@ -306,6 +306,9 @@ abstract class DiffusionRequest {
* a symbolic commit reference. * a symbolic commit reference.
*/ */
public function getStableCommitName() { public function getStableCommitName() {
if (!$this->stableCommitName) {
$this->queryStableCommitName();
}
return $this->stableCommitName; return $this->stableCommitName;
} }
@ -628,4 +631,23 @@ abstract class DiffusionRequest {
$this->tagContent = $commit_data['tagContent']; $this->tagContent = $commit_data['tagContent'];
} }
private function queryStableCommitName() {
if ($this->commit) {
$this->stableCommitName = $this->commit;
} else if ($this->shouldInitFromConduit()) {
$this->stableCommitName = DiffusionQuery::callConduitWithDiffusionRequest(
$this->getUser(),
$this,
'diffusion.stablecommitnamequery',
array(
'branch' => $this->getBranch()
));
} else {
$query = DiffusionStableCommitNameQuery::newFromRepository(
$this->getRepository());
$query->setBranch($this->getBranch());
$this->stableCommitName = $query->load();
}
return $this->stableCommitName;
}
} }

View file

@ -22,28 +22,6 @@ final class DiffusionSvnRequest extends DiffusionRequest {
return 'svn'; return 'svn';
} }
public function getStableCommitName() {
if ($this->commit) {
return $this->commit;
}
if ($this->stableCommitName === null) {
$commit = id(new PhabricatorRepositoryCommit())
->loadOneWhere(
'repositoryID = %d ORDER BY epoch DESC LIMIT 1',
$this->getRepository()->getID());
if ($commit) {
$this->stableCommitName = $commit->getCommitIdentifier();
} else {
// For new repositories, we may not have parsed any commits yet. Call
// the stable commit "1" and avoid fataling.
$this->stableCommitName = 1;
}
}
return $this->stableCommitName;
}
public function getCommit() { public function getCommit() {
if ($this->commit) { if ($this->commit) {
return $this->commit; return $this->commit;