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 $repo = forge_get_config('repos_path', 'scmbzr') . '/' . $project->getUnixName() ;
161 $unix_group = 'scm_' . $project->getUnixName() ;
163 $repo_exists = false ;
164 if (is_dir ($repo)) {
165 $pipe = popen ("bzr info $repo 2>/dev/null", "r") ;
166 $line = fgets ($pipe) ;
169 if (preg_match ("/^Shared repository/", $line) != 0) {
170 $repo_exists = true ;
175 system ("mkdir -p $repo") ;
176 system ("bzr init-repo --no-trees $repo >/dev/null") ;
177 system ("find $repo -type d | xargs chmod g+s") ;
180 system ("chgrp -R $unix_group $repo") ;
181 if ($project->enableAnonSCM()) {
182 system ("chmod -R g+wX,o+rX-w $repo") ;
184 system ("chmod -R g+wX,o-rwx $repo") ;
188 function updateRepositoryList ($params) {
189 $groups = $this->getGroups () ;
191 $dir = '/var/lib/gforge/plugins/scmbzr/public-repositories' ;
193 $oldlist = array () ;
194 $dh = opendir ($dir) ;
195 while (($file = readdir($dh)) !== false) {
196 if ($file != '.' && $file != '..') {
203 $newlist = array () ;
204 foreach ($groups as $project) {
205 if ($this->browserDisplayable ($project)) {
206 $newlist[] = $project->getUnixName() ;
211 $dellist = array () ;
212 $createlist = array () ;
214 while (count ($oldlist) > 0 && count ($newlist) > 0) {
218 $createlist[] = array_shift ($newlist) ;
220 $dellist[] = array_shift ($oldlist) ;
222 array_shift ($newlist) ;
223 array_shift ($oldlist) ;
226 $dellist = array_merge ($dellist, $oldlist) ;
227 $createlist = array_merge ($createlist, $newlist) ;
229 foreach ($dellist as $del) {
230 unlink ($dir . '/' . $del) ;
232 foreach ($createlist as $create) {
233 symlink (forge_get_config('repos_path', 'scmbzr') . '/' . $create, $dir . '/' . $create) ;
237 function gatherStats ($params) {
238 $project = $this->checkParams ($params) ;
243 if (! $project->usesPlugin ($this->name)) {
247 if ($params['mode'] == 'day') {
250 $year = $params ['year'] ;
251 $month = $params ['month'] ;
252 $day = $params ['day'] ;
253 $month_string = sprintf( "%04d%02d", $year, $month );
254 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
255 $end_time = $start_time + 86400;
257 $date = sprintf ("%04d-%02d-%02d", $year, $month, $day);
261 $usr_updates = array () ;
262 $usr_adds = array () ;
264 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
265 $repo = $toprepo . '/' . $project->getUnixName() ;
267 $branch = $this->findMainBranch ($project) ;
274 $pipe = popen ("bzr log file://$repo/$branch --long --verbose 2> /dev/null", 'r' ) ;
276 // cleaning stats_cvs_* table for the current day
277 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
278 array ($month_string,
280 $project->getID())) ;
282 echo "Error while cleaning stats_cvs_group\n" ;
287 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
288 array ($month_string,
290 $project->getID())) ;
292 echo "Error while cleaning stats_cvs_user\n" ;
297 // Analyzing history stream
298 $sep = '------------------------------------------------------------' ;
305 while (! feof ($pipe) &&
306 $line = rtrim (fgets ($pipe))) {
308 if ($curdate == $date) {
309 $adds = $adds + $curadds ;
310 $updates = $updates + $curupdates ;
312 if ($curdate != '' && $curdate < $date) {
322 } elseif (preg_match( '/^revno: ([0-9]+)$/', $line, $matches)) {
323 $currev = $matches[1] ;
324 } elseif (preg_match( '/^committer: (.*)$/', $line, $matches)) {
325 $curuser = $matches[1] ;
326 } elseif (preg_match( '/^timestamp: ... (\d\d\d\d-\d\d-\d\d)/', $line, $matches)) {
327 $curdate = $matches[1] ;
328 } elseif (preg_match( '/^modified:/', $line, $matches)) {
329 $state = 'modified' ;
330 } elseif (preg_match( '/^renamed:/', $line, $matches)) {
332 } elseif (preg_match( '/^removed:/', $line, $matches)) {
334 } elseif (preg_match( '/^added/', $line, $matches)) {
347 if ($curdate == $date) {
348 $adds = $adds + $curadds ;
349 $updates = $updates + $curupdates ;
352 // inserting group results in stats_cvs_groups
353 if ($updates > 0 || $adds > 0) {
354 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
355 array ($month_string,
361 echo "Error while inserting into stats_cvs_group\n" ;
367 // building the user list
368 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
370 foreach ( $user_list as $user ) {
371 // trying to get user id from user name
372 $u = &user_get_object_by_name ($user) ;
374 $user_id = $u->getID();
379 $uu = $usr_updates[$user] ? $usr_updates[$user] : 0 ;
380 $ua = $usr_adds[$user] ? $usr_adds[$user] : 0 ;
381 if ($uu > 0 || $ua > 0) {
382 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
383 array ($month_string,
389 echo "Error while inserting into stats_cvs_user\n" ;
399 function findMainBranch ($project) {
400 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
401 $repo = $toprepo . '/' . $project->getUnixName() ;
405 foreach ($this->main_branch_names as $bname) {
406 system ("bzr ls file://$repo/$bname > /dev/null 2>&1", $code) ;
415 function generateSnapshots ($params) {
419 $project = $this->checkParams ($params) ;
424 $group_name = $project->getUnixName() ;
426 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar'.util_get_compressed_file_extension();
427 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar'.util_get_compressed_file_extension();
429 if (! $project->usesPlugin ($this->name)) {
433 if (! $project->enableAnonSCM()) {
439 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
440 $repo = $toprepo . '/' . $project->getUnixName() ;
442 if (!is_dir ($repo) || !is_file ("$repo/format")) {
448 $tmp = trim (`mktemp -d`) ;
452 $today = date ('Y-m-d') ;
453 $branch = $this->findMainBranch ($project) ;
455 system ("bzr export --root=$group_name-scm-$today --format=tar - $repo/$bname |".forge_get_config('compression_method')."> $tmp/snapshot") ;
456 chmod ("$tmp/snapshot", 0644) ;
457 copy ("$tmp/snapshot", $snapshot) ;
458 unlink ("$tmp/snapshot") ;
459 system ("rm -rf $tmp/$dir") ;
464 system ("tar cCf $toprepo - ".$project->getUnixName() ."|".forge_get_config('compression_method')."> $tmp/tarball") ;
465 chmod ("$tmp/tarball", 0644) ;
466 copy ("$tmp/tarball", $tarball) ;
467 unlink ("$tmp/tarball") ;
468 system ("rm -rf $tmp") ;
474 // c-file-style: "bsd"