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 if (session_logged_in()) {
87 $u =& user_get_object(user_getid()) ;
88 $d = $u->getUnixName() ;
89 $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>');
90 $b .= '<p><tt>git clone git+ssh://'.$d.'@' . $project->getSCMBox() . $this->git_root .'/'. $project->getUnixName() .'/'. $project->getUnixName() .'.git</tt></p>' ;
92 $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>');
93 $b .= '<p><tt>git clone git+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . $this->git_root .'/'. $project->getUnixName() .'/'. $project->getUnixName() .'.git</tt></p>' ;
98 function getSnapshotPara ($project) {
99 global $sys_scm_snapshots_path ;
101 $filename = $project->getUnixName().'-scm-latest.tar.gz';
102 if (file_exists($sys_scm_snapshots_path.'/'.$filename)) {
104 $b .= util_make_link ("/snapshots.php?group_id=".$project->getID(),
105 _('Download the nightly snapshot')
112 function printBrowserPage ($params) {
115 $project = $this->checkParams ($params) ;
120 if ($project->usesPlugin ($this->name)) {
121 if ($this->browserDisplayable ($project)) {
122 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>' ;
127 function getBrowserLinkBlock ($project) {
129 $b = $HTML->boxMiddle(_('Git Repository Browser'));
130 $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>');
132 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
133 _('Browse Git Repository')
139 // function getStatsBlock ($project) {
143 // $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',
144 // array ($project->getID()));
146 // if (db_numrows($result) > 0) {
147 // $b .= $HTML->boxMiddle(_('Repository Statistics'));
149 // $tableHeaders = array(
154 // $b .= $HTML->listTableTop($tableHeaders);
157 // $total = array('adds' => 0, 'commits' => 0);
159 // while($data = db_fetch_array($result)) {
160 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
161 // $b .= '<td width="50%">' ;
162 // $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
163 // $b .= '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
164 // '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
165 // $total['adds'] += $data['adds'];
166 // $total['commits'] += $data['commits'];
169 // $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
170 // $b .= '<td width="50%"><strong>'._('Total').':</strong></td>'.
171 // '<td width="25%" align="right"><strong>'.$total['adds']. '</strong></td>'.
172 // '<td width="25%" align="right"><strong>'.$total['commits'].'</strong></td>';
174 // $b .= $HTML->listTableBottom();
179 function getStatsBlock ($project) {
183 function createOrUpdateRepo ($params) {
184 $project = $this->checkParams ($params) ;
189 if (! $project->usesPlugin ($this->name)) {
193 $project_name = $project->getUnixName() ;
194 $root = $this->git_root . '/' . $project_name ;
195 $repo = $root . '/' . $project_name . '.git' ;
196 $unix_group = 'scm_' . $project_name ;
198 system ("mkdir -p $repo") ;
199 if (!is_file ("$repo/HEAD") && !is_dir("$repo/objects") && !is_dir("$repo/refs")) {
200 system ("GIT_DIR=\"$repo\" git init --bare --shared=group") ;
201 system ("GIT_DIR=\"$repo\" git update-server-info") ;
202 if (is_file ("$repo/hooks/post-update.sample")) {
203 rename ("$repo/hooks/post-update.sample",
204 "$repo/hooks/post-update") ;
206 if (!is_file ("$repo/hooks/post-update")) {
207 $f = fopen ("$repo/hooks/post-update") ;
208 fwrite ($f, "exec git-update-server-info\n") ;
211 if (is_file ("$repo/hooks/post-update")) {
212 system ("chmod +x $repo/hooks/post-update") ;
214 system ("echo \"Git repository for $project_name\" > $repo/description") ;
215 system ("find $repo -type d | xargs chmod g+s") ;
218 system ("chgrp -R $unix_group $root") ;
219 system ("chmod g+s $root") ;
220 if ($project->enableAnonSCM()) {
221 system ("chmod -R g+wX,o+rX-w $root") ;
223 system ("chmod -R g+wX,o-rwx $root") ;
227 function updateRepositoryList ($params) {
228 $groups = $this->getGroups () ;
230 foreach ($groups as $project) {
231 if ($this->browserDisplayable ($project)) {
236 $config_dir = '/etc/gforge/plugins/scmgit' ;
237 $fname = $config_dir . '/gitweb.conf' ;
238 $config_f = fopen ($fname.'.new', 'w') ;
239 $rootdir = $this->git_root;
240 fwrite($config_f, "\$projectroot = '$rootdir';\n");
241 fwrite($config_f, "\$projects_list = '$config_dir/gitweb.list';\n");
242 fwrite($config_f, "@git_base_url_list = ('". util_make_url ('/anonscm/git') . "');\n");
243 fwrite($config_f, "\$logo = '". util_make_url ('/plugins/scmgit/gitweb/git-logo.png') . "';\n");
244 fwrite($config_f, "\$favicon = '". util_make_url ('/plugins/scmgit/gitweb/git-favicon.png')."';\n");
245 fwrite($config_f, "\$stylesheet = '". util_make_url ('/plugins/scmgit/gitweb/gitweb.css')."';\n");
246 fwrite($config_f, "\$prevent_xss = 'true';\n");
248 chmod ($fname.'.new', 0644) ;
249 rename ($fname.'.new', $fname) ;
251 $fname = $config_dir . '/gitweb.list' ;
253 $f = fopen ($fname.'.new', 'w') ;
254 foreach ($list as $project) {
255 $repos = $this->getRepositories($rootdir . "/" . $project->getUnixName());
256 foreach ($repos as $repo) {
257 $reldir = substr($repo, strlen($rootdir) + 1);
258 fwrite ($f, $reldir . "\n");
262 chmod ($fname.'.new', 0644) ;
263 rename ($fname.'.new', $fname) ;
266 function getRepositories($path) {
270 $entries = scandir($path);
271 foreach ($entries as $entry) {
272 $fullname = $path . "/" . $entry;
273 if (($entry == ".") or ($entry == ".."))
275 if (is_dir($fullname)) {
276 if (is_link($fullname))
278 $result = $this->getRepositories($fullname);
279 $list = array_merge($list, $result);
280 } else if ($entry == "HEAD") {
287 function generateSnapshots ($params) {
288 global $sys_scm_tarballs_path ;
290 $project = $this->checkParams ($params) ;
295 $group_name = $project->getUnixName() ;
297 $snapshot = $sys_scm_snapshots_path.'/'.$group_name.'-scm-latest.tar.gz';
298 $tarball = $sys_scm_tarballs_path.'/'.$group_name.'-scmroot.tar.gz';
300 if (! $project->usesPlugin ($this->name)) {
304 if (! $project->enableAnonSCM()) {
309 // TODO: ideally we generate one snapshot per git repository
310 $toprepo = $this->git_root ;
311 $repo = $toprepo . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git' ;
313 if (!is_dir ($repo)) {
318 $today = date ('Y-m-d') ;
319 $tmp = trim (`mktemp -d`) ;
324 system ("GIT_DIR=\"$repo\" git archive --format=tar --prefix=$group_name-scm-$today/ HEAD | gzip > $tmp/snapshot.tar.gz");
325 chmod ("$tmp/snapshot.tar.gz", 0644) ;
326 copy ("$tmp/snapshot.tar.gz", $snapshot) ;
327 unlink ("$tmp/snapshot.tar.gz") ;
329 system ("tar czCf $toprepo $tmp/tarball.tar.gz " . $project->getUnixName()) ;
330 chmod ("$tmp/tarball.tar.gz", 0644) ;
331 copy ("$tmp/tarball.tar.gz", $tarball) ;
332 unlink ("$tmp/tarball.tar.gz") ;
333 system ("rm -rf $tmp") ;
336 function gatherStats ($params) {
337 global $last_user, $usr_adds, $usr_deletes,
338 $usr_updates, $updates, $adds;
340 $project = $this->checkParams ($params) ;
345 if (! $project->usesPlugin ($this->name)) {
349 if ($params['mode'] == 'day') {
352 $year = $params ['year'] ;
353 $month = $params ['month'] ;
354 $day = $params ['day'] ;
355 $month_string = sprintf( "%04d%02d", $year, $month );
356 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
357 $end_time = $start_time + 86400;
359 $usr_adds = array () ;
360 $usr_updates = array () ;
361 $usr_deletes = array () ;
366 $repo = $this->git_root . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git';
367 if (!is_dir ($repo) || !is_dir ("$repo/refs")) {
368 // echo "No repository\n" ;
373 $pipe = popen ("GIT_DIR=\"$repo\" git log --since=@$start_time --until=@$end_time --all --pretty='format:%n%an <%ae>' --name-status", 'r' ) ;
375 // cleaning stats_cvs_* table for the current day
376 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
377 array ($month_string,
379 $project->getID())) ;
381 echo "Error while cleaning stats_cvs_group\n" ;
387 while (!feof($pipe) && $data = fgets ($pipe)) {
389 if (strlen($line) > 0) {
390 $result = preg_match("/^(?<name>.+) <(?<mail>.+)>/", $line, $matches);
393 $last_user = $matches['name'];
395 // Short-commit stats line
396 preg_match("/^(?<mode>[AM])\s+(?<file>.+)$/", $line, $matches);
397 if ($last_user == "") continue;
398 if ($matches['mode'] == 'A') {
399 $usr_adds[$last_user]++;
401 } elseif ($matches['mode'] == 'M') {
402 $usr_updates[$last_user]++;
404 } elseif ($matches['mode'] == 'D') {
405 $usr_deletes[$last_user]++;
411 // inserting group results in stats_cvs_groups
412 if ($updates > 0 || $adds > 0) {
413 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
414 array ($month_string,
420 echo "Error while inserting into stats_cvs_group\n" ;
426 // building the user list
427 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
429 foreach ( $user_list as $user ) {
430 // trying to get user id from user name
431 $u = &user_get_object_by_name ($user) ;
433 $user_id = $u->getID();
438 $uu = $usr_updates[$user] ? $usr_updates[$user] : 0 ;
439 $ua = $usr_adds[$user] ? $usr_adds[$user] : 0 ;
440 if ($uu > 0 || $ua > 0) {
441 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
442 array ($month_string,
448 echo "Error while inserting into stats_cvs_user\n" ;
461 // c-file-style: "bsd"