2 /** FusionForge Git plugin
4 * Copyright 2009, Roland Mas
5 * Copyright 2009, Mehdi Dogguy <mehdi@debian.org>
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 class GitPlugin extends SCMPlugin {
26 function GitPlugin () {
29 $this->name = 'scmgit';
31 $this->hooks[] = 'scm_update_repolist' ;
32 $this->hooks[] = 'scm_browser_page' ;
33 $this->hooks[] = 'scm_gather_stats' ;
34 $this->hooks[] = 'scm_generate_snapshots' ;
36 require_once $gfconfig.'plugins/scmgit/config.php' ;
38 $this->default_git_server = $default_git_server ;
39 if (isset ($git_root)) {
40 $this->git_root = $git_root;
42 $this->git_root = $GLOBALS['sys_chroot'].'/scmrepos/git' ;
48 function getDefaultServer() {
49 return $this->default_git_server ;
52 function printShortStats ($params) {
53 $project = $this->checkParams ($params) ;
58 if ($project->usesPlugin($this->name)) {
59 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
60 array ($project->getID())) ;
61 $commit_num = db_result($result,0,'commits');
62 $add_num = db_result($result,0,'adds');
69 echo ' (Git: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
73 function getBlurb () {
74 return _('<p>Documentation for Git is available <a href="http://git-scm.com/">here</a>.</p>') ;
77 function getInstructionsForAnon ($project) {
78 $b = _('<p><b>Anonymous Git Access</b></p><p>This project\'s Git repository can be checked out through anonymous access with the following command.</p>');
80 $b .= '<tt>git clone '.util_make_url ('/anonscm/git/'.$project->getUnixName().'/'.$project->getUnixName().'.git').'</tt><br />';
85 function getInstructionsForRW ($project) {
86 $b = _('<p><b>Developer GIT Access via SSH</b></p><p>Only project developers can access the GIT tree via this method. SSH must be installed on your client machine. Substitute <i>developername</i> with the proper values. Enter your site password when prompted.</p>');
87 $b .= '<p><tt>git clone git+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . $this->git_root .'/'. $project->getUnixName() .'/'. $project->getUnixName() .'.git</tt></p>' ;
91 function getSnapshotPara ($project) {
92 global $sys_scm_snapshots_path ;
94 $filename = $project->getUnixName().'-scm-latest.tar.gz';
95 if (file_exists($sys_scm_snapshots_path.'/'.$filename)) {
97 $b .= util_make_link ("/snapshots.php?group_id=".$project->getID(),
98 _('Download the nightly snapshot')
105 function printBrowserPage ($params) {
108 $project = $this->checkParams ($params) ;
113 if ($project->usesPlugin ($this->name)) {
114 if ($this->browserDisplayable ($project)) {
115 print '<iframe src="'.util_make_url ("/plugins/scmgit/cgi-bin/gitweb.cgi?p=".$project->getUnixName().'/'.$project->getUnixName().'.git').'" frameborder="no" width=100% height=700></iframe>' ;
120 function getBrowserLinkBlock ($project) {
122 $b = $HTML->boxMiddle(_('Git Repository Browser'));
123 $b .= _('<p>Browsing the Git tree gives you a view into the current status of this project\'s code. You may also view the complete histories of any file in the repository.</p>');
125 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
126 _('Browse Git Repository')
132 // function getStatsBlock ($project) {
136 // $result = db_query_params('SELECT u.realname, u.user_name, u.user_id, sum(commits) as commits, sum(adds) as adds, sum(adds+commits) as combined FROM stats_cvs_user s, users u WHERE group_id=$1 AND s.user_id=u.user_id AND (commits>0 OR adds >0) GROUP BY u.user_id, realname, user_name, u.user_id ORDER BY combined DESC, realname',
137 // array ($project->getID()));
139 // if (db_numrows($result) > 0) {
140 // $b .= $HTML->boxMiddle(_('Repository Statistics'));
142 // $tableHeaders = array(
147 // $b .= $HTML->listTableTop($tableHeaders);
150 // $total = array('adds' => 0, 'commits' => 0);
152 // while($data = db_fetch_array($result)) {
153 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
154 // $b .= '<td width="50%">' ;
155 // $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
156 // $b .= '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
157 // '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
158 // $total['adds'] += $data['adds'];
159 // $total['commits'] += $data['commits'];
162 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
163 // $b .= '<td width="50%"><strong>'._('Total').':</strong></td>'.
164 // '<td width="25%" align="right"><strong>'.$total['adds']. '</strong></td>'.
165 // '<td width="25%" align="right"><strong>'.$total['commits'].'</strong></td>';
167 // $b .= $HTML->listTableBottom();
168 // $b .= '<hr size="1" />';
173 function getStatsBlock ($project) {
177 function createOrUpdateRepo ($params) {
178 $project = $this->checkParams ($params) ;
183 if (! $project->usesPlugin ($this->name)) {
187 $project_name = $project->getUnixName() ;
188 $root = $this->git_root . '/' . $project_name ;
189 $repo = $root . '/' . $project_name . '.git' ;
190 $unix_group = 'scm_' . $project_name ;
192 system ("mkdir -p $repo") ;
193 if (!is_file ("$repo/HEAD") && !is_dir("$repo/objects") && !is_dir("$repo/refs")) {
194 system ("GIT_DIR=\"$repo\" git init --bare --shared=group") ;
195 system ("GIT_DIR=\"$repo\" git update-server-info") ;
196 if (is_file ("$repo/hooks/post-update.sample")) {
197 rename ("$repo/hooks/post-update.sample",
198 "$repo/hooks/post-update") ;
200 if (!is_file ("$repo/hooks/post-update")) {
201 $f = fopen ("$repo/hooks/post-update") ;
202 fwrite ($f, "exec git-update-server-info\n") ;
205 if (is_file ("$repo/hooks/post-update")) {
206 system ("chmod +x $repo/hooks/post-update") ;
208 system ("echo \"Git repository for $project_name\" > $repo/description") ;
209 system ("find $repo -type d | xargs chmod g+s") ;
212 system ("chgrp -R $unix_group $root") ;
213 system ("chmod g+s $root") ;
214 if ($project->enableAnonSCM()) {
215 system ("chmod -R g+wX,o+rX-w $root") ;
217 system ("chmod -R g+wX,o-rwx $root") ;
221 function updateRepositoryList ($params) {
222 $groups = $this->getGroups () ;
224 foreach ($groups as $project) {
225 if ($this->browserDisplayable ($project)) {
230 $config_dir = '/etc/gforge/plugins/scmgit' ;
231 $fname = $config_dir . '/gitweb.conf' ;
232 $config_f = fopen ($fname.'.new', 'w') ;
233 $rootdir = $this->git_root;
234 fwrite($config_f, "\$projectroot = '$rootdir';\n");
235 fwrite($config_f, "\$projects_list = '$config_dir/gitweb.list';\n");
236 fwrite($config_f, "@git_base_url_list = ('". util_make_url ('/anonscm/git') . "');\n");
237 fwrite($config_f, "\$logo = '". util_make_url ('/plugins/scmgit/gitweb/git-logo.png') . "';\n");
238 fwrite($config_f, "\$favicon = '". util_make_url ('/plugins/scmgit/gitweb/git-favicon.png')."';\n");
239 fwrite($config_f, "\$stylesheet = '". util_make_url ('/plugins/scmgit/gitweb/gitweb.css')."';\n");
240 fwrite($config_f, "\$prevent_xss = 'true';\n");
242 chmod ($fname.'.new', 0644) ;
243 rename ($fname.'.new', $fname) ;
245 $fname = $config_dir . '/gitweb.list' ;
247 $f = fopen ($fname.'.new', 'w') ;
248 foreach ($list as $project) {
249 $repos = $this->getRepositories($rootdir . "/" . $project->getUnixName());
250 foreach ($repos as $repo) {
251 $reldir = substr($repo, strlen($rootdir) + 1);
252 fwrite ($f, $reldir . "\n");
256 chmod ($fname.'.new', 0644) ;
257 rename ($fname.'.new', $fname) ;
260 function getRepositories($path) {
264 $entries = scandir($path);
265 foreach ($entries as $entry) {
266 $fullname = $path . "/" . $entry;
267 if (($entry == ".") or ($entry == ".."))
269 if (is_dir($fullname)) {
270 if (is_link($fullname))
272 $result = $this->getRepositories($fullname);
273 $list = array_merge($list, $result);
274 } else if ($entry == "HEAD") {
281 function generateSnapshots ($params) {
282 global $sys_scm_tarballs_path ;
284 $project = $this->checkParams ($params) ;
289 $group_name = $project->getUnixName() ;
291 $snapshot = $sys_scm_snapshots_path.'/'.$group_name.'-scm-latest.tar.gz';
292 $tarball = $sys_scm_tarballs_path.'/'.$group_name.'-scmroot.tar.gz';
294 if (! $project->usesPlugin ($this->name)) {
298 if (! $project->enableAnonSCM()) {
303 // TODO: ideally we generate one snapshot per git repository
304 $toprepo = $this->git_root ;
305 $repo = $toprepo . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git' ;
307 if (!is_dir ($repo)) {
312 $today = date ('Y-m-d') ;
313 $tmp = trim (`mktemp -d`) ;
318 system ("GIT_DIR=\"$repo\" git archive --format=tar --prefix=$group_name-scm-$today/ HEAD | gzip > $tmp/snapshot.tar.gz");
319 chmod ("$tmp/snapshot.tar.gz", 0644) ;
320 copy ("$tmp/snapshot.tar.gz", $snapshot) ;
321 unlink ("$tmp/snapshot.tar.gz") ;
323 system ("tar czCf $toprepo $tmp/tarball.tar.gz " . $project->getUnixName()) ;
324 chmod ("$tmp/tarball.tar.gz", 0644) ;
325 copy ("$tmp/tarball.tar.gz", $tarball) ;
326 unlink ("$tmp/tarball.tar.gz") ;
327 system ("rm -rf $tmp") ;
330 function gatherStats ($params) {
331 global $last_user, $usr_adds, $usr_deletes,
332 $usr_updates, $updates, $adds;
334 $project = $this->checkParams ($params) ;
339 if (! $project->usesPlugin ($this->name)) {
343 if ($params['mode'] == 'day') {
346 $year = $params ['year'] ;
347 $month = $params ['month'] ;
348 $day = $params ['day'] ;
349 $month_string = sprintf( "%04d%02d", $year, $month );
350 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
351 $end_time = $start_time + 86400;
353 $usr_adds = array () ;
354 $usr_updates = array () ;
355 $usr_deletes = array () ;
360 $repo = $this->git_root . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git';
361 if (!is_dir ($repo) || !is_dir ("$repo/refs")) {
362 // echo "No repository\n" ;
367 $pipe = popen ("GIT_DIR=\"$repo\" git log --since=@$start_time --until=@$end_time --all --pretty='format:%n%an <%ae>' --name-status", 'r' ) ;
369 // cleaning stats_cvs_* table for the current day
370 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
371 array ($month_string,
373 $project->getID())) ;
375 echo "Error while cleaning stats_cvs_group\n" ;
381 while (!feof($pipe) && $data = fgets ($pipe)) {
383 if (strlen($line) > 0) {
384 $result = preg_match("/^(?<name>.+) <(?<mail>.+)>/", $line, $matches);
387 $last_user = $matches['name'];
389 // Short-commit stats line
390 preg_match("/^(?<mode>[AM])\s+(?<file>.+)$/", $line, $matches);
391 if ($last_user == "") continue;
392 if ($matches['mode'] == 'A') {
393 $usr_adds[$last_user]++;
395 } elseif ($matches['mode'] == 'M') {
396 $usr_updates[$last_user]++;
398 } elseif ($matches['mode'] == 'D') {
399 $usr_deletes[$last_user]++;
405 // inserting group results in stats_cvs_groups
406 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
407 array ($month_string,
413 echo "Error while inserting into stats_cvs_group\n" ;
418 // building the user list
419 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
421 foreach ( $user_list as $user ) {
422 // trying to get user id from user name
423 $u = &user_get_object_by_name ($user) ;
425 $user_id = $u->getID();
430 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
431 array ($month_string,
435 $usr_updates[$user] ? $usr_updates[$user] : 0,
436 $usr_adds[$user] ? $usr_adds[$user] : 0))) {
437 echo "Error while inserting into stats_cvs_user\n" ;
449 // c-file-style: "bsd"