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
38 * Load Repository data from GET-Request
42 * @throws Exception When something is wrong
44 public function loadFromRequest()
46 if (!isset($_GET['id'])) {
47 throw new Exception_Input('Paste ID missing');
49 if (!is_numeric($_GET['id'])) {
50 throw new Exception_Input('Paste ID not numeric');
52 $this->id = (int)$_GET['id'];
57 protected function loadDirs()
59 $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
60 if (!is_dir($gitDir)) {
61 throw new Exception_NotFound(
62 sprintf('Paste %d .git dir not found', $this->id)
65 $this->gitDir = $gitDir;
67 $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
68 if (!is_dir($workDir)) {
69 throw new Exception_NotFound(
70 sprintf('Paste %d work dir not found', $this->id)
73 $this->workDir = $workDir;
76 public function loadHash()
78 if ($this->hash !== null) {
82 $output = $this->getVc()->getCommand('log')
83 ->setOption('pretty', 'format:%H')
84 ->setOption('max-count', 1)
86 $output = trim($output);
87 if (strlen($output) !== 40) {
89 'Loading commit hash failed: ' . $output
92 $this->hash = $output;
95 public function loadById($id)
97 if (!is_numeric($id)) {
98 throw new Exception_Input('Paste ID not numeric');
100 $this->id = (int)$id;
105 public function getVc()
107 return new \VersionControl_Git($this->gitDir);
111 * Loads the list of files in this repository
113 * @return File[] Array of files
115 public function getFiles()
117 $files = glob($this->workDir . '/*');
119 foreach ($files as $path) {
120 $arFiles[] = new File($path, $this);
125 public function getFileByName($name, $bHasToExist = true)
127 $base = basename($name);
128 if ($base != $name) {
129 throw new Exception('No directories supported for now');
132 throw new Exception_Input('Empty file name given');
134 $path = $this->workDir . '/' . $base;
135 if ($bHasToExist && !is_readable($path)) {
136 throw new Exception_Input('File does not exist');
138 return new File($path, $this);
141 public function hasFile($name)
144 $this->getFileByName($name);
145 } catch (Exception $e) {
152 * Permanently deletes the paste repository without any way to get
155 * @return boolean True if all went well, false if not
157 public function delete()
159 return Tools::recursiveDelete($this->workDir)
160 && Tools::recursiveDelete($this->gitDir);
163 public function getDescription()
165 if (!is_readable($this->gitDir . '/description')) {
168 return file_get_contents($this->gitDir . '/description');
171 public function setDescription($description)
173 file_put_contents($this->gitDir . '/description', $description);
177 * Get a link to the repository
179 * @param string $type Link type. Supported are:
186 * @param string $option
190 public function getLink($type, $option = null)
192 if ($type == 'edit') {
193 return '/' . $this->id . '/edit';
194 } else if ($type == 'display') {
195 return '/' . $this->id;
196 } else if ($type == 'fork') {
197 return '/' . $this->id . '/fork';
198 } else if ($type == 'delete') {
199 return '/' . $this->id . '/delete';
200 } else if ($type == 'delete-confirm') {
201 return '/' . $this->id . '/delete/confirm';
202 } else if ($type == 'commit') {
203 return '/' . $this->id . '/' . $option;
205 throw new Exception('Unknown link type');
208 public function getCloneURL($public = true)
210 $var = $public ? 'public' : 'private';
211 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
212 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
218 * Returns the history of the repository.
219 * We don't use VersionControl_Git's rev list fetcher since it does not
220 * give us separate email addresses and names, and it does not give us
221 * the amount of changed (added/deleted) lines.
223 * @return array Array of history objects
225 public function getHistory()
227 $output = $this->getVc()->getCommand('log')
228 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
229 ->setOption('max-count', 10)
230 ->setOption('shortstat')
233 $arCommits = array();
234 $arOutput = explode("\n", $output);
235 $lines = count($arOutput);
237 while ($current < $lines) {
238 $commit = new Repository_Commit();
239 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
240 if ($name !== 'commit') {
242 'Git log output format not as expected: ' . $arOutput[$current]
245 $commit->committerTime = $arOutput[$current + 1];
246 $commit->committerName = $arOutput[$current + 2];
247 $commit->committerEmail = $arOutput[$current + 3];
249 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
250 $commit->filesChanged = $arLineParts[0];
251 $commit->linesAdded = $arLineParts[3];
252 $commit->linesDeleted = $arLineParts[5];
256 $arCommits[] = $commit;