2011-01-24 20:01:53 +01:00
|
|
|
<?php
|
|
|
|
|
2014-05-25 17:09:01 +02:00
|
|
|
final class DifferentialChangeset extends DifferentialDAO
|
|
|
|
implements PhabricatorPolicyInterface {
|
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();
|
2013-09-03 15:02:14 +02:00
|
|
|
private $hunks = self::ATTACHABLE;
|
2014-05-25 17:09:01 +02:00
|
|
|
private $diff = self::ATTACHABLE;
|
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,
|
2014-09-29 00:12:58 +02:00
|
|
|
),
|
|
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
2016-12-15 20:27:49 +01:00
|
|
|
'oldFile' => 'bytes?',
|
|
|
|
'filename' => 'bytes',
|
2014-09-29 00:12:58 +02:00
|
|
|
'changeType' => 'uint32',
|
|
|
|
'fileType' => 'uint32',
|
|
|
|
'addLines' => 'uint32',
|
|
|
|
'delLines' => 'uint32',
|
2014-10-01 16:59:44 +02:00
|
|
|
|
|
|
|
// T6203/NULLABILITY
|
|
|
|
// These should all be non-nullable, and store reasonable default
|
|
|
|
// JSON values if empty.
|
|
|
|
'awayPaths' => 'text?',
|
|
|
|
'metadata' => 'text?',
|
|
|
|
'oldProperties' => 'text?',
|
|
|
|
'newProperties' => 'text?',
|
2014-09-29 00:12:58 +02:00
|
|
|
),
|
|
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
|
|
'diffID' => array(
|
|
|
|
'columns' => array('diffID'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
) + parent::getConfiguration();
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAffectedLineCount() {
|
|
|
|
return $this->getAddLines() + $this->getDelLines();
|
|
|
|
}
|
|
|
|
|
2011-01-25 00:52:35 +01:00
|
|
|
public function attachHunks(array $hunks) {
|
2012-04-04 22:13:08 +02:00
|
|
|
assert_instances_of($hunks, 'DifferentialHunk');
|
2011-01-25 00:52:35 +01:00
|
|
|
$this->hunks = $hunks;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHunks() {
|
2013-09-03 15:02:14 +02:00
|
|
|
return $this->assertAttached($this->hunks);
|
2011-01-25 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-09-03 15:02:14 +02:00
|
|
|
if ($this->hunks === self::ATTACHABLE) {
|
2011-01-25 00:52:35 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
public function save() {
|
2012-06-19 20:52:50 +02:00
|
|
|
$this->openTransaction();
|
2011-01-24 20:36:53 +01:00
|
|
|
$ret = parent::save();
|
|
|
|
foreach ($this->unsavedHunks as $hunk) {
|
|
|
|
$hunk->setChangesetID($this->getID());
|
|
|
|
$hunk->save();
|
|
|
|
}
|
2012-06-19 20:52:50 +02:00
|
|
|
$this->saveTransaction();
|
2011-01-24 20:36:53 +01:00
|
|
|
return $ret;
|
|
|
|
}
|
2011-01-24 20:01:53 +01:00
|
|
|
|
|
|
|
public function delete() {
|
2012-06-19 20:52:50 +02:00
|
|
|
$this->openTransaction();
|
2014-05-25 17:59:31 +02:00
|
|
|
|
2015-01-22 21:17:04 +01:00
|
|
|
$modern_hunks = id(new DifferentialModernHunk())->loadAllWhere(
|
2014-05-25 21:02:34 +02:00
|
|
|
'changesetID = %d',
|
|
|
|
$this->getID());
|
|
|
|
foreach ($modern_hunks as $modern_hunk) {
|
|
|
|
$modern_hunk->delete();
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|
2014-05-25 17:59:31 +02:00
|
|
|
|
2012-12-30 22:44:48 +01:00
|
|
|
$this->unsavedHunks = array();
|
2012-06-23 01:16:44 +02:00
|
|
|
|
|
|
|
queryfx(
|
|
|
|
$this->establishConnection('w'),
|
|
|
|
'DELETE FROM %T WHERE id = %d',
|
|
|
|
self::TABLE_CACHE,
|
|
|
|
$this->getID());
|
|
|
|
|
|
|
|
$ret = parent::delete();
|
2012-06-19 20:52:50 +02: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() {
|
2012-06-15 09:53:26 +02:00
|
|
|
$file = mpull($this->getHunks(), 'makeNewFile');
|
|
|
|
return implode('', $file);
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function makeOldFile() {
|
2012-06-15 09:53:26 +02:00
|
|
|
$file = mpull($this->getHunks(), 'makeOldFile');
|
|
|
|
return implode('', $file);
|
|
|
|
}
|
|
|
|
|
2012-05-23 01:09:49 +02:00
|
|
|
public function makeChangesWithContext($num_lines = 3) {
|
|
|
|
$with_context = array();
|
|
|
|
foreach ($this->getHunks() as $hunk) {
|
|
|
|
$context = array();
|
|
|
|
$changes = explode("\n", $hunk->getChanges());
|
|
|
|
foreach ($changes as $l => $line) {
|
2012-12-06 16:52:33 +01:00
|
|
|
$type = substr($line, 0, 1);
|
|
|
|
if ($type == '+' || $type == '-') {
|
2012-06-18 21:48:05 +02:00
|
|
|
$context += array_fill($l - $num_lines, 2 * $num_lines + 1, true);
|
2012-05-23 01:09:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$with_context[] = array_intersect_key($changes, $context);
|
|
|
|
}
|
2012-06-15 05:39:48 +02:00
|
|
|
return array_mergev($with_context);
|
2012-05-23 01:09:49 +02: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(
|
2012-05-23 01:09:49 +02:00
|
|
|
PhabricatorRepository $repository = null,
|
2012-04-10 08:42:12 +02:00
|
|
|
DifferentialDiff $diff = null) {
|
2011-04-06 05:49:31 +02:00
|
|
|
|
|
|
|
$base = '/';
|
2012-04-10 08:42:12 +02:00
|
|
|
if ($diff && $diff->getSourceControlPath()) {
|
2011-04-06 05:49:31 +02:00
|
|
|
$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, '/');
|
|
|
|
|
2012-05-23 01:09:49 +02:00
|
|
|
$svn = PhabricatorRepositoryType::REPOSITORY_TYPE_SVN;
|
|
|
|
if ($repository && $repository->getVersionControlSystem() == $svn) {
|
2011-04-06 05:49:31 +02:00
|
|
|
$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-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;
|
|
|
|
}
|
|
|
|
|
2014-05-25 17:59:31 +02:00
|
|
|
public function attachDiff(DifferentialDiff $diff) {
|
|
|
|
$this->diff = $diff;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDiff() {
|
|
|
|
return $this->assertAttached($this->diff);
|
|
|
|
}
|
|
|
|
|
2014-05-25 17:09:01 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
2014-05-25 17:59:31 +02:00
|
|
|
return $this->getDiff()->getPolicy($capability);
|
2014-05-25 17:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
|
2014-05-25 17:59:31 +02:00
|
|
|
return $this->getDiff()->hasAutomaticCapability($capability, $viewer);
|
2014-05-25 17:09:01 +02:00
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|