public function testBranchFilter()
 {
     $git = PhabricatorRepositoryType::REPOSITORY_TYPE_GIT;
     $repo = new PhabricatorRepository();
     $repo->setVersionControlSystem($git);
     $this->assertEqual(true, $repo->shouldTrackBranch('imaginary'), 'Track all branches by default.');
     $repo->setDetail('branch-filter', array('master' => true));
     $this->assertEqual(true, $repo->shouldTrackBranch('master'), 'Track listed branches.');
     $this->assertEqual(false, $repo->shouldTrackBranch('imaginary'), 'Do not track unlisted branches.');
 }
 /**
  * @task git
  */
 private function executeGitDiscover(PhabricatorRepository $repository)
 {
     list($remotes) = $repository->execxLocalCommand('remote show -n origin');
     $matches = null;
     if (!preg_match('/^\\s*Fetch URL:\\s*(.*?)\\s*$/m', $remotes, $matches)) {
         throw new Exception("Expected 'Fetch URL' in 'git remote show -n origin'.");
     }
     self::executeGitVerifySameOrigin($matches[1], $repository->getRemoteURI(), $repository->getLocalPath());
     list($stdout) = $repository->execxLocalCommand('branch -r --verbose --no-abbrev');
     $branches = DiffusionGitBranchQuery::parseGitRemoteBranchOutput($stdout, $only_this_remote = DiffusionBranchInformation::DEFAULT_GIT_REMOTE);
     $callsign = $repository->getCallsign();
     $tracked_something = false;
     $this->log("Discovering commits in repository '{$callsign}'...");
     foreach ($branches as $name => $commit) {
         $this->log("Examining branch '{$name}', at {$commit}.");
         if (!$repository->shouldTrackBranch($name)) {
             $this->log("Skipping, branch is untracked.");
             continue;
         }
         $tracked_something = true;
         if ($this->isKnownCommit($repository, $commit)) {
             $this->log("Skipping, HEAD is known.");
             continue;
         }
         $this->log("Looking for new commits.");
         $this->executeGitDiscoverCommit($repository, $commit, $name, false);
     }
     if (!$tracked_something) {
         $repo_name = $repository->getName();
         $repo_callsign = $repository->getCallsign();
         throw new Exception("Repository r{$repo_callsign} '{$repo_name}' has no tracked branches! " . "Verify that your branch filtering settings are correct.");
     }
     $this->log("Discovering commits on autoclose branches...");
     foreach ($branches as $name => $commit) {
         $this->log("Examining branch '{$name}', at {$commit}'.");
         if (!$repository->shouldTrackBranch($name)) {
             $this->log("Skipping, branch is untracked.");
             continue;
         }
         if (!$repository->shouldAutocloseBranch($name)) {
             $this->log("Skipping, branch is not autoclose.");
             continue;
         }
         if ($this->isKnownCommitOnAnyAutocloseBranch($repository, $commit)) {
             $this->log("Skipping, commit is known on an autoclose branch.");
             continue;
         }
         $this->log("Looking for new autoclose commits.");
         $this->executeGitDiscoverCommit($repository, $commit, $name, true);
     }
 }
 private function rebuildRepository(PhabricatorRepository $repo)
 {
     $console = PhutilConsole::getConsole();
     $console->writeOut("%s\n", pht('Rebuilding "%s"...', $repo->getMonogram()));
     $refs = id(new PhabricatorRepositoryRefCursorQuery())->setViewer($this->getViewer())->withRefTypes(array(PhabricatorRepositoryRefCursor::TYPE_BRANCH))->withRepositoryPHIDs(array($repo->getPHID()))->execute();
     $graph = array();
     foreach ($refs as $ref) {
         if (!$repo->shouldTrackBranch($ref->getRefName())) {
             continue;
         }
         $console->writeOut("%s\n", pht('Rebuilding branch "%s"...', $ref->getRefName()));
         $commit = $ref->getCommitIdentifier();
         if ($repo->isGit()) {
             $stream = new PhabricatorGitGraphStream($repo, $commit);
         } else {
             $stream = new PhabricatorMercurialGraphStream($repo, $commit);
         }
         $discover = array($commit);
         while ($discover) {
             $target = array_pop($discover);
             if (isset($graph[$target])) {
                 continue;
             }
             $graph[$target] = $stream->getParents($target);
             foreach ($graph[$target] as $parent) {
                 $discover[] = $parent;
             }
         }
     }
     $console->writeOut("%s\n", pht('Found %s total commit(s); updating...', phutil_count($graph)));
     $commit_table = id(new PhabricatorRepositoryCommit());
     $commit_table_name = $commit_table->getTableName();
     $conn_w = $commit_table->establishConnection('w');
     $bar = id(new PhutilConsoleProgressBar())->setTotal(count($graph));
     $need = array();
     foreach ($graph as $child => $parents) {
         foreach ($parents as $parent) {
             $need[$parent] = $parent;
         }
         $need[$child] = $child;
     }
     $map = array();
     foreach (array_chunk($need, 2048) as $chunk) {
         $rows = queryfx_all($conn_w, 'SELECT id, commitIdentifier FROM %T
       WHERE commitIdentifier IN (%Ls) AND repositoryID = %d', $commit_table_name, $chunk, $repo->getID());
         foreach ($rows as $row) {
             $map[$row['commitIdentifier']] = $row['id'];
         }
     }
     $insert_sql = array();
     $delete_sql = array();
     foreach ($graph as $child => $parents) {
         $names = $parents;
         $names[] = $child;
         foreach ($names as $name) {
             if (empty($map[$name])) {
                 throw new Exception(pht('Unknown commit "%s"!', $name));
             }
         }
         if (!$parents) {
             // Write an explicit 0 to indicate "no parents" instead of "no data".
             $insert_sql[] = qsprintf($conn_w, '(%d, 0)', $map[$child]);
         } else {
             foreach ($parents as $parent) {
                 $insert_sql[] = qsprintf($conn_w, '(%d, %d)', $map[$child], $map[$parent]);
             }
         }
         $delete_sql[] = $map[$child];
         $bar->update(1);
     }
     $commit_table->openTransaction();
     foreach (PhabricatorLiskDAO::chunkSQL($delete_sql) as $chunk) {
         queryfx($conn_w, 'DELETE FROM %T WHERE childCommitID IN (%Q)', PhabricatorRepository::TABLE_PARENTS, $chunk);
     }
     foreach (PhabricatorLiskDAO::chunkSQL($insert_sql) as $chunk) {
         queryfx($conn_w, 'INSERT INTO %T (childCommitID, parentCommitID) VALUES %Q', PhabricatorRepository::TABLE_PARENTS, $chunk);
     }
     $commit_table->saveTransaction();
     $bar->done();
 }