~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to phabricator/src/applications/repository/storage/PhabricatorRepositoryPushLog.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • Revision ID: package-import@ubuntu.com-20141023204926-ar20vnfjqwxysrce
Tags: upstream-0~git20141023
ImportĀ upstreamĀ versionĀ 0~git20141023

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Records a push to a hosted repository. This allows us to store metadata
 
5
 * about who pushed commits, when, and from where. We can also record the
 
6
 * history of branches and tags, which is not normally persisted outside of
 
7
 * the reflog.
 
8
 *
 
9
 * This log is written by commit hooks installed into hosted repositories.
 
10
 * See @{class:DiffusionCommitHookEngine}.
 
11
 */
 
12
final class PhabricatorRepositoryPushLog
 
13
  extends PhabricatorRepositoryDAO
 
14
  implements PhabricatorPolicyInterface {
 
15
 
 
16
  const REFTYPE_BRANCH = 'branch';
 
17
  const REFTYPE_TAG = 'tag';
 
18
  const REFTYPE_BOOKMARK = 'bookmark';
 
19
  const REFTYPE_COMMIT = 'commit';
 
20
 
 
21
  const CHANGEFLAG_ADD = 1;
 
22
  const CHANGEFLAG_DELETE = 2;
 
23
  const CHANGEFLAG_APPEND = 4;
 
24
  const CHANGEFLAG_REWRITE = 8;
 
25
  const CHANGEFLAG_DANGEROUS = 16;
 
26
 
 
27
  const REJECT_ACCEPT = 0;
 
28
  const REJECT_DANGEROUS = 1;
 
29
  const REJECT_HERALD = 2;
 
30
  const REJECT_EXTERNAL = 3;
 
31
  const REJECT_BROKEN = 4;
 
32
 
 
33
  protected $repositoryPHID;
 
34
  protected $epoch;
 
35
  protected $pusherPHID;
 
36
  protected $pushEventPHID;
 
37
  protected $refType;
 
38
  protected $refNameHash;
 
39
  protected $refNameRaw;
 
40
  protected $refNameEncoding;
 
41
  protected $refOld;
 
42
  protected $refNew;
 
43
  protected $mergeBase;
 
44
  protected $changeFlags;
 
45
 
 
46
  private $dangerousChangeDescription = self::ATTACHABLE;
 
47
  private $pushEvent = self::ATTACHABLE;
 
48
  private $repository = self::ATTACHABLE;
 
49
 
 
50
  public static function initializeNewLog(PhabricatorUser $viewer) {
 
51
    return id(new PhabricatorRepositoryPushLog())
 
52
      ->setPusherPHID($viewer->getPHID());
 
53
  }
 
54
 
 
55
  public static function getHeraldChangeFlagConditionOptions() {
 
56
    return array(
 
57
      PhabricatorRepositoryPushLog::CHANGEFLAG_ADD =>
 
58
        pht('change creates ref'),
 
59
      PhabricatorRepositoryPushLog::CHANGEFLAG_DELETE =>
 
60
        pht('change deletes ref'),
 
61
      PhabricatorRepositoryPushLog::CHANGEFLAG_REWRITE =>
 
62
        pht('change rewrites ref'),
 
63
      PhabricatorRepositoryPushLog::CHANGEFLAG_DANGEROUS =>
 
64
        pht('dangerous change'),
 
65
    );
 
66
  }
 
67
 
 
68
  public function getConfiguration() {
 
69
    return array(
 
70
      self::CONFIG_AUX_PHID => true,
 
71
      self::CONFIG_TIMESTAMPS => false,
 
72
      self::CONFIG_BINARY => array(
 
73
        'refNameRaw' => true,
 
74
      ),
 
75
      self::CONFIG_COLUMN_SCHEMA => array(
 
76
        'refType' => 'text12',
 
77
        'refNameHash' => 'bytes12?',
 
78
        'refNameRaw' => 'bytes?',
 
79
        'refNameEncoding' => 'text16?',
 
80
        'refOld' => 'text40?',
 
81
        'refNew' => 'text40',
 
82
        'mergeBase' => 'text40?',
 
83
        'changeFlags' => 'uint32',
 
84
      ),
 
85
      self::CONFIG_KEY_SCHEMA => array(
 
86
        'key_repository' => array(
 
87
          'columns' => array('repositoryPHID'),
 
88
        ),
 
89
        'key_ref' => array(
 
90
          'columns' => array('repositoryPHID', 'refNew'),
 
91
        ),
 
92
        'key_name' => array(
 
93
          'columns' => array('repositoryPHID', 'refNameHash'),
 
94
        ),
 
95
        'key_event' => array(
 
96
          'columns' => array('pushEventPHID'),
 
97
        ),
 
98
        'key_pusher' => array(
 
99
          'columns' => array('pusherPHID'),
 
100
        ),
 
101
      ),
 
102
    ) + parent::getConfiguration();
 
103
  }
 
104
 
 
105
  public function generatePHID() {
 
106
    return PhabricatorPHID::generateNewPHID(
 
107
      PhabricatorRepositoryPushLogPHIDType::TYPECONST);
 
108
  }
 
109
 
 
110
  public function attachPushEvent(PhabricatorRepositoryPushEvent $push_event) {
 
111
    $this->pushEvent = $push_event;
 
112
    return $this;
 
113
  }
 
114
 
 
115
  public function getPushEvent() {
 
116
    return $this->assertAttached($this->pushEvent);
 
117
  }
 
118
 
 
119
  public function getRefName() {
 
120
    return $this->getUTF8StringFromStorage(
 
121
      $this->getRefNameRaw(),
 
122
      $this->getRefNameEncoding());
 
123
  }
 
124
 
 
125
  public function setRefName($ref_raw) {
 
126
    $this->setRefNameRaw($ref_raw);
 
127
    $this->setRefNameHash(PhabricatorHash::digestForIndex($ref_raw));
 
128
    $this->setRefNameEncoding($this->detectEncodingForStorage($ref_raw));
 
129
 
 
130
    return $this;
 
131
  }
 
132
 
 
133
  public function getRefOldShort() {
 
134
    if ($this->getRepository()->isSVN()) {
 
135
      return $this->getRefOld();
 
136
    }
 
137
    return substr($this->getRefOld(), 0, 12);
 
138
  }
 
139
 
 
140
  public function getRefNewShort() {
 
141
    if ($this->getRepository()->isSVN()) {
 
142
      return $this->getRefNew();
 
143
    }
 
144
    return substr($this->getRefNew(), 0, 12);
 
145
  }
 
146
 
 
147
  public function hasChangeFlags($mask) {
 
148
    return ($this->changeFlags & $mask);
 
149
  }
 
150
 
 
151
  public function attachDangerousChangeDescription($description) {
 
152
    $this->dangerousChangeDescription = $description;
 
153
    return $this;
 
154
  }
 
155
 
 
156
  public function getDangerousChangeDescription() {
 
157
    return $this->assertAttached($this->dangerousChangeDescription);
 
158
  }
 
159
 
 
160
  public function attachRepository(PhabricatorRepository $repository) {
 
161
    // NOTE: Some gymnastics around this because of object construction order
 
162
    // in the hook engine. Particularly, web build the logs before we build
 
163
    // their push event.
 
164
    $this->repository = $repository;
 
165
    return $this;
 
166
  }
 
167
 
 
168
  public function getRepository() {
 
169
    if ($this->repository == self::ATTACHABLE) {
 
170
      return $this->getPushEvent()->getRepository();
 
171
    }
 
172
    return $this->assertAttached($this->repository);
 
173
  }
 
174
 
 
175
 
 
176
/* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 
177
 
 
178
 
 
179
  public function getCapabilities() {
 
180
    return array(
 
181
      PhabricatorPolicyCapability::CAN_VIEW,
 
182
    );
 
183
  }
 
184
 
 
185
  public function getPolicy($capability) {
 
186
    // NOTE: We're passing through the repository rather than the push event
 
187
    // mostly because we need to do policy checks in Herald before we create
 
188
    // the event. The two approaches are equivalent in practice.
 
189
    return $this->getRepository()->getPolicy($capability);
 
190
  }
 
191
 
 
192
  public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 
193
    return $this->getRepository()->hasAutomaticCapability($capability, $viewer);
 
194
  }
 
195
 
 
196
  public function describeAutomaticCapability($capability) {
 
197
    return pht(
 
198
      "A repository's push logs are visible to users who can see the ".
 
199
      "repository.");
 
200
  }
 
201
 
 
202
}