Page MenuHomePhabricator

D7982.id18060.diff
No OneTemporary

D7982.id18060.diff

Index: resources/sql/autopatches/20140116.reporefcursor.sql
===================================================================
--- /dev/null
+++ resources/sql/autopatches/20140116.reporefcursor.sql
@@ -0,0 +1,11 @@
+CREATE TABLE {$NAMESPACE}_repository.repository_refcursor (
+ id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+ repositoryPHID VARCHAR(64) NOT NULL COLLATE utf8_bin,
+ refType VARCHAR(32) NOT NULL COLLATE utf8_bin,
+ refNameHash VARCHAR(12) NOT NULL COLLATE latin1_bin,
+ refNameRaw LONGTEXT NOT NULL COLLATE latin1_bin,
+ refNameEncoding VARCHAR(16) COLLATE utf8_bin,
+ commitIdentifier VARCHAR(40) NOT NULL COLLATE utf8_bin,
+
+ UNIQUE KEY `key_cursor` (repositoryPHID, refType, refNameHash)
+) ENGINE=InnoDB, COLLATE=utf8_general_ci;
Index: src/__phutil_library_map__.php
===================================================================
--- src/__phutil_library_map__.php
+++ src/__phutil_library_map__.php
@@ -1870,6 +1870,8 @@
'PhabricatorRepositoryPushLogQuery' => 'applications/repository/query/PhabricatorRepositoryPushLogQuery.php',
'PhabricatorRepositoryPushLogSearchEngine' => 'applications/repository/query/PhabricatorRepositoryPushLogSearchEngine.php',
'PhabricatorRepositoryQuery' => 'applications/repository/query/PhabricatorRepositoryQuery.php',
+ 'PhabricatorRepositoryRefCursor' => 'applications/repository/storage/PhabricatorRepositoryRefCursor.php',
+ 'PhabricatorRepositoryRefCursorQuery' => 'applications/repository/query/PhabricatorRepositoryRefCursorQuery.php',
'PhabricatorRepositorySearchEngine' => 'applications/repository/query/PhabricatorRepositorySearchEngine.php',
'PhabricatorRepositoryStatusMessage' => 'applications/repository/storage/PhabricatorRepositoryStatusMessage.php',
'PhabricatorRepositorySvnCommitChangeParserWorker' => 'applications/repository/worker/commitchangeparser/PhabricatorRepositorySvnCommitChangeParserWorker.php',
@@ -4537,6 +4539,12 @@
'PhabricatorRepositoryPushLogQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorRepositoryPushLogSearchEngine' => 'PhabricatorApplicationSearchEngine',
'PhabricatorRepositoryQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
+ 'PhabricatorRepositoryRefCursor' =>
+ array(
+ 0 => 'PhabricatorRepositoryDAO',
+ 1 => 'PhabricatorPolicyInterface',
+ ),
+ 'PhabricatorRepositoryRefCursorQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorRepositorySearchEngine' => 'PhabricatorApplicationSearchEngine',
'PhabricatorRepositoryStatusMessage' => 'PhabricatorRepositoryDAO',
'PhabricatorRepositorySvnCommitChangeParserWorker' => 'PhabricatorRepositoryCommitChangeParserWorker',
Index: src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php
===================================================================
--- src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php
+++ src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php
@@ -612,8 +612,120 @@
$this->log("Looking for new autoclose commits.");
$this->executeGitDiscoverCommit($repository, $commit, $name, true);
}
+
+ $tags = id(new DiffusionLowLevelGitRefQuery())
+ ->setRepository($repository)
+ ->withIsTag(true)
+ ->execute();
+ $tags = mpull($tags, 'getCommitIdentifier', 'getShortName');
+
+ $ref_cursors = id(new PhabricatorRepositoryRefCursorQuery())
+ ->setViewer($this->getViewer())
+ ->withRepositoryPHIDs(array($repository->getPHID()))
+ ->execute();
+
+ $all_updates = array();
+ $all_deletes = array();
+
+ // Update all the branches.
+ list($updates, $deletes) = $this->updateCursors(
+ $repository,
+ $ref_cursors,
+ $branches,
+ PhabricatorRepositoryRefCursor::TYPE_BRANCH);
+ $all_updates[] = $updates;
+ $all_deletes[] = $deletes;
+
+ // Update all the tags.
+ list($updates, $deletes) = $this->updateCursors(
+ $repository,
+ $ref_cursors,
+ $tags,
+ PhabricatorRepositoryRefCursor::TYPE_TAG);
+ $all_updates[] = $updates;
+ $all_deletes[] = $deletes;
+
+ $all_updates = array_mergev($all_updates);
+ $all_deletes = array_mergev($all_deletes);
+ if ($all_updates || $all_deletes) {
+ $repository->openTransaction();
+ foreach ($all_updates as $ref) {
+ $ref->save();
+ }
+ foreach ($all_deletes as $ref) {
+ $ref->delete();
+ }
+ $repository->saveTransaction();
+ }
}
+ private function updateCursors(
+ PhabricatorRepository $repository,
+ array $cursors,
+ array $new_positions,
+ $ref_type) {
+
+ $cursors = mgroup($cursors, 'getRefType');
+ $cursors = idx($cursors, $ref_type, array());
+ $cursors = mpull($cursors, null, 'getRefNameRaw');
+
+ $updates = array();
+ $deletes = array();
+ foreach ($new_positions as $name => $commit) {
+ $cursor = idx($cursors, $name);
+ if (!$cursor) {
+ // This is a brand new ref which we've never seen before (or maybe
+ // we saw it long ago, but we saw it get deleted after that).
+ $this->log(
+ pht(
+ 'Discovered new %s "%s", at %s.',
+ $ref_type,
+ $name,
+ $commit));
+
+ // TODO: Find all new commits on this ref which are not ancestors
+ // of a known ref and do autoclose junk.
+
+ $updates[] = id(new PhabricatorRepositoryRefCursor())
+ ->setRepositoryPHID($repository->getPHID())
+ ->setRefType($ref_type)
+ ->setRefName($name)
+ ->setCommitIdentifier($commit);
+ } else if ($cursor->getCommitIdentifier() != $commit) {
+ // This is an updated ref; we've seen it before but the HEAD is
+ // different now.
+ $this->log(
+ pht(
+ 'Discovered %s update for "%s", from %s -> %s.',
+ $ref_type,
+ $cursor->getRefName(),
+ $cursor->getCommitIdentifier(),
+ $commit));
+
+ // TODO: Find all new commits on this ref which are not ancestors
+ // of the previous ref HEAD and do autoclose junk.
+
+ $cursor->setCommitIdentifier($commit);
+ $updates[] = $cursor;
+ } else {
+ // This is an existing ref which hasn't moved.
+ $this->log(
+ pht(
+ 'Ref %s "%s" is unchanged, at %s.',
+ $ref_type,
+ $cursor->getRefName(),
+ $cursor->getCommitIdentifier()));
+ }
+ }
+
+ foreach ($cursors as $name => $cursor) {
+ if (idx($new_positions, $name) === null) {
+ $deletes[] = $cursor;
+ }
+ }
+
+ return array($updates, $deletes);
+ }
/**
* @task git
Index: src/applications/repository/query/PhabricatorRepositoryRefCursorQuery.php
===================================================================
--- /dev/null
+++ src/applications/repository/query/PhabricatorRepositoryRefCursorQuery.php
@@ -0,0 +1,82 @@
+<?php
+
+final class PhabricatorRepositoryRefCursorQuery
+ extends PhabricatorCursorPagedPolicyAwareQuery {
+
+ private $repositoryPHIDs;
+ private $refTypes;
+
+ public function withRepositoryPHIDs(array $phids) {
+ $this->repositoryPHIDs = $phids;
+ return $this;
+ }
+
+ public function withRefTypes(array $types) {
+ $this->refTypes = $types;
+ return $this;
+ }
+
+ protected function loadPage() {
+ $table = new PhabricatorRepositoryRefCursor();
+ $conn_r = $table->establishConnection('r');
+
+ $data = queryfx_all(
+ $conn_r,
+ 'SELECT * FROM %T r %Q %Q %Q',
+ $table->getTableName(),
+ $this->buildWhereClause($conn_r),
+ $this->buildOrderClause($conn_r),
+ $this->buildLimitClause($conn_r));
+
+ return $table->loadAllFromArray($data);
+ }
+
+ public function willFilterPage(array $refs) {
+ $repository_phids = mpull($refs, 'getRepositoryPHID');
+
+ $repositories = id(new PhabricatorRepositoryQuery())
+ ->setViewer($this->getViewer())
+ ->setParentQuery($this)
+ ->withPHIDs($repository_phids)
+ ->execute();
+ $repositories = mpull($repositories, null, 'getPHID');
+
+ foreach ($refs as $key => $ref) {
+ $repository = idx($repositories, $ref->getRepositoryPHID());
+ if (!$repository) {
+ unset($refs[$key]);
+ continue;
+ }
+ $ref->attachRepository($repository);
+ }
+
+ return $refs;
+ }
+ private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
+ $where = array();
+
+ if ($this->repositoryPHIDs) {
+ $where[] = qsprintf(
+ $conn_r,
+ 'repositoryPHID IN (%Ls)',
+ $this->repositoryPHIDs);
+ }
+
+ if ($this->refTypes) {
+ $where[] = qsprintf(
+ $conn_r,
+ 'refType IN (%Ls)',
+ $this->refTypes);
+ }
+
+ $where[] = $this->buildPagingClause($conn_r);
+
+ return $this->formatWhereClause($where);
+ }
+
+
+ public function getQueryApplicationClass() {
+ return 'PhabricatorApplicationDiffusion';
+ }
+
+}
Index: src/applications/repository/storage/PhabricatorRepository.php
===================================================================
--- src/applications/repository/storage/PhabricatorRepository.php
+++ src/applications/repository/storage/PhabricatorRepository.php
@@ -802,6 +802,12 @@
$mirror->delete();
}
+ $ref_cursors = id(new PhabricatorRepositoryRefCursor())
+ ->loadAllWhere('repositoryPHID = %s', $this->getPHID());
+ foreach ($ref_cursors as $cursor) {
+ $cursor->delete();
+ }
+
$conn_w = $this->establishConnection('w');
queryfx(
Index: src/applications/repository/storage/PhabricatorRepositoryPushLog.php
===================================================================
--- src/applications/repository/storage/PhabricatorRepositoryPushLog.php
+++ src/applications/repository/storage/PhabricatorRepositoryPushLog.php
@@ -77,18 +77,15 @@
}
public function getRefName() {
- if ($this->getRefNameEncoding() == 'utf8') {
- return $this->getRefNameRaw();
- }
- return phutil_utf8ize($this->getRefNameRaw());
+ return $this->getUTF8StringFromStorage(
+ $this->getRefNameRaw(),
+ $this->getRefNameEncoding());
}
public function setRefName($ref_raw) {
- $encoding = phutil_is_utf8($ref_raw) ? 'utf8' : null;
-
$this->setRefNameRaw($ref_raw);
$this->setRefNameHash(PhabricatorHash::digestForIndex($ref_raw));
- $this->setRefNameEncoding($encoding);
+ $this->setRefNameEncoding($this->detectEncodingForStorage($ref_raw));
return $this;
}
Index: src/applications/repository/storage/PhabricatorRepositoryRefCursor.php
===================================================================
--- /dev/null
+++ src/applications/repository/storage/PhabricatorRepositoryRefCursor.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * Stores the previous value of a ref (like a branch or tag) so we can figure
+ * out how a repository has changed when we discover new commits or branch
+ * heads.
+ */
+final class PhabricatorRepositoryRefCursor extends PhabricatorRepositoryDAO
+ implements PhabricatorPolicyInterface {
+
+ const TYPE_BRANCH = 'branch';
+ const TYPE_TAG = 'tag';
+
+ protected $repositoryPHID;
+ protected $refType;
+ protected $refNameHash;
+ protected $refNameRaw;
+ protected $refNameEncoding;
+ protected $commitIdentifier;
+
+ private $repository = self::ATTACHABLE;
+
+ public function getConfiguration() {
+ return array(
+ self::CONFIG_TIMESTAMPS => false,
+ ) + parent::getConfiguration();
+ }
+
+ public function getRefName() {
+ return $this->getUTF8StringFromStorage(
+ $this->getRefNameRaw(),
+ $this->getRefNameEncoding());
+ }
+
+ public function setRefName($ref_raw) {
+ $this->setRefNameRaw($ref_raw);
+ $this->setRefNameHash(PhabricatorHash::digestForIndex($ref_raw));
+ $this->setRefNameEncoding($this->detectEncodingForStorage($ref_raw));
+
+ return $this;
+ }
+
+ public function attachRepository(PhabricatorRepository $repository) {
+ $this->repository = $repository;
+ return $this;
+ }
+
+ public function getRepository() {
+ return $this->assertAttached($this->repository);
+ }
+
+
+/* -( PhabricatorPolicyInterface )----------------------------------------- */
+
+
+ public function getCapabilities() {
+ return array(
+ PhabricatorPolicyCapability::CAN_VIEW,
+ );
+ }
+
+ public function getPolicy($capability) {
+ return $this->getRepository()->getPolicy($capability);
+ }
+
+ public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
+ return $this->getRepository()->hasAutomaticCapability($capability, $viewer);
+ }
+
+ public function describeAutomaticCapability($capability) {
+ return pht('Repository refs have the same policies as their repository.');
+ }
+
+}
Index: src/infrastructure/storage/lisk/PhabricatorLiskDAO.php
===================================================================
--- src/infrastructure/storage/lisk/PhabricatorLiskDAO.php
+++ src/infrastructure/storage/lisk/PhabricatorLiskDAO.php
@@ -175,4 +175,15 @@
return $value[$key];
}
+ protected function detectEncodingForStorage($string) {
+ return phutil_is_utf8($string) ? 'utf8' : null;
+ }
+
+ protected function getUTF8StringFromStorage($string, $encoding) {
+ if ($encoding == 'utf8') {
+ return $string;
+ }
+ return phutil_utf8ize($string);
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 7, 12:36 AM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6718064
Default Alt Text
D7982.id18060.diff (13 KB)

Event Timeline