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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 forge_define_config_item ('default_server', 'scmbzr', forge_get_config ('web_host')) ;
24 forge_define_config_item ('repos_path', 'scmbzr', forge_get_config('chroot').'/scmrepos/bzr') ;
26 class BzrPlugin extends SCMPlugin {
27 function BzrPlugin () {
30 $this->name = 'scmbzr';
31 $this->text = 'Bazaar';
32 $this->hooks[] = 'scm_generate_snapshots' ;
33 $this->hooks[] = 'scm_browser_page';
34 $this->hooks[] = 'scm_update_repolist' ;
35 $this->hooks[] = 'scm_gather_stats' ;
37 $this->main_branch_names = array () ;
38 $this->main_branch_names[] = 'trunk' ;
39 $this->main_branch_names[] = 'master' ;
40 $this->main_branch_names[] = 'main' ;
41 $this->main_branch_names[] = 'head' ;
42 $this->main_branch_names[] = 'HEAD' ;
47 function getDefaultServer() {
48 return forge_get_config('default_server', 'scmbzr') ;
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 ' (Bazaar: '.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 Bazaar (sometimes referred to as "bzr") is available <a href="http://bazaar-vcs.org/Documentation">here</a>.') . '</p>';
76 function getInstructionsForAnon ($project) {
78 $b .= _('Anonymous Bazaar Access');
81 $b .= _('This project\'s Bazaar repository can be checked out through anonymous access with the following command.');
84 $b .= '<tt>bzr checkout '.util_make_url ('/anonscm/bzr/'.$project->getUnixName().'/').'</tt><br />';
89 function getInstructionsForRW ($project) {
91 if (session_loggedin()) {
92 $u =& user_get_object(user_getid()) ;
93 $d = $u->getUnixName() ;
95 $b .= _('Developer Bazaar Access via SSH');
98 $b .= _('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.');
100 $b .= '<p><tt>bzr checkout bzr+ssh://'.$d.'@' . $this->getBoxForProject($project) . forge_get_config('repos_path', 'scmbzr') .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
103 $b .= _('Developer Bazaar Access via SSH');
106 $b .= _('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.');
108 $b .= '<p><tt>bzr checkout bzr+ssh://<i>'._('developername').'</i>@' . $this->getBoxForProject($project) . forge_get_config('repos_path', 'scmbzr') .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
113 function getSnapshotPara ($project) {
117 function getBrowserLinkBlock ($project) {
119 $b = $HTML->boxMiddle(_('Bazaar Repository Browser'));
121 $b .= _('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.');
124 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
125 _('Browse Bazaar Repository')
131 function getStatsBlock ($project) {
135 function printBrowserPage ($params) {
138 $project = $this->checkParams ($params) ;
143 if ($project->usesPlugin ($this->name)) {
144 if ($this->browserDisplayable ($project)) {
145 print '<iframe src="'.util_make_url ("/scm/loggerhead/".$project->getUnixName()).'" frameborder="0" width=100% height=700></iframe>' ;
150 function createOrUpdateRepo ($params) {
151 $project = $this->checkParams ($params) ;
156 if (! $project->usesPlugin ($this->name)) {
160 $project_name = $project->getUnixName();
162 $repo = forge_get_config('repos_path', 'scmbzr') . '/' . $project->getUnixName() ;
163 $unix_group = 'scm_' . $project_name ;
165 $repo_exists = false ;
166 if (is_dir ($repo)) {
167 $pipe = popen ("bzr info $repo 2>/dev/null", "r") ;
168 $line = fgets ($pipe) ;
171 if (preg_match ("/^Shared repository/", $line) != 0) {
172 $repo_exists = true ;
177 $tmp_repo = util_mkdtemp('.bzr', $project_name);
178 if ($tmp_repo == false) {
182 system ("bzr init-repo --no-trees $tmp_repo >/dev/null") ;
183 system ("find $tmp_repo/.bzr -type d | xargs chmod g+s") ;
184 system ("chmod -R g+wX,o+rX-w $tmp_repo/.bzr") ;
185 system ("chgrp $unix_group $tmp_repo/.bzr") ;
187 system ("mkdir -p $repo") ;
188 system ("chgrp $unix_group $repo") ;
189 system ("chmod g+s $repo") ;
190 system ("mv $tmp_repo/.bzr $repo/.bzr");
194 if ($project->enableAnonSCM()) {
195 system ("chmod o+rX-w $repo") ;
197 system ("chmod o-rwx $repo") ;
201 function updateRepositoryList ($params) {
202 $groups = $this->getGroups () ;
204 $dir = '/var/lib/gforge/plugins/scmbzr/public-repositories' ;
206 $oldlist = array () ;
207 $dh = opendir ($dir) ;
208 while (($file = readdir($dh)) !== false) {
209 if ($file != '.' && $file != '..') {
216 $newlist = array () ;
217 foreach ($groups as $project) {
218 if ($this->browserDisplayable ($project)) {
219 $newlist[] = $project->getUnixName() ;
224 $dellist = array () ;
225 $createlist = array () ;
227 while (count ($oldlist) > 0 && count ($newlist) > 0) {
231 $createlist[] = array_shift ($newlist) ;
233 $dellist[] = array_shift ($oldlist) ;
235 array_shift ($newlist) ;
236 array_shift ($oldlist) ;
239 $dellist = array_merge ($dellist, $oldlist) ;
240 $createlist = array_merge ($createlist, $newlist) ;
242 foreach ($dellist as $del) {
243 unlink ($dir . '/' . $del) ;
245 foreach ($createlist as $create) {
246 symlink (forge_get_config('repos_path', 'scmbzr') . '/' . $create, $dir . '/' . $create) ;
250 function gatherStats ($params) {
251 $project = $this->checkParams ($params) ;
256 if (! $project->usesPlugin ($this->name)) {
260 if ($params['mode'] == 'day') {
263 $year = $params ['year'] ;
264 $month = $params ['month'] ;
265 $day = $params ['day'] ;
266 $month_string = sprintf( "%04d%02d", $year, $month );
267 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
268 $end_time = $start_time + 86400;
270 $date = sprintf ("%04d-%02d-%02d", $year, $month, $day);
274 $usr_updates = array () ;
275 $usr_adds = array () ;
277 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
278 $repo = $toprepo . '/' . $project->getUnixName() ;
280 $branch = $this->findMainBranch ($project) ;
287 $pipe = popen ("bzr log file://$repo/$branch --long --verbose 2> /dev/null", 'r' ) ;
289 // cleaning stats_cvs_* table for the current day
290 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
291 array ($month_string,
293 $project->getID())) ;
295 echo "Error while cleaning stats_cvs_group\n" ;
300 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
301 array ($month_string,
303 $project->getID())) ;
305 echo "Error while cleaning stats_cvs_user\n" ;
310 // Analyzing history stream
311 $sep = '------------------------------------------------------------' ;
318 while (! feof ($pipe) &&
319 $line = rtrim (fgets ($pipe))) {
321 if ($curdate == $date) {
322 $adds = $adds + $curadds ;
323 $updates = $updates + $curupdates ;
325 if ($curdate != '' && $curdate < $date) {
335 } elseif (preg_match( '/^revno: ([0-9]+)$/', $line, $matches)) {
336 $currev = $matches[1] ;
337 } elseif (preg_match( '/^committer: (.*)$/', $line, $matches)) {
338 $curuser = $matches[1] ;
339 } elseif (preg_match( '/^timestamp: ... (\d\d\d\d-\d\d-\d\d)/', $line, $matches)) {
340 $curdate = $matches[1] ;
341 } elseif (preg_match( '/^modified:/', $line, $matches)) {
342 $state = 'modified' ;
343 } elseif (preg_match( '/^renamed:/', $line, $matches)) {
345 } elseif (preg_match( '/^removed:/', $line, $matches)) {
347 } elseif (preg_match( '/^added/', $line, $matches)) {
360 if ($curdate == $date) {
361 $adds = $adds + $curadds ;
362 $updates = $updates + $curupdates ;
365 // inserting group results in stats_cvs_groups
366 if ($updates > 0 || $adds > 0) {
367 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
368 array ($month_string,
374 echo "Error while inserting into stats_cvs_group\n" ;
380 // building the user list
381 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
383 foreach ( $user_list as $user ) {
384 // trying to get user id from user name
385 $u = &user_get_object_by_name ($user) ;
387 $user_id = $u->getID();
392 $uu = $usr_updates[$user] ? $usr_updates[$user] : 0 ;
393 $ua = $usr_adds[$user] ? $usr_adds[$user] : 0 ;
394 if ($uu > 0 || $ua > 0) {
395 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
396 array ($month_string,
402 echo "Error while inserting into stats_cvs_user\n" ;
412 function findMainBranch ($project) {
413 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
414 $repo = $toprepo . '/' . $project->getUnixName() ;
418 foreach ($this->main_branch_names as $bname) {
419 system ("bzr ls file://$repo/$bname > /dev/null 2>&1", $code) ;
428 function generateSnapshots ($params) {
432 $project = $this->checkParams ($params) ;
437 $group_name = $project->getUnixName() ;
439 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar'.util_get_compressed_file_extension();
440 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar'.util_get_compressed_file_extension();
442 if (! $project->usesPlugin ($this->name)) {
446 if (! $project->enableAnonSCM()) {
452 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
453 $repo = $toprepo . '/' . $project->getUnixName() ;
455 if (!is_dir ($repo) || !is_file ("$repo/format")) {
461 $tmp = trim (`mktemp -d`) ;
465 $today = date ('Y-m-d') ;
466 $branch = $this->findMainBranch ($project) ;
468 system ("bzr export --root=$group_name-scm-$today --format=tar - $repo/$bname |".forge_get_config('compression_method')."> $tmp/snapshot") ;
469 chmod ("$tmp/snapshot", 0644) ;
470 copy ("$tmp/snapshot", $snapshot) ;
471 unlink ("$tmp/snapshot") ;
472 system ("rm -rf $tmp/$dir") ;
477 system ("tar cCf $toprepo - ".$project->getUnixName() ."|".forge_get_config('compression_method')."> $tmp/tarball") ;
478 chmod ("$tmp/tarball", 0644) ;
479 copy ("$tmp/tarball", $tarball) ;
480 unlink ("$tmp/tarball") ;
481 system ("rm -rf $tmp") ;
487 // c-file-style: "bsd"