2011-01-24 20:01:53 +01:00
|
|
|
<?php
|
|
|
|
|
2013-07-01 21:38:42 +02:00
|
|
|
final class DifferentialDiff
|
|
|
|
extends DifferentialDAO
|
2013-12-26 19:40:52 +01:00
|
|
|
implements
|
|
|
|
PhabricatorPolicyInterface,
|
2014-04-18 01:03:24 +02:00
|
|
|
HarbormasterBuildableInterface,
|
|
|
|
PhabricatorApplicationTransactionInterface {
|
2011-01-24 20:01:53 +01:00
|
|
|
|
|
|
|
protected $revisionID;
|
2011-01-30 19:37:36 +01:00
|
|
|
protected $authorPHID;
|
2014-01-27 00:29:22 +01:00
|
|
|
protected $repositoryPHID;
|
2011-01-24 20:01:53 +01:00
|
|
|
|
|
|
|
protected $sourceMachine;
|
|
|
|
protected $sourcePath;
|
|
|
|
|
|
|
|
protected $sourceControlSystem;
|
|
|
|
protected $sourceControlBaseRevision;
|
|
|
|
protected $sourceControlPath;
|
|
|
|
|
|
|
|
protected $lintStatus;
|
|
|
|
protected $unitStatus;
|
|
|
|
|
|
|
|
protected $lineCount;
|
|
|
|
|
|
|
|
protected $branch;
|
2012-06-30 23:45:30 +02:00
|
|
|
protected $bookmark;
|
2011-01-24 20:01:53 +01:00
|
|
|
|
2011-04-06 05:49:31 +02:00
|
|
|
protected $arcanistProjectPHID;
|
2011-01-24 20:01:53 +01:00
|
|
|
protected $creationMethod;
|
2011-04-06 05:49:31 +02:00
|
|
|
protected $repositoryUUID;
|
2011-01-24 20:01:53 +01:00
|
|
|
|
2011-02-05 02:53:14 +01:00
|
|
|
protected $description;
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
private $unsavedChangesets = array();
|
2013-09-03 15:02:14 +02:00
|
|
|
private $changesets = self::ATTACHABLE;
|
2013-09-17 22:55:41 +02:00
|
|
|
private $arcanistProject = self::ATTACHABLE;
|
2013-09-27 03:45:04 +02:00
|
|
|
private $revision = self::ATTACHABLE;
|
2014-02-27 20:06:37 +01:00
|
|
|
private $properties = array();
|
2011-01-24 20:36:53 +01:00
|
|
|
|
2013-11-06 22:59:06 +01:00
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_AUX_PHID => true,
|
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generatePHID() {
|
|
|
|
return PhabricatorPHID::generateNewPHID(
|
|
|
|
DifferentialPHIDTypeDiff::TYPECONST);
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
public function addUnsavedChangeset(DifferentialChangeset $changeset) {
|
2011-01-25 00:52:35 +01:00
|
|
|
if ($this->changesets === null) {
|
|
|
|
$this->changesets = array();
|
|
|
|
}
|
2011-01-24 20:36:53 +01:00
|
|
|
$this->unsavedChangesets[] = $changeset;
|
2011-01-25 00:52:35 +01:00
|
|
|
$this->changesets[] = $changeset;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attachChangesets(array $changesets) {
|
2012-04-04 22:13:08 +02:00
|
|
|
assert_instances_of($changesets, 'DifferentialChangeset');
|
2011-01-25 00:52:35 +01:00
|
|
|
$this->changesets = $changesets;
|
2011-01-24 20:36:53 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-01-25 00:52:35 +01:00
|
|
|
public function getChangesets() {
|
2013-09-03 15:02:14 +02:00
|
|
|
return $this->assertAttached($this->changesets);
|
2011-01-25 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
public function loadChangesets() {
|
|
|
|
if (!$this->getID()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return id(new DifferentialChangeset())->loadAllWhere(
|
|
|
|
'diffID = %d',
|
|
|
|
$this->getID());
|
|
|
|
}
|
|
|
|
|
2013-09-17 22:55:41 +02:00
|
|
|
public function attachArcanistProject(
|
2013-09-24 19:48:40 +02:00
|
|
|
PhabricatorRepositoryArcanistProject $project = null) {
|
2013-09-17 22:55:41 +02:00
|
|
|
$this->arcanistProject = $project;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArcanistProject() {
|
|
|
|
return $this->assertAttached($this->arcanistProject);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArcanistProjectName() {
|
|
|
|
$name = '';
|
|
|
|
if ($this->arcanistProject) {
|
|
|
|
$project = $this->getArcanistProject();
|
|
|
|
$name = $project->getName();
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
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->unsavedChangesets as $changeset) {
|
2011-01-24 21:07:34 +01:00
|
|
|
$changeset->setDiffID($this->getID());
|
|
|
|
$changeset->save();
|
2011-01-24 20:36:53 +01:00
|
|
|
}
|
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();
|
2011-01-24 20:01:53 +01:00
|
|
|
foreach ($this->loadChangesets() as $changeset) {
|
|
|
|
$changeset->delete();
|
|
|
|
}
|
2012-06-19 20:52:50 +02:00
|
|
|
|
|
|
|
$properties = id(new DifferentialDiffProperty())->loadAllWhere(
|
|
|
|
'diffID = %d',
|
|
|
|
$this->getID());
|
|
|
|
foreach ($properties as $prop) {
|
|
|
|
$prop->delete();
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
$ret = parent::delete();
|
2012-06-19 20:52:50 +02:00
|
|
|
$this->saveTransaction();
|
2011-01-24 20:01:53 +01:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function newFromRawChanges(array $changes) {
|
2012-04-04 22:13:08 +02:00
|
|
|
assert_instances_of($changes, 'ArcanistDiffChange');
|
2011-01-24 20:01:53 +01:00
|
|
|
$diff = new DifferentialDiff();
|
|
|
|
|
2013-07-01 18:02:55 +02:00
|
|
|
// There may not be any changes; initialize the changesets list so that
|
|
|
|
// we don't throw later when accessing it.
|
|
|
|
$diff->attachChangesets(array());
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
$lines = 0;
|
|
|
|
foreach ($changes as $change) {
|
2012-10-19 19:29:19 +02:00
|
|
|
if ($change->getType() == ArcanistDiffChangeType::TYPE_MESSAGE) {
|
|
|
|
// If a user pastes a diff into Differential which includes a commit
|
|
|
|
// message (e.g., they ran `git show` to generate it), discard that
|
|
|
|
// change when constructing a DifferentialDiff.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
$changeset = new DifferentialChangeset();
|
|
|
|
$add_lines = 0;
|
|
|
|
$del_lines = 0;
|
2012-09-21 20:57:45 +02:00
|
|
|
$first_line = PHP_INT_MAX;
|
Allow DifferentialDiff to construct proper DifferentialChangeset objects from
diffs which add empty files
Summary:
See T507 and some others. We now parse empty git diffs correctly, but the logic
to build DifferentialDiffs out of them leaves the objects with 'null' for
$changesets, when it should be array().
Further layers later throw, believing we have not loaded the changesets, when we
actually have, there just aren't any.
Test Plan: Viewed rJX05d493e17fbbb29f29e4880be6834d1d7415374e in Diffusion,
which adds an empty README file. No exception thrown.
Reviewers: jungejason, nh, tuomaspelkonen, aran
Reviewed By: nh
CC: aran, nh
Differential Revision: 1038
2011-10-23 23:33:53 +02:00
|
|
|
$hunks = $change->getHunks();
|
|
|
|
if ($hunks) {
|
|
|
|
foreach ($hunks as $hunk) {
|
|
|
|
$dhunk = new DifferentialHunk();
|
|
|
|
$dhunk->setOldOffset($hunk->getOldOffset());
|
|
|
|
$dhunk->setOldLen($hunk->getOldLength());
|
|
|
|
$dhunk->setNewOffset($hunk->getNewOffset());
|
|
|
|
$dhunk->setNewLen($hunk->getNewLength());
|
|
|
|
$dhunk->setChanges($hunk->getCorpus());
|
|
|
|
$changeset->addUnsavedHunk($dhunk);
|
|
|
|
$add_lines += $hunk->getAddLines();
|
|
|
|
$del_lines += $hunk->getDelLines();
|
2012-09-21 20:57:45 +02:00
|
|
|
$added_lines = $hunk->getChangedLines('new');
|
|
|
|
if ($added_lines) {
|
|
|
|
$first_line = min($first_line, head_key($added_lines));
|
|
|
|
}
|
Allow DifferentialDiff to construct proper DifferentialChangeset objects from
diffs which add empty files
Summary:
See T507 and some others. We now parse empty git diffs correctly, but the logic
to build DifferentialDiffs out of them leaves the objects with 'null' for
$changesets, when it should be array().
Further layers later throw, believing we have not loaded the changesets, when we
actually have, there just aren't any.
Test Plan: Viewed rJX05d493e17fbbb29f29e4880be6834d1d7415374e in Diffusion,
which adds an empty README file. No exception thrown.
Reviewers: jungejason, nh, tuomaspelkonen, aran
Reviewed By: nh
CC: aran, nh
Differential Revision: 1038
2011-10-23 23:33:53 +02:00
|
|
|
}
|
2012-06-27 23:45:37 +02:00
|
|
|
$lines += $add_lines + $del_lines;
|
Allow DifferentialDiff to construct proper DifferentialChangeset objects from
diffs which add empty files
Summary:
See T507 and some others. We now parse empty git diffs correctly, but the logic
to build DifferentialDiffs out of them leaves the objects with 'null' for
$changesets, when it should be array().
Further layers later throw, believing we have not loaded the changesets, when we
actually have, there just aren't any.
Test Plan: Viewed rJX05d493e17fbbb29f29e4880be6834d1d7415374e in Diffusion,
which adds an empty README file. No exception thrown.
Reviewers: jungejason, nh, tuomaspelkonen, aran
Reviewed By: nh
CC: aran, nh
Differential Revision: 1038
2011-10-23 23:33:53 +02:00
|
|
|
} else {
|
|
|
|
// This happens when you add empty files.
|
|
|
|
$changeset->attachHunks(array());
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|
|
|
|
|
2012-09-21 20:57:45 +02:00
|
|
|
$metadata = $change->getAllMetadata();
|
|
|
|
if ($first_line != PHP_INT_MAX) {
|
|
|
|
$metadata['line:first'] = $first_line;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
$changeset->setOldFile($change->getOldPath());
|
|
|
|
$changeset->setFilename($change->getCurrentPath());
|
|
|
|
$changeset->setChangeType($change->getType());
|
|
|
|
|
|
|
|
$changeset->setFileType($change->getFileType());
|
2012-09-21 20:57:45 +02:00
|
|
|
$changeset->setMetadata($metadata);
|
2011-01-24 20:01:53 +01:00
|
|
|
$changeset->setOldProperties($change->getOldProperties());
|
|
|
|
$changeset->setNewProperties($change->getNewProperties());
|
|
|
|
$changeset->setAwayPaths($change->getAwayPaths());
|
|
|
|
$changeset->setAddLines($add_lines);
|
|
|
|
$changeset->setDelLines($del_lines);
|
|
|
|
|
2011-01-24 20:36:53 +01:00
|
|
|
$diff->addUnsavedChangeset($changeset);
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|
|
|
|
$diff->setLineCount($lines);
|
|
|
|
|
2013-01-09 22:11:17 +01:00
|
|
|
$parser = new DifferentialChangesetParser();
|
|
|
|
$changesets = $parser->detectCopiedCode(
|
|
|
|
$diff->getChangesets(),
|
|
|
|
$min_width = 30,
|
|
|
|
$min_lines = 3);
|
|
|
|
$diff->attachChangesets($changesets);
|
2012-04-28 08:00:30 +02:00
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
return $diff;
|
|
|
|
}
|
|
|
|
|
2012-04-28 08:00:30 +02:00
|
|
|
|
2011-10-14 21:08:31 +02:00
|
|
|
public function getDiffDict() {
|
|
|
|
$dict = array(
|
|
|
|
'id' => $this->getID(),
|
|
|
|
'revisionID' => $this->getRevisionID(),
|
2013-01-31 05:25:07 +01:00
|
|
|
'dateCreated' => $this->getDateCreated(),
|
|
|
|
'dateModified' => $this->getDateModified(),
|
2011-10-14 21:08:31 +02:00
|
|
|
'sourceControlBaseRevision' => $this->getSourceControlBaseRevision(),
|
|
|
|
'sourceControlPath' => $this->getSourceControlPath(),
|
2012-01-09 20:42:37 +01:00
|
|
|
'sourceControlSystem' => $this->getSourceControlSystem(),
|
|
|
|
'branch' => $this->getBranch(),
|
2012-06-30 23:45:30 +02:00
|
|
|
'bookmark' => $this->getBookmark(),
|
2012-06-16 00:09:42 +02:00
|
|
|
'creationMethod' => $this->getCreationMethod(),
|
2012-06-16 01:16:03 +02:00
|
|
|
'description' => $this->getDescription(),
|
2011-10-14 21:08:31 +02:00
|
|
|
'unitStatus' => $this->getUnitStatus(),
|
|
|
|
'lintStatus' => $this->getLintStatus(),
|
|
|
|
'changes' => array(),
|
|
|
|
'properties' => array(),
|
2013-09-17 22:55:41 +02:00
|
|
|
'projectName' => $this->getArcanistProjectName()
|
2011-10-14 21:08:31 +02:00
|
|
|
);
|
|
|
|
|
2013-09-17 22:55:41 +02:00
|
|
|
$dict['changes'] = $this->buildChangesList();
|
|
|
|
|
|
|
|
$properties = id(new DifferentialDiffProperty())->loadAllWhere(
|
|
|
|
'diffID = %d',
|
|
|
|
$this->getID());
|
|
|
|
foreach ($properties as $property) {
|
|
|
|
$dict['properties'][$property->getName()] = $property->getData();
|
|
|
|
|
|
|
|
if ($property->getName() == 'local:commits') {
|
|
|
|
foreach ($property->getData() as $commit) {
|
|
|
|
$dict['authorName'] = $commit['author'];
|
|
|
|
$dict['authorEmail'] = idx($commit, 'authorEmail');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildChangesList() {
|
|
|
|
$changes = array();
|
2011-10-14 21:08:31 +02:00
|
|
|
foreach ($this->getChangesets() as $changeset) {
|
|
|
|
$hunks = array();
|
|
|
|
foreach ($changeset->getHunks() as $hunk) {
|
|
|
|
$hunks[] = array(
|
|
|
|
'oldOffset' => $hunk->getOldOffset(),
|
|
|
|
'newOffset' => $hunk->getNewOffset(),
|
|
|
|
'oldLength' => $hunk->getOldLen(),
|
|
|
|
'newLength' => $hunk->getNewLen(),
|
|
|
|
'addLines' => null,
|
|
|
|
'delLines' => null,
|
|
|
|
'isMissingOldNewline' => null,
|
|
|
|
'isMissingNewNewline' => null,
|
|
|
|
'corpus' => $hunk->getChanges(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$change = array(
|
2013-05-03 17:12:43 +02:00
|
|
|
'id' => $changeset->getID(),
|
2011-10-14 21:08:31 +02:00
|
|
|
'metadata' => $changeset->getMetadata(),
|
|
|
|
'oldPath' => $changeset->getOldFile(),
|
2012-01-17 08:05:44 +01:00
|
|
|
'currentPath' => $changeset->getFilename(),
|
2011-10-14 21:08:31 +02:00
|
|
|
'awayPaths' => $changeset->getAwayPaths(),
|
|
|
|
'oldProperties' => $changeset->getOldProperties(),
|
|
|
|
'newProperties' => $changeset->getNewProperties(),
|
|
|
|
'type' => $changeset->getChangeType(),
|
|
|
|
'fileType' => $changeset->getFileType(),
|
|
|
|
'commitHash' => null,
|
2011-12-14 20:52:28 +01:00
|
|
|
'addLines' => $changeset->getAddLines(),
|
|
|
|
'delLines' => $changeset->getDelLines(),
|
2011-10-14 21:08:31 +02:00
|
|
|
'hunks' => $hunks,
|
|
|
|
);
|
2013-09-17 22:55:41 +02:00
|
|
|
$changes[] = $change;
|
2011-10-14 21:08:31 +02:00
|
|
|
}
|
2013-09-17 22:55:41 +02:00
|
|
|
return $changes;
|
2012-11-09 22:33:58 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 03:45:04 +02:00
|
|
|
public function getRevision() {
|
|
|
|
return $this->assertAttached($this->revision);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attachRevision(DifferentialRevision $revision = null) {
|
|
|
|
$this->revision = $revision;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-02-27 20:06:37 +01:00
|
|
|
public function attachProperty($key, $value) {
|
|
|
|
$this->properties[$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProperty($key) {
|
|
|
|
return $this->assertAttachedKey($this->properties, $key);
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:38:42 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
2013-09-27 03:45:04 +02:00
|
|
|
if ($this->getRevision()) {
|
|
|
|
return $this->getRevision()->getPolicy($capability);
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:38:42 +02:00
|
|
|
return PhabricatorPolicies::POLICY_USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
|
2013-09-27 03:45:04 +02:00
|
|
|
if ($this->getRevision()) {
|
|
|
|
return $this->getRevision()->hasAutomaticCapability($capability, $viewer);
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:38:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-27 17:43:41 +02:00
|
|
|
public function describeAutomaticCapability($capability) {
|
|
|
|
if ($this->getRevision()) {
|
|
|
|
return pht(
|
|
|
|
'This diff is attached to a revision, and inherits its policies.');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:40:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* -( HarbormasterBuildableInterface )------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getHarbormasterBuildablePHID() {
|
|
|
|
return $this->getPHID();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHarbormasterContainerPHID() {
|
|
|
|
if ($this->getRevisionID()) {
|
|
|
|
$revision = id(new DifferentialRevision())->load($this->getRevisionID());
|
|
|
|
if ($revision) {
|
|
|
|
return $revision->getPHID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-18 01:03:24 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getApplicationTransactionEditor() {
|
|
|
|
if (!$this->getRevisionID()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->getRevision()->getApplicationTransactionEditor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getApplicationTransactionObject() {
|
|
|
|
if (!$this->getRevisionID()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->getRevision();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getApplicationTransactionTemplate() {
|
|
|
|
if (!$this->getRevisionID()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->getRevision()->getApplicationTransactionTemplate();
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:01:53 +01:00
|
|
|
}
|