From dec8bac3a3af6065166d485db80fffa70dc2abe3 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 24 Jan 2011 11:01:53 -0800 Subject: [PATCH] Conduit: differential.creatediff --- .arcconfig | 2 +- src/__phutil_library_map__.php | 13 ++ ...duitAPI_differential_creatediff_Method.php | 134 ++++++++++++++++++ .../differential/creatediff/__init__.php | 21 +++ .../lintstatus/DifferentialLintStatus.php | 8 +- .../DifferentialRevisionControlSystem.php | 24 ++++ .../revisioncontrolsystem/__init__.php | 10 ++ .../unitstatus/DifferentialUnitStatus.php | 10 +- .../storage/base/DifferentialDAO.php | 25 ++++ .../differential/storage/base/__init__.php | 10 ++ .../changeset/DifferentialChangeset.php | 129 +++++++++++++++++ .../storage/changeset/__init__.php | 16 +++ .../storage/diff/DifferentialDiff.php | 103 ++++++++++++++ .../differential/storage/diff/__init__.php | 15 ++ .../storage/hunk/DifferentialHunk.php | 52 +++++++ .../differential/storage/hunk/__init__.php | 12 ++ .../storage/revision/DifferentialRevision.php | 35 +++++ .../storage/revision/__init__.php | 12 ++ webroot/index.php | 1 + 19 files changed, 622 insertions(+), 10 deletions(-) create mode 100644 src/applications/conduit/method/differential/creatediff/ConduitAPI_differential_creatediff_Method.php create mode 100644 src/applications/conduit/method/differential/creatediff/__init__.php create mode 100755 src/applications/differential/constants/revisioncontrolsystem/DifferentialRevisionControlSystem.php create mode 100644 src/applications/differential/constants/revisioncontrolsystem/__init__.php create mode 100644 src/applications/differential/storage/base/DifferentialDAO.php create mode 100644 src/applications/differential/storage/base/__init__.php create mode 100644 src/applications/differential/storage/changeset/DifferentialChangeset.php create mode 100644 src/applications/differential/storage/changeset/__init__.php create mode 100644 src/applications/differential/storage/diff/DifferentialDiff.php create mode 100644 src/applications/differential/storage/diff/__init__.php create mode 100644 src/applications/differential/storage/hunk/DifferentialHunk.php create mode 100644 src/applications/differential/storage/hunk/__init__.php create mode 100755 src/applications/differential/storage/revision/DifferentialRevision.php create mode 100644 src/applications/differential/storage/revision/__init__.php diff --git a/.arcconfig b/.arcconfig index 2e452e56a3..7a3efdff76 100644 --- a/.arcconfig +++ b/.arcconfig @@ -1,6 +1,6 @@ { "project_id" : "phabricator", - "conduit_uri" : "http://tools.epriestley-conduit.dev1557.facebook.com/api/", + "conduit_uri" : "http://local.aphront.com/api/", "lint_engine" : "PhutilLintEngine", "unit_engine" : "PhutilUnitTestEngine", "copyright_holder" : "Facebook, Inc.", diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 044b3158f6..181cd51b8f 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -47,11 +47,18 @@ phutil_register_library_map(array( 'AphrontWebpageResponse' => 'aphront/response/webpage', 'ConduitAPIMethod' => 'applications/conduit/method/base', 'ConduitAPIRequest' => 'applications/conduit/protocol/request', + 'ConduitAPI_differential_creatediff_Method' => 'applications/conduit/method/differential/creatediff', 'ConduitAPI_file_upload_Method' => 'applications/conduit/method/file/upload', 'ConduitException' => 'applications/conduit/protocol/exception', 'DifferentialAction' => 'applications/differential/constants/action', 'DifferentialChangeType' => 'applications/differential/constants/changetype', + 'DifferentialChangeset' => 'applications/differential/storage/changeset', + 'DifferentialDAO' => 'applications/differential/storage/base', + 'DifferentialDiff' => 'applications/differential/storage/diff', + 'DifferentialHunk' => 'applications/differential/storage/hunk', 'DifferentialLintStatus' => 'applications/differential/constants/lintstatus', + 'DifferentialRevision' => 'applications/differential/storage/revision', + 'DifferentialRevisionControlSystem' => 'applications/differential/constants/revisioncontrolsystem', 'DifferentialRevisionStatus' => 'applications/differential/constants/revisionstatus', 'DifferentialUnitStatus' => 'applications/differential/constants/unitstatus', 'LiskDAO' => 'storage/lisk/dao', @@ -141,7 +148,13 @@ phutil_register_library_map(array( 'AphrontSideNavView' => 'AphrontView', 'AphrontTableView' => 'AphrontView', 'AphrontWebpageResponse' => 'AphrontResponse', + 'ConduitAPI_differential_creatediff_Method' => 'ConduitAPIMethod', 'ConduitAPI_file_upload_Method' => 'ConduitAPIMethod', + 'DifferentialChangeset' => 'DifferentialDAO', + 'DifferentialDAO' => 'PhabricatorLiskDAO', + 'DifferentialDiff' => 'DifferentialDAO', + 'DifferentialHunk' => 'DifferentialDAO', + 'DifferentialRevision' => 'DifferentialDAO', 'PhabricatorConduitAPIController' => 'PhabricatorConduitController', 'PhabricatorConduitConnectionLog' => 'PhabricatorConduitDAO', 'PhabricatorConduitConsoleController' => 'PhabricatorConduitController', diff --git a/src/applications/conduit/method/differential/creatediff/ConduitAPI_differential_creatediff_Method.php b/src/applications/conduit/method/differential/creatediff/ConduitAPI_differential_creatediff_Method.php new file mode 100644 index 0000000000..63bc4d7ba6 --- /dev/null +++ b/src/applications/conduit/method/differential/creatediff/ConduitAPI_differential_creatediff_Method.php @@ -0,0 +1,134 @@ + 'required list', + 'sourceMachine' => 'required string', + 'sourcePath' => 'required string', + 'branch' => 'required string', + 'sourceControlSystem' => 'required enum', + 'sourceControlPath' => 'required string', + 'sourceControlBaseRevision' => 'required string', + 'parentRevisionID' => 'optional revisionid', + 'creationMethod' => 'optional string', + 'ownerPHID' => 'optional phid', + 'arcanistProject' => 'optional string', + 'lintStatus' => + 'required enum', + 'unitStatus' => + 'required enum', + ); + } + + public function defineReturnType() { + return 'nonempty dict'; + } + + public function defineErrorTypes() { + return array( + ); + } + + protected function execute(ConduitAPIRequest $request) { + $change_data = $request->getValue('changes'); + + $changes = array(); + foreach ($change_data as $dict) { + $changes[] = ArcanistDiffChange::newFromDictionary($dict); + } + + $diff = DifferentialDiff::newFromRawChanges($changes); + $diff->setSourcePath($request->getValue('sourcePath')); + $diff->setSourceMachine($request->getValue('sourceMachine')); + + $diff->setBranch($request->getValue('branch')); + $diff->setCreationMethod($request->getValue('creationMethod')); + $diff->setOwnerPHID($request->getValue('ownerPHID')); + + $parent_id = $request->getValue('parentRevisionID'); + if ($parent_id) { + $parent_rev = id(new DifferentialRevision())->load($parent_id); + if ($parent_rev) { + if ($parent_rev->getStatus() != DifferentialRevisionStatus::COMMITTED) { + $diff->setParentRevisionID($parent_id); + } + } + } + + $system = $request->getValue('sourceControlSystem'); + $diff->setSourceControlSystem($system); + $diff->setSourceControlPath($request->getValue('sourceControlPath')); + $diff->setSourceControlBaseRevision( + $request->getValue('sourceControlBaseRevision')); + + $diff->setArcanistProject($request->getValue('arcanistProject')); + + switch ($request->getValue('lintStatus')) { + case 'skip': + $diff->setLintStatus(DifferentialLintStatus::LINT_SKIP); + break; + case 'okay': + $diff->setLintStatus(DifferentialLintStatus::LINT_OKAY); + break; + case 'warn': + $diff->setLintStatus(DifferentialLintStatus::LINT_WARN); + break; + case 'fail': + $diff->setLintStatus(DifferentialLintStatus::LINT_FAIL); + break; + case 'none': + default: + $diff->setLintStatus(DifferentialLintStatus::LINT_NONE); + break; + } + + switch ($request->getValue('unitStatus')) { + case 'skip': + $diff->setUnitStatus(DifferentialUnitStatus::UNIT_SKIP); + break; + case 'okay': + $diff->setUnitStatus(DifferentialUnitStatus::UNIT_OKAY); + break; + case 'warn': + $diff->setUnitStatus(DifferentialUnitStatus::UNIT_WARN); + break; + case 'fail': + $diff->setUnitStatus(DifferentialUnitStatus::UNIT_FAIL); + break; + case 'none': + default: + $diff->setUnitStatus(DifferentialUnitStatus::UNIT_NONE); + break; + } + + $diff->save(); + + return array( + 'diffid' => $diff->getID(), + 'uri' => '?'//$diff->getURI(), + ); + } + +} diff --git a/src/applications/conduit/method/differential/creatediff/__init__.php b/src/applications/conduit/method/differential/creatediff/__init__.php new file mode 100644 index 0000000000..f43bd3996d --- /dev/null +++ b/src/applications/conduit/method/differential/creatediff/__init__.php @@ -0,0 +1,21 @@ + 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; + } + + public function getDisplayFilename() { + $name = $this->getFilename(); + if ($this->getFileType() == DifferentialChangeType::FILE_DIRECTORY) { + $name .= '/'; + } + return $name; + } + + public function addHunk(DifferentialHunk $hunk) { + if (!isset($this->_hunks)) { + $this->_hunks = array(); + } + $this->_hunks[] = $hunk; + + return $this; + } + + public function attachHunks(array $hunks) { + $this->_hunks = $hunks; + return $this; + } + + public function getHunks() { + if (!isset($this->_hunks)) { + throw new Exception("You must load hunks before accessing them."); + } + return $this->_hunks; + } + + public function loadHunks() { + if (!$this->getID()) { + return array(); + } + return id(new DifferentialHunk())->loadAllWhere( + 'changesetID = %d', + $this->getID()); + } + + + public function delete() { + $this->openTransaction(); + foreach ($this->loadHunks() as $hunk) { + $hunk->delete(); + } + $this->_hunks = array(); + $ret = parent::delete(); + $this->saveTransaction(); + 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); + } + +} diff --git a/src/applications/differential/storage/changeset/__init__.php b/src/applications/differential/storage/changeset/__init__.php new file mode 100644 index 0000000000..3b8085af0a --- /dev/null +++ b/src/applications/differential/storage/changeset/__init__.php @@ -0,0 +1,16 @@ +getID()) { + return array(); + } + return id(new DifferentialChangeset())->loadAllWhere( + 'diffID = %d', + $this->getID()); + } + + public function delete() { + $this->openTransaction(); + foreach ($this->loadChangesets() as $changeset) { + $changeset->delete(); + } + $ret = parent::delete(); + $this->saveTransaction(); + return $ret; + } + + public static function newFromRawChanges(array $changes) { + $diff = new DifferentialDiff(); + + $lines = 0; + foreach ($changes as $change) { + $changeset = new DifferentialChangeset(); + $add_lines = 0; + $del_lines = 0; + foreach ($change->getHunks() as $hunk) { + $dhunk = new Hunk(); + $dhunk->setOldOffset($hunk->getOldOffset()); + $dhunk->setOldLen($hunk->getOldLength()); + $dhunk->setNewOffset($hunk->getNewOffset()); + $dhunk->setNewLen($hunk->getNewLength()); + $dhunk->setChanges($hunk->getCorpus()); + $changeset->addHunk($dhunk); + $add_lines += $hunk->getAddLines(); + $del_lines += $hunk->getDelLines(); + $lines += $add_lines + $del_lines; + } + $changeset->setHunkCount(count($change->getHunks())); + + $changeset->setOldFile($change->getOldPath()); + $changeset->setNewFile($change->getCurrentPath()); + $changeset->setFilename($change->getCurrentPath()); + $changeset->setChangeType($change->getType()); + + $changeset->setFileType($change->getFileType()); + $changeset->setMetadata($change->getAllMetadata()); + $changeset->setOldProperties($change->getOldProperties()); + $changeset->setNewProperties($change->getNewProperties()); + $changeset->setAwayPaths($change->getAwayPaths()); + $changeset->setAddLines($add_lines); + $changeset->setDelLines($del_lines); + + $diff->addChangeset($changeset); + } + $diff->setLineCount($lines); + + return $diff; + } + +} diff --git a/src/applications/differential/storage/diff/__init__.php b/src/applications/differential/storage/diff/__init__.php new file mode 100644 index 0000000000..95814ba1f3 --- /dev/null +++ b/src/applications/differential/storage/diff/__init__.php @@ -0,0 +1,15 @@ +makeContent($exclude = '-'); + } + + public function makeOldFile() { + return $this->makeContent($exclude = '+'); + } + + public function makeChanges() { + return $this->makeContent($exclude = ' '); + } + + final private function makeContent($exclude) { + $results = array(); + $lines = explode("\n", $this->changes); + foreach ($lines as $line) { + if (isset($line[0]) && $line[0] == $exclude) { + continue; + } + $results[] = substr($line, 1); + } + return implode("\n", $results); + } + +} diff --git a/src/applications/differential/storage/hunk/__init__.php b/src/applications/differential/storage/hunk/__init__.php new file mode 100644 index 0000000000..6d095cc21f --- /dev/null +++ b/src/applications/differential/storage/hunk/__init__.php @@ -0,0 +1,12 @@ +