2011-01-24 20:01:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-17 08:05:44 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-01-24 20:01:53 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-03-13 19:18:11 +01:00
|
|
|
final class DifferentialChangeset extends DifferentialDAO {
|
2011-01-24 20:01:53 +01:00
|
|
|
|
|
|
|
protected $diffID;
|
|
|
|
protected $oldFile;
|
2012-01-17 08:05:44 +01:00
|
|
|
protected $filename;
|
2011-01-24 20:01:53 +01:00
|
|
|
protected $awayPaths;
|
|
|
|
protected $changeType;
|
|
|
|
protected $fileType;
|
|
|
|
protected $metadata;
|
|
|
|
protected $oldProperties;
|
|
|
|
protected $newProperties;
|
|
|
|
protected $addLines;
|
|
|
|
protected $delLines;
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
private $unsavedHunks = array();
|
2011-01-25 00:52:35 +01:00
|
|
|
private $hunks;
|
2011-01-24 20:36:53 +01:00
|
|
|
|
2011-07-09 00:26:33 +02:00
|
|
|
const TABLE_CACHE = 'differential_changeset_parse_cache';
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
protected function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_SERIALIZATION => array(
|
|
|
|
'metadata' => self::SERIALIZATION_JSON,
|
|
|
|
'oldProperties' => self::SERIALIZATION_JSON,
|
|
|
|
'newProperties' => self::SERIALIZATION_JSON,
|
|
|
|
'awayPaths' => self::SERIALIZATION_JSON,
|
|
|
|
)) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAffectedLineCount() {
|
|
|
|
return $this->getAddLines() + $this->getDelLines();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileType() {
|
|
|
|
return $this->fileType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getChangeType() {
|
|
|
|
return $this->changeType;
|
|
|
|
}
|
|
|
|
|
2011-01-25 00:52:35 +01:00
|
|
|
public function attachHunks(array $hunks) {
|
|
|
|
$this->hunks = $hunks;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHunks() {
|
|
|
|
if ($this->hunks === null) {
|
|
|
|
throw new Exception("Must load and attach hunks first!");
|
|
|
|
}
|
|
|
|
return $this->hunks;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
public function getDisplayFilename() {
|
|
|
|
$name = $this->getFilename();
|
|
|
|
if ($this->getFileType() == DifferentialChangeType::FILE_DIRECTORY) {
|
|
|
|
$name .= '/';
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
public function addUnsavedHunk(DifferentialHunk $hunk) {
|
2011-01-25 00:52:35 +01:00
|
|
|
if ($this->hunks === null) {
|
|
|
|
$this->hunks = array();
|
|
|
|
}
|
|
|
|
$this->hunks[] = $hunk;
|
2011-01-24 20:36:53 +01:00
|
|
|
$this->unsavedHunks[] = $hunk;
|
2011-01-24 20:01:53 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadHunks() {
|
|
|
|
if (!$this->getID()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return id(new DifferentialHunk())->loadAllWhere(
|
|
|
|
'changesetID = %d',
|
|
|
|
$this->getID());
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
public function save() {
|
|
|
|
// TODO: Sort out transactions
|
|
|
|
// $this->openTransaction();
|
|
|
|
$ret = parent::save();
|
|
|
|
foreach ($this->unsavedHunks as $hunk) {
|
|
|
|
$hunk->setChangesetID($this->getID());
|
|
|
|
$hunk->save();
|
|
|
|
}
|
|
|
|
// $this->saveTransaction();
|
|
|
|
return $ret;
|
|
|
|
}
|
2011-01-24 20:01:53 +01:00
|
|
|
|
|
|
|
public function delete() {
|
2011-01-24 20:36:53 +01:00
|
|
|
// $this->openTransaction();
|
2011-01-24 20:01:53 +01:00
|
|
|
foreach ($this->loadHunks() as $hunk) {
|
|
|
|
$hunk->delete();
|
|
|
|
}
|
|
|
|
$this->_hunks = array();
|
|
|
|
$ret = parent::delete();
|
2011-01-24 20:36:53 +01:00
|
|
|
// $this->saveTransaction();
|
2011-01-24 20:01:53 +01:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSortKey() {
|
|
|
|
$sort_key = $this->getFilename();
|
|
|
|
// Sort files with ".h" in them first, so headers (.h, .hpp) come before
|
|
|
|
// implementations (.c, .cpp, .cs).
|
|
|
|
$sort_key = str_replace('.h', '.!h', $sort_key);
|
|
|
|
return $sort_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeNewFile() {
|
|
|
|
$file = array();
|
|
|
|
foreach ($this->getHunks() as $hunk) {
|
|
|
|
$file[] = $hunk->makeNewFile();
|
|
|
|
}
|
|
|
|
return implode("\n", $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeOldFile() {
|
|
|
|
$file = array();
|
|
|
|
foreach ($this->getHunks() as $hunk) {
|
|
|
|
$file[] = $hunk->makeOldFile();
|
|
|
|
}
|
|
|
|
return implode("\n", $file);
|
|
|
|
}
|
2011-02-05 02:53:14 +01:00
|
|
|
|
2011-02-05 01:18:08 +01:00
|
|
|
public function getAnchorName() {
|
|
|
|
return substr(md5($this->getFilename()), 0, 8);
|
|
|
|
}
|
2011-01-24 20:01:53 +01:00
|
|
|
|
2011-04-06 05:49:31 +02:00
|
|
|
public function getAbsoluteRepositoryPath(
|
|
|
|
DifferentialDiff $diff,
|
|
|
|
PhabricatorRepository $repository) {
|
|
|
|
|
|
|
|
$base = '/';
|
|
|
|
if ($diff->getSourceControlPath()) {
|
|
|
|
$base = id(new PhutilURI($diff->getSourceControlPath()))->getPath();
|
|
|
|
}
|
|
|
|
|
2012-01-17 08:05:44 +01:00
|
|
|
$path = $this->getFilename();
|
2011-04-06 05:49:31 +02:00
|
|
|
$path = rtrim($base, '/').'/'.ltrim($path, '/');
|
|
|
|
|
|
|
|
$vcs = $repository->getVersionControlSystem();
|
|
|
|
if ($vcs == PhabricatorRepositoryType::REPOSITORY_TYPE_SVN) {
|
|
|
|
$prefix = $repository->getDetail('remote-uri');
|
|
|
|
$prefix = id(new PhutilURI($prefix))->getPath();
|
|
|
|
if (!strncmp($path, $prefix, strlen($prefix))) {
|
|
|
|
$path = substr($path, strlen($prefix));
|
|
|
|
}
|
|
|
|
$path = '/'.ltrim($path, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2011-06-08 21:39:03 +02:00
|
|
|
/**
|
|
|
|
* Retreive the configured wordwrap width for this changeset.
|
|
|
|
*/
|
|
|
|
public function getWordWrapWidth() {
|
|
|
|
$config = PhabricatorEnv::getEnvConfig('differential.wordwrap');
|
|
|
|
foreach ($config as $regexp => $width) {
|
2012-01-17 08:05:44 +01:00
|
|
|
if (preg_match($regexp, $this->getFilename())) {
|
2011-06-08 21:39:03 +02:00
|
|
|
return $width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 80;
|
|
|
|
}
|
|
|
|
|
2011-07-22 22:15:11 +02:00
|
|
|
public function getWhitespaceMatters() {
|
|
|
|
$config = PhabricatorEnv::getEnvConfig('differential.whitespace-matters');
|
|
|
|
foreach ($config as $regexp) {
|
2012-01-17 08:05:44 +01:00
|
|
|
if (preg_match($regexp, $this->getFilename())) {
|
2011-07-22 22:15:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|