3 * FusionForge Git plugin
5 * Copyright 2009, Roland Mas
6 * Copyright 2009, Mehdi Dogguy <mehdi@debian.org>
7 * Copyright 2012-2013, Franck Villaume - TrivialDev
8 * http://fusionforge.org
10 * This file is part of FusionForge.
12 * FusionForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published
14 * by the Free Software Foundation; either version 2 of the License,
15 * or (at your option) any later version.
17 * FusionForge is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 forge_define_config_item('default_server', 'scmgit', forge_get_config ('web_host'));
28 forge_define_config_item('repos_path', 'scmgit', forge_get_config('chroot').'/scmrepos/git');
29 forge_define_config_item('use_ssh', 'scmgit', false);
30 forge_set_config_item_bool('use_ssh', 'scmgit');
31 forge_define_config_item('use_dav', 'scmgit', true);
32 forge_set_config_item_bool('use_dav', 'scmgit');
33 forge_define_config_item('use_ssl', 'scmgit', true);
34 forge_set_config_item_bool('use_ssl', 'scmgit');
37 class GitPlugin extends SCMPlugin {
38 function GitPlugin() {
40 $this->name = 'scmgit';
42 $this->_addHook('scm_browser_page');
43 $this->_addHook('scm_update_repolist');
44 $this->_addHook('scm_generate_snapshots');
45 $this->_addHook('scm_gather_stats');
46 $this->_addHook('scm_admin_form');
47 $this->_addHook('scm_add_repo');
48 $this->_addHook('scm_delete_repo');
49 $this->_addHook('widget_instance', 'myPageBox', false);
50 $this->_addHook('widgets', 'widgets', false);
51 $this->_addHook('activity');
52 $this->_addHook('weekly');
56 function getDefaultServer() {
57 return forge_get_config('default_server', 'scmgit') ;
60 function printShortStats ($params) {
61 $project = $this->checkParams($params);
66 if ($project->usesPlugin($this->name) && forge_check_perm('scm', $project->getID(), 'read')) {
67 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
68 array ($project->getID())) ;
69 $commit_num = db_result($result,0,'commits');
70 $add_num = db_result($result,0,'adds');
77 echo ' (Git: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
83 . sprintf(_('Documentation for %1$s is available at <a href="%2$s">%2$s</a>.'),
85 'http://git-scm.com/')
89 function getInstructionsForAnon($project) {
90 $repo_list = array($project->getUnixName());
91 $result = db_query_params ('SELECT repo_name FROM scm_secondary_repos WHERE group_id=$1 AND next_action = $2 AND plugin_id=$3 ORDER BY repo_name',
92 array ($project->getID(),
93 SCM_EXTRA_REPO_ACTION_UPDATE,
95 $rows = db_numrows ($result) ;
96 for ($i=0; $i<$rows; $i++) {
97 $repo_list[] = db_result($result,$i,'repo_name');
100 $b = '<h2>' . ngettext('Anonymous Access to the Git repository',
101 'Anonymous Access to the Git repositories',
102 count($repo_list)) . '</h2>';
105 $b .= ngettext('This project\'s Git repository can be checked out through anonymous access with the following command.',
106 'This project\'s Git repositories can be checked out through anonymous access with the following commands.',
111 foreach ($repo_list as $repo_name) {
113 $b .= '<tt>git clone '.util_make_url ('/anonscm/git/'.$project->getUnixName().'/'.$repo_name.'.git').'</tt><br />';
117 $result = db_query_params('SELECT u.user_id, u.user_name, u.realname FROM scm_personal_repos p, users u WHERE p.group_id=$1 AND u.user_id=p.user_id AND u.unix_status=$2 AND plugin_id=$3',
118 array ($project->getID(),
121 $rows = db_numrows($result);
125 $b .= ngettext('Developer\'s repository',
126 'Developer\'s repositories',
130 $b .= ngettext('One of this project\'s members also has a personal Git repository that can be checked out anonymously.',
131 'Some of this project\'s members also have personal Git repositories that can be checked out anonymously.',
135 for ($i=0; $i<$rows; $i++) {
136 $user_id = db_result($result,$i,'user_id');
137 $user_name = db_result($result,$i,'user_name');
138 $real_name = db_result($result,$i,'realname');
139 $b .= '<tt>git clone '.util_make_url('/anonscm/git/'.$project->getUnixName().'/users/'.$user_name.'.git').'</tt> ('.util_make_link_u ($user_name, $user_id, $real_name).') ['.util_make_link('/scm/browser.php?group_id='.$project->getID().'&user_id='.$user_id, _('Browse Git Repository')).']<br />';
147 function getInstructionsForRW($project) {
148 $repo_list = array($project->getUnixName());
150 $result = db_query_params ('SELECT repo_name FROM scm_secondary_repos WHERE group_id=$1 AND next_action = $2 AND plugin_id=$3 ORDER BY repo_name',
151 array ($project->getID(),
152 SCM_EXTRA_REPO_ACTION_UPDATE,
154 $rows = db_numrows ($result) ;
155 for ($i=0; $i<$rows; $i++) {
156 $repo_list[] = db_result($result,$i,'repo_name');
159 if (session_loggedin()) {
160 $u = user_get_object(user_getid());
161 $d = $u->getUnixName();
164 if (forge_get_config('use_ssh', 'scmgit')) {
166 $b = '<h2>' . ngettext('Developer Access to the Git repository via SSH',
167 'Developer Access to the Git repositories via SSH',
168 count($repo_list)) . '</h2>';
171 $b .= ngettext('Only project developers can access the GIT repository via this method. SSH must be installed on your client machine. Enter your site password when prompted.',
172 'Only project developers can access the GIT repositories via this method. SSH must be installed on your client machine. Enter your site password when prompted.',
176 foreach ($repo_list as $repo_name) {
177 $b .= '<p><tt>git clone git+ssh://'.$d.'@' . $project->getSCMBox() . '/'. forge_get_config('repos_path', 'scmgit') .'/'. $project->getUnixName() .'/'. $repo_name .'.git</tt></p>' ;
182 if (forge_get_config('use_dav', 'scmgit')) {
183 $protocol = forge_get_config('use_ssl', 'scmgit')? 'https' : 'http';
185 $b = '<h2>' . ngettext('Developer Access to the Git repository via HTTP',
186 'Developer Access to the Git repositories via HTTP',
187 count($repo_list)) . '</h2>';
191 $b .= ngettext('Only project developers can access the GIT repository via this method. Enter your site password when prompted.',
192 'Only project developers can access the GIT repositories via this method. Enter your site password when prompted.',
196 foreach ($repo_list as $repo_name) {
197 $b .= '<p><tt>git clone '.$protocol.'://'.$d.'@' . $project->getSCMBox() . '/'. forge_get_config('scm_root', 'scmgit') .'/'. $project->getUnixName() .'/'. $repo_name .'.git</tt></p>' ;
203 if ($validSetup == 0) {
204 $b = '<p class="warning">'._('Missing configuration for access in scmgit.ini : use_ssh and use_dav disabled').'</p>';
207 if (forge_get_config('use_ssh', 'scmgit')) {
209 $b = '<h2>' . ngettext('Developer Access to the Git repository via SSH',
210 'Developer Access to the Git repositories via SSH',
211 count($repo_list)) . '</h2>';
215 $b .= ngettext('Only project developers can access the GIT repository 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.',
216 'Only project developers can access the GIT repositories 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.',
220 foreach ($repo_list as $repo_name) {
221 $b .= '<p><tt>git clone git+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . '/'. forge_get_config('repos_path', 'scmgit') .'/'. $project->getUnixName() .'/'. $repo_name .'.git</tt></p>' ;
225 if (forge_get_config('use_dav', 'scmgit')) {
226 $protocol = forge_get_config('use_ssl', 'scmgit')? 'https' : 'http';
228 $b = '<h2>' . ngettext('Developer Access to the Git repository via HTTP',
229 'Developer Access to the Git repositories via HTTP',
230 count($repo_list)) . '</h2>';
233 $b .= ngettext('Only project developers can access the GIT repository via this method. Enter your site password when prompted.',
234 'Only project developers can access the GIT repositories via this method. Enter your site password when prompted.',
238 foreach ($repo_list as $repo_name) {
239 $b .= '<p><tt>git clone '.$protocol.'://<i>'._('developername').'</i>@' . $project->getSCMBox() . '/'. forge_get_config('scm_root', 'scmgit') .'/'. $project->getUnixName() .'/'. $repo_name .'.git</tt></p>' ;
245 if (session_loggedin()) {
246 $u =& user_get_object(user_getid()) ;
247 if ($u->getUnixStatus() == 'A') {
248 $result = db_query_params('SELECT * FROM scm_personal_repos p WHERE p.group_id=$1 AND p.user_id=$2 AND plugin_id=$3',
249 array ($project->getID(),
252 if ($result && db_numrows ($result) > 0) {
254 $b .= _('Access to your personal repository');
257 $b .= _('You have a personal repository for this project, accessible through SSH with the following method. Enter your site password when prompted.');
259 $b .= '<p><tt>git clone git+ssh://'.$u->getUnixName().'@' . $this->getBoxForProject($project) . '/'. forge_get_config('repos_path', 'scmgit') .'/'. $project->getUnixName() .'/users/'. $u->getUnixName() .'.git</tt></p>' ;
261 $glist = $u->getGroups();
262 foreach ($glist as $g) {
263 if ($g->getID() == $project->getID()) {
265 $b .= _('Request a personal repository');
268 $b .= _('You can clone the project repository into a personal one into which you alone will be able to write. Other members of the project will only have read access. Access for non-members will follow the same rules as for the project\'s main repository. Note that the personal repository may take some time before it is created (less than an hour in most situations).');
271 $b .= sprintf (_('<a href="%s">Request a personal repository</a>.'),
272 util_make_url ('/plugins/scmgit/index.php?func=request-personal-repo&group_id='.$project->getID()));
282 function getSnapshotPara($project) {
285 $filename = $project->getUnixName().'-scm-latest.tar'.util_get_compressed_file_extension();
286 if (file_exists(forge_get_config('scm_snapshots_path').'/'.$filename)) {
288 $b .= util_make_link("/snapshots.php?group_id=".$project->getID(),
289 _('Download the nightly snapshot')
296 function printBrowserPage($params) {
297 $project = $this->checkParams($params);
302 if ($project->usesPlugin($this->name)) {
303 if ($params['user_id']) {
304 $user = user_get_object($params['user_id']);
305 echo $project->getUnixName().'/users/'.$user->getUnixName();
306 print '<iframe src="'.util_make_url("/plugins/scmgit/cgi-bin/gitweb.cgi?p=".$project->getUnixName().'/users/'.$user->getUnixName().'.git').'" frameborder="0" width=100% height=700></iframe>' ;
307 } elseif ($this->browserDisplayable($project)) {
308 print '<iframe src="'.util_make_url("/plugins/scmgit/cgi-bin/gitweb.cgi?p=".$project->getUnixName().'/'.$project->getUnixName().'.git').'" frameborder="0" width=100% height=700></iframe>' ;
313 function getBrowserLinkBlock($project) {
315 $b = $HTML->boxMiddle(_('Git Repository Browser'));
317 $b .= _('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.');
320 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
321 _('Browse Git Repository')
327 function getStatsBlock ($project) {
331 $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',
332 array ($project->getID()));
334 if (db_numrows($result) > 0) {
335 // $b .= $HTML->boxMiddle(_('Repository Statistics'));
337 $tableHeaders = array(
342 $b .= $HTML->listTableTop($tableHeaders, false, '', 'repo-history');
345 $total = array('adds' => 0, 'commits' => 0);
347 while($data = db_fetch_array($result)) {
348 $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
349 $b .= '<td class="halfwidth">' ;
350 $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
351 $b .= '</td><td class="onequarterwidth align-right">'.$data['adds']. '</td>'.
352 '<td class="onequarterwidth align-right">'.$data['commits'].'</td></tr>';
353 $total['adds'] += $data['adds'];
354 $total['commits'] += $data['commits'];
357 $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
358 $b .= '<td class="halfwidth"><strong>'._('Total').':</strong></td>'.
359 '<td class="onequarterwidth align-right"><strong>'.$total['adds']. '</strong></td>'.
360 '<td class="onequarterwidth align-right"><strong>'.$total['commits'].'</strong></td>';
362 $b .= $HTML->listTableBottom();
368 static function createUserRepo($params) {
369 $project = $params['project'];
370 $project_name = $project->getUnixName();
371 $user_name = $params['user_name'];
372 $unix_group = $params['unix_group'];
373 $main_repo = $params['main_repo'];
374 $root = $params['root'];
376 $repodir = $root . '/users/' . $user_name . '.git' ;
377 chgrp($repodir, $unix_group);
378 if ($project->enableAnonSCM()) {
379 chmod ($repodir, 02755);
381 chmod ($repodir, 02750);
383 if (!is_file ("$repodir/HEAD") && !is_dir("$repodir/objects") && !is_dir("$repodir/refs")) {
384 system ("git clone --bare $main_repo $repodir") ;
385 system ("GIT_DIR=\"$repodir\" git update-server-info") ;
386 if (is_file ("$repodir/hooks/post-update.sample")) {
387 rename ("$repodir/hooks/post-update.sample",
388 "$repodir/hooks/post-update") ;
390 if (!is_file ("$repodir/hooks/post-update")) {
391 $f = fopen ("$repodir/hooks/post-update","x+") ;
392 fwrite ($f, "exec git-update-server-info\n") ;
395 if (is_file ("$repodir/hooks/post-update")) {
396 system ("chmod +x $repodir/hooks/post-update") ;
398 system("echo \"Git repository for user $user_name in project $project_name\" > $repodir/description");
402 function createOrUpdateRepo($params) {
403 $project = $this->checkParams ($params) ;
408 if (! $project->usesPlugin($this->name)) {
412 $project_name = $project->getUnixName();
413 $root = forge_get_config('repos_path', 'scmgit') . '/' . $project_name;
414 if (!is_dir($root)) {
415 system("mkdir -p $root");
419 if (forge_get_config('use_ssh','scmgit')) {
420 $unix_group = 'scm_' . $project_name ;
422 $unix_group = forge_get_config('apache_group');
424 system ("chgrp $unix_group $root") ;
426 // Create main repository
427 $main_repo = $root . '/' . $project_name . '.git' ;
428 if (!is_dir($main_repo) || (!is_file("$main_repo/HEAD") &&
429 !is_dir("$main_repo/objects") && !is_dir("$main_repo/refs"))) {
430 $tmp_repo = util_mkdtemp('.git', $project_name);
431 if ($tmp_repo == false) {
435 exec ("GIT_DIR=\"$tmp_repo\" git init --bare --shared=group", $result) ;
436 $output .= join("<br />", $result);
438 exec ("GIT_DIR=\"$tmp_repo\" git update-server-info", $result) ;
439 $output .= join("<br />", $result);
440 if (is_file ("$tmp_repo/hooks/post-update.sample")) {
441 rename ("$tmp_repo/hooks/post-update.sample",
442 "$tmp_repo/hooks/post-update") ;
444 if (!is_file ("$tmp_repo/hooks/post-update")) {
445 $f = fopen ("$tmp_repo/hooks/post-update", 'w') ;
446 fwrite ($f, "exec git-update-server-info\n") ;
449 if (is_file ("$tmp_repo/hooks/post-update")) {
450 system ("chmod +x $tmp_repo/hooks/post-update") ;
452 system ("echo \"Git repository for $project_name\" > $tmp_repo/description") ;
453 system ("find $tmp_repo -type d | xargs chmod g+s") ;
454 system ("chgrp -R $unix_group $tmp_repo") ;
455 system ("chmod -R g+wX,o+rX-w $tmp_repo") ;
456 if ($project->enableAnonSCM()) {
457 system ("chmod g+wX,o+rX-w $root") ;
459 system ("chmod g+wX,o-rwx $root") ;
460 system ("chmod g+wX,o-rwx $tmp_repo") ;
464 * $main_repo can already exist, for example if it’s
465 * not a directory or doesn’t contain a HEAD file or
466 * an objects or refs subdirectory… move it out of
467 * the way in these cases
469 system("if test -e $main_repo || test -h $main_repo; then d=\$(mktemp -d $main_repo.scmgit-moved.XXXXXXXXXX) && mv -f $main_repo \$d/; fi");
470 /* here’s still a TOCTOU but we check $ret below */
471 system("mv $tmp_repo $main_repo", $ret);
475 system ("echo \"Git repository for $project_name\" > $main_repo/description") ;
476 system ("find $main_repo -type d | xargs chmod g+s");
477 if (forge_get_config('use_dav','scmgit')) {
478 $f = fopen(forge_get_config('config_path').'/httpd.conf.d/plugin-scmgit-dav.inc','a');
479 fputs($f,'Use Project '.$project_name."\n");
481 system(forge_get_config('httpd_reload_cmd','scmgit'));
484 if (forge_get_config('use_ssh','scmgit')) {
485 if ($project->enableAnonSCM()) {
486 system ("chmod g+wX,o+rX-w $root") ;
487 system ("chmod g+rwX,o+rX-w $main_repo") ;
489 system ("chmod g+wX,o-rwx $root") ;
490 system ("chmod g+rwX,o-rwx $main_repo") ;
493 $unix_user = forge_get_config('apache_user');
494 system ("chown $unix_user:$unix_group $main_repo") ;
495 system ("chmod g-rwx,o-rwx $main_repo") ;
498 // Create project-wide secondary repositories
499 $result = db_query_params ('SELECT repo_name, description, clone_url FROM scm_secondary_repos WHERE group_id=$1 AND next_action = $2 AND plugin_id=$3',
500 array ($project->getID(),
501 SCM_EXTRA_REPO_ACTION_UPDATE,
503 $rows = db_numrows ($result) ;
504 for ($i=0; $i<$rows; $i++) {
505 $repo_name = db_result($result,$i,'repo_name');
506 $description = db_result($result,$i,'description');
507 $clone_url = db_result($result,$i,'clone_url');
508 $repodir = $root . '/' . $repo_name . '.git' ;
509 if (!is_file ("$repodir/HEAD") && !is_dir("$repodir/objects") && !is_dir("$repodir/refs")) {
510 if ($clone_url != '') {
511 system ("cd $root;git clone --bare $clone_url $repodir") ;
513 system ("GIT_DIR=\"$repodir\" git init --bare --shared=group") ;
515 system ("GIT_DIR=\"$repodir\" git update-server-info") ;
516 if (is_file ("$repodir/hooks/post-update.sample")) {
517 rename ("$repodir/hooks/post-update.sample",
518 "$repodir/hooks/post-update") ;
520 if (!is_file ("$repodir/hooks/post-update")) {
521 $f = fopen ("$repodir/hooks/post-update") ;
522 fwrite ($f, "exec git-update-server-info\n") ;
525 if (is_file ("$repodir/hooks/post-update")) {
526 system ("chmod +x $repodir/hooks/post-update") ;
528 $f = fopen("$repodir/description", "w");
529 fwrite($f, $description."\n");
531 system ("chgrp -R $unix_group $repodir") ;
532 system ("chmod g+s $root") ;
533 if ($project->enableAnonSCM()) {
534 system ("chmod -R g+wX,o+rX-w $main_repo") ;
536 system ("chmod -R g+wX,o-rwx $main_repo") ;
541 // Delete project-wide secondary repositories
542 $result = db_query_params ('SELECT repo_name FROM scm_secondary_repos WHERE group_id=$1 AND next_action = $2 AND plugin_id=$3',
543 array ($project->getID(),
544 SCM_EXTRA_REPO_ACTION_DELETE,
546 $rows = db_numrows ($result) ;
547 for ($i=0; $i<$rows; $i++) {
548 $repo_name = db_result($result,$i,'repo_name');
549 $repodir = $root . '/' . $repo_name . '.git' ;
550 if (util_is_valid_repository_name($repo_name)) {
551 system ("rm -rf $repodir");
553 db_query_params ('DELETE FROM scm_secondary_repos WHERE group_id=$1 AND repo_name=$2 AND next_action = $3 AND plugin_id=$4',
554 array ($project->getID(),
556 SCM_EXTRA_REPO_ACTION_DELETE,
560 // Create users' personal repositories
561 $result = db_query_params ('SELECT u.user_name FROM scm_personal_repos p, users u WHERE p.group_id=$1 AND u.user_id=p.user_id AND u.unix_status=$2 AND plugin_id=$3',
562 array ($project->getID(),
565 $rows = db_numrows ($result) ;
566 for ($i=0; $i<$rows; $i++) {
567 system ("mkdir -p $root/users") ;
568 $user_name = db_result($result,$i,'user_name');
569 $repodir = $root . '/users/' . $user_name . '.git' ;
571 if (!is_dir($repodir) && mkdir ($repodir, 0700)) {
572 chown ($repodir, $user_name) ;
575 $params['project'] = $project;
576 $params['user_name'] = $user_name;
577 $params['unix_group'] = $unix_group;
578 $params['root'] = $root;
579 $params['main_repo'] = $main_repo;
581 util_sudo_effective_user($user_name,
582 array("GitPlugin","createUserRepo"),
586 if (is_dir ("$root/users")) {
587 if ($project->enableAnonSCM()) {
588 system ("chmod g+rX-w,o+rX-w $root/users") ;
590 system ("chmod g+rX-w,o-rwx $root/users") ;
593 $params['output'] = $output;
596 function updateRepositoryList($params) {
597 $groups = $this->getGroups();
599 foreach ($groups as $project) {
600 if ($this->browserDisplayable($project)) {
605 $config_dir = forge_get_config('config_path').'/plugins/scmgit';
606 if (!is_dir($config_dir)) {
607 mkdir($config_dir, 0755, true);
609 $fname = $config_dir . '/gitweb.conf' ;
610 $config_f = fopen($fname.'.new', 'w') ;
611 $rootdir = forge_get_config('repos_path', 'scmgit');
612 fwrite($config_f, "\$projectroot = '$rootdir';\n");
613 fwrite($config_f, "\$projects_list = '$config_dir/gitweb.list';\n");
614 fwrite($config_f, "@git_base_url_list = ('". util_make_url('/anonscm/git') . "');\n");
615 fwrite($config_f, "\$logo = '". util_make_url('/plugins/scmgit/git-logo.png') . "';\n");
616 fwrite($config_f, "\$favicon = '". util_make_url('/plugins/scmgit/git-favicon.png')."';\n");
617 fwrite($config_f, "\$stylesheet = '". util_make_url('/plugins/scmgit/gitweb.css')."';\n");
618 fwrite($config_f, "\$javascript = '". util_make_url('/plugins/scmgit/gitweb.js')."';\n");
619 fwrite($config_f, "\$prevent_xss = 'true';\n");
621 chmod($fname.'.new', 0644);
622 rename($fname.'.new', $fname);
624 $fname = $config_dir . '/gitweb.list';
625 $f = fopen($fname.'.new', 'w');
627 $engine = RBACEngine::getInstance();
628 foreach ($list as $project) {
629 $repos = $this->getRepositories($rootdir . "/" . $project->getUnixName());
630 foreach ($repos as $repo) {
631 $reldir = substr($repo, strlen($rootdir) + 1);
632 fwrite($f, $reldir . "\n");
634 $users = $engine->getUsersByAllowedAction('scm',$project->getID(),'write');
636 foreach ($users as $user) {
637 $password_data .= $user->getUnixName().':'.$user->getUnixPasswd()."\n";
639 $faname = forge_get_config('data_path').'/gituser-authfile.'.$project->getUnixName();
640 $fa = fopen($faname.'.new', 'w');
641 fwrite($fa, $password_data);
643 chmod($faname.'.new', 0644);
644 rename($faname.'.new', $faname);
647 chmod($fname.'.new', 0644);
648 rename($fname.'.new', $fname);
651 function getRepositories($path) {
652 if (! is_dir($path)) {
656 $entries = scandir($path);
657 foreach ($entries as $entry) {
658 $fullname = $path . "/" . $entry;
659 if (($entry == ".") or ($entry == ".."))
661 if (is_dir($fullname)) {
662 if (is_link($fullname))
664 $result = $this->getRepositories($fullname);
665 $list = array_merge($list, $result);
666 } elseif ($entry == "HEAD") {
673 function gatherStats ($params) {
674 $project = $this->checkParams ($params) ;
679 if (! $project->usesPlugin ($this->name)) {
683 if ($params['mode'] == 'day') {
684 $year = $params ['year'] ;
685 $month = $params ['month'] ;
686 $day = $params ['day'] ;
687 $month_string = sprintf( "%04d%02d", $year, $month );
688 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
689 $end_time = $start_time + 86400;
691 $usr_adds = array () ;
692 $usr_updates = array () ;
693 $usr_deletes = array () ;
698 $repo = forge_get_config('repos_path', 'scmgit') . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git';
699 if (!is_dir ($repo) || !is_dir ("$repo/refs")) {
700 // echo "No repository $repo\n" ;
704 $pipe = popen ("GIT_DIR=\"$repo\" git log --since=@$start_time --until=@$end_time --all --pretty='format:%n%an <%ae>' --name-status 2>/dev/null", 'r' ) ;
708 // cleaning stats_cvs_* table for the current day
709 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
710 array ($month_string,
712 $project->getID())) ;
714 echo "Error while cleaning stats_cvs_group\n" ;
720 while (!feof($pipe) && $data = fgets ($pipe)) {
722 if (strlen($line) > 0) {
723 $result = preg_match("/^(?P<name>.+) <(?P<mail>.+)>/", $line, $matches);
726 $last_user = $matches['name'];
727 $user2email[$last_user] = strtolower($matches['mail']);
728 if (!isset($usr_adds[$last_user])) {
729 $usr_adds[$last_user] = 0;
730 $usr_updates[$last_user] = 0;
731 $usr_deletes[$last_user] = 0;
734 // Short-commit stats line
735 preg_match("/^(?P<mode>[AM])\s+(?P<file>.+)$/", $line, $matches);
736 if ($last_user == "") continue;
737 if (!isset ($usr_adds[$last_user])) $usr_adds[$last_user] = 0;
738 if (!isset ($usr_updates[$last_user])) $usr_updates[$last_user] = 0;
739 if (!isset ($usr_deletes[$last_user])) $usr_deletes[$last_user] = 0;
740 if ($matches['mode'] == 'A') {
741 $usr_adds[$last_user]++;
743 } elseif ($matches['mode'] == 'M') {
744 $usr_updates[$last_user]++;
746 } elseif ($matches['mode'] == 'D') {
747 $usr_deletes[$last_user]++;
753 // inserting group results in stats_cvs_groups
754 if ($updates > 0 || $adds > 0) {
755 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
756 array ($month_string,
762 echo "Error while inserting into stats_cvs_group\n" ;
768 // building the user list
769 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
771 foreach ( $user_list as $user ) {
772 // Trying to get user id from user name or email
773 $u = &user_get_object_by_name ($user) ;
775 $user_id = $u->getID();
777 $res=db_query_params('SELECT user_id FROM users WHERE lower(realname)=$1 OR email=$2',
778 array (strtolower($user), $user2email[$user]));
779 if ($res && db_numrows($res) > 0) {
780 $user_id = db_result($res,0,'user_id');
786 $uu = isset ($usr_updates[$user]) ? $usr_updates[$user] : 0 ;
787 $ua = isset ($usr_adds[$user]) ? $usr_adds[$user] : 0 ;
788 if ($uu > 0 || $ua > 0) {
789 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
790 array ($month_string,
796 echo "Error while inserting into stats_cvs_user\n" ;
806 function generateSnapshots ($params) {
808 $project = $this->checkParams ($params) ;
813 $group_name = $project->getUnixName() ;
815 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar'.util_get_compressed_file_extension();
816 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar'.util_get_compressed_file_extension();
818 if (! $project->usesPlugin ($this->name)) {
822 if (! $project->enableAnonSCM()) {
823 if (is_file($snapshot)) {
826 if (is_file($tarball)) {
832 // TODO: ideally we generate one snapshot per git repository
833 $toprepo = forge_get_config('repos_path', 'scmgit') ;
834 $repo = $toprepo . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git' ;
836 if (!is_dir ($repo)) {
837 if (is_file($snapshot)) {
840 if (is_file($tarball)) {
846 // Skip empty repo (no HEAD present in repository)
847 $ref = trim(`GIT_DIR=$repo git symbolic-ref HEAD`);
848 if (!file_exists($repo.'/'.$ref)) {
852 $tmp = trim (`mktemp -d`) ;
856 $today = date ('Y-m-d') ;
857 system ("GIT_DIR=\"$repo\" git archive --format=tar --prefix=$group_name-scm-$today/ HEAD |".forge_get_config('compression_method')." > $tmp/snapshot");
858 chmod ("$tmp/snapshot", 0644) ;
859 copy ("$tmp/snapshot", $snapshot) ;
860 unlink ("$tmp/snapshot") ;
862 system ("tar cCf $toprepo - ".$project->getUnixName() ."|".forge_get_config('compression_method')."> $tmp/tarball") ;
863 chmod ("$tmp/tarball", 0644) ;
864 copy ("$tmp/tarball", $tarball) ;
865 unlink ("$tmp/tarball") ;
866 system ("rm -rf $tmp") ;
870 * widgets - 'widgets' hook handler
871 * @param array $params
874 function widgets($params) {
875 require_once 'common/widget/WidgetLayoutManager.class.php';
876 if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_GROUP) {
877 $params['fusionforge_widgets'][] = 'plugin_scmgit_project_latestcommits';
879 if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_USER) {
880 $params['fusionforge_widgets'][] = 'plugin_scmgit_user_myrepositories';
886 * Process the 'widget_instance' hook to create instances of the widgets
887 * @param array $params
889 function myPageBox($params) {
891 $user = UserManager::instance()->getCurrentUser();
892 require_once 'common/widget/WidgetLayoutManager.class.php';
893 if ($params['widget'] == 'plugin_scmgit_user_myrepositories') {
894 require_once $gfplugins.$this->name.'/common/scmgit_Widget_MyRepositories.class.php';
895 $params['instance'] = new scmgit_Widget_MyRepositories(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
899 function weekly(&$params) {
900 $res = db_query_params('SELECT group_id FROM groups WHERE status=$1 AND use_scm=1 ORDER BY group_id DESC',
903 $params['output'] .= 'ScmGit Plugin: Unable to get list of projects using SCM: '.db_error();
907 $params['output'] .= 'ScmGit Plugin: Running "git gc --quiet" on '.db_numrows($res).' repositories.'."\n";
908 while ($row = db_fetch_array($res)) {
909 $project = group_get_object($row['group_id']);
910 if (!$project || !is_object($project)) {
912 } elseif ($project->isError()) {
915 if (!$project->usesPlugin($this->name)) {
919 $project_name = $project->getUnixName();
920 $repo = forge_get_config('repos_path', 'scmgit') . '/' . $project_name . '/' . $project_name .'.git';
923 $params['output'] .= $project_name.': '.`git gc --quiet 2>&1`;
928 function activity($params) {
929 $group_id = $params['group'];
930 $project = group_get_object($group_id);
931 if (! $project->usesPlugin($this->name)) {
934 if (in_array('scmgit', $params['show']) || (count($params['show']) < 1)) {
935 $start_time = $params['begin'];
936 $end_time = $params['end'];
937 $repo = forge_get_config('repos_path', 'scmgit') . '/' . $project->getUnixName() . '/' . $project->getUnixName() . '.git';
938 $pipe = popen("GIT_DIR=\"$repo\" git log --date=raw --since=@$start_time --until=@$end_time --all --pretty='format:%ad||%ae||%s||%h' --name-status", 'r' );
939 while (!feof($pipe) && $data = fgets($pipe)) {
941 $splitedLine = explode('||', $line);
942 if (sizeof($splitedLine) == 4) {
944 $result['section'] = 'scm';
945 $result['group_id'] = $group_id;
946 $result['ref_id'] = 'browser.php?group_id='.$group_id;
947 $result['description'] = $splitedLine[2].' (commit '.$splitedLine[3].')';
948 $userObject = user_get_object_by_email($splitedLine[1]);
949 if (is_a($userObject, 'GFUser')) {
950 $result['realname'] = make_user_link($userObject->getUnixName(), $userObject->getRealName());
952 $result['realname'] = '';
954 $splitedDate = explode(' ', $splitedLine[0]);
955 $result['activity_date'] = $splitedDate[0];
956 $result['subref_id'] = '';
957 $params['results'][] = $result;
961 $params['ids'][] = $this->name;
962 $params['texts'][] = _('Git Commits');
966 function scm_add_repo(&$params) {
967 $project = $this->checkParams($params);
971 if (! $project->usesPlugin ($this->name)) {
975 if (!isset($params['repo_name'])) {
979 if ($params['repo_name'] == $project->getUnixName()) {
980 $params['error_msg'] = _('Cannot create a secondary repository with the same name as the primary');
984 if (! util_is_valid_repository_name($params['repo_name'])) {
985 $params['error_msg'] = _('This repository name is not valid');
989 $result = db_query_params('SELECT count(*) AS count FROM scm_secondary_repos WHERE group_id=$1 AND repo_name = $2 AND plugin_id=$3',
990 array ($params['group_id'],
991 $params['repo_name'],
994 $params['error_msg'] = db_error();
997 if (db_result($result, 0, 'count')) {
998 $params['error_msg'] = sprintf(_('A repository %s already exists'), $params['repo_name']);
1004 if (isset($params['clone'])) {
1005 $url = $params['clone'];
1009 } elseif (preg_match('|^git://|', $url) || preg_match('|^https?://|', $url)) {
1010 // External URLs: OK
1012 } elseif ($url == $project->getUnixName()) {
1014 } elseif (($result = db_query_params('SELECT count(*) AS count FROM scm_secondary_repos WHERE group_id=$1 AND repo_name = $2 AND plugin_id=$3',
1015 array ($project->getID(),
1018 && db_result($result, 0, 'count')) {
1019 // Local repo: try to clone from an existing repo in same project
1023 $params['error_msg'] = _('Invalid URL from which to clone');
1028 if (isset($params['description'])) {
1029 $description = $params['description'];
1031 if ($clone && !$description) {
1032 $description = sprintf(_('Clone of %s'), $params['clone']);
1034 if (!$description) {
1035 $description = "Git repository $params[repo_name] for project ".$project->getUnixName();
1038 $result = db_query_params ('INSERT INTO scm_secondary_repos (group_id, repo_name, description, clone_url, plugin_id) VALUES ($1, $2, $3, $4, $5)',
1039 array ($params['group_id'],
1040 $params['repo_name'],
1045 $params['error_msg'] = db_error();
1049 plugin_hook ("scm_admin_update", $params);
1053 function scm_admin_form(&$params) {
1054 $project = $this->checkParams($params);
1058 if (! $project->usesPlugin ($this->name)) {
1062 session_require_perm('project_admin', $params['group_id']);
1064 $project_name = $project->getUnixName();
1066 $select_repo = '<select name="frontpage">' . "\n";
1067 $result = db_query_params('SELECT repo_name, description, clone_url FROM scm_secondary_repos WHERE group_id=$1 AND next_action = $2 AND plugin_id=$3 ORDER BY repo_name',
1068 array ($params['group_id'],
1069 SCM_EXTRA_REPO_ACTION_UPDATE,
1072 $params['error_msg'] = db_error();
1075 $existing_repos = array();
1076 while($data = db_fetch_array($result)) {
1077 $existing_repos[] = array('repo_name' => $data['repo_name'],
1078 'description' => $data['description'],
1079 'clone_url' => $data['clone_url']);
1081 if (count($existing_repos) == 0) {
1082 printf('<h2>'._('No extra Git repository for project %1$s').'</h2>', $project_name);
1084 $t = sprintf(ngettext('Extra Git repository for project %1$s',
1085 'Extra Git repositories for project %1$s',
1086 count($existing_repos)), $project_name);
1087 print '<h2>'.$t.'</h2>';
1088 print '<table><thead><tr><th>'._('Repository name').'</th><th>'._('Initial repository description').'</th><th>'._('Initial clone URL (if any)').'</th><th>'._('Delete').'</th></tr></thead><tbody>';
1089 foreach ($existing_repos as $repo) {
1090 print "<tr><td><tt>$repo[repo_name]</tt></td><td>$repo[description]</td><td>$repo[clone_url]</td><td>";
1092 <form name="form_delete_repo_<?php echo $repo['repo_name']?>"
1093 action="<?php echo getStringFromServer('PHP_SELF'); ?>" method="post">
1094 <input type="hidden" name="group_id" value="<?php echo $params['group_id'] ?>" />
1095 <input type="hidden" name="delete_repository" value="1" />
1096 <input type="hidden" name="repo_name" value="<?php echo $repo['repo_name']?>" />
1097 <input type="submit" name="submit" value="<?php echo _('Delete') ?>" />
1100 print "</td></tr>\n";
1102 print '</tbody></table>';
1105 printf('<h2>'._('Create new Git repository for project %1$s').'</h2>', $project_name);
1108 <form name="form_create_repo"
1109 action="<?php echo getStringFromServer('PHP_SELF'); ?>" method="post">
1110 <input type="hidden" name="group_id" value="<?php echo $params['group_id'] ?>" />
1111 <input type="hidden" name="create_repository" value="1" />
1112 <p><strong><?php echo _('Repository name:') ?></strong><?php echo utils_requiredField(); ?><br />
1113 <input type="text" required="required" size="20" name="repo_name" value="" /></p>
1114 <p><strong><?php echo _('Description:'); ?></strong><br />
1115 <input type="text" size="60" name="description" value="" /></p>
1116 <p><strong><?php echo _('Initial clone URL (or name of an existing repository in this project; leave empty to start with an empty repository):') ?></strong><br />
1117 <input type="text" size="60" name="clone" value="<?php echo $project_name; ?>" /></p>
1118 <input type="submit" name="cancel" value="<?php echo _('Cancel') ?>" />
1119 <input type="submit" name="submit" value="<?php echo _('Submit') ?>" />
1129 // c-file-style: "bsd"