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 {
|
|
|
|
|
|
|
|
protected function initializeFromAphrontRequestDictionary() {
|
|
|
|
parent::initializeFromAphrontRequestDictionary();
|
|
|
|
|
|
|
|
$path = $this->path;
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
|
|
|
|
$branch = array_shift($parts);
|
|
|
|
$this->branch = $this->decodeBranchName($branch);
|
|
|
|
|
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) {
|
|
|
|
$local_path = $this->repository->getDetail('local-path');
|
|
|
|
|
|
|
|
// 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-03-08 23:29:02 +01:00
|
|
|
execx(
|
2011-03-14 17:42:32 +01:00
|
|
|
'(cd %s && git rev-parse --verify %s)',
|
2011-03-08 23:29:02 +01:00
|
|
|
$local_path,
|
|
|
|
$branch);
|
|
|
|
|
|
|
|
if ($this->commit) {
|
2011-03-14 00:19:39 +01:00
|
|
|
list($commit) = execx(
|
2011-03-14 17:42:32 +01:00
|
|
|
'(cd %s && git rev-parse --verify %s)',
|
2011-03-08 23:29:02 +01:00
|
|
|
$local_path,
|
|
|
|
$this->commit);
|
2011-03-14 00:19:39 +01:00
|
|
|
|
|
|
|
// Beyond verifying them, expand commit short forms to full 40-character
|
|
|
|
// sha1s.
|
|
|
|
$this->commit = trim($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-03-08 23:29:02 +01:00
|
|
|
list($contains) = execx(
|
2011-03-14 17:42:32 +01:00
|
|
|
'(cd %s && git branch --contains %s)',
|
2011-03-08 23:29:02 +01:00
|
|
|
$local_path,
|
|
|
|
$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-03-31 07:08:41 +02:00
|
|
|
return $this->repository->getDetail('default-branch', 'origin/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/'.
|
|
|
|
$this->branch.'/'.$this->path;
|
|
|
|
}
|
|
|
|
|
2011-03-08 23:29:02 +01:00
|
|
|
public function getCommit() {
|
|
|
|
if ($this->commit) {
|
|
|
|
return $this->commit;
|
|
|
|
}
|
|
|
|
return $this->getBranch();
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return str_replace(':', '/', $branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function encodeBranchName($branch) {
|
|
|
|
return str_replace('/', ':', $branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|