2012-01-19 20:49:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
abstract class DiffusionQuery {
|
|
|
|
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
final protected function __construct() {
|
|
|
|
// <protected>
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function newQueryObject(
|
|
|
|
$base_class,
|
|
|
|
DiffusionRequest $request) {
|
|
|
|
|
|
|
|
$repository = $request->getRepository();
|
|
|
|
|
|
|
|
$map = array(
|
|
|
|
PhabricatorRepositoryType::REPOSITORY_TYPE_GIT => 'Git',
|
|
|
|
PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL => 'Mercurial',
|
2012-03-24 20:11:51 +01:00
|
|
|
PhabricatorRepositoryType::REPOSITORY_TYPE_SVN => 'Svn',
|
2012-01-19 20:49:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$name = idx($map, $repository->getVersionControlSystem());
|
|
|
|
if (!$name) {
|
|
|
|
throw new Exception("Unsupported VCS!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = str_replace('Diffusion', 'Diffusion'.$name, $base_class);
|
|
|
|
$obj = new $class();
|
|
|
|
$obj->request = $request;
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
final protected function getRequest() {
|
|
|
|
return $this->request;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract protected function executeQuery();
|
2012-03-23 23:32:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* -( Query Utilities )---------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-03-26 21:21:48 +02:00
|
|
|
final protected function loadCommitsByIdentifiers(array $identifiers) {
|
2012-03-23 23:32:26 +01:00
|
|
|
if (!$identifiers) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$commits = array();
|
|
|
|
$commit_data = array();
|
|
|
|
|
|
|
|
$drequest = $this->getRequest();
|
|
|
|
$repository = $drequest->getRepository();
|
|
|
|
|
|
|
|
$commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
|
|
|
|
'repositoryID = %d AND commitIdentifier IN (%Ls)',
|
|
|
|
$repository->getID(),
|
|
|
|
$identifiers);
|
|
|
|
$commits = mpull($commits, null, 'getCommitIdentifier');
|
|
|
|
|
2012-05-10 02:28:57 +02:00
|
|
|
// Build empty commit objects for every commit, so we can show unparsed
|
|
|
|
// commits in history views as "unparsed" instead of not showing them. This
|
|
|
|
// makes the process of importing and parsing commits much clearer to the
|
|
|
|
// user.
|
2012-03-26 21:21:48 +02:00
|
|
|
|
2012-05-10 02:28:57 +02:00
|
|
|
$commit_list = array();
|
|
|
|
foreach ($identifiers as $identifier) {
|
|
|
|
$commit_obj = idx($commits, $identifier);
|
|
|
|
if (!$commit_obj) {
|
|
|
|
$commit_obj = new PhabricatorRepositoryCommit();
|
|
|
|
$commit_obj->setRepositoryID($repository->getID());
|
|
|
|
$commit_obj->setCommitIdentifier($identifier);
|
|
|
|
$commit_obj->setIsUnparsed(true);
|
|
|
|
$commit_obj->makeEphemeral();
|
|
|
|
}
|
|
|
|
$commit_list[$identifier] = $commit_obj;
|
|
|
|
}
|
|
|
|
$commits = $commit_list;
|
|
|
|
|
|
|
|
$commit_ids = array_filter(mpull($commits, 'getID'));
|
|
|
|
if ($commit_ids) {
|
|
|
|
$commit_data = id(new PhabricatorRepositoryCommitData())->loadAllWhere(
|
|
|
|
'commitID in (%Ld)',
|
|
|
|
$commit_ids);
|
|
|
|
$commit_data = mpull($commit_data, null, 'getCommitID');
|
2012-03-23 23:32:26 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 21:21:48 +02:00
|
|
|
foreach ($commits as $commit) {
|
2012-05-10 02:28:57 +02:00
|
|
|
if (!$commit->getID()) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-26 21:21:48 +02:00
|
|
|
if (idx($commit_data, $commit->getID())) {
|
|
|
|
$commit->attachCommitData($commit_data[$commit->getID()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $commits;
|
|
|
|
}
|
|
|
|
|
|
|
|
final protected function loadHistoryForCommitIdentifiers(array $identifiers) {
|
|
|
|
if (!$identifiers) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$drequest = $this->getRequest();
|
|
|
|
$repository = $drequest->getRepository();
|
|
|
|
$commits = self::loadCommitsByIdentifiers($identifiers);
|
|
|
|
|
2012-04-29 01:37:41 +02:00
|
|
|
if (!$commits) {
|
|
|
|
return array();
|
|
|
|
}
|
2012-03-26 21:21:48 +02:00
|
|
|
|
|
|
|
$path = $drequest->getPath();
|
|
|
|
|
2012-03-23 23:32:26 +01:00
|
|
|
$conn_r = $repository->establishConnection('r');
|
|
|
|
|
|
|
|
$path_normal = DiffusionPathIDQuery::normalizePath($path);
|
|
|
|
$paths = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT id, path FROM %T WHERE pathHash IN (%Ls)',
|
|
|
|
PhabricatorRepository::TABLE_PATH,
|
|
|
|
array(md5($path_normal)));
|
|
|
|
$paths = ipull($paths, 'id', 'path');
|
|
|
|
$path_id = idx($paths, $path_normal);
|
|
|
|
|
2012-05-10 02:28:57 +02:00
|
|
|
$commit_ids = array_filter(mpull($commits, 'getID'));
|
|
|
|
|
|
|
|
$path_changes = array();
|
|
|
|
if ($path_id && $commit_ids) {
|
|
|
|
$path_changes = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT * FROM %T WHERE commitID IN (%Ld) AND pathID = %d',
|
|
|
|
PhabricatorRepository::TABLE_PATHCHANGE,
|
|
|
|
$commit_ids,
|
|
|
|
$path_id);
|
|
|
|
$path_changes = ipull($path_changes, null, 'commitID');
|
|
|
|
}
|
2012-03-23 23:32:26 +01:00
|
|
|
|
|
|
|
$history = array();
|
|
|
|
foreach ($identifiers as $identifier) {
|
|
|
|
$item = new DiffusionPathChange();
|
|
|
|
$item->setCommitIdentifier($identifier);
|
|
|
|
$commit = idx($commits, $identifier);
|
|
|
|
if ($commit) {
|
|
|
|
$item->setCommit($commit);
|
2012-03-26 21:21:48 +02:00
|
|
|
try {
|
|
|
|
$item->setCommitData($commit->getCommitData());
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
// Ignore, commit just doesn't have data.
|
2012-03-23 23:32:26 +01:00
|
|
|
}
|
|
|
|
$change = idx($path_changes, $commit->getID());
|
|
|
|
if ($change) {
|
|
|
|
$item->setChangeType($change['changeType']);
|
|
|
|
$item->setFileType($change['fileType']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$history[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $history;
|
|
|
|
}
|
2012-01-19 20:49:51 +01:00
|
|
|
}
|