1<?php
2
3final class ArcanistBrowseCommitHardpointQuery
4  extends ArcanistRuntimeHardpointQuery {
5
6  public function getHardpoints() {
7    return array(
8      ArcanistBrowseRef::HARDPOINT_COMMITREFS,
9    );
10  }
11
12  protected function canLoadRef(ArcanistRef $ref) {
13    return ($ref instanceof ArcanistBrowseRef);
14  }
15
16  public function loadHardpoint(array $refs, $hardpoint) {
17    $api = $this->getRepositoryAPI();
18
19    $commit_map = array();
20    foreach ($refs as $key => $ref) {
21      $token = $ref->getToken();
22
23      if ($token === '.') {
24        // Git resolves "." like HEAD, but we want to treat it as "browse the
25        // current directory" instead in all cases.
26        continue;
27      }
28
29      // Always resolve the empty token; top-level loaders filter out
30      // irrelevant tokens before this stage.
31      if ($token === null) {
32        $token = $api->getHeadCommit();
33      }
34
35      // TODO: We should pull a full commit ref out of the API as soon as it
36      // is able to provide them. In particular, we currently miss Git tree
37      // hashes which reduces the accuracy of lookups.
38
39      try {
40        $commit = $api->getCanonicalRevisionName($token);
41        if ($commit) {
42          $commit_map[$commit][] = $key;
43        }
44      } catch (Exception $ex) {
45        // Ignore anything we can't resolve.
46      }
47    }
48
49    if (!$commit_map) {
50      yield $this->yieldMap(array());
51    }
52
53    $results = array();
54    foreach ($commit_map as $commit_identifier => $ref_keys) {
55      foreach ($ref_keys as $key) {
56        $commit_ref = id(new ArcanistCommitRef())
57          ->setCommitHash($commit_identifier);
58        $results[$key][] = $commit_ref;
59      }
60    }
61
62    yield $this->yieldMap($results);
63  }
64
65}
66