forked from tizzo/git_repository_viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_repository_viewer.module
402 lines (370 loc) · 13.5 KB
/
git_repository_viewer.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
// $Id$
/**
* @file
* git_repository_viewer provides a simple git repository browser.
*/
/**
* Implementation of hook_menu().
*/
function git_repository_viewer_menu() {
// This generic callback is used for all repository browsing regardless of whether the browsed item
$menu['git-repository-viewer/%versioncontrol_repository'] = array(
'title callback' => 'git_repository_viewer_view_repository_title_callback',
'title arguments' => array(1),
'page callback' => 'git_repository_viewer_view_repository',
'page arguments' => array(1),
'access arguments' => array('browse all versioncontrol repositories'),
);
$menu['git-repository-viewer/commit/%versioncontrol_repository'] = array(
'title' => 'Git Diff',
'page callback' => 'git_repository_viewer_view_commit',
'page arguments' => array(2),
'access arguments' => array('browse all versioncontrol repositories'),
);
return $menu;
}
/**
* Implementation of hook_theme().
*/
function git_repository_viewer_theme() {
$template_path = drupal_get_path('module', 'git_repository_viewer') . '/templates';
return array(
'git_repository_browser' => array(
'template' => 'git-repo-browser',
'path' => $template_path,
'arguments' => array(
'navigation' => '',
'content' => NULL,
),
),
'git_repository_viewer_navigation' => array(
'template' => 'git-repo-viewer-navigation',
'path' => $template_path,
'arguments' => array(
'previous_commit_link' => NULL,
'next_commit_link' => NULL,
'parent_folder_link' => NULL,
'history' => NULL,
'branches_form' => '',
),
),
'git_repository_viewer_view_directory' => array(
'arguments' => array(
'tree' => NULL,
'name' => '',
),
),
'git_repository_viewer_view_file' => array(
'arguments' => array(
'blob' => NULL,
'name' => '',
),
),
'git_repository_viewer_view_image' => array(
'template' => 'git-repo-viewer-view-image',
'path' => $template_path,
'arguments' => array(
'image_data' => '',
'type' => '',
),
),
);
}
/**
* Implementation of hook_perm().
*/
function git_repository_viewer_perm() {
return array(
'browse all versioncontrol repositories',
);
}
/**
* Title callback for git_repository_viewer.
*/
function git_repository_viewer_view_repository_title_callback($versioncontol_repository) {
return t('Repository contents for @reposiotry_name', array('@reposiotry_name' => $versioncontol_repository->name));
}
/**
* TODO: figure out exactly what this function needs to do....
*
* This function maintains details about the repository item currently being viewed
*
* Ideally, this function should be used for to take a git repository and get a bunch of information
*/
function git_repository_viewer_get_repository_page_details($versioncontol_repository = FALSE) {
static $repository_data = FALSE;
if ($repository_data == FALSE && $versioncontol_repository != FALSE) {
// Get the handler from with which to load repo data.
$handler = git_repository_viewer_handler_factory();
// Get information about what path within the repository we are viewing.
$path = isset($_GET['path']) ? $_GET['path'] : '';
$branch = isset($_GET['branch']) ? $_GET['branch'] : FALSE;
$commit = isset($_GET['commit']) ? $_GET['commit'] : FALSE;
// If we don't have a textual representation of the branch needed, find a default.
if (!$branch) {
$branch = array_shift($versioncontol_repository->loadBranches())->name;
}
// TODO: consider whether we should find the most recent commit from the DB instead.
$data = $handler->getRepoData($versioncontol_repository->root, $branch, $commit);
foreach($data as $name => $value) {
$$name = $value;
}
$repository_data = array(
'repository' => $versioncontol_repository,
'path' => $path,
'branch' => $branch,
'commit' => $commit,
'previous_commits' => $parents,
);
}
return $repository_data;
}
/**
* Page callback for git_repository_viewer.
*/
function git_repository_viewer_view_repository(VersioncontrolGitRepository $versioncontol_repository) {
// Get the repository data for this particular repository.
$repo_data = git_repository_viewer_get_repository_page_details($versioncontol_repository);
// Get the object we are trying to view.
$data = git_repository_viewer_get_object_from_path($repo_data);
$name = $data->name ? $data->name : $versioncontol_repository->name;
// Determine whether we are trying to view a folder or a file.
if ($data->type == 'directory') {
$content = theme('git_repository_viewer_view_directory', $data->contents, $name);
}
elseif ($data->type == 'file') {
$content = theme('git_repository_viewer_view_file', $data->contents, $name);
}
// If we have neither a directory or a file, return page not found.
else {
drupal_not_found();
return;
}
$navigation = git_repository_viewer_get_browser_navigation($repo_data);
return theme('git_repository_browser', $navigation, $content);
}
/**
*
* TODO: make themeable
*/
function git_repository_viewer_get_link_to_node($node, $other_data = array()) {
$info = git_repository_viewer_get_repository_page_details();
if (!isset($other_data['path']) || $other_data['path'] == '') {
$other_data['path'] = ($info['path'] == '' ? '' : $info['path'] . '/');
}
if (isset($info['branch'])) {
$other_data['branch'] = $info['branch'];
}
if (isset($info['commit'])) {
$other_data['commit'] = sha1_hex($info['commit']);
}
$other_data['path'] = $other_data['path'] . $node['name'];
$name = $node['name'];
return git_repository_viewer_get_link($name, $other_data);
}
function git_repository_viewer_get_link($name, $other_data = array()) {
$info = git_repository_viewer_get_repository_page_details();
if (isset($other_data['path']) && $other_data['path'] != '') {
$path = ($info['path'] == '' ? '' : $info['path'] . '/');
}
$name = $name != '' ? $name : $node->name;
$link = l($name, 'git-repository-viewer/' . $info['repository']->repo_id, $options = array('query' => $other_data));
return $link;
}
/**
*
*/
function git_repository_viewer_get_object_from_path($repo_data) {
$object = git_repository_viewer_handler_factory()->getObject($repo_data['repository']->root, $repo_data['path'], $repo_data['branch'], $repo_data['commit']);
return $object;
}
function git_repository_viewer_branch_form($form_state, $repo_data) {
$options = array();
foreach ($repo_data['repository']->loadBranches() as $branch) {
$options[$branch->name] = $branch->name;
}
$form['branches'] = array(
'#title' => t('Branches'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $repo_data['branch'],
);
return $form;
}
/**
* Default theme implementation for git_repository_viewer_view_directory.
*/
function theme_git_repository_viewer_view_directory($contents, $name) {
// Create the header columns for our table.
drupal_add_css(drupal_get_path('module', 'git_repository_viewer') . '/css/folder.css');
$header = array(
t('Name'),
t('Date'),
t('Author'),
t('Commit'),
t('Message'),
);
// WE NEED TO FIND OUR PATHS FROM THE ROOT REPOSITORY HERE...
$data = git_repository_viewer_get_repository_page_details();
$paths = array();
$base_path = $data['path'] == '' ? '' : '/' . $data['path'];
foreach ($contents as $name => $value) {
$paths[] = $base_path . '/' . $name;
}
$query = db_select('versioncontrol_item_revisions', 'vir')
->condition('vir.path', $paths, 'IN')
->condition('vir.repo_id', $data['repository']->repo_id);
$query->join('versioncontrol_operations', 'vo', 'vir.vc_op_id = vo.vc_op_id');
// TODO: make sure we're getting what we want here...
$results = $query
->fields('vir', array('path', 'revision'))
->fields('vo', array('date', 'committer', 'message'))
->distinct('path')
->addTag('git_repository_viewer_directory_list_metadata')
->execute()
->fetchAllAssoc('path');
$git_backend = new VersioncontrolGitBackend;
$rows = array();
foreach ($contents as $name => $item) {
$item_data = new stdClass;
$item_path = $base_path . '/' . $name;
if (isset($results[$item_path])) {
$item_data = $results[$item_path];
}
if (isset($item_data->revision)) {
$commit_link = git_repository_viewer_get_link($git_backend->formatRevisionIdentifier($item_data->revision, 'short'), array('commit' => $item_data->revision));
}
if (isset($item_data->message) && $item_data->message) {
$message_link = l($item_data->message, 'git-repository-viewer/commit/' . $data['repository']->repo_id, array('query' => array('commit' => $item_data->revision)));
}
else {
$message_link = '';
}
$rows[] = array(
'data' => array(
'name' => array(
'data' => git_repository_viewer_get_link_to_node($item),
'class' => $item['directory'] ? 'folder' : 'file',
),
'date' => isset($item_data->date) ? t('@time ago', array('@time' => format_interval(time() - $item_data->date))) : '',
'author' => isset($item_data->committer) ? $item_data->committer : '',
'commit' => isset($commit_link) ? $commit_link : '',
'message' => $message_link,
),
);
}
return theme('table', $header, $rows);
}
/**
* Default theme implementation for git_repository_viewer_view_file.
*
* @param (fileContents) $file_contents
* The actual contents of the file being viewed.
* @param (string) $name
* The name of the file.
*/
function theme_git_repository_viewer_view_file($file_contents, $name) {
$type = git_repository_viewer_get_blob_type($name);
$image_extensions = array(
'jpg',
'jpeg',
'gif',
'png',
'ico',
);
if (in_array($type, $image_extensions)) {
$output = theme('git_repository_viewer_view_image', base64_encode($file_contents), $type);
}
else {
// TODO: turn this into a theme function?
$output = git_repository_viewer_format_text($file_contents, $type);
}
return $output;
}
function git_repository_viewer_format_text($text, $type) {
if (strtolower($type) == 'md' || strtolower($type) == 'mdown' && module_exists('markdown')) {
$output = _markdown_process($text, '');
}
elseif (module_exists('geshifilter')) {
module_load_include('inc', 'geshifilter', 'geshifilter.pages');
$output = geshifilter_process($text, $type, TRUE);
}
else {
$output = "<code type='$type'>";
$output .= check_plain($text);
$output .= '</code>';
}
return $output;
}
function git_repository_viewer_get_blob_type($name) {
$ext = end(explode('.', $name));
$type = $ext;
$drupal_extensions = array(
'module',
'install',
'test',
'inc',
'engine',
);
if (in_array($ext, $drupal_extensions)) {
$type = 'php';
}
return $type;
}
/**
* View the contents of an individual commit.
*/
function git_repository_viewer_view_commit(VersioncontrolGitRepository $versioncontrol_repository) {
// Sanity check to make sure that this commit id is only numbers and letters
// and does not attempt to execute aribtrary code on our 'git-show system call'
if (preg_match('/^[a-zA-Z0-9]+$/', $_GET['commit']) && ctype_xdigit($_GET['commit'])) {
$data = git_repository_viewer_get_repository_page_details($versioncontrol_repository);
if ($commit_diff = git_repository_viewer_handler_factory()->getCommit($data['repository']->root, $data['branch'], sha1_hex($data['commit']))) {
$output = git_repository_viewer_format_text($commit_diff, 'diff');
return $output;
}
else {
drupal_set_message(t('The commit you were looking for was not found'), 'error');
}
}
else {
drupal_set_message(t('Invalid commit id.'), 'error');
}
drupal_not_found();
}
/**
* Build the links necessary for the browser navigation.
*/
function git_repository_viewer_get_browser_navigation(array $repo_data) {
drupal_add_css(drupal_get_path('module', 'git_repository_viewer') . '/css/navigation.css');
drupal_add_js(drupal_get_path('module', 'git_repository_viewer') . '/js/git_repo_viewer.js');
$branches_form = drupal_get_form('git_repository_viewer_branch_form', $repo_data);
// Look up previous commit.
if ($repo_data['previous_commits'][0] != '') {
$previous_commit_link = git_repository_viewer_get_link(t('previous commit'), array('commit' => sha1_hex($repo_data['previous_commits'][0])));
}
// TODO: We should be able to get the next commit from our MySQL data.
//$next_commit_link = git_repository_viewer_get_link(t('next commit'), array('commit' => sha1_hex($repo_data['previous_commits'])));
$next_commit_link = '';
// Get the parent folder.
$parent_folder_path = explode('/', $repo_data['path']);
array_pop($parent_folder_path);
$parent_folder_path = implode('/', $parent_folder_path);
$parent_folder_link = git_repository_viewer_get_link(t('parent folder'), array('path' => $parent_folder_path));
// Build the history link.
$history = module_exists('commitlog') ? l(t('history'), 'commitlog/repository/' . $repo_data['repository']->repo_id) : NULL;
return theme('git_repository_viewer_navigation', $previous_commit_link, $next_commit_link, $parent_folder_link, $history, $branches_form);
}
/**
* A factory method for repository viewer handlers.
*
* TODO: Make this configurable so that a remote handler can be used.
*/
function git_repository_viewer_handler_factory() {
static $handler = FALSE;
if (!$handler) {
$handler = new GitRepoViewerLocalGlip;
}
return $handler;
}