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 if ($this->hash === null) {
127 $output = $this->getVc()->getCommand('ls-tree')
129 ->setOption('name-only')
132 $files = explode("\n", trim($output));
134 foreach ($files as $name) {
135 $arFiles[] = new File($name, $this);
140 public function getFileByName($name, $bHasToExist = true)
142 $base = basename($name);
143 if ($base != $name) {
144 throw new Exception('No directories supported for now');
147 throw new Exception_Input('Empty file name given');
149 $fullpath = $this->workDir . '/' . $base;
150 if ($bHasToExist && !is_readable($fullpath)) {
151 throw new Exception_Input('File does not exist');
153 return new File($base, $this);
156 public function hasFile($name)
159 $this->getFileByName($name);
160 } catch (Exception $e) {
167 * Permanently deletes the paste repository without any way to get
170 * @return boolean True if all went well, false if not
172 public function delete()
174 return Tools::recursiveDelete($this->workDir)
175 && Tools::recursiveDelete($this->gitDir);
178 public function getDescription()
180 if (!is_readable($this->gitDir . '/description')) {
183 return file_get_contents($this->gitDir . '/description');
186 public function setDescription($description)
188 file_put_contents($this->gitDir . '/description', $description);
192 * Get a link to the repository
194 * @param string $type Link type. Supported are:
201 * @param string $option
205 public function getLink($type, $option = null)
207 if ($type == 'edit') {
208 return '/' . $this->id . '/edit';
209 } else if ($type == 'display') {
210 return '/' . $this->id;
211 } else if ($type == 'fork') {
212 return '/' . $this->id . '/fork';
213 } else if ($type == 'delete') {
214 return '/' . $this->id . '/delete';
215 } else if ($type == 'delete-confirm') {
216 return '/' . $this->id . '/delete/confirm';
217 } else if ($type == 'revision') {
218 return '/' . $this->id . '/rev/' . $option;
220 throw new Exception('Unknown link type');
223 public function getCloneURL($public = true)
225 $var = $public ? 'public' : 'private';
226 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
227 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
233 * Returns the history of the repository.
234 * We don't use VersionControl_Git's rev list fetcher since it does not
235 * give us separate email addresses and names, and it does not give us
236 * the amount of changed (added/deleted) lines.
238 * @return array Array of history objects
240 public function getHistory()
242 $output = $this->getVc()->getCommand('log')
243 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
244 ->setOption('max-count', 10)
245 ->setOption('shortstat')
248 $arCommits = array();
249 $arOutput = explode("\n", $output);
250 $lines = count($arOutput);
252 while ($current < $lines) {
253 $commit = new Repository_Commit();
254 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
255 if ($name !== 'commit') {
257 'Git log output format not as expected: ' . $arOutput[$current]
260 $commit->committerTime = $arOutput[$current + 1];
261 $commit->committerName = $arOutput[$current + 2];
262 $commit->committerEmail = $arOutput[$current + 3];
264 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
265 $commit->filesChanged = $arLineParts[0];
266 $commit->linesAdded = $arLineParts[3];
267 $commit->linesDeleted = $arLineParts[5];
271 $arCommits[] = $commit;