2 /** FusionForge Git plugin
4 * Copyright 2009, Roland Mas
6 * This file is part of FusionForge.
8 * FusionForge is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License,
11 * or (at your option) any later version.
13 * FusionForge is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with FusionForge; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 class GitPlugin extends SCMPlugin {
25 function GitPlugin () {
28 $this->name = 'scmgit';
30 $this->hooks[] = 'scm_update_repolist' ;
31 $this->hooks[] = 'scm_browser_page' ;
32 $this->hooks[] = 'scm_gather_stats' ;
33 $this->hooks[] = 'scm_generate_snapshots' ;
35 require_once $gfconfig.'plugins/scmgit/config.php' ;
37 $this->default_git_server = $default_git_server ;
38 if (isset ($git_root)) {
39 $this->git_root = $git_root;
41 $this->git_root = $GLOBALS['sys_chroot'].'/scmrepos/git' ;
47 function getDefaultServer() {
48 return $this->default_git_server ;
51 function printShortStats ($params) {
52 $project = $this->checkParams ($params) ;
57 if ($project->usesPlugin($this->name)) {
58 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
59 array ($project->getID())) ;
60 $commit_num = db_result($result,0,'commits');
61 $add_num = db_result($result,0,'adds');
68 echo ' (Git: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
72 function getBlurb () {
73 return _('<p>Documentation for Git is available <a href="http://git-scm.com/">here</a>.</p>') ;
76 function getInstructionsForAnon ($project) {
77 $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>');
79 $b .= '<tt>git clone '.util_make_url ('/anonscm/git/'.$project->getUnixName()).'</tt><br />';
84 function getInstructionsForRW ($project) {
85 $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>');
86 $b .= '<p><tt>git clone git+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . $this->git_root .'/'. $project->getUnixName().'</tt></p>' ;
90 function getSnapshotPara ($project) {
91 global $sys_scm_snapshots_path ;
93 $filename = $project->getUnixName().'-scm-latest.tar.gz';
94 if (file_exists($sys_scm_snapshots_path.'/'.$filename)) {
96 $b .= util_make_link ("/snapshots.php?group_id=".$project->getID(),
97 _('Download the nightly snapshot')
104 function printBrowserPage ($params) {
107 $project = $this->checkParams ($params) ;
112 if ($project->usesPlugin ($this->name)) {
113 if ($this->browserDisplayable ($project)) {
114 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>' ;
119 function getBrowserLinkBlock ($project) {
121 $b = $HTML->boxMiddle(_('Git Repository Browser'));
122 $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>');
124 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
125 _('Browse Git Repository')
131 // function getStatsBlock ($project) {
135 // $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',
136 // array ($project->getID()));
138 // if (db_numrows($result) > 0) {
139 // $b .= $HTML->boxMiddle(_('Repository Statistics'));
141 // $tableHeaders = array(
146 // $b .= $HTML->listTableTop($tableHeaders);
149 // $total = array('adds' => 0, 'commits' => 0);
151 // while($data = db_fetch_array($result)) {
152 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
153 // $b .= '<td width="50%">' ;
154 // $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
155 // $b .= '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
156 // '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
157 // $total['adds'] += $data['adds'];
158 // $total['commits'] += $data['commits'];
161 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
162 // $b .= '<td width="50%"><strong>'._('Total').':</strong></td>'.
163 // '<td width="25%" align="right"><strong>'.$total['adds']. '</strong></td>'.
164 // '<td width="25%" align="right"><strong>'.$total['commits'].'</strong></td>';
166 // $b .= $HTML->listTableBottom();
167 // $b .= '<hr size="1" />';
172 function getStatsBlock ($project) {
176 function createOrUpdateRepo ($params) {
177 $project = $this->checkParams ($params) ;
182 if (! $project->usesPlugin ($this->name)) {
186 $project_name = $project->getUnixName() ;
187 $root = $this->git_root . '/' . $project_name ;
188 $repo = $root . '/' . $project_name . '.git' ;
189 $unix_group = 'scm_' . $project_name ;
191 system ("mkdir -p $repo") ;
192 if (!is_file ("$repo/HEAD") && !is_dir("$repo/objects") && !is_dir("$repo/refs")) {
193 system ("GIT_DIR=\"$repo\" git init --bare --shared=group") ;
194 system ("GIT_DIR=\"$repo\" git update-server-info") ;
195 if (is_file ("$repo/hooks/post-update.sample")) {
196 rename ("$repo/hooks/post-update.sample",
197 "$repo/hooks/post-update") ;
199 if (!is_file ("$repo/hooks/post-update")) {
200 $f = fopen ("$repo/hooks/post-update") ;
201 fwrite ($f, "exec git-update-server-info\n") ;
204 if (is_file ("$repo/hooks/post-update")) {
205 system ("chmod +x $repo/hooks/post-update") ;
207 system ("echo \"Git repository for $project_name\" > $repo/description") ;
208 system ("find $repo -type d | xargs chmod g+s") ;
211 system ("chgrp -R $unix_group $root") ;
212 if ($project->enableAnonSCM()) {
213 system ("chmod -R g+wX,o+rX-w $root") ;
214 system ("find $root -type d -print0 | xargs --null chmod g+s");
216 system ("chmod -R g+wX,o-rwx $root") ;
217 system ("find $root -type d -print0 | xargs --null chmod g+s");
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 fwrite ($f, $project->getUnixName() . "/" . $project->getUnixName() . ".git\n");
252 chmod ($fname.'.new', 0644) ;
253 rename ($fname.'.new', $fname) ;
256 function generateSnapshots ($params) {
257 global $sys_scm_tarballs_path ;
259 $project = $this->checkParams ($params) ;
264 $group_name = $project->getUnixName() ;
266 $snapshot = $sys_scm_snapshots_path.'/'.$group_name.'-scm-latest.tar.gz';
267 $tarball = $sys_scm_tarballs_path.'/'.$group_name.'-scmroot.tar.gz';
269 if (! $project->usesPlugin ($this->name)) {
273 if (! $project->enableAnonSCM()) {
278 // TODO: ideally we generate one snapshot per git repository
279 $toprepo = $this->git_root ;
280 $repo = $toprepo . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git' ;
282 if (!is_dir ($repo)) {
287 $today = date ('Y-m-d') ;
288 $tmp = trim (`mktemp -d`) ;
293 system ("git archive --format=tar --prefix=$group_name-scm-$today/ HEAD | gzip > $tmp/snapshot.tar.gz");
294 chmod ("$tmp/snapshot.tar.gz", 0644) ;
295 copy ("$tmp/snapshot.tar.gz", $snapshot) ;
296 unlink ("$tmp/snapshot.tar.gz") ;
298 system ("tar czCf $toprepo $tmp/tarball.tar.gz " . $project->getUnixName()) ;
299 chmod ("$tmp/tarball.tar.gz", 0644) ;
300 copy ("$tmp/tarball.tar.gz", $tarball) ;
301 unlink ("$tmp/tarball.tar.gz") ;
302 system ("rm -rf $tmp") ;
305 function gatherStats ($params) {
306 global $last_user, $usr_adds, $usr_deletes,
307 $usr_updates, $updates, $adds;
309 $project = $this->checkParams ($params) ;
314 if (! $project->usesPlugin ($this->name)) {
318 if ($params['mode'] == 'day') {
321 $year = $params ['year'] ;
322 $month = $params ['month'] ;
323 $day = $params ['day'] ;
324 $month_string = sprintf( "%04d%02d", $year, $month );
325 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
326 $end_time = $start_time + 86400;
328 $usr_adds = array () ;
329 $usr_updates = array () ;
330 $usr_deletes = array () ;
335 $repo = $this->git_root . '/' . $project->getUnixName() ;
336 if (!is_dir ($repo) || !is_dir ("$repo/refs")) {
337 // echo "No repository\n" ;
342 $pipe = popen ("GIT_DIR=$repo git log --since=@$start_time --until=@$end_time --all --pretty='format:%n%an <%ae>' --name-status", 'r' ) ;
344 // cleaning stats_cvs_* table for the current day
345 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
346 array ($month_string,
348 $project->getID())) ;
350 echo "Error while cleaning stats_cvs_group\n" ;
356 while (!feof($pipe) && $data = fgets ($pipe)) {
358 if (strlen($line) > 0) {
359 $result = preg_match("/^(?<name>.+) <(?<mail>.+)>/", $line, $matches);
362 $last_user = $matches['name'];
364 // Short-commit stats line
365 preg_match("/^(?<mode>[AM])\s+(?<file>.+)$/", $line, $matches);
366 if ($last_user == "") continue;
367 if ($matches['mode'] == 'A') {
368 $usr_adds[$last_user]++;
370 } elseif ($matches['mode'] == 'M') {
371 $usr_updates[$last_user]++;
373 } elseif ($matches['mode'] == 'D') {
374 $usr_deletes[$last_user]++;
380 // inserting group results in stats_cvs_groups
381 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
382 array ($month_string,
388 echo "Error while inserting into stats_cvs_group\n" ;
393 // building the user list
394 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
396 foreach ( $user_list as $user ) {
397 // trying to get user id from user name
398 $u = &user_get_object_by_name ($user) ;
400 $user_id = $u->getID();
405 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
406 array ($month_string,
410 $usr_updates[$user] ? $usr_updates[$user] : 0,
411 $usr_adds[$user] ? $usr_adds[$user] : 0))) {
412 echo "Error while inserting into stats_cvs_user\n" ;
424 // c-file-style: "bsd"