2011-03-08 23:29:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DiffusionGitRequest extends DiffusionRequest {
|
|
|
|
|
2011-04-04 04:20:47 +02:00
|
|
|
protected function initializeFromAphrontRequestDictionary(array $data) {
|
|
|
|
parent::initializeFromAphrontRequestDictionary($data);
|
2011-03-08 23:29:02 +01:00
|
|
|
|
|
|
|
$path = $this->path;
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
|
2011-04-04 07:03:27 +02:00
|
|
|
$branch = array_shift($parts);
|
|
|
|
if ($branch != ':') {
|
2011-04-04 04:20:47 +02:00
|
|
|
$this->branch = $this->decodeBranchName($branch);
|
|
|
|
}
|
2011-03-08 23:29:02 +01:00
|
|
|
|
2011-03-13 01:17:34 +01:00
|
|
|
foreach ($parts as $key => $part) {
|
|
|
|
// Prevent any hyjinx since we're ultimately shipping this to the
|
|
|
|
// filesystem under a lot of git workflows.
|
|
|
|
if ($part == '..') {
|
|
|
|
unset($parts[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 23:29:02 +01:00
|
|
|
$this->path = implode('/', $parts);
|
|
|
|
|
|
|
|
if ($this->repository) {
|
2011-11-10 00:29:41 +01:00
|
|
|
$repository = $this->repository;
|
2011-03-08 23:29:02 +01:00
|
|
|
|
|
|
|
// TODO: This is not terribly efficient and does not produce terribly
|
|
|
|
// good error messages, but it seems better to put error handling code
|
|
|
|
// here than to try to do it in every query.
|
|
|
|
|
|
|
|
$branch = $this->getBranch();
|
2011-03-09 02:31:44 +01:00
|
|
|
|
2011-04-02 02:11:05 +02:00
|
|
|
// TODO: Here, particularly, we should give the user a specific error
|
|
|
|
// message to indicate whether they've typed in some bogus branch and/or
|
|
|
|
// followed a bad link, or misconfigured the default branch in the
|
|
|
|
// Repository tool.
|
2011-11-10 00:29:41 +01:00
|
|
|
list($this->stableCommitName) = $repository->execxLocalCommand(
|
2011-12-22 21:24:12 +01:00
|
|
|
'rev-parse --verify %s/%s',
|
|
|
|
DiffusionBranchInformation::DEFAULT_GIT_REMOTE,
|
2011-03-08 23:29:02 +01:00
|
|
|
$branch);
|
|
|
|
|
|
|
|
if ($this->commit) {
|
2011-11-10 00:29:41 +01:00
|
|
|
list($commit) = $repository->execxLocalCommand(
|
|
|
|
'rev-parse --verify %s',
|
2011-03-08 23:29:02 +01:00
|
|
|
$this->commit);
|
2011-03-14 00:19:39 +01:00
|
|
|
|
|
|
|
// Beyond verifying them, expand commit short forms to full 40-character
|
2011-11-17 20:44:28 +01:00
|
|
|
// hashes.
|
2011-03-14 00:19:39 +01:00
|
|
|
$this->commit = trim($commit);
|
|
|
|
|
2011-05-18 16:44:53 +02:00
|
|
|
// If we have a commit, overwrite the branch commit with the more
|
|
|
|
// specific commit.
|
|
|
|
$this->stableCommitName = $this->commit;
|
|
|
|
|
2011-03-31 07:08:41 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
TODO: Unclear if this is actually a good idea or not; it breaks commit views
|
|
|
|
at the very least.
|
|
|
|
|
2011-11-10 00:29:41 +01:00
|
|
|
list($contains) = $repository->execxLocalCommand(
|
|
|
|
'branch --contains %s',
|
2011-03-08 23:29:02 +01:00
|
|
|
$this->commit);
|
|
|
|
$contains = array_filter(explode("\n", $contains));
|
|
|
|
$found = false;
|
|
|
|
foreach ($contains as $containing_branch) {
|
|
|
|
$containing_branch = trim($containing_branch, "* \n");
|
|
|
|
if ($containing_branch == $branch) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
throw new Exception(
|
|
|
|
"Commit does not exist on this branch!");
|
|
|
|
}
|
2011-03-31 07:08:41 +02:00
|
|
|
*/
|
|
|
|
|
2011-03-08 23:29:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBranch() {
|
|
|
|
if ($this->branch) {
|
|
|
|
return $this->branch;
|
|
|
|
}
|
|
|
|
if ($this->repository) {
|
2011-12-22 21:24:12 +01:00
|
|
|
return $this->repository->getDetail('default-branch', 'master');
|
2011-03-08 23:29:02 +01:00
|
|
|
}
|
|
|
|
throw new Exception("Unable to determine branch!");
|
|
|
|
}
|
|
|
|
|
2011-03-26 08:14:04 +01:00
|
|
|
public function getUriPath() {
|
|
|
|
return '/diffusion/'.$this->getCallsign().'/browse/'.
|
2011-04-04 08:42:33 +02:00
|
|
|
$this->getBranchURIComponent($this->branch).$this->path;
|
2011-03-26 08:14:04 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 23:29:02 +01:00
|
|
|
public function getCommit() {
|
|
|
|
if ($this->commit) {
|
|
|
|
return $this->commit;
|
|
|
|
}
|
2011-12-22 21:24:12 +01:00
|
|
|
$remote = DiffusionBranchInformation::DEFAULT_GIT_REMOTE;
|
|
|
|
return $remote.'/'.$this->getBranch();
|
2011-03-08 23:29:02 +01:00
|
|
|
}
|
|
|
|
|
2011-05-18 16:44:53 +02:00
|
|
|
public function getStableCommitName() {
|
|
|
|
return substr($this->stableCommitName, 0, 16);
|
|
|
|
}
|
|
|
|
|
2011-03-13 01:17:34 +01:00
|
|
|
public function getBranchURIComponent($branch) {
|
|
|
|
return $this->encodeBranchName($branch).'/';
|
|
|
|
}
|
|
|
|
|
2011-03-08 23:29:02 +01:00
|
|
|
private function decodeBranchName($branch) {
|
2011-12-22 21:24:12 +01:00
|
|
|
$branch = str_replace(':', '/', $branch);
|
|
|
|
|
|
|
|
// Backward compatibility for older-style URIs which had an explicit
|
|
|
|
// "origin" remote in the branch name. If a remote is specified, strip it
|
|
|
|
// away.
|
|
|
|
if (strpos($branch, '/') !== false) {
|
|
|
|
$parts = explode('/', $branch);
|
|
|
|
$branch = end($parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $branch;
|
2011-03-08 23:29:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function encodeBranchName($branch) {
|
|
|
|
return str_replace('/', ':', $branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|