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 file objects
149 public function getFiles()
151 $files = $this->getFilePaths();
153 foreach ($files as $name) {
154 $arFiles[] = new File($name, $this);
160 * Decodes unicode characters in git filenames
161 * They begin and end with double quote characters, and may contain
162 * backslash + 3 letter octal code numbers representing the character.
165 * > "t\303\244st.txt"
169 * On the shell, you can pipe them into "printf" and have them decoded.
171 * @param string Encoded git file name
173 * @return string Decoded file name
175 protected function decodeFileName($name)
177 $name = substr($name, 1, -1);
178 $name = str_replace('\"', '"', $name);
179 $name = preg_replace_callback(
182 return chr(octdec(substr($ar[0], 1)));
190 * Return array with all file paths in this repository
194 protected function getFilePaths()
196 if ($this->hash === null) {
201 $output = $this->getVc()->getCommand('ls-tree')
203 ->setOption('name-only')
206 $files = explode("\n", trim($output));
207 foreach ($files as &$file) {
208 if ($file{0} == '"') {
209 $file = $this->decodeFileName($file);
215 public function getFileByName($name, $bHasToExist = true)
217 $name = Tools::sanitizeFilename($name);
219 throw new Exception_Input('Empty file name given');
223 $files = $this->getFilePaths();
224 if (array_search($name, $files) === false) {
225 throw new Exception_Input('File does not exist');
228 return new File($name, $this);
231 public function hasFile($name)
234 $this->getFileByName($name);
235 } catch (Exception $e) {
242 * Permanently deletes the paste repository without any way to get
245 * @return boolean True if all went well, false if not
247 public function delete()
249 $db = new Database();
250 $db->getIndexer()->deleteRepo($this);
252 $bOk = Tools::recursiveDelete($this->workDir)
253 && Tools::recursiveDelete($this->gitDir);
255 $not = new Notificator();
261 public function getTitle()
263 $desc = $this->getDescription();
264 if (trim($desc) != '') {
268 return 'paste #' . $this->id;
271 public function getDescription()
273 if (!is_readable($this->gitDir . '/description')) {
276 return file_get_contents($this->gitDir . '/description');
279 public function setDescription($description)
281 file_put_contents($this->gitDir . '/description', $description);
285 * @return array Array with keys "email" and "name"
287 public function getOwner()
290 $name = $this->getVc()->getCommand('config')
291 ->addArgument('owner.name')->execute();
292 } catch (\VersionControl_Git_Exception $e) {
293 $name = $GLOBALS['phorkie']['auth']['anonymousName'];
296 $email = $this->getVc()->getCommand('config')
297 ->addArgument('owner.email')->execute();
298 } catch (\VersionControl_Git_Exception $e) {
299 $email = $GLOBALS['phorkie']['auth']['anonymousEmail'];
302 return array('name' => trim($name), 'email' => trim($email));
306 * Get a link to the repository
308 * @param string $type Link type. Supported are:
315 * @param string $option Additional link option, e.g. revision number
316 * @param boolean $full Return full URL or normal relative
320 public function getLink($type, $option = null, $full = false)
322 if ($type == 'edit') {
323 $link = $this->id . '/edit';
324 } else if ($type == 'display') {
326 } else if ($type == 'fork') {
327 $link = $this->id . '/fork';
328 } else if ($type == 'doap') {
329 $link = $this->id . '/doap';
330 } else if ($type == 'delete') {
331 $link = $this->id . '/delete';
332 } else if ($type == 'delete-confirm') {
333 $link = $this->id . '/delete/confirm';
334 } else if ($type == 'remotefork') {
335 return 'web+fork:' . $this->getLink('display', null, true);
336 } else if ($type == 'revision') {
337 $link = $this->id . '/rev/' . $option;
338 } else if ($type == 'linkback') {
339 $link = $this->id . '/linkback';
341 throw new Exception('Unknown link type');
345 $link = Tools::fullUrl($link);
350 public function getCloneURL($public = true)
352 $var = $public ? 'public' : 'private';
353 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
354 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
360 * Returns the history of the repository.
361 * We don't use VersionControl_Git's rev list fetcher since it does not
362 * give us separate email addresses and names, and it does not give us
363 * the amount of changed (added/deleted) lines.
365 * @return array Array of history objects
367 public function getHistory()
369 $output = $this->getVc()->getCommand('log')
370 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
371 ->setOption('max-count', 10)
372 ->setOption('shortstat')
375 $arCommits = array();
376 $arOutput = explode("\n", $output);
377 $lines = count($arOutput);
379 while ($current < $lines) {
380 $commit = new Repository_Commit();
381 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
382 if ($name !== 'commit') {
384 'Git log output format not as expected: ' . $arOutput[$current]
387 $commit->committerTime = $arOutput[$current + 1];
388 $commit->committerName = $arOutput[$current + 2];
389 $commit->committerEmail = $arOutput[$current + 3];
391 if (substr($arOutput[$current + 4], 0, 1) != ' ') {
392 //commit without changed lines
393 $arCommits[] = $commit;
398 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
399 $commit->filesChanged = $arLineParts[0];
400 $commit->linesAdded = $arLineParts[3];
401 if (isset($arLineParts[5])) {
402 $commit->linesDeleted = $arLineParts[5];
407 $arCommits[] = $commit;
414 * @return Repository_ConnectionInfo
416 public function getConnectionInfo()
418 return new Repository_ConnectionInfo($this);