8 * Repository ID (number in repositories directory)
15 * Full path to the .git repository
22 * Full path to the work tree directory
29 * Revision of the repository that shall be shown
36 * Commit message of the last (or current) revision
44 * Load Repository data from GET-Request
48 * @throws Exception When something is wrong
50 public function loadFromRequest()
52 if (!isset($_GET['id'])) {
53 throw new Exception_Input('Paste ID missing');
55 if (!is_numeric($_GET['id'])) {
56 throw new Exception_Input('Paste ID not numeric');
58 if (isset($_GET['rev'])) {
59 $this->hash = $_GET['rev'];
62 $this->id = (int)$_GET['id'];
68 public function loadById($id)
70 if (!is_numeric($id)) {
71 throw new Exception_Input('Paste ID not numeric');
78 protected function loadDirs()
80 $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
81 if (!is_dir($gitDir)) {
82 throw new Exception_NotFound(
83 sprintf('Paste %d .git dir not found', $this->id)
86 $this->gitDir = $gitDir;
88 $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
89 if (!is_dir($workDir)) {
90 throw new Exception_NotFound(
91 sprintf('Paste %d work dir not found', $this->id)
94 $this->workDir = $workDir;
97 public function loadHash()
100 if ($this->hash !== null) {
104 $output = $this->getVc()->getCommand('log')
105 ->setOption('pretty', 'format:%H')
106 ->setOption('max-count', 1)
108 $output = trim($output);
109 if (strlen($output) !== 40) {
111 'Loading commit hash failed: ' . $output
114 $this->hash = $output;
118 * Populates $this->message
122 public function loadMessage()
124 $rev = (isset($this->hash)) ? $this->hash : 'HEAD';
125 $output = $this->getVc()->getCommand('log')
126 ->setOption('oneline')
130 $output = trim($output);
131 if (strpos($output, ' ') > 0) {
132 $output = substr($output, strpos($output, ' '), strlen($output));
133 $this->message = trim($output);
135 $this->message = "This commit message intentionally left blank.";
139 public function getVc()
141 return new \VersionControl_Git($this->gitDir);
145 * Loads the list of files in this repository
147 * @return File[] Array of files
149 public function getFiles()
151 $files = $this->getFilePaths();
153 foreach ($files as $name) {
154 $arFiles[] = new File($name, $this);
159 protected function getFilePaths()
161 if ($this->hash === null) {
166 $output = $this->getVc()->getCommand('ls-tree')
168 ->setOption('name-only')
171 return explode("\n", trim($output));
174 public function getFileByName($name, $bHasToExist = true)
176 $name = Tools::sanitizeFilename($name);
178 throw new Exception_Input('Empty file name given');
182 $files = $this->getFilePaths();
183 if (array_search($name, $files) === false) {
184 throw new Exception_Input('File does not exist');
187 return new File($name, $this);
190 public function hasFile($name)
193 $this->getFileByName($name);
194 } catch (Exception $e) {
201 * Permanently deletes the paste repository without any way to get
204 * @return boolean True if all went well, false if not
206 public function delete()
208 $db = new Database();
209 $db->getIndexer()->deleteRepo($this);
211 return Tools::recursiveDelete($this->workDir)
212 && Tools::recursiveDelete($this->gitDir);
215 public function getTitle()
217 $desc = $this->getDescription();
218 if (trim($desc) != '') {
222 return 'paste #' . $this->id;
225 public function getDescription()
227 if (!is_readable($this->gitDir . '/description')) {
230 return file_get_contents($this->gitDir . '/description');
233 public function setDescription($description)
235 file_put_contents($this->gitDir . '/description', $description);
239 * @return array Array with keys "email" and "name"
241 public function getOwner()
244 $name = $this->getVc()->getCommand('config')
245 ->addArgument('owner.name')->execute();
246 } catch (\VersionControl_Git_Exception $e) {
247 $name = $GLOBALS['phorkie']['auth']['anonymousName'];
250 $email = $this->getVc()->getCommand('config')
251 ->addArgument('owner.email')->execute();
252 } catch (\VersionControl_Git_Exception $e) {
253 $email = $GLOBALS['phorkie']['auth']['anonymousEmail'];
256 return array('name' => trim($name), 'email' => trim($email));
260 * Get a link to the repository
262 * @param string $type Link type. Supported are:
269 * @param string $option Additional link option, e.g. revision number
270 * @param boolean $full Return full URL or normal relative
274 public function getLink($type, $option = null, $full = false)
276 if ($type == 'edit') {
277 $link = $this->id . '/edit';
278 } else if ($type == 'display') {
280 } else if ($type == 'fork') {
281 $link = $this->id . '/fork';
282 } else if ($type == 'doap') {
283 $link = $this->id . '/doap';
284 } else if ($type == 'delete') {
285 $link = $this->id . '/delete';
286 } else if ($type == 'delete-confirm') {
287 $link = $this->id . '/delete/confirm';
288 } else if ($type == 'revision') {
289 $link = $this->id . '/rev/' . $option;
291 throw new Exception('Unknown link type');
295 $link = Tools::fullUrl($link);
300 public function getCloneURL($public = true)
302 $var = $public ? 'public' : 'private';
303 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
304 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
310 * Returns the history of the repository.
311 * We don't use VersionControl_Git's rev list fetcher since it does not
312 * give us separate email addresses and names, and it does not give us
313 * the amount of changed (added/deleted) lines.
315 * @return array Array of history objects
317 public function getHistory()
319 $output = $this->getVc()->getCommand('log')
320 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
321 ->setOption('max-count', 10)
322 ->setOption('shortstat')
325 $arCommits = array();
326 $arOutput = explode("\n", $output);
327 $lines = count($arOutput);
329 while ($current < $lines) {
330 $commit = new Repository_Commit();
331 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
332 if ($name !== 'commit') {
334 'Git log output format not as expected: ' . $arOutput[$current]
337 $commit->committerTime = $arOutput[$current + 1];
338 $commit->committerName = $arOutput[$current + 2];
339 $commit->committerEmail = $arOutput[$current + 3];
341 if (substr($arOutput[$current + 4], 0, 1) != ' ') {
342 //commit without changed lines
343 $arCommits[] = $commit;
348 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
349 $commit->filesChanged = $arLineParts[0];
350 $commit->linesAdded = $arLineParts[3];
351 if (isset($arLineParts[5])) {
352 $commit->linesDeleted = $arLineParts[5];
357 $arCommits[] = $commit;
364 * @return Repository_ConnectionInfo
366 public function getConnectionInfo()
368 return new Repository_ConnectionInfo($this);