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 if ($option !== null) {
325 $link .= '/' . urlencode($option);
327 } else if ($type == 'display') {
329 } else if ($type == 'fork') {
330 $link = $this->id . '/fork';
331 } else if ($type == 'doap') {
332 $link = $this->id . '/doap';
333 } else if ($type == 'delete') {
334 $link = $this->id . '/delete';
335 } else if ($type == 'delete-confirm') {
336 $link = $this->id . '/delete/confirm';
337 } else if ($type == 'remotefork') {
338 return 'web+fork:' . $this->getLink('display', null, true);
339 } else if ($type == 'revision') {
340 $link = $this->id . '/rev/' . $option;
341 } else if ($type == 'linkback') {
342 $link = $this->id . '/linkback';
344 throw new Exception('Unknown link type');
348 $link = Tools::fullUrl($link);
353 public function getCloneURL($public = true)
355 $var = $public ? 'public' : 'private';
356 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
357 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
363 * Returns the history of the repository.
364 * We don't use VersionControl_Git's rev list fetcher since it does not
365 * give us separate email addresses and names, and it does not give us
366 * the amount of changed (added/deleted) lines.
368 * @return array Array of history objects
370 public function getHistory()
372 $output = $this->getVc()->getCommand('log')
373 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
374 ->setOption('max-count', 10)
375 ->setOption('shortstat')
378 $arCommits = array();
379 $arOutput = explode("\n", $output);
380 $lines = count($arOutput);
382 while ($current < $lines) {
383 $commit = new Repository_Commit();
384 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
385 if ($name !== 'commit') {
387 'Git log output format not as expected: ' . $arOutput[$current]
390 $commit->committerTime = $arOutput[$current + 1];
391 $commit->committerName = $arOutput[$current + 2];
392 $commit->committerEmail = $arOutput[$current + 3];
394 if (substr($arOutput[$current + 4], 0, 1) != ' ') {
395 //commit without changed lines
396 $arCommits[] = $commit;
401 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
402 $commit->filesChanged = $arLineParts[0];
403 $commit->linesAdded = $arLineParts[3];
404 if (isset($arLineParts[5])) {
405 $commit->linesDeleted = $arLineParts[5];
410 $arCommits[] = $commit;
417 * @return Repository_ConnectionInfo
419 public function getConnectionInfo()
421 return new Repository_ConnectionInfo($this);