diff --git a/src/applications/differential/parser/DifferentialChangesetParser.php b/src/applications/differential/parser/DifferentialChangesetParser.php index 686979bca0..fb7c9394af 100644 --- a/src/applications/differential/parser/DifferentialChangesetParser.php +++ b/src/applications/differential/parser/DifferentialChangesetParser.php @@ -1,1893 +1,1895 @@ rangeStart = $start; $this->rangeEnd = $end; return $this; } public function setMask(array $mask) { $this->mask = $mask; return $this; } public function renderChangeset() { return $this->render($this->rangeStart, $this->rangeEnd, $this->mask); } public function setShowEditAndReplyLinks($bool) { $this->showEditAndReplyLinks = $bool; return $this; } public function getShowEditAndReplyLinks() { return $this->showEditAndReplyLinks; } public function setViewState(PhabricatorChangesetViewState $view_state) { $this->viewState = $view_state; return $this; } public function getViewState() { return $this->viewState; } public function setRenderer(DifferentialChangesetRenderer $renderer) { $this->renderer = $renderer; return $this; } public function getRenderer() { return $this->renderer; } public function setDisableCache($disable_cache) { $this->disableCache = $disable_cache; return $this; } public function getDisableCache() { return $this->disableCache; } public function setCanMarkDone($can_mark_done) { $this->canMarkDone = $can_mark_done; return $this; } public function getCanMarkDone() { return $this->canMarkDone; } public function setObjectOwnerPHID($phid) { $this->objectOwnerPHID = $phid; return $this; } public function getObjectOwnerPHID() { return $this->objectOwnerPHID; } public function setOffsetMode($offset_mode) { $this->offsetMode = $offset_mode; return $this; } public function getOffsetMode() { return $this->offsetMode; } public function setViewer(PhabricatorUser $viewer) { $this->viewer = $viewer; return $this; } public function getViewer() { return $this->viewer; } private function newRenderer() { $viewer = $this->getViewer(); $viewstate = $this->getViewstate(); $renderer_key = $viewstate->getRendererKey(); if ($renderer_key === null) { $is_unified = $viewer->compareUserSetting( PhabricatorUnifiedDiffsSetting::SETTINGKEY, PhabricatorUnifiedDiffsSetting::VALUE_ALWAYS_UNIFIED); if ($is_unified) { $renderer_key = '1up'; } else { $renderer_key = $viewstate->getDefaultDeviceRendererKey(); } } switch ($renderer_key) { case '1up': $renderer = new DifferentialChangesetOneUpRenderer(); break; default: $renderer = new DifferentialChangesetTwoUpRenderer(); break; } return $renderer; } const CACHE_VERSION = 14; const CACHE_MAX_SIZE = 8e6; const ATTR_GENERATED = 'attr:generated'; const ATTR_DELETED = 'attr:deleted'; const ATTR_UNCHANGED = 'attr:unchanged'; const ATTR_MOVEAWAY = 'attr:moveaway'; public function setOldLines(array $lines) { $this->old = $lines; return $this; } public function setNewLines(array $lines) { $this->new = $lines; return $this; } public function setSpecialAttributes(array $attributes) { $this->specialAttributes = $attributes; return $this; } public function setIntraLineDiffs(array $diffs) { $this->intra = $diffs; return $this; } public function setDepthOnlyLines(array $lines) { $this->depthOnlyLines = $lines; return $this; } public function getDepthOnlyLines() { return $this->depthOnlyLines; } public function setVisibleLinesMask(array $mask) { $this->visible = $mask; return $this; } public function setLinesOfContext($lines_of_context) { $this->linesOfContext = $lines_of_context; return $this; } public function getLinesOfContext() { return $this->linesOfContext; } /** * Configure which Changeset comments added to the right side of the visible * diff will be attached to. The ID must be the ID of a real Differential * Changeset. * * The complexity here is that we may show an arbitrary side of an arbitrary * changeset as either the left or right part of a diff. This method allows * the left and right halves of the displayed diff to be correctly mapped to * storage changesets. * * @param id The Differential Changeset ID that comments added to the right * side of the visible diff should be attached to. * @param bool If true, attach new comments to the right side of the storage * changeset. Note that this may be false, if the left side of * some storage changeset is being shown as the right side of * a display diff. * @return this */ public function setRightSideCommentMapping($id, $is_new) { $this->rightSideChangesetID = $id; $this->rightSideAttachesToNewFile = $is_new; return $this; } /** * See setRightSideCommentMapping(), but this sets information for the left * side of the display diff. */ public function setLeftSideCommentMapping($id, $is_new) { $this->leftSideChangesetID = $id; $this->leftSideAttachesToNewFile = $is_new; return $this; } public function setOriginals( DifferentialChangeset $left, DifferentialChangeset $right) { $this->originalLeft = $left; $this->originalRight = $right; return $this; } public function diffOriginals() { $engine = new PhabricatorDifferenceEngine(); $changeset = $engine->generateChangesetFromFileContent( implode('', mpull($this->originalLeft->getHunks(), 'getChanges')), implode('', mpull($this->originalRight->getHunks(), 'getChanges'))); $parser = new DifferentialHunkParser(); return $parser->parseHunksForHighlightMasks( $changeset->getHunks(), $this->originalLeft->getHunks(), $this->originalRight->getHunks()); } /** * Set a key for identifying this changeset in the render cache. If set, the * parser will attempt to use the changeset render cache, which can improve * performance for frequently-viewed changesets. * * By default, there is no render cache key and parsers do not use the cache. * This is appropriate for rarely-viewed changesets. * * NOTE: Currently, this key must be a valid Differential Changeset ID. * * @param string Key for identifying this changeset in the render cache. * @return this */ public function setRenderCacheKey($key) { $this->renderCacheKey = $key; return $this; } private function getRenderCacheKey() { return $this->renderCacheKey; } public function setChangeset(DifferentialChangeset $changeset) { $this->changeset = $changeset; $this->setFilename($changeset->getFilename()); return $this; } public function setRenderingReference($ref) { $this->renderingReference = $ref; return $this; } private function getRenderingReference() { return $this->renderingReference; } public function getChangeset() { return $this->changeset; } public function setFilename($filename) { $this->filename = $filename; return $this; } public function setHandles(array $handles) { assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setMarkupEngine(PhabricatorMarkupEngine $engine) { $this->markupEngine = $engine; return $this; } public function setCoverage($coverage) { $this->coverage = $coverage; return $this; } private function getCoverage() { return $this->coverage; } public function parseInlineComment( PhabricatorInlineCommentInterface $comment) { // Parse only comments which are actually visible. if ($this->isCommentVisibleOnRenderedDiff($comment)) { $this->comments[] = $comment; } return $this; } private function loadCache() { $render_cache_key = $this->getRenderCacheKey(); if (!$render_cache_key) { return false; } $data = null; $changeset = new DifferentialChangeset(); $conn_r = $changeset->establishConnection('r'); $data = queryfx_one( $conn_r, 'SELECT * FROM %T WHERE id = %d', $changeset->getTableName().'_parse_cache', $render_cache_key); if (!$data) { return false; } if ($data['cache'][0] == '{') { // This is likely an old-style JSON cache which we will not be able to // deserialize. return false; } $data = unserialize($data['cache']); if (!is_array($data) || !$data) { return false; } foreach (self::getCacheableProperties() as $cache_key) { if (!array_key_exists($cache_key, $data)) { // If we're missing a cache key, assume we're looking at an old cache // and ignore it. return false; } } if ($data['cacheVersion'] !== self::CACHE_VERSION) { return false; } // Someone displays contents of a partially cached shielded file. if (!isset($data['newRender']) && (!$this->isTopLevel || $this->comments)) { return false; } unset($data['cacheVersion'], $data['cacheHost']); $cache_prop = array_select_keys($data, self::getCacheableProperties()); foreach ($cache_prop as $cache_key => $v) { $this->$cache_key = $v; } return true; } protected static function getCacheableProperties() { return array( 'visible', 'new', 'old', 'intra', 'depthOnlyLines', 'newRender', 'oldRender', 'specialAttributes', 'hunkStartLines', 'cacheVersion', 'cacheHost', 'highlightingDisabled', ); } public function saveCache() { if (PhabricatorEnv::isReadOnly()) { return false; } if ($this->highlightErrors) { return false; } $render_cache_key = $this->getRenderCacheKey(); if (!$render_cache_key) { return false; } $cache = array(); foreach (self::getCacheableProperties() as $cache_key) { switch ($cache_key) { case 'cacheVersion': $cache[$cache_key] = self::CACHE_VERSION; break; case 'cacheHost': $cache[$cache_key] = php_uname('n'); break; default: $cache[$cache_key] = $this->$cache_key; break; } } $cache = serialize($cache); // We don't want to waste too much space by a single changeset. if (strlen($cache) > self::CACHE_MAX_SIZE) { return; } $changeset = new DifferentialChangeset(); $conn_w = $changeset->establishConnection('w'); $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); try { queryfx( $conn_w, 'INSERT INTO %T (id, cache, dateCreated) VALUES (%d, %B, %d) ON DUPLICATE KEY UPDATE cache = VALUES(cache)', DifferentialChangeset::TABLE_CACHE, $render_cache_key, $cache, time()); } catch (AphrontQueryException $ex) { // Ignore these exceptions. A common cause is that the cache is // larger than 'max_allowed_packet', in which case we're better off // not writing it. // TODO: It would be nice to tailor this more narrowly. } unset($unguarded); } private function markGenerated($new_corpus_block = '') { $generated_guess = (strpos($new_corpus_block, '@'.'generated') !== false); if (!$generated_guess) { $generated_path_regexps = PhabricatorEnv::getEnvConfig( 'differential.generated-paths'); foreach ($generated_path_regexps as $regexp) { if (preg_match($regexp, $this->changeset->getFilename())) { $generated_guess = true; break; } } } $event = new PhabricatorEvent( PhabricatorEventType::TYPE_DIFFERENTIAL_WILLMARKGENERATED, array( 'corpus' => $new_corpus_block, 'is_generated' => $generated_guess, ) ); PhutilEventEngine::dispatchEvent($event); $generated = $event->getValue('is_generated'); $attribute = $this->changeset->isGeneratedChangeset(); if ($attribute) { $generated = true; } $this->specialAttributes[self::ATTR_GENERATED] = $generated; } public function isGenerated() { return idx($this->specialAttributes, self::ATTR_GENERATED, false); } public function isDeleted() { return idx($this->specialAttributes, self::ATTR_DELETED, false); } public function isUnchanged() { return idx($this->specialAttributes, self::ATTR_UNCHANGED, false); } public function isMoveAway() { return idx($this->specialAttributes, self::ATTR_MOVEAWAY, false); } private function applyIntraline(&$render, $intra, $corpus) { foreach ($render as $key => $text) { $result = $text; if (isset($intra[$key])) { $result = PhabricatorDifferenceEngine::applyIntralineDiff( $result, $intra[$key]); } $result = $this->adjustRenderedLineForDisplay($result); $render[$key] = $result; } } private function getHighlightFuture($corpus) { $language = $this->getViewState()->getHighlightLanguage(); if (!$language) { $language = $this->highlightEngine->getLanguageFromFilename( $this->filename); if (($language != 'txt') && (strlen($corpus) > self::HIGHLIGHT_BYTE_LIMIT)) { $this->highlightingDisabled = true; $language = 'txt'; } } return $this->highlightEngine->getHighlightFuture( $language, $corpus); } protected function processHighlightedSource($data, $result) { $result_lines = phutil_split_lines($result); foreach ($data as $key => $info) { if (!$info) { unset($result_lines[$key]); } } return $result_lines; } private function tryCacheStuff() { + $changeset = $this->getChangeset(); + if (!$changeset->hasSourceTextBody()) { + + // TODO: This isn't really correct (the change is not "generated"), the + // intent is just to not render a text body for Subversion directory + // changes, etc. + $this->markGenerated(); + + return; + } + $viewstate = $this->getViewState(); $skip_cache = false; if ($this->disableCache) { $skip_cache = true; } $character_encoding = $viewstate->getCharacterEncoding(); if ($character_encoding !== null) { $skip_cache = true; } $highlight_language = $viewstate->getHighlightLanguage(); if ($highlight_language !== null) { $skip_cache = true; } - $changeset = $this->changeset; - - if ($changeset->getFileType() != DifferentialChangeType::FILE_TEXT && - $changeset->getFileType() != DifferentialChangeType::FILE_SYMLINK) { - - $this->markGenerated(); - - } else { - if ($skip_cache || !$this->loadCache()) { - $this->process(); - if (!$skip_cache) { - $this->saveCache(); - } + if ($skip_cache || !$this->loadCache()) { + $this->process(); + if (!$skip_cache) { + $this->saveCache(); } } } private function process() { $changeset = $this->changeset; $hunk_parser = new DifferentialHunkParser(); $hunk_parser->parseHunksForLineData($changeset->getHunks()); $this->realignDiff($changeset, $hunk_parser); $hunk_parser->reparseHunksForSpecialAttributes(); $unchanged = false; if (!$hunk_parser->getHasAnyChanges()) { $filetype = $this->changeset->getFileType(); if ($filetype == DifferentialChangeType::FILE_TEXT || $filetype == DifferentialChangeType::FILE_SYMLINK) { $unchanged = true; } } $moveaway = false; $changetype = $this->changeset->getChangeType(); if ($changetype == DifferentialChangeType::TYPE_MOVE_AWAY) { $moveaway = true; } $this->setSpecialAttributes(array( self::ATTR_UNCHANGED => $unchanged, self::ATTR_DELETED => $hunk_parser->getIsDeleted(), self::ATTR_MOVEAWAY => $moveaway, )); $lines_context = $this->getLinesOfContext(); $hunk_parser->generateIntraLineDiffs(); $hunk_parser->generateVisibleLinesMask($lines_context); $this->setOldLines($hunk_parser->getOldLines()); $this->setNewLines($hunk_parser->getNewLines()); $this->setIntraLineDiffs($hunk_parser->getIntraLineDiffs()); $this->setDepthOnlyLines($hunk_parser->getDepthOnlyLines()); $this->setVisibleLinesMask($hunk_parser->getVisibleLinesMask()); $this->hunkStartLines = $hunk_parser->getHunkStartLines( $changeset->getHunks()); $new_corpus = $hunk_parser->getNewCorpus(); $new_corpus_block = implode('', $new_corpus); $this->markGenerated($new_corpus_block); if ($this->isTopLevel && !$this->comments && ($this->isGenerated() || $this->isUnchanged() || $this->isDeleted())) { return; } $old_corpus = $hunk_parser->getOldCorpus(); $old_corpus_block = implode('', $old_corpus); $old_future = $this->getHighlightFuture($old_corpus_block); $new_future = $this->getHighlightFuture($new_corpus_block); $futures = array( 'old' => $old_future, 'new' => $new_future, ); $corpus_blocks = array( 'old' => $old_corpus_block, 'new' => $new_corpus_block, ); $this->highlightErrors = false; foreach (new FutureIterator($futures) as $key => $future) { try { try { $highlighted = $future->resolve(); } catch (PhutilSyntaxHighlighterException $ex) { $this->highlightErrors = true; $highlighted = id(new PhutilDefaultSyntaxHighlighter()) ->getHighlightFuture($corpus_blocks[$key]) ->resolve(); } switch ($key) { case 'old': $this->oldRender = $this->processHighlightedSource( $this->old, $highlighted); break; case 'new': $this->newRender = $this->processHighlightedSource( $this->new, $highlighted); break; } } catch (Exception $ex) { phlog($ex); throw $ex; } } $this->applyIntraline( $this->oldRender, ipull($this->intra, 0), $old_corpus); $this->applyIntraline( $this->newRender, ipull($this->intra, 1), $new_corpus); } private function shouldRenderPropertyChangeHeader($changeset) { if (!$this->isTopLevel) { // We render properties only at top level; otherwise we get multiple // copies of them when a user clicks "Show More". return false; } return true; } public function render( $range_start = null, $range_len = null, $mask_force = array()) { $renderer = $this->getRenderer(); if (!$renderer) { $renderer = $this->newRenderer(); $this->setRenderer($renderer); } // "Top level" renders are initial requests for the whole file, versus // requests for a specific range generated by clicking "show more". We // generate property changes and "shield" UI elements only for toplevel // requests. $this->isTopLevel = (($range_start === null) && ($range_len === null)); $this->highlightEngine = PhabricatorSyntaxHighlighter::newEngine(); $viewstate = $this->getViewState(); $encoding = null; $character_encoding = $viewstate->getCharacterEncoding(); if ($character_encoding) { // We are forcing this changeset to be interpreted with a specific // character encoding, so force all the hunks into that encoding and // propagate it to the renderer. $encoding = $character_encoding; foreach ($this->changeset->getHunks() as $hunk) { $hunk->forceEncoding($character_encoding); } } else { // We're just using the default, so tell the renderer what that is // (by reading the encoding from the first hunk). foreach ($this->changeset->getHunks() as $hunk) { $encoding = $hunk->getDataEncoding(); break; } } $this->tryCacheStuff(); // If we're rendering in an offset mode, treat the range numbers as line // numbers instead of rendering offsets. $offset_mode = $this->getOffsetMode(); if ($offset_mode) { if ($offset_mode == 'new') { $offset_map = $this->new; } else { $offset_map = $this->old; } // NOTE: Inline comments use zero-based lengths. For example, a comment // that starts and ends on line 123 has length 0. Rendering considers // this range to have length 1. Probably both should agree, but that // ship likely sailed long ago. Tweak things here to get the two systems // to agree. See PHI985, where this affected mail rendering of inline // comments left on the final line of a file. $range_end = $this->getOffset($offset_map, $range_start + $range_len); $range_start = $this->getOffset($offset_map, $range_start); $range_len = ($range_end - $range_start) + 1; } $render_pch = $this->shouldRenderPropertyChangeHeader($this->changeset); $rows = max( count($this->old), count($this->new)); $renderer = $this->getRenderer() ->setUser($this->getViewer()) ->setChangeset($this->changeset) ->setRenderPropertyChangeHeader($render_pch) ->setIsTopLevel($this->isTopLevel) ->setOldRender($this->oldRender) ->setNewRender($this->newRender) ->setHunkStartLines($this->hunkStartLines) ->setOldChangesetID($this->leftSideChangesetID) ->setNewChangesetID($this->rightSideChangesetID) ->setOldAttachesToNewFile($this->leftSideAttachesToNewFile) ->setNewAttachesToNewFile($this->rightSideAttachesToNewFile) ->setCodeCoverage($this->getCoverage()) ->setRenderingReference($this->getRenderingReference()) ->setMarkupEngine($this->markupEngine) ->setHandles($this->handles) ->setOldLines($this->old) ->setNewLines($this->new) ->setOriginalCharacterEncoding($encoding) ->setShowEditAndReplyLinks($this->getShowEditAndReplyLinks()) ->setCanMarkDone($this->getCanMarkDone()) ->setObjectOwnerPHID($this->getObjectOwnerPHID()) ->setHighlightingDisabled($this->highlightingDisabled) ->setDepthOnlyLines($this->getDepthOnlyLines()); list($engine, $old_ref, $new_ref) = $this->newDocumentEngine(); if ($engine) { $engine_blocks = $engine->newEngineBlocks( $old_ref, $new_ref); } else { $engine_blocks = null; } $has_document_engine = ($engine_blocks !== null); // See T13515. Sometimes, we collapse file content by default: for // example, if the file is marked as containing generated code. // If a file has inline comments, that normally means we never collapse // it. However, if the viewer has already collapsed all of the inlines, // it's fine to collapse the file. $expanded_comments = array(); foreach ($this->comments as $comment) { if ($comment->isHidden()) { continue; } $expanded_comments[] = $comment; } $collapsed_count = (count($this->comments) - count($expanded_comments)); $shield_raw = null; $shield_text = null; $shield_type = null; if ($this->isTopLevel && !$expanded_comments && !$has_document_engine) { if ($this->isGenerated()) { $shield_text = pht( 'This file contains generated code, which does not normally '. 'need to be reviewed.'); } else if ($this->isMoveAway()) { // We put an empty shield on these files. Normally, they do not have // any diff content anyway. However, if they come through `arc`, they // may have content. We don't want to show it (it's not useful) and // we bailed out of fully processing it earlier anyway. // We could show a message like "this file was moved", but we show // that as a change header anyway, so it would be redundant. Instead, // just render an empty shield to skip rendering the diff body. $shield_raw = ''; } else if ($this->isUnchanged()) { $type = 'text'; if (!$rows) { // NOTE: Normally, diffs which don't change files do not include // file content (for example, if you "chmod +x" a file and then // run "git show", the file content is not available). Similarly, // if you move a file from A to B without changing it, diffs normally // do not show the file content. In some cases `arc` is able to // synthetically generate content for these diffs, but for raw diffs // we'll never have it so we need to be prepared to not render a link. $type = 'none'; } $shield_type = $type; $type_add = DifferentialChangeType::TYPE_ADD; if ($this->changeset->getChangeType() == $type_add) { // Although the generic message is sort of accurate in a technical // sense, this more-tailored message is less confusing. $shield_text = pht('This is an empty file.'); } else { $shield_text = pht('The contents of this file were not changed.'); } } else if ($this->isDeleted()) { $shield_text = pht('This file was completely deleted.'); } else if ($this->changeset->getAffectedLineCount() > 2500) { $shield_text = pht( 'This file has a very large number of changes (%s lines).', new PhutilNumber($this->changeset->getAffectedLineCount())); } } $shield = null; if ($shield_raw !== null) { $shield = $shield_raw; } else if ($shield_text !== null) { if ($shield_type === null) { $shield_type = 'default'; } // If we have inlines and the shield would normally show the whole file, // downgrade it to show only text around the inlines. if ($collapsed_count) { if ($shield_type === 'text') { $shield_type = 'default'; } $shield_text = array( $shield_text, ' ', pht( 'This file has %d collapsed inline comment(s).', new PhutilNumber($collapsed_count)), ); } $shield = $renderer->renderShield($shield_text, $shield_type); } if ($shield !== null) { return $renderer->renderChangesetTable($shield); } // This request should render the "undershield" headers if it's a top-level // request which made it this far (indicating the changeset has no shield) // or it's a request with no mask information (indicating it's the request // that removes the rendering shield). Possibly, this second class of // request might need to be made more explicit. $is_undershield = (empty($mask_force) || $this->isTopLevel); $renderer->setIsUndershield($is_undershield); $old_comments = array(); $new_comments = array(); $old_mask = array(); $new_mask = array(); $feedback_mask = array(); $lines_context = $this->getLinesOfContext(); if ($this->comments) { // If there are any comments which appear in sections of the file which // we don't have, we're going to move them backwards to the closest // earlier line. Two cases where this may happen are: // // - Porting ghost comments forward into a file which was mostly // deleted. // - Porting ghost comments forward from a full-context diff to a // partial-context diff. list($old_backmap, $new_backmap) = $this->buildLineBackmaps(); foreach ($this->comments as $comment) { $new_side = $this->isCommentOnRightSideWhenDisplayed($comment); $line = $comment->getLineNumber(); // See T13524. Lint inlines from Harbormaster may not have a line // number. if ($line === null) { $back_line = null; } else if ($new_side) { $back_line = idx($new_backmap, $line); } else { $back_line = idx($old_backmap, $line); } if ($back_line != $line) { // TODO: This should probably be cleaner, but just be simple and // obvious for now. $ghost = $comment->getIsGhost(); if ($ghost) { $moved = pht( 'This comment originally appeared on line %s, but that line '. 'does not exist in this version of the diff. It has been '. 'moved backward to the nearest line.', new PhutilNumber($line)); $ghost['reason'] = $ghost['reason']."\n\n".$moved; $comment->setIsGhost($ghost); } $comment->setLineNumber($back_line); $comment->setLineLength(0); } $start = max($comment->getLineNumber() - $lines_context, 0); $end = $comment->getLineNumber() + $comment->getLineLength() + $lines_context; for ($ii = $start; $ii <= $end; $ii++) { if ($new_side) { $new_mask[$ii] = true; } else { $old_mask[$ii] = true; } } } foreach ($this->old as $ii => $old) { if (isset($old['line']) && isset($old_mask[$old['line']])) { $feedback_mask[$ii] = true; } } foreach ($this->new as $ii => $new) { if (isset($new['line']) && isset($new_mask[$new['line']])) { $feedback_mask[$ii] = true; } } $this->comments = id(new PHUIDiffInlineThreader()) ->reorderAndThreadCommments($this->comments); foreach ($this->comments as $comment) { $final = $comment->getLineNumber() + $comment->getLineLength(); $final = max(1, $final); if ($this->isCommentOnRightSideWhenDisplayed($comment)) { $new_comments[$final][] = $comment; } else { $old_comments[$final][] = $comment; } } } $renderer ->setOldComments($old_comments) ->setNewComments($new_comments); if ($engine_blocks !== null) { $reference = $this->getRenderingReference(); $parts = explode('/', $reference); if (count($parts) == 2) { list($id, $vs) = $parts; } else { $id = $parts[0]; $vs = 0; } // If we don't have an explicit "vs" changeset, it's the left side of // the "id" changeset. if (!$vs) { $vs = $id; } $renderer ->setDocumentEngine($engine) ->setDocumentEngineBlocks($engine_blocks); return $renderer->renderDocumentEngineBlocks( $engine_blocks, (string)$id, (string)$vs); } // If we've made it here with a type of file we don't know how to render, // bail out with a default empty rendering. Normally, we'd expect a // document engine to catch these changes before we make it this far. switch ($this->changeset->getFileType()) { case DifferentialChangeType::FILE_DIRECTORY: case DifferentialChangeType::FILE_BINARY: case DifferentialChangeType::FILE_IMAGE: $output = $renderer->renderChangesetTable(null); return $output; } if ($this->originalLeft && $this->originalRight) { list($highlight_old, $highlight_new) = $this->diffOriginals(); $highlight_old = array_flip($highlight_old); $highlight_new = array_flip($highlight_new); $renderer ->setHighlightOld($highlight_old) ->setHighlightNew($highlight_new); } $renderer ->setOriginalOld($this->originalLeft) ->setOriginalNew($this->originalRight); if ($range_start === null) { $range_start = 0; } if ($range_len === null) { $range_len = $rows; } $range_len = min($range_len, $rows - $range_start); list($gaps, $mask) = $this->calculateGapsAndMask( $mask_force, $feedback_mask, $range_start, $range_len); $renderer ->setGaps($gaps) ->setMask($mask); $html = $renderer->renderTextChange( $range_start, $range_len, $rows); return $renderer->renderChangesetTable($html); } /** * This function calculates a lot of stuff we need to know to display * the diff: * * Gaps - compute gaps in the visible display diff, where we will render * "Show more context" spacers. If a gap is smaller than the context size, * we just display it. Otherwise, we record it into $gaps and will render a * "show more context" element instead of diff text below. A given $gap * is a tuple of $gap_line_number_start and $gap_length. * * Mask - compute the actual lines that need to be shown (because they * are near changes lines, near inline comments, or the request has * explicitly asked for them, i.e. resulting from the user clicking * "show more"). The $mask returned is a sparsely populated dictionary * of $visible_line_number => true. * * @return array($gaps, $mask) */ private function calculateGapsAndMask( $mask_force, $feedback_mask, $range_start, $range_len) { $lines_context = $this->getLinesOfContext(); $gaps = array(); $gap_start = 0; $in_gap = false; $base_mask = $this->visible + $mask_force + $feedback_mask; $base_mask[$range_start + $range_len] = true; for ($ii = $range_start; $ii <= $range_start + $range_len; $ii++) { if (isset($base_mask[$ii])) { if ($in_gap) { $gap_length = $ii - $gap_start; if ($gap_length <= $lines_context) { for ($jj = $gap_start; $jj <= $gap_start + $gap_length; $jj++) { $base_mask[$jj] = true; } } else { $gaps[] = array($gap_start, $gap_length); } $in_gap = false; } } else { if (!$in_gap) { $gap_start = $ii; $in_gap = true; } } } $gaps = array_reverse($gaps); $mask = $base_mask; return array($gaps, $mask); } /** * Determine if an inline comment will appear on the rendered diff, * taking into consideration which halves of which changesets will actually * be shown. * * @param PhabricatorInlineCommentInterface Comment to test for visibility. * @return bool True if the comment is visible on the rendered diff. */ private function isCommentVisibleOnRenderedDiff( PhabricatorInlineCommentInterface $comment) { $changeset_id = $comment->getChangesetID(); $is_new = $comment->getIsNewFile(); if ($changeset_id == $this->rightSideChangesetID && $is_new == $this->rightSideAttachesToNewFile) { return true; } if ($changeset_id == $this->leftSideChangesetID && $is_new == $this->leftSideAttachesToNewFile) { return true; } return false; } /** * Determine if a comment will appear on the right side of the display diff. * Note that the comment must appear somewhere on the rendered changeset, as * per isCommentVisibleOnRenderedDiff(). * * @param PhabricatorInlineCommentInterface Comment to test for display * location. * @return bool True for right, false for left. */ private function isCommentOnRightSideWhenDisplayed( PhabricatorInlineCommentInterface $comment) { if (!$this->isCommentVisibleOnRenderedDiff($comment)) { throw new Exception(pht('Comment is not visible on changeset!')); } $changeset_id = $comment->getChangesetID(); $is_new = $comment->getIsNewFile(); if ($changeset_id == $this->rightSideChangesetID && $is_new == $this->rightSideAttachesToNewFile) { return true; } return false; } /** * Parse the 'range' specification that this class and the client-side JS * emit to indicate that a user clicked "Show more..." on a diff. Generally, * use is something like this: * * $spec = $request->getStr('range'); * $parsed = DifferentialChangesetParser::parseRangeSpecification($spec); * list($start, $end, $mask) = $parsed; * $parser->render($start, $end, $mask); * * @param string Range specification, indicating the range of the diff that * should be rendered. * @return tuple List of suitable for passing to * @{method:render}. */ public static function parseRangeSpecification($spec) { $range_s = null; $range_e = null; $mask = array(); if ($spec) { $match = null; if (preg_match('@^(\d+)-(\d+)(?:/(\d+)-(\d+))?$@', $spec, $match)) { $range_s = (int)$match[1]; $range_e = (int)$match[2]; if (count($match) > 3) { $start = (int)$match[3]; $len = (int)$match[4]; for ($ii = $start; $ii < $start + $len; $ii++) { $mask[$ii] = true; } } } } return array($range_s, $range_e, $mask); } /** * Render "modified coverage" information; test coverage on modified lines. * This synthesizes diff information with unit test information into a useful * indicator of how well tested a change is. */ public function renderModifiedCoverage() { $na = phutil_tag('em', array(), '-'); $coverage = $this->getCoverage(); if (!$coverage) { return $na; } $covered = 0; $not_covered = 0; foreach ($this->new as $k => $new) { if (!$new['line']) { continue; } if (!$new['type']) { continue; } if (empty($coverage[$new['line'] - 1])) { continue; } switch ($coverage[$new['line'] - 1]) { case 'C': $covered++; break; case 'U': $not_covered++; break; } } if (!$covered && !$not_covered) { return $na; } return sprintf('%d%%', 100 * ($covered / ($covered + $not_covered))); } /** * Build maps from lines comments appear on to actual lines. */ private function buildLineBackmaps() { $old_back = array(); $new_back = array(); foreach ($this->old as $ii => $old) { if ($old === null) { continue; } $old_back[$old['line']] = $old['line']; } foreach ($this->new as $ii => $new) { if ($new === null) { continue; } $new_back[$new['line']] = $new['line']; } $max_old_line = 0; $max_new_line = 0; foreach ($this->comments as $comment) { if ($this->isCommentOnRightSideWhenDisplayed($comment)) { $max_new_line = max($max_new_line, $comment->getLineNumber()); } else { $max_old_line = max($max_old_line, $comment->getLineNumber()); } } $cursor = 1; for ($ii = 1; $ii <= $max_old_line; $ii++) { if (empty($old_back[$ii])) { $old_back[$ii] = $cursor; } else { $cursor = $old_back[$ii]; } } $cursor = 1; for ($ii = 1; $ii <= $max_new_line; $ii++) { if (empty($new_back[$ii])) { $new_back[$ii] = $cursor; } else { $cursor = $new_back[$ii]; } } return array($old_back, $new_back); } private function getOffset(array $map, $line) { if (!$map) { return null; } $line = (int)$line; foreach ($map as $key => $spec) { if ($spec && isset($spec['line'])) { if ((int)$spec['line'] >= $line) { return $key; } } } return $key; } private function realignDiff( DifferentialChangeset $changeset, DifferentialHunkParser $hunk_parser) { // Normalizing and realigning the diff depends on rediffing the files, and // we currently need complete representations of both files to do anything // reasonable. If we only have parts of the files, skip realignment. // We have more than one hunk, so we're definitely missing part of the file. $hunks = $changeset->getHunks(); if (count($hunks) !== 1) { return null; } // The first hunk doesn't start at the beginning of the file, so we're // missing some context. $first_hunk = head($hunks); if ($first_hunk->getOldOffset() != 1 || $first_hunk->getNewOffset() != 1) { return null; } $old_file = $changeset->makeOldFile(); $new_file = $changeset->makeNewFile(); if ($old_file === $new_file) { // If the old and new files are exactly identical, the synthetic // diff below will give us nonsense and whitespace modes are // irrelevant anyway. This occurs when you, e.g., copy a file onto // itself in Subversion (see T271). return null; } $engine = id(new PhabricatorDifferenceEngine()) ->setNormalize(true); $normalized_changeset = $engine->generateChangesetFromFileContent( $old_file, $new_file); $type_parser = new DifferentialHunkParser(); $type_parser->parseHunksForLineData($normalized_changeset->getHunks()); $hunk_parser->setNormalized(true); $hunk_parser->setOldLineTypeMap($type_parser->getOldLineTypeMap()); $hunk_parser->setNewLineTypeMap($type_parser->getNewLineTypeMap()); } private function adjustRenderedLineForDisplay($line) { // IMPORTANT: We're using "str_replace()" against raw HTML here, which can // easily become unsafe. The input HTML has already had syntax highlighting // and intraline diff highlighting applied, so it's full of "" tags. static $search; static $replace; if ($search === null) { $rules = $this->newSuspiciousCharacterRules(); $map = array(); foreach ($rules as $key => $spec) { $tag = phutil_tag( 'span', array( 'data-copy-text' => $key, 'class' => $spec['class'], 'title' => $spec['title'], ), $spec['replacement']); $map[$key] = phutil_string_cast($tag); } $search = array_keys($map); $replace = array_values($map); } $is_html = false; if ($line instanceof PhutilSafeHTML) { $is_html = true; $line = hsprintf('%s', $line); } $line = phutil_string_cast($line); // TODO: This should be flexible, eventually. $tab_width = 2; $line = self::replaceTabsWithSpaces($line, $tab_width); $line = str_replace($search, $replace, $line); if ($is_html) { $line = phutil_safe_html($line); } return $line; } private function newSuspiciousCharacterRules() { // The "title" attributes are cached in the database, so they're // intentionally not wrapped in "pht(...)". $rules = array( "\xE2\x80\x8B" => array( 'title' => 'ZWS', 'class' => 'suspicious-character', 'replacement' => '!', ), "\xC2\xA0" => array( 'title' => 'NBSP', 'class' => 'suspicious-character', 'replacement' => '!', ), "\x7F" => array( 'title' => 'DEL (0x7F)', 'class' => 'suspicious-character', 'replacement' => "\xE2\x90\xA1", ), ); // Unicode defines special pictures for the control characters in the // range between "0x00" and "0x1F". $control = array( 'NULL', 'SOH', 'STX', 'ETX', 'EOT', 'ENQ', 'ACK', 'BEL', 'BS', null, // "\t" Tab null, // "\n" New Line 'VT', 'FF', null, // "\r" Carriage Return, 'SO', 'SI', 'DLE', 'DC1', 'DC2', 'DC3', 'DC4', 'NAK', 'SYN', 'ETB', 'CAN', 'EM', 'SUB', 'ESC', 'FS', 'GS', 'RS', 'US', ); foreach ($control as $idx => $label) { if ($label === null) { continue; } $rules[chr($idx)] = array( 'title' => sprintf('%s (0x%02X)', $label, $idx), 'class' => 'suspicious-character', 'replacement' => "\xE2\x90".chr(0x80 + $idx), ); } return $rules; } public static function replaceTabsWithSpaces($line, $tab_width) { static $tags = array(); if (empty($tags[$tab_width])) { for ($ii = 1; $ii <= $tab_width; $ii++) { $tag = phutil_tag( 'span', array( 'data-copy-text' => "\t", ), str_repeat(' ', $ii)); $tag = phutil_string_cast($tag); $tags[$ii] = $tag; } } // Expand all prefix tabs until we encounter any non-tab character. This // is cheap and often immediately produces the correct result with no // further work (and, particularly, no need to handle any unicode cases). $len = strlen($line); $head = 0; for ($head = 0; $head < $len; $head++) { $char = $line[$head]; if ($char !== "\t") { break; } } if ($head) { if (empty($tags[$tab_width * $head])) { $tags[$tab_width * $head] = str_repeat($tags[$tab_width], $head); } $prefix = $tags[$tab_width * $head]; $line = substr($line, $head); } else { $prefix = ''; } // If we have no remaining tabs elsewhere in the string after taking care // of all the prefix tabs, we're done. if (strpos($line, "\t") === false) { return $prefix.$line; } $len = strlen($line); // If the line is particularly long, don't try to do anything special with // it. Use a faster approximation of the correct tabstop expansion instead. // This usually still arrives at the right result. if ($len > 256) { return $prefix.str_replace("\t", $tags[$tab_width], $line); } $in_tag = false; $pos = 0; // See PHI1210. If the line only has single-byte characters, we don't need // to vectorize it and can avoid an expensive UTF8 call. $fast_path = preg_match('/^[\x01-\x7F]*\z/', $line); if ($fast_path) { $replace = array(); for ($ii = 0; $ii < $len; $ii++) { $char = $line[$ii]; if ($char === '>') { $in_tag = false; continue; } if ($in_tag) { continue; } if ($char === '<') { $in_tag = true; continue; } if ($char === "\t") { $count = $tab_width - ($pos % $tab_width); $pos += $count; $replace[$ii] = $tags[$count]; continue; } $pos++; } if ($replace) { // Apply replacements starting at the end of the string so they // don't mess up the offsets for following replacements. $replace = array_reverse($replace, true); foreach ($replace as $replace_pos => $replacement) { $line = substr_replace($line, $replacement, $replace_pos, 1); } } } else { $line = phutil_utf8v_combined($line); foreach ($line as $key => $char) { if ($char === '>') { $in_tag = false; continue; } if ($in_tag) { continue; } if ($char === '<') { $in_tag = true; continue; } if ($char === "\t") { $count = $tab_width - ($pos % $tab_width); $pos += $count; $line[$key] = $tags[$count]; continue; } $pos++; } $line = implode('', $line); } return $prefix.$line; } private function newDocumentEngine() { $changeset = $this->changeset; $viewer = $this->getViewer(); list($old_file, $new_file) = $this->loadFileObjectsForChangeset(); $no_old = !$changeset->hasOldState(); $no_new = !$changeset->hasNewState(); if ($no_old) { $old_ref = null; } else { $old_ref = id(new PhabricatorDocumentRef()) ->setName($changeset->getOldFile()); if ($old_file) { $old_ref->setFile($old_file); } else { $old_data = $this->getRawDocumentEngineData($this->old); $old_ref->setData($old_data); } } if ($no_new) { $new_ref = null; } else { $new_ref = id(new PhabricatorDocumentRef()) ->setName($changeset->getFilename()); if ($new_file) { $new_ref->setFile($new_file); } else { $new_data = $this->getRawDocumentEngineData($this->new); $new_ref->setData($new_data); } } $old_engines = null; if ($old_ref) { $old_engines = PhabricatorDocumentEngine::getEnginesForRef( $viewer, $old_ref); } $new_engines = null; if ($new_ref) { $new_engines = PhabricatorDocumentEngine::getEnginesForRef( $viewer, $new_ref); } if ($new_engines !== null && $old_engines !== null) { $shared_engines = array_intersect_key($new_engines, $old_engines); $default_engine = head_key($new_engines); } else if ($new_engines !== null) { $shared_engines = $new_engines; $default_engine = head_key($shared_engines); } else if ($old_engines !== null) { $shared_engines = $old_engines; $default_engine = head_key($shared_engines); } else { return null; } foreach ($shared_engines as $key => $shared_engine) { if (!$shared_engine->canDiffDocuments($old_ref, $new_ref)) { unset($shared_engines[$key]); } } $viewstate = $this->getViewState(); $engine_key = $viewstate->getDocumentEngineKey(); if (strlen($engine_key)) { if (isset($shared_engines[$engine_key])) { $document_engine = $shared_engines[$engine_key]; } else { $document_engine = null; } } else { // If we aren't rendering with a specific engine, only use a default // engine if the best engine for the new file is a shared engine which // can diff files. If we're less picky (for example, by accepting any // shared engine) we can end up with silly behavior (like ".json" files // rendering as Jupyter documents). if (isset($shared_engines[$default_engine])) { $document_engine = $shared_engines[$default_engine]; } else { $document_engine = null; } } if ($document_engine) { return array( $document_engine, $old_ref, $new_ref); } return null; } private function loadFileObjectsForChangeset() { $changeset = $this->changeset; $viewer = $this->getViewer(); $old_phid = $changeset->getOldFileObjectPHID(); $new_phid = $changeset->getNewFileObjectPHID(); $old_file = null; $new_file = null; if ($old_phid || $new_phid) { $file_phids = array(); if ($old_phid) { $file_phids[] = $old_phid; } if ($new_phid) { $file_phids[] = $new_phid; } $files = id(new PhabricatorFileQuery()) ->setViewer($viewer) ->withPHIDs($file_phids) ->execute(); $files = mpull($files, null, 'getPHID'); if ($old_phid) { $old_file = idx($files, $old_phid); if (!$old_file) { throw new Exception( pht( 'Failed to load file data for changeset ("%s").', $old_phid)); } $changeset->attachOldFileObject($old_file); } if ($new_phid) { $new_file = idx($files, $new_phid); if (!$new_file) { throw new Exception( pht( 'Failed to load file data for changeset ("%s").', $new_phid)); } $changeset->attachNewFileObject($new_file); } } return array($old_file, $new_file); } public function newChangesetResponse() { // NOTE: This has to happen first because it has side effects. Yuck. $rendered_changeset = $this->renderChangeset(); $renderer = $this->getRenderer(); $renderer_key = $renderer->getRendererKey(); $viewstate = $this->getViewState(); $undo_templates = $renderer->renderUndoTemplates(); foreach ($undo_templates as $key => $undo_template) { $undo_templates[$key] = hsprintf('%s', $undo_template); } $state = array( 'undoTemplates' => $undo_templates, 'rendererKey' => $renderer_key, 'highlight' => $viewstate->getHighlightLanguage(), 'characterEncoding' => $viewstate->getCharacterEncoding(), 'documentEngine' => $viewstate->getDocumentEngineKey(), 'isHidden' => $viewstate->getHidden(), ); return id(new PhabricatorChangesetResponse()) ->setRenderedChangeset($rendered_changeset) ->setChangesetState($state); } private function getRawDocumentEngineData(array $lines) { $text = array(); foreach ($lines as $line) { if ($line === null) { continue; } // If this is a "No newline at end of file." annotation, don't hand it // off to the DocumentEngine. if ($line['type'] === '\\') { continue; } $text[] = $line['text']; } return implode('', $text); } } diff --git a/src/applications/differential/storage/DifferentialChangeset.php b/src/applications/differential/storage/DifferentialChangeset.php index 966e93fd28..fa4cc37ec8 100644 --- a/src/applications/differential/storage/DifferentialChangeset.php +++ b/src/applications/differential/storage/DifferentialChangeset.php @@ -1,685 +1,730 @@ array( 'metadata' => self::SERIALIZATION_JSON, 'oldProperties' => self::SERIALIZATION_JSON, 'newProperties' => self::SERIALIZATION_JSON, 'awayPaths' => self::SERIALIZATION_JSON, ), self::CONFIG_COLUMN_SCHEMA => array( 'oldFile' => 'bytes?', 'filename' => 'bytes', 'changeType' => 'uint32', 'fileType' => 'uint32', 'addLines' => 'uint32', 'delLines' => 'uint32', // T6203/NULLABILITY // These should all be non-nullable, and store reasonable default // JSON values if empty. 'awayPaths' => 'text?', 'metadata' => 'text?', 'oldProperties' => 'text?', 'newProperties' => 'text?', ), self::CONFIG_KEY_SCHEMA => array( 'diffID' => array( 'columns' => array('diffID'), ), ), ) + parent::getConfiguration(); } public function getAffectedLineCount() { return $this->getAddLines() + $this->getDelLines(); } public function attachHunks(array $hunks) { assert_instances_of($hunks, 'DifferentialHunk'); $this->hunks = $hunks; return $this; } public function getHunks() { return $this->assertAttached($this->hunks); } public function getDisplayFilename() { $name = $this->getFilename(); if ($this->getFileType() == DifferentialChangeType::FILE_DIRECTORY) { $name .= '/'; } return $name; } public function getOwnersFilename() { // TODO: For Subversion, we should adjust these paths to be relative to // the repository root where possible. $path = $this->getFilename(); if (!isset($path[0])) { return '/'; } if ($path[0] != '/') { $path = '/'.$path; } return $path; } public function addUnsavedHunk(DifferentialHunk $hunk) { if ($this->hunks === self::ATTACHABLE) { $this->hunks = array(); } $this->hunks[] = $hunk; $this->unsavedHunks[] = $hunk; return $this; } public function setAuthorityPackages(array $authority_packages) { $this->authorityPackages = mpull($authority_packages, null, 'getPHID'); return $this; } public function getAuthorityPackages() { return $this->authorityPackages; } public function setChangesetPackages($changeset_packages) { $this->changesetPackages = mpull($changeset_packages, null, 'getPHID'); return $this; } public function getChangesetPackages() { return $this->changesetPackages; } public function setHasOldState($has_old_state) { $this->hasOldState = $has_old_state; return $this; } public function setHasNewState($has_new_state) { $this->hasNewState = $has_new_state; return $this; } public function hasOldState() { if ($this->hasOldState !== null) { return $this->hasOldState; } $change_type = $this->getChangeType(); return !DifferentialChangeType::isCreateChangeType($change_type); } public function hasNewState() { if ($this->hasNewState !== null) { return $this->hasNewState; } $change_type = $this->getChangeType(); return !DifferentialChangeType::isDeleteChangeType($change_type); } public function save() { $this->openTransaction(); $ret = parent::save(); foreach ($this->unsavedHunks as $hunk) { $hunk->setChangesetID($this->getID()); $hunk->save(); } $this->saveTransaction(); return $ret; } public function delete() { $this->openTransaction(); $hunks = id(new DifferentialHunk())->loadAllWhere( 'changesetID = %d', $this->getID()); foreach ($hunks as $hunk) { $hunk->delete(); } $this->unsavedHunks = array(); queryfx( $this->establishConnection('w'), 'DELETE FROM %T WHERE id = %d', self::TABLE_CACHE, $this->getID()); $ret = parent::delete(); $this->saveTransaction(); return $ret; } /** * Test if this changeset and some other changeset put the affected file in * the same state. * * @param DifferentialChangeset Changeset to compare against. * @return bool True if the two changesets have the same effect. */ public function hasSameEffectAs(DifferentialChangeset $other) { if ($this->getFilename() !== $other->getFilename()) { return false; } $hash_key = self::METADATA_EFFECT_HASH; $u_hash = $this->getChangesetMetadata($hash_key); if ($u_hash === null) { return false; } $v_hash = $other->getChangesetMetadata($hash_key); if ($v_hash === null) { return false; } if ($u_hash !== $v_hash) { return false; } // Make sure the final states for the file properties (like the "+x" // executable bit) match one another. $u_props = $this->getNewProperties(); $v_props = $other->getNewProperties(); ksort($u_props); ksort($v_props); if ($u_props !== $v_props) { return false; } return true; } 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 = mpull($this->getHunks(), 'makeNewFile'); return implode('', $file); } public function makeOldFile() { $file = mpull($this->getHunks(), 'makeOldFile'); return implode('', $file); } 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) { $type = substr($line, 0, 1); if ($type == '+' || $type == '-') { $context += array_fill($l - $num_lines, 2 * $num_lines + 1, true); } } $with_context[] = array_intersect_key($changes, $context); } return array_mergev($with_context); } public function getAnchorName() { return 'change-'.PhabricatorHash::digestForAnchor($this->getFilename()); } public function getAbsoluteRepositoryPath( PhabricatorRepository $repository = null, DifferentialDiff $diff = null) { $base = '/'; if ($diff && $diff->getSourceControlPath()) { $base = id(new PhutilURI($diff->getSourceControlPath()))->getPath(); } $path = $this->getFilename(); $path = rtrim($base, '/').'/'.ltrim($path, '/'); $svn = PhabricatorRepositoryType::REPOSITORY_TYPE_SVN; if ($repository && $repository->getVersionControlSystem() == $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; } public function attachDiff(DifferentialDiff $diff) { $this->diff = $diff; return $this; } public function getDiff() { return $this->assertAttached($this->diff); } public function getOldStatePathVector() { $path = $this->getOldFile(); if (!strlen($path)) { $path = $this->getFilename(); } $path = trim($path, '/'); $path = explode('/', $path); return $path; } public function getNewStatePathVector() { if (!$this->hasNewState()) { return null; } $path = $this->getFilename(); $path = trim($path, '/'); $path = explode('/', $path); return $path; } public function newFileTreeIcon() { $icon = $this->getPathIconIcon(); $color = $this->getPathIconColor(); return id(new PHUIIconView()) ->setIcon("{$icon} {$color}"); } public function getIsOwnedChangeset() { $authority_packages = $this->getAuthorityPackages(); $changeset_packages = $this->getChangesetPackages(); if (!$authority_packages || !$changeset_packages) { return false; } return (bool)array_intersect_key($authority_packages, $changeset_packages); } public function getIsLowImportanceChangeset() { if (!$this->hasNewState()) { return true; } if ($this->isGeneratedChangeset()) { return true; } return false; } public function getPathIconIcon() { return idx($this->getPathIconDetails(), 'icon'); } public function getPathIconColor() { return idx($this->getPathIconDetails(), 'color'); } private function getPathIconDetails() { $change_icons = array( DifferentialChangeType::TYPE_DELETE => array( 'icon' => 'fa-times', 'color' => 'delete-color', ), DifferentialChangeType::TYPE_ADD => array( 'icon' => 'fa-plus', 'color' => 'create-color', ), DifferentialChangeType::TYPE_MOVE_AWAY => array( 'icon' => 'fa-circle-o', 'color' => 'grey', ), DifferentialChangeType::TYPE_MULTICOPY => array( 'icon' => 'fa-circle-o', 'color' => 'grey', ), DifferentialChangeType::TYPE_MOVE_HERE => array( 'icon' => 'fa-plus-circle', 'color' => 'create-color', ), DifferentialChangeType::TYPE_COPY_HERE => array( 'icon' => 'fa-plus-circle', 'color' => 'create-color', ), ); $change_type = $this->getChangeType(); if (isset($change_icons[$change_type])) { return $change_icons[$change_type]; } if ($this->isGeneratedChangeset()) { return array( 'icon' => 'fa-cogs', 'color' => 'grey', ); } $file_type = $this->getFileType(); $icon = DifferentialChangeType::getIconForFileType($file_type); return array( 'icon' => $icon, 'color' => 'bluetext', ); } public function setChangesetMetadata($key, $value) { if (!is_array($this->metadata)) { $this->metadata = array(); } $this->metadata[$key] = $value; return $this; } public function getChangesetMetadata($key, $default = null) { if (!is_array($this->metadata)) { return $default; } return idx($this->metadata, $key, $default); } private function setInternalChangesetAttribute($trusted, $key, $value) { if ($trusted) { $meta_key = self::METADATA_TRUSTED_ATTRIBUTES; } else { $meta_key = self::METADATA_UNTRUSTED_ATTRIBUTES; } $attributes = $this->getChangesetMetadata($meta_key, array()); $attributes[$key] = $value; $this->setChangesetMetadata($meta_key, $attributes); return $this; } private function getInternalChangesetAttributes($trusted) { if ($trusted) { $meta_key = self::METADATA_TRUSTED_ATTRIBUTES; } else { $meta_key = self::METADATA_UNTRUSTED_ATTRIBUTES; } return $this->getChangesetMetadata($meta_key, array()); } public function setTrustedChangesetAttribute($key, $value) { return $this->setInternalChangesetAttribute(true, $key, $value); } public function getTrustedChangesetAttributes() { return $this->getInternalChangesetAttributes(true); } public function getTrustedChangesetAttribute($key, $default = null) { $map = $this->getTrustedChangesetAttributes(); return idx($map, $key, $default); } public function setUntrustedChangesetAttribute($key, $value) { return $this->setInternalChangesetAttribute(false, $key, $value); } public function getUntrustedChangesetAttributes() { return $this->getInternalChangesetAttributes(false); } public function getUntrustedChangesetAttribute($key, $default = null) { $map = $this->getUntrustedChangesetAttributes(); return idx($map, $key, $default); } public function getChangesetAttributes() { // Prefer trusted values over untrusted values when both exist. return $this->getTrustedChangesetAttributes() + $this->getUntrustedChangesetAttributes(); } public function getChangesetAttribute($key, $default = null) { $map = $this->getChangesetAttributes(); return idx($map, $key, $default); } public function isGeneratedChangeset() { return $this->getChangesetAttribute(self::ATTRIBUTE_GENERATED); } public function getNewFileObjectPHID() { $metadata = $this->getMetadata(); return idx($metadata, 'new:binary-phid'); } public function getOldFileObjectPHID() { $metadata = $this->getMetadata(); return idx($metadata, 'old:binary-phid'); } public function attachNewFileObject(PhabricatorFile $file) { $this->newFileObject = $file; return $this; } public function getNewFileObject() { return $this->assertAttached($this->newFileObject); } public function attachOldFileObject(PhabricatorFile $file) { $this->oldFileObject = $file; return $this; } public function getOldFileObject() { return $this->assertAttached($this->oldFileObject); } public function newComparisonChangeset( DifferentialChangeset $against = null) { $left = $this; $right = $against; $left_data = $left->makeNewFile(); $left_properties = $left->getNewProperties(); $left_metadata = $left->getNewStateMetadata(); $left_state = $left->hasNewState(); $shared_metadata = $left->getMetadata(); + $left_type = $left->getNewFileType(); if ($right) { $right_data = $right->makeNewFile(); $right_properties = $right->getNewProperties(); $right_metadata = $right->getNewStateMetadata(); $right_state = $right->hasNewState(); $shared_metadata = $right->getMetadata(); + $right_type = $right->getNewFileType(); } else { $right_data = $left->makeOldFile(); $right_properties = $left->getOldProperties(); $right_metadata = $left->getOldStateMetadata(); $right_state = $left->hasOldState(); + $right_type = $left->getOldFileType(); } $engine = new PhabricatorDifferenceEngine(); $synthetic = $engine->generateChangesetFromFileContent( $left_data, $right_data); $comparison = id(new self()) ->makeEphemeral(true) ->attachDiff($left->getDiff()) ->setOldFile($left->getFilename()) ->setFilename($right->getFilename()); // TODO: Change type? - // TODO: File type? // TODO: Away paths? // TODO: View state key? $comparison->attachHunks($synthetic->getHunks()); $comparison->setOldProperties($left_properties); $comparison->setNewProperties($right_properties); $comparison ->setOldStateMetadata($left_metadata) ->setNewStateMetadata($right_metadata) ->setHasOldState($left_state) - ->setHasNewState($right_state); + ->setHasNewState($right_state) + ->setOldFileType($left_type) + ->setNewFileType($right_type); // NOTE: Some metadata is not stored statefully, like the "generated" // flag. For now, use the rightmost "new state" metadata to fill in these // values. $metadata = $comparison->getMetadata(); $metadata = $metadata + $shared_metadata; $comparison->setMetadata($metadata); return $comparison; } + + public function setNewFileType($new_file_type) { + $this->newFileType = $new_file_type; + return $this; + } + + public function getNewFileType() { + if ($this->newFileType !== null) { + return $this->newFileType; + } + + return $this->getFiletype(); + } + + public function setOldFileType($old_file_type) { + $this->oldFileType = $old_file_type; + return $this; + } + + public function getOldFileType() { + if ($this->oldFileType !== null) { + return $this->oldFileType; + } + + return $this->getFileType(); + } + + public function hasSourceTextBody() { + $type_map = array( + DifferentialChangeType::FILE_TEXT => true, + DifferentialChangeType::FILE_SYMLINK => true, + ); + + $old_body = isset($type_map[$this->getOldFileType()]); + $new_body = isset($type_map[$this->getNewFileType()]); + + return ($old_body || $new_body); + } + public function getNewStateMetadata() { return $this->getMetadataWithPrefix('new:'); } public function setNewStateMetadata(array $metadata) { return $this->setMetadataWithPrefix($metadata, 'new:'); } public function getOldStateMetadata() { return $this->getMetadataWithPrefix('old:'); } public function setOldStateMetadata(array $metadata) { return $this->setMetadataWithPrefix($metadata, 'old:'); } private function getMetadataWithPrefix($prefix) { $length = strlen($prefix); $result = array(); foreach ($this->getMetadata() as $key => $value) { if (strncmp($key, $prefix, $length)) { continue; } $key = substr($key, $length); $result[$key] = $value; } return $result; } private function setMetadataWithPrefix(array $metadata, $prefix) { foreach ($metadata as $key => $value) { $key = $prefix.$key; $this->metadata[$key] = $value; } return $this; } /* -( PhabricatorPolicyInterface )----------------------------------------- */ public function getCapabilities() { return array( PhabricatorPolicyCapability::CAN_VIEW, ); } public function getPolicy($capability) { return $this->getDiff()->getPolicy($capability); } public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { return $this->getDiff()->hasAutomaticCapability($capability, $viewer); } /* -( PhabricatorDestructibleInterface )----------------------------------- */ public function destroyObjectPermanently( PhabricatorDestructionEngine $engine) { $this->openTransaction(); $hunks = id(new DifferentialHunk())->loadAllWhere( 'changesetID = %d', $this->getID()); foreach ($hunks as $hunk) { $engine->destroyObject($hunk); } $this->delete(); $this->saveTransaction(); } }