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 if (isset($_GET['rev'])) {
53 $this->hash = $_GET['rev'];
56 $this->id = (int)$_GET['id'];
61 protected function loadDirs()
63 $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
64 if (!is_dir($gitDir)) {
65 throw new Exception_NotFound(
66 sprintf('Paste %d .git dir not found', $this->id)
69 $this->gitDir = $gitDir;
71 $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
72 if (!is_dir($workDir)) {
73 throw new Exception_NotFound(
74 sprintf('Paste %d work dir not found', $this->id)
77 $this->workDir = $workDir;
80 public function loadHash()
83 if ($this->hash !== null) {
87 $output = $this->getVc()->getCommand('log')
88 ->setOption('pretty', 'format:%H')
89 ->setOption('max-count', 1)
91 $output = trim($output);
92 if (strlen($output) !== 40) {
94 'Loading commit hash failed: ' . $output
97 $this->hash = $output;
100 public function loadById($id)
102 if (!is_numeric($id)) {
103 throw new Exception_Input('Paste ID not numeric');
105 $this->id = (int)$id;
110 public function getVc()
112 return new \VersionControl_Git($this->gitDir);
116 * Loads the list of files in this repository
118 * @return File[] Array of files
120 public function getFiles()
122 $files = $this->getFilePaths();
124 foreach ($files as $name) {
125 $arFiles[] = new File($name, $this);
130 protected function getFilePaths()
132 if ($this->hash === null) {
137 $output = $this->getVc()->getCommand('ls-tree')
139 ->setOption('name-only')
142 return explode("\n", trim($output));
145 public function getFileByName($name, $bHasToExist = true)
147 $name = Tools::sanitizeFilename($name);
149 throw new Exception_Input('Empty file name given');
153 $files = $this->getFilePaths();
154 if (array_search($name, $files) === false) {
155 throw new Exception_Input('File does not exist');
158 return new File($name, $this);
161 public function hasFile($name)
164 $this->getFileByName($name);
165 } catch (Exception $e) {
172 * Permanently deletes the paste repository without any way to get
175 * @return boolean True if all went well, false if not
177 public function delete()
179 return Tools::recursiveDelete($this->workDir)
180 && Tools::recursiveDelete($this->gitDir);
183 public function getDescription()
185 if (!is_readable($this->gitDir . '/description')) {
188 return file_get_contents($this->gitDir . '/description');
191 public function setDescription($description)
193 file_put_contents($this->gitDir . '/description', $description);
197 * Get a link to the repository
199 * @param string $type Link type. Supported are:
206 * @param string $option
210 public function getLink($type, $option = null)
212 if ($type == 'edit') {
213 return '/' . $this->id . '/edit';
214 } else if ($type == 'display') {
215 return '/' . $this->id;
216 } else if ($type == 'fork') {
217 return '/' . $this->id . '/fork';
218 } else if ($type == 'delete') {
219 return '/' . $this->id . '/delete';
220 } else if ($type == 'delete-confirm') {
221 return '/' . $this->id . '/delete/confirm';
222 } else if ($type == 'revision') {
223 return '/' . $this->id . '/rev/' . $option;
225 throw new Exception('Unknown link type');
228 public function getCloneURL($public = true)
230 $var = $public ? 'public' : 'private';
231 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
232 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
238 * Returns the history of the repository.
239 * We don't use VersionControl_Git's rev list fetcher since it does not
240 * give us separate email addresses and names, and it does not give us
241 * the amount of changed (added/deleted) lines.
243 * @return array Array of history objects
245 public function getHistory()
247 $output = $this->getVc()->getCommand('log')
248 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
249 ->setOption('max-count', 10)
250 ->setOption('shortstat')
253 $arCommits = array();
254 $arOutput = explode("\n", $output);
255 $lines = count($arOutput);
257 while ($current < $lines) {
258 $commit = new Repository_Commit();
259 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
260 if ($name !== 'commit') {
262 'Git log output format not as expected: ' . $arOutput[$current]
265 $commit->committerTime = $arOutput[$current + 1];
266 $commit->committerName = $arOutput[$current + 2];
267 $commit->committerEmail = $arOutput[$current + 3];
269 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
270 $commit->filesChanged = $arLineParts[0];
271 $commit->linesAdded = $arLineParts[3];
272 $commit->linesDeleted = $arLineParts[5];
276 $arCommits[] = $commit;