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 $db = new Database();
180 $db->getIndexer()->deleteRepo($this);
182 return Tools::recursiveDelete($this->workDir)
183 && Tools::recursiveDelete($this->gitDir);
186 public function getTitle()
188 $desc = $this->getDescription();
189 if (trim($desc) != '') {
193 return 'paste #' . $this->id;
196 public function getDescription()
198 if (!is_readable($this->gitDir . '/description')) {
201 return file_get_contents($this->gitDir . '/description');
204 public function setDescription($description)
206 file_put_contents($this->gitDir . '/description', $description);
210 * Get a link to the repository
212 * @param string $type Link type. Supported are:
219 * @param string $option
223 public function getLink($type, $option = null)
225 if ($type == 'edit') {
226 return '/' . $this->id . '/edit';
227 } else if ($type == 'display') {
228 return '/' . $this->id;
229 } else if ($type == 'fork') {
230 return '/' . $this->id . '/fork';
231 } else if ($type == 'delete') {
232 return '/' . $this->id . '/delete';
233 } else if ($type == 'delete-confirm') {
234 return '/' . $this->id . '/delete/confirm';
235 } else if ($type == 'revision') {
236 return '/' . $this->id . '/rev/' . $option;
238 throw new Exception('Unknown link type');
241 public function getCloneURL($public = true)
243 $var = $public ? 'public' : 'private';
244 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
245 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
251 * Returns the history of the repository.
252 * We don't use VersionControl_Git's rev list fetcher since it does not
253 * give us separate email addresses and names, and it does not give us
254 * the amount of changed (added/deleted) lines.
256 * @return array Array of history objects
258 public function getHistory()
260 $output = $this->getVc()->getCommand('log')
261 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
262 ->setOption('max-count', 10)
263 ->setOption('shortstat')
266 $arCommits = array();
267 $arOutput = explode("\n", $output);
268 $lines = count($arOutput);
270 while ($current < $lines) {
271 $commit = new Repository_Commit();
272 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
273 if ($name !== 'commit') {
275 'Git log output format not as expected: ' . $arOutput[$current]
278 $commit->committerTime = $arOutput[$current + 1];
279 $commit->committerName = $arOutput[$current + 2];
280 $commit->committerEmail = $arOutput[$current + 3];
282 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
283 $commit->filesChanged = $arLineParts[0];
284 $commit->linesAdded = $arLineParts[3];
285 if (isset($arLineParts[5])) {
286 $commit->linesDeleted = $arLineParts[5];
291 $arCommits[] = $commit;