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 protected function loadDirs()
70 $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
71 if (!is_dir($gitDir)) {
72 throw new Exception_NotFound(
73 sprintf('Paste %d .git dir not found', $this->id)
76 $this->gitDir = $gitDir;
78 $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
79 if (!is_dir($workDir)) {
80 throw new Exception_NotFound(
81 sprintf('Paste %d work dir not found', $this->id)
84 $this->workDir = $workDir;
87 public function loadHash()
90 if ($this->hash !== null) {
94 $output = $this->getVc()->getCommand('log')
95 ->setOption('pretty', 'format:%H')
96 ->setOption('max-count', 1)
98 $output = trim($output);
99 if (strlen($output) !== 40) {
101 'Loading commit hash failed: ' . $output
104 $this->hash = $output;
108 * Populates $this->message
112 public function loadMessage()
114 $rev = (isset($this->hash)) ? $this->hash : 'HEAD';
115 $output = $this->getVc()->getCommand('log')
116 ->setOption('oneline')
120 $output = trim($output);
121 if (strpos($output, ' ') > 0) {
122 $output = substr($output, strpos($output, ' '), strlen($output));
123 $this->message = trim($output);
125 $this->message = "This commit message intentionally left blank.";
129 public function loadById($id)
131 if (!is_numeric($id)) {
132 throw new Exception_Input('Paste ID not numeric');
134 $this->id = (int)$id;
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
273 public function getLink($type, $option = null)
275 if ($type == 'edit') {
276 return '/' . $this->id . '/edit';
277 } else if ($type == 'display') {
278 return '/' . $this->id;
279 } else if ($type == 'fork') {
280 return '/' . $this->id . '/fork';
281 } else if ($type == 'doap') {
282 return '/' . $this->id . '/doap';
283 } else if ($type == 'delete') {
284 return '/' . $this->id . '/delete';
285 } else if ($type == 'delete-confirm') {
286 return '/' . $this->id . '/delete/confirm';
287 } else if ($type == 'revision') {
288 return '/' . $this->id . '/rev/' . $option;
290 throw new Exception('Unknown link type');
293 public function getCloneURL($public = true)
295 $var = $public ? 'public' : 'private';
296 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
297 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
303 * Returns the history of the repository.
304 * We don't use VersionControl_Git's rev list fetcher since it does not
305 * give us separate email addresses and names, and it does not give us
306 * the amount of changed (added/deleted) lines.
308 * @return array Array of history objects
310 public function getHistory()
312 $output = $this->getVc()->getCommand('log')
313 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
314 ->setOption('max-count', 10)
315 ->setOption('shortstat')
318 $arCommits = array();
319 $arOutput = explode("\n", $output);
320 $lines = count($arOutput);
322 while ($current < $lines) {
323 $commit = new Repository_Commit();
324 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
325 if ($name !== 'commit') {
327 'Git log output format not as expected: ' . $arOutput[$current]
330 $commit->committerTime = $arOutput[$current + 1];
331 $commit->committerName = $arOutput[$current + 2];
332 $commit->committerEmail = $arOutput[$current + 3];
334 if (substr($arOutput[$current + 4], 0, 1) != ' ') {
335 //commit without changed lines
336 $arCommits[] = $commit;
341 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
342 $commit->filesChanged = $arLineParts[0];
343 $commit->linesAdded = $arLineParts[3];
344 if (isset($arLineParts[5])) {
345 $commit->linesDeleted = $arLineParts[5];
350 $arCommits[] = $commit;