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 />';
83 $result = db_query_params ('SELECT u.user_id, u.user_name, u.realname FROM plugin_scmgit_personal_repos p, users u WHERE p.group_id=$1 AND u.user_id=p.user_id AND u.unix_status=$2',
84 array ($project->getID(),
86 $rows = db_numrows ($result) ;
89 $b = ngettext ('<p><b>Developer\'s repository</b></p><p>One of this project\'s members also has a personal Git repository that can be checked out anonymously.</p>',
90 '<p><b>Developers\' repositories</b></p><p>Some of this project\'s members also have personal Git repositories that can be checked out anonymously.</p>',
93 for ($i=0; $i<$rows; $i++) {
94 $user_id = db_result($result,$i,'user_id');
95 $user_name = db_result($result,$i,'user_name');
96 $real_name = db_result($result,$i,'realname');
97 $repos[] = array ($root . '/users/' . $user_name . '.git' => $user_name) ;
98 $b .= '<tt>git clone '.util_make_url ('/anonscm/git/'.$project->getUnixName().'/users/'.$user_name.'.git').'</tt> ('.util_make_link_u ($user_name, $user_id, $realname).')<br />';
106 function getInstructionsForRW ($project) {
107 if (session_loggedin()) {
108 $u =& user_get_object(user_getid()) ;
109 $d = $u->getUnixName() ;
110 $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. Enter your site password when prompted.</p>');
111 $b .= '<p><tt>git clone git+ssh://'.$d.'@' . $project->getSCMBox() . $this->git_root .'/'. $project->getUnixName() .'/'. $project->getUnixName() .'.git</tt></p>' ;
113 $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 value. Enter your site password when prompted.</p>');
114 $b .= '<p><tt>git clone git+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . $this->git_root .'/'. $project->getUnixName() .'/'. $project->getUnixName() .'.git</tt></p>' ;
119 function getSnapshotPara ($project) {
120 global $sys_scm_snapshots_path ;
122 $filename = $project->getUnixName().'-scm-latest.tar.gz';
123 if (file_exists($sys_scm_snapshots_path.'/'.$filename)) {
125 $b .= util_make_link ("/snapshots.php?group_id=".$project->getID(),
126 _('Download the nightly snapshot')
133 function printBrowserPage ($params) {
136 $project = $this->checkParams ($params) ;
141 if ($project->usesPlugin ($this->name)) {
142 if ($this->browserDisplayable ($project)) {
143 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>' ;
148 function getBrowserLinkBlock ($project) {
150 $b = $HTML->boxMiddle(_('Git Repository Browser'));
151 $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>');
153 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
154 _('Browse Git Repository')
160 // function getStatsBlock ($project) {
164 // $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',
165 // array ($project->getID()));
167 // if (db_numrows($result) > 0) {
168 // $b .= $HTML->boxMiddle(_('Repository Statistics'));
170 // $tableHeaders = array(
175 // $b .= $HTML->listTableTop($tableHeaders);
178 // $total = array('adds' => 0, 'commits' => 0);
180 // while($data = db_fetch_array($result)) {
181 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
182 // $b .= '<td width="50%">' ;
183 // $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
184 // $b .= '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
185 // '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
186 // $total['adds'] += $data['adds'];
187 // $total['commits'] += $data['commits'];
190 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
191 // $b .= '<td width="50%"><strong>'._('Total').':</strong></td>'.
192 // '<td width="25%" align="right"><strong>'.$total['adds']. '</strong></td>'.
193 // '<td width="25%" align="right"><strong>'.$total['commits'].'</strong></td>';
195 // $b .= $HTML->listTableBottom();
200 function getStatsBlock ($project) {
204 function createOrUpdateRepo ($params) {
205 $project = $this->checkParams ($params) ;
210 if (! $project->usesPlugin ($this->name)) {
214 $project_name = $project->getUnixName() ;
215 $root = $this->git_root . '/' . $project_name ;
216 $unix_group = 'scm_' . $project_name ;
218 $main_repo = array ($root . '/' . $project_name . '.git') ;
219 if (!is_file ("$main_repo/HEAD") && !is_dir("$main_repo/objects") && !is_dir("$main_repo/refs")) {
220 system ("GIT_DIR=\"$main_repo\" git init --bare --shared=group") ;
221 system ("GIT_DIR=\"$main_repo\" git update-server-info") ;
222 if (is_file ("$main_repo/hooks/post-update.sample")) {
223 rename ("$main_repo/hooks/post-update.sample",
224 "$main_repo/hooks/post-update") ;
226 if (!is_file ("$main_repo/hooks/post-update")) {
227 $f = fopen ("$main_repo/hooks/post-update") ;
228 fwrite ($f, "exec git-update-server-info\n") ;
231 if (is_file ("$main_repo/hooks/post-update")) {
232 system ("chmod +x $main_repo/hooks/post-update") ;
234 system ("echo \"Git repository for $project_name\" > $main_repo/description") ;
235 system ("find $main_repo -type d | xargs chmod g+s") ;
237 system ("chgrp -R $unix_group $root") ;
238 system ("chmod g+s $root") ;
239 if ($project->enableAnonSCM()) {
240 system ("chmod g+wX,o+rX-w $root") ;
241 system ("chmod -R g+wX,o+rX-w $main_repo") ;
243 system ("chmod g+wX,o-rwx $root") ;
244 system ("chmod -R g+wX,o-rwx $main_repo") ;
247 $result = db_query_params ('SELECT u.user_name FROM plugin_scmgit_personal_repos p, users u WHERE p.group_id=$1 AND u.user_id=p.user_id AND u.unix_status=$2',
248 array ($project->getID(),
250 $rows = db_numrows ($result) ;
251 for ($i=0; $i<$rows; $i++) {
252 system ("mkdir -p $root/users") ;
253 $user_name = db_result($result,$i,'user_name');
254 $repodir = array ($root . '/users/' . $user_name . '.git') ;
256 if (!is_file ("$repodir/HEAD") && !is_dir("$repodir/objects") && !is_dir("$repodir/refs")) {
257 system ("git clone --bare $main_repo $repodir") ;
258 system ("echo \"Git repository for user $owner in project $project_name\" > $repodir/description") ;
259 system ("chown -R $user_name:$unix_group $repodir") ;
261 if ($project->enableAnonSCM()) {
262 system ("chmod -R g+rX-w,o+rX-w $repodir") ;
264 system ("chmod -R g+rX-w,o-rwx $repodir") ;
269 function updateRepositoryList ($params) {
270 $groups = $this->getGroups () ;
272 foreach ($groups as $project) {
273 if ($this->browserDisplayable ($project)) {
278 $config_dir = '/etc/gforge/plugins/scmgit' ;
279 $fname = $config_dir . '/gitweb.conf' ;
280 $config_f = fopen ($fname.'.new', 'w') ;
281 $rootdir = $this->git_root;
282 fwrite($config_f, "\$projectroot = '$rootdir';\n");
283 fwrite($config_f, "\$projects_list = '$config_dir/gitweb.list';\n");
284 fwrite($config_f, "@git_base_url_list = ('". util_make_url ('/anonscm/git') . "');\n");
285 fwrite($config_f, "\$logo = '". util_make_url ('/plugins/scmgit/gitweb/git-logo.png') . "';\n");
286 fwrite($config_f, "\$favicon = '". util_make_url ('/plugins/scmgit/gitweb/git-favicon.png')."';\n");
287 fwrite($config_f, "\$stylesheet = '". util_make_url ('/plugins/scmgit/gitweb/gitweb.css')."';\n");
288 fwrite($config_f, "\$prevent_xss = 'true';\n");
290 chmod ($fname.'.new', 0644) ;
291 rename ($fname.'.new', $fname) ;
293 $fname = $config_dir . '/gitweb.list' ;
295 $f = fopen ($fname.'.new', 'w') ;
296 foreach ($list as $project) {
297 $repos = $this->getRepositories($rootdir . "/" . $project->getUnixName());
298 foreach ($repos as $repo) {
299 $reldir = substr($repo, strlen($rootdir) + 1);
300 fwrite ($f, $reldir . "\n");
304 chmod ($fname.'.new', 0644) ;
305 rename ($fname.'.new', $fname) ;
308 function getRepositories($path) {
312 $entries = scandir($path);
313 foreach ($entries as $entry) {
314 $fullname = $path . "/" . $entry;
315 if (($entry == ".") or ($entry == ".."))
317 if (is_dir($fullname)) {
318 if (is_link($fullname))
320 $result = $this->getRepositories($fullname);
321 $list = array_merge($list, $result);
322 } else if ($entry == "HEAD") {
329 function generateSnapshots ($params) {
330 global $sys_scm_tarballs_path ;
332 $project = $this->checkParams ($params) ;
337 $group_name = $project->getUnixName() ;
339 $snapshot = $sys_scm_snapshots_path.'/'.$group_name.'-scm-latest.tar.gz';
340 $tarball = $sys_scm_tarballs_path.'/'.$group_name.'-scmroot.tar.gz';
342 if (! $project->usesPlugin ($this->name)) {
346 if (! $project->enableAnonSCM()) {
351 // TODO: ideally we generate one snapshot per git repository
352 $toprepo = $this->git_root ;
353 $repo = $toprepo . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git' ;
355 if (!is_dir ($repo)) {
360 $today = date ('Y-m-d') ;
361 $tmp = trim (`mktemp -d`) ;
366 system ("GIT_DIR=\"$repo\" git archive --format=tar --prefix=$group_name-scm-$today/ HEAD | gzip > $tmp/snapshot.tar.gz");
367 chmod ("$tmp/snapshot.tar.gz", 0644) ;
368 copy ("$tmp/snapshot.tar.gz", $snapshot) ;
369 unlink ("$tmp/snapshot.tar.gz") ;
371 system ("tar czCf $toprepo $tmp/tarball.tar.gz " . $project->getUnixName()) ;
372 chmod ("$tmp/tarball.tar.gz", 0644) ;
373 copy ("$tmp/tarball.tar.gz", $tarball) ;
374 unlink ("$tmp/tarball.tar.gz") ;
375 system ("rm -rf $tmp") ;
378 function gatherStats ($params) {
379 global $last_user, $usr_adds, $usr_deletes,
380 $usr_updates, $updates, $adds;
382 $project = $this->checkParams ($params) ;
387 if (! $project->usesPlugin ($this->name)) {
391 if ($params['mode'] == 'day') {
394 $year = $params ['year'] ;
395 $month = $params ['month'] ;
396 $day = $params ['day'] ;
397 $month_string = sprintf( "%04d%02d", $year, $month );
398 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
399 $end_time = $start_time + 86400;
401 $usr_adds = array () ;
402 $usr_updates = array () ;
403 $usr_deletes = array () ;
408 $repo = $this->git_root . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git';
409 if (!is_dir ($repo) || !is_dir ("$repo/refs")) {
410 // echo "No repository\n" ;
415 $pipe = popen ("GIT_DIR=\"$repo\" git log --since=@$start_time --until=@$end_time --all --pretty='format:%n%an <%ae>' --name-status", 'r' ) ;
417 // cleaning stats_cvs_* table for the current day
418 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
419 array ($month_string,
421 $project->getID())) ;
423 echo "Error while cleaning stats_cvs_group\n" ;
429 while (!feof($pipe) && $data = fgets ($pipe)) {
431 if (strlen($line) > 0) {
432 $result = preg_match("/^(?<name>.+) <(?<mail>.+)>/", $line, $matches);
435 $last_user = $matches['name'];
437 // Short-commit stats line
438 preg_match("/^(?<mode>[AM])\s+(?<file>.+)$/", $line, $matches);
439 if ($last_user == "") continue;
440 if ($matches['mode'] == 'A') {
441 $usr_adds[$last_user]++;
443 } elseif ($matches['mode'] == 'M') {
444 $usr_updates[$last_user]++;
446 } elseif ($matches['mode'] == 'D') {
447 $usr_deletes[$last_user]++;
453 // inserting group results in stats_cvs_groups
454 if ($updates > 0 || $adds > 0) {
455 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
456 array ($month_string,
462 echo "Error while inserting into stats_cvs_group\n" ;
468 // building the user list
469 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
471 foreach ( $user_list as $user ) {
472 // trying to get user id from user name
473 $u = &user_get_object_by_name ($user) ;
475 $user_id = $u->getID();
480 $uu = $usr_updates[$user] ? $usr_updates[$user] : 0 ;
481 $ua = $usr_adds[$user] ? $usr_adds[$user] : 0 ;
482 if ($uu > 0 || $ua > 0) {
483 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
484 array ($month_string,
490 echo "Error while inserting into stats_cvs_user\n" ;
503 // c-file-style: "bsd"