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 * Get a link to the repository
241 * @param string $type Link type. Supported are:
248 * @param string $option
252 public function getLink($type, $option = null)
254 if ($type == 'edit') {
255 return '/' . $this->id . '/edit';
256 } else if ($type == 'display') {
257 return '/' . $this->id;
258 } else if ($type == 'fork') {
259 return '/' . $this->id . '/fork';
260 } else if ($type == 'doap') {
261 return '/' . $this->id . '/doap';
262 } else if ($type == 'delete') {
263 return '/' . $this->id . '/delete';
264 } else if ($type == 'delete-confirm') {
265 return '/' . $this->id . '/delete/confirm';
266 } else if ($type == 'revision') {
267 return '/' . $this->id . '/rev/' . $option;
269 throw new Exception('Unknown link type');
272 public function getCloneURL($public = true)
274 $var = $public ? 'public' : 'private';
275 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
276 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
282 * Returns the history of the repository.
283 * We don't use VersionControl_Git's rev list fetcher since it does not
284 * give us separate email addresses and names, and it does not give us
285 * the amount of changed (added/deleted) lines.
287 * @return array Array of history objects
289 public function getHistory()
291 $output = $this->getVc()->getCommand('log')
292 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
293 ->setOption('max-count', 10)
294 ->setOption('shortstat')
297 $arCommits = array();
298 $arOutput = explode("\n", $output);
299 $lines = count($arOutput);
301 while ($current < $lines) {
302 $commit = new Repository_Commit();
303 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
304 if ($name !== 'commit') {
306 'Git log output format not as expected: ' . $arOutput[$current]
309 $commit->committerTime = $arOutput[$current + 1];
310 $commit->committerName = $arOutput[$current + 2];
311 $commit->committerEmail = $arOutput[$current + 3];
313 if (substr($arOutput[$current + 4], 0, 1) != ' ') {
314 //commit without changed lines
315 $arCommits[] = $commit;
320 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
321 $commit->filesChanged = $arLineParts[0];
322 $commit->linesAdded = $arLineParts[3];
323 if (isset($arLineParts[5])) {
324 $commit->linesDeleted = $arLineParts[5];
329 $arCommits[] = $commit;