do not show tools when looking at revision
[phorkie.git] / src / phorkie / Repository.php
1 <?php
2 namespace phorkie;
3
4
5 class Repository
6 {
7     /**
8      * Repository ID (number in repositories directory)
9      *
10      * @var integer
11      */
12     public $id;
13
14     /**
15      * Full path to the .git repository
16      *
17      * @var string
18      */
19     public $gitDir;
20
21     /**
22      * Full path to the work tree directory
23      *
24      * @var string
25      */
26     public $workDir;
27
28     /**
29      * Revision of the repository that shall be shown
30      *
31      * @var string
32      */
33     public $hash;
34
35
36
37     /**
38      * Load Repository data from GET-Request
39      *
40      * @return void
41      *
42      * @throws Exception When something is wrong
43      */
44     public function loadFromRequest()
45     {
46         if (!isset($_GET['id'])) {
47             throw new Exception_Input('Paste ID missing');
48         }
49         if (!is_numeric($_GET['id'])) {
50             throw new Exception_Input('Paste ID not numeric');
51         }
52         if (isset($_GET['rev'])) {
53             $this->hash = $_GET['rev'];
54         }
55
56         $this->id = (int)$_GET['id'];
57         $this->loadDirs();
58         $this->loadHash();
59     }
60
61     protected function loadDirs()
62     {
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)
67             );
68         }
69         $this->gitDir = $gitDir;
70
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)
75             );
76         }
77         $this->workDir = $workDir;
78     }
79
80     public function loadHash()
81     {
82         return;
83         if ($this->hash !== null) {
84             return;
85         }
86
87         $output = $this->getVc()->getCommand('log')
88             ->setOption('pretty', 'format:%H')
89             ->setOption('max-count', 1)
90             ->execute();
91         $output = trim($output);
92         if (strlen($output) !== 40) {
93             throw new Exception(
94                 'Loading commit hash failed: ' . $output
95             );
96         }
97         $this->hash = $output;
98     }
99
100     public function loadById($id)
101     {
102         if (!is_numeric($id)) {
103             throw new Exception_Input('Paste ID not numeric');
104         }
105         $this->id = (int)$id;
106         $this->loadDirs();
107         $this->loadHash();
108     }
109
110     public function getVc()
111     {
112         return new \VersionControl_Git($this->gitDir);
113     }
114
115     /**
116      * Loads the list of files in this repository
117      *
118      * @return File[] Array of files
119      */
120     public function getFiles()
121     {
122         if ($this->hash === null) {
123             $hash = 'HEAD';
124         } else {
125             $hash = $this->hash;
126         }
127         $output = $this->getVc()->getCommand('ls-tree')
128             ->setOption('r')
129             ->setOption('name-only')
130             ->addArgument($hash)
131             ->execute();
132         $files = explode("\n", trim($output));
133         $arFiles = array();
134         foreach ($files as $name) {
135             $arFiles[] = new File($name, $this);
136         }
137         return $arFiles;
138     }
139
140     public function getFileByName($name, $bHasToExist = true)
141     {
142         $base = basename($name);
143         if ($base != $name) {
144             throw new Exception('No directories supported for now');
145         }
146         if ($name == '') {
147             throw new Exception_Input('Empty file name given');
148         }
149         $fullpath = $this->workDir . '/' . $base;
150         if ($bHasToExist && !is_readable($fullpath)) {
151             throw new Exception_Input('File does not exist');
152         }
153         return new File($base, $this);
154     }
155
156     public function hasFile($name)
157     {
158         try {
159             $this->getFileByName($name);
160         } catch (Exception $e) {
161             return false;
162         }
163         return true;
164     }
165
166     /**
167      * Permanently deletes the paste repository without any way to get
168      * it back.
169      *
170      * @return boolean True if all went well, false if not
171      */
172     public function delete()
173     {
174         return Tools::recursiveDelete($this->workDir)
175             && Tools::recursiveDelete($this->gitDir);
176     }
177
178     public function getDescription()
179     {
180         if (!is_readable($this->gitDir . '/description')) {
181             return null;
182         }
183         return file_get_contents($this->gitDir . '/description');
184     }
185
186     public function setDescription($description)
187     {
188         file_put_contents($this->gitDir . '/description', $description);
189     }
190
191     /**
192      * Get a link to the repository
193      *
194      * @param string $type Link type. Supported are:
195      *                     - "edit"
196      *                     - "delete"
197      *                     - "delete-confirm"
198      *                     - "display"
199      *                     - "fork"
200      *                     - "revision"
201      * @param string $option
202      *
203      * @return string
204      */
205     public function getLink($type, $option = null)
206     {
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;
219         }
220         throw new Exception('Unknown link type');
221     }
222
223     public function getCloneURL($public = true)
224     {
225         $var = $public ? 'public' : 'private';
226         if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
227             return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
228         }
229         return null;
230     }
231
232     /**
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.
237      *
238      * @return array Array of history objects
239      */
240     public function getHistory()
241     {
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')
246             ->execute();
247
248         $arCommits = array();
249         $arOutput = explode("\n", $output);
250         $lines = count($arOutput);
251         $current = 0;
252         while ($current < $lines) {
253             $commit = new Repository_Commit();
254             list($name,$commit->hash) = explode(' ', $arOutput[$current]);
255             if ($name !== 'commit') {
256                 throw new Exception(
257                     'Git log output format not as expected: ' . $arOutput[$current]
258                 );
259             }
260             $commit->committerTime  = $arOutput[$current + 1];
261             $commit->committerName  = $arOutput[$current + 2];
262             $commit->committerEmail = $arOutput[$current + 3];
263
264             $arLineParts = explode(' ', trim($arOutput[$current + 4]));
265             $commit->filesChanged = $arLineParts[0];
266             $commit->linesAdded   = $arLineParts[3];
267             $commit->linesDeleted = $arLineParts[5];
268
269             $current += 6;
270
271             $arCommits[] = $commit;
272         }
273
274         return $arCommits;
275     }
276 }
277
278 ?>