2 /** FusionForge Bazaar 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 BzrPlugin extends SCMPlugin {
25 function BzrPlugin () {
28 $this->name = 'scmbzr';
29 $this->text = 'Bazaar';
30 $this->hooks[] = 'scm_generate_snapshots' ;
31 $this->hooks[] = 'scm_browser_page';
32 $this->hooks[] = 'scm_update_repolist' ;
33 $this->hooks[] = 'scm_gather_stats' ;
35 require_once $gfconfig.'plugins/scmbzr/config.php' ;
37 $this->default_bzr_server = $default_bzr_server ;
38 if (isset ($bzr_root)) {
39 $this->bzr_root = $bzr_root;
41 $this->bzr_root = forge_get_config('chroot').'/scmrepos/bzr' ;
44 $this->main_branch_names = array () ;
45 $this->main_branch_names[] = 'trunk' ;
46 $this->main_branch_names[] = 'master' ;
47 $this->main_branch_names[] = 'main' ;
48 $this->main_branch_names[] = 'head' ;
49 $this->main_branch_names[] = 'HEAD' ;
54 function getDefaultServer() {
55 return $this->default_bzr_server ;
58 function printShortStats ($params) {
59 $project = $this->checkParams ($params) ;
64 if ($project->usesPlugin($this->name)) {
65 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
66 array ($project->getID())) ;
67 $commit_num = db_result($result,0,'commits');
68 $add_num = db_result($result,0,'adds');
75 echo ' (Bazaar: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
79 function getBlurb () {
80 return _('<p>Documentation for Bazaar (sometimes referred to as "bzr") is available <a href="http://bazaar-vcs.org/Documentation">here</a>.</p>') ;
83 function getInstructionsForAnon ($project) {
84 $b = _('<p><b>Anonymous Bazaar Access</b></p><p>This project\'s Bazaar repository can be checked out through anonymous access with the following command.</p>');
86 $b .= '<tt>bzr checkout '.util_make_url ('/anonscm/bzr/'.$project->getUnixName().'/').'</tt><br />';
91 function getInstructionsForRW ($project) {
93 if (session_loggedin()) {
94 $u =& user_get_object(user_getid()) ;
95 $d = $u->getUnixName() ;
96 $b .= _('<p><b>Developer Bazaar Access via SSH</b></p><p>Only project developers can access the Bazaar branches via this method. SSH must be installed on your client machine. Enter your site password when prompted.</p>');
97 $b .= '<p><tt>bzr checkout bzr+ssh://'.$d.'@' . $project->getSCMBox() . $this->bzr_root .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
99 $b .= _('<p><b>Developer Bazaar Access via SSH</b></p><p>Only project developers can access the Bazaar branches 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>');
100 $b .= '<p><tt>bzr checkout bzr+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . $this->bzr_root .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
105 function getSnapshotPara ($project) {
109 function getBrowserLinkBlock ($project) {
111 $b = $HTML->boxMiddle(_('Bazaar Repository Browser'));
112 $b .= _('<p>Browsing the Bazaar 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>');
114 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
115 _('Browse Bazaar Repository')
121 function getStatsBlock ($project) {
125 function printBrowserPage ($params) {
128 $project = $this->checkParams ($params) ;
133 if ($project->usesPlugin ($this->name)) {
134 if ($this->browserDisplayable ($project)) {
135 print '<iframe src="'.util_make_url ("/scm/loggerhead/".$project->getUnixName()).'" frameborder="no" width=100% height=700></iframe>' ;
140 function createOrUpdateRepo ($params) {
141 $project = $this->checkParams ($params) ;
146 if (! $project->usesPlugin ($this->name)) {
150 $repo = $this->bzr_root . '/' . $project->getUnixName() ;
151 $unix_group = 'scm_' . $project->getUnixName() ;
153 $repo_exists = false ;
154 if (is_dir ($repo)) {
155 $pipe = popen ("bzr info $repo 2>/dev/null", "r") ;
156 $line = fgets ($pipe) ;
159 if (preg_match ("/^Shared repository/", $line) != 0) {
160 $repo_exists = true ;
165 system ("mkdir -p $repo") ;
166 system ("bzr init-repo --no-trees $repo >/dev/null") ;
167 system ("find $repo -type d | xargs chmod g+s") ;
170 system ("chgrp -R $unix_group $repo") ;
171 if ($project->enableAnonSCM()) {
172 system ("chmod -R g+wX,o+rX-w $repo") ;
174 system ("chmod -R g+wX,o-rwx $repo") ;
178 function updateRepositoryList ($params) {
179 $groups = $this->getGroups () ;
181 $dir = '/var/lib/gforge/plugins/scmbzr/public-repositories' ;
183 $oldlist = array () ;
184 $dh = opendir ($dir) ;
185 while (($file = readdir($dh)) !== false) {
186 if ($file != '.' && $file != '..') {
193 $newlist = array () ;
194 foreach ($groups as $project) {
195 if ($this->browserDisplayable ($project)) {
196 $newlist[] = $project->getUnixName() ;
201 $dellist = array () ;
202 $createlist = array () ;
204 while (count ($oldlist) > 0 && count ($newlist) > 0) {
208 $createlist[] = array_shift ($newlist) ;
210 $dellist[] = array_shift ($oldlist) ;
212 array_shift ($newlist) ;
213 array_shift ($oldlist) ;
216 $dellist = array_merge ($dellist, $oldlist) ;
217 $createlist = array_merge ($createlist, $newlist) ;
219 foreach ($dellist as $del) {
220 unlink ($dir . '/' . $del) ;
222 foreach ($createlist as $create) {
223 symlink ($this->bzr_root . '/' . $create, $dir . '/' . $create) ;
227 function gatherStats ($params) {
228 $project = $this->checkParams ($params) ;
233 if (! $project->usesPlugin ($this->name)) {
237 if ($params['mode'] == 'day') {
240 $year = $params ['year'] ;
241 $month = $params ['month'] ;
242 $day = $params ['day'] ;
243 $month_string = sprintf( "%04d%02d", $year, $month );
244 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
245 $end_time = $start_time + 86400;
247 $date = sprintf ("%04d-%02d-%02d", $year, $month, $day);
251 $usr_updates = array () ;
252 $usr_adds = array () ;
254 $toprepo = $this->bzr_root ;
255 $repo = $toprepo . '/' . $project->getUnixName() ;
257 $branch = $this->findMainBranch ($project) ;
264 $pipe = popen ("bzr log file://$repo/$branch --long --verbose 2> /dev/null", 'r' ) ;
266 // cleaning stats_cvs_* table for the current day
267 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
268 array ($month_string,
270 $project->getID())) ;
272 echo "Error while cleaning stats_cvs_group\n" ;
277 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
278 array ($month_string,
280 $project->getID())) ;
282 echo "Error while cleaning stats_cvs_user\n" ;
287 // Analyzing history stream
288 $sep = '------------------------------------------------------------' ;
295 while (! feof ($pipe) &&
296 $line = rtrim (fgets ($pipe))) {
298 if ($curdate == $date) {
299 $adds = $adds + $curadds ;
300 $updates = $updates + $curupdates ;
302 if ($curdate != '' && $curdate < $date) {
312 } elseif (preg_match( '/^revno: ([0-9]+)$/', $line, $matches)) {
313 $currev = $matches[1] ;
314 } elseif (preg_match( '/^committer: (.*)$/', $line, $matches)) {
315 $curuser = $matches[1] ;
316 } elseif (preg_match( '/^timestamp: ... (\d\d\d\d-\d\d-\d\d)/', $line, $matches)) {
317 $curdate = $matches[1] ;
318 } elseif (preg_match( '/^modified:/', $line, $matches)) {
319 $state = 'modified' ;
320 } elseif (preg_match( '/^renamed:/', $line, $matches)) {
322 } elseif (preg_match( '/^removed:/', $line, $matches)) {
324 } elseif (preg_match( '/^added/', $line, $matches)) {
337 if ($curdate == $date) {
338 $adds = $adds + $curadds ;
339 $updates = $updates + $curupdates ;
342 // inserting group results in stats_cvs_groups
343 if ($updates > 0 || $adds > 0) {
344 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
345 array ($month_string,
351 echo "Error while inserting into stats_cvs_group\n" ;
357 // building the user list
358 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
360 foreach ( $user_list as $user ) {
361 // trying to get user id from user name
362 $u = &user_get_object_by_name ($user) ;
364 $user_id = $u->getID();
369 $uu = $usr_updates[$user] ? $usr_updates[$user] : 0 ;
370 $ua = $usr_adds[$user] ? $usr_adds[$user] : 0 ;
371 if ($uu > 0 || $ua > 0) {
372 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
373 array ($month_string,
379 echo "Error while inserting into stats_cvs_user\n" ;
389 function findMainBranch ($project) {
390 $toprepo = $this->bzr_root ;
391 $repo = $toprepo . '/' . $project->getUnixName() ;
395 foreach ($this->main_branch_names as $bname) {
396 system ("bzr ls file://$repo/$bname > /dev/null 2>&1", $code) ;
405 function generateSnapshots ($params) {
409 $project = $this->checkParams ($params) ;
414 $group_name = $project->getUnixName() ;
416 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar.gz';
417 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar.gz';
419 if (! $project->usesPlugin ($this->name)) {
423 if (! $project->enableAnonSCM()) {
429 $toprepo = $this->bzr_root ;
430 $repo = $toprepo . '/' . $project->getUnixName() ;
432 if (!is_dir ($repo) || !is_file ("$repo/format")) {
438 $tmp = trim (`mktemp -d`) ;
442 $today = date ('Y-m-d') ;
443 $branch = $this->findMainBranch ($project) ;
445 system ("bzr export --root=$group_name-scm-$today $tmp/snapshot.tar.gz $repo/$bname") ;
446 chmod ("$tmp/snapshot.tar.gz", 0644) ;
447 copy ("$tmp/snapshot.tar.gz", $snapshot) ;
448 unlink ("$tmp/snapshot.tar.gz") ;
449 system ("rm -rf $tmp/$dir") ;
454 system ("tar czCf $toprepo $tmp/tarball.tar.gz " . $project->getUnixName()) ;
455 chmod ("$tmp/tarball.tar.gz", 0644) ;
456 copy ("$tmp/tarball.tar.gz", $tarball) ;
457 unlink ("$tmp/tarball.tar.gz") ;
458 system ("rm -rf $tmp") ;
464 // c-file-style: "bsd"