3 * FusionForge Bazaar plugin
5 * Copyright 2009, Roland Mas
6 * Copyright 2013, Franck Villaume - TrivialDev
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 forge_define_config_item ('default_server', 'scmbzr', forge_get_config ('web_host')) ;
26 forge_define_config_item ('repos_path', 'scmbzr', forge_get_config('chroot').'/scmrepos/bzr') ;
28 class BzrPlugin extends SCMPlugin {
29 function BzrPlugin () {
32 $this->name = 'scmbzr';
33 $this->text = 'Bazaar';
34 $this->hooks[] = 'scm_generate_snapshots' ;
35 $this->hooks[] = 'scm_browser_page';
36 $this->hooks[] = 'scm_update_repolist' ;
37 $this->hooks[] = 'scm_gather_stats' ;
39 $this->main_branch_names = array () ;
40 $this->main_branch_names[] = 'trunk' ;
41 $this->main_branch_names[] = 'master' ;
42 $this->main_branch_names[] = 'main' ;
43 $this->main_branch_names[] = 'head' ;
44 $this->main_branch_names[] = 'HEAD' ;
49 function getDefaultServer() {
50 return forge_get_config('default_server', 'scmbzr') ;
53 function printShortStats ($params) {
54 $project = $this->checkParams ($params) ;
59 if ($project->usesPlugin($this->name) && forge_check_perm('scm', $project->getID(), 'read')) {
60 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
61 array ($project->getID())) ;
62 $commit_num = db_result($result,0,'commits');
63 $add_num = db_result($result,0,'adds');
70 echo ' (Bazaar: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
74 function getBlurb () {
75 return '<p>' . _('Documentation for Bazaar (sometimes referred to as "bzr") is available <a href="http://bazaar-vcs.org/Documentation">here</a>.') . '</p>';
78 function getInstructionsForAnon ($project) {
80 $b .= _('Anonymous Bazaar Access');
83 $b .= _('This project\'s Bazaar repository can be checked out through anonymous access with the following command.');
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() ;
97 $b .= _('Developer Bazaar Access via SSH');
100 $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.');
102 $b .= '<p><tt>bzr checkout bzr+ssh://'.$d.'@' . $this->getBoxForProject($project) . forge_get_config('repos_path', 'scmbzr') .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
105 $b .= _('Developer Bazaar Access via SSH');
108 $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.');
110 $b .= '<p><tt>bzr checkout bzr+ssh://<i>'._('developername').'</i>@' . $this->getBoxForProject($project) . forge_get_config('repos_path', 'scmbzr') .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
115 function getSnapshotPara ($project) {
119 function getBrowserLinkBlock ($project) {
121 $b = $HTML->boxMiddle(_('Bazaar Repository Browser'));
123 $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.');
126 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
127 _('Browse Bazaar Repository')
133 function getStatsBlock ($project) {
137 function printBrowserPage ($params) {
140 $project = $this->checkParams ($params) ;
145 if ($project->usesPlugin ($this->name)) {
146 if ($this->browserDisplayable ($project)) {
147 print '<iframe src="'.util_make_url ("/scm/loggerhead/".$project->getUnixName()).'" frameborder="0" width=100% height=700></iframe>' ;
152 function createOrUpdateRepo ($params) {
153 $project = $this->checkParams ($params) ;
158 if (! $project->usesPlugin ($this->name)) {
162 $project_name = $project->getUnixName();
164 $repo = forge_get_config('repos_path', 'scmbzr') . '/' . $project->getUnixName() ;
165 $unix_group = 'scm_' . $project_name ;
167 $repo_exists = false ;
168 if (is_dir ($repo)) {
169 $pipe = popen ("bzr info $repo 2>/dev/null", "r") ;
170 $line = fgets ($pipe) ;
173 if (preg_match ("/^Shared repository/", $line) != 0) {
174 $repo_exists = true ;
179 $tmp_repo = util_mkdtemp('.bzr', $project_name);
180 if ($tmp_repo == false) {
184 system ("bzr init-repo --no-trees $tmp_repo >/dev/null") ;
185 system ("find $tmp_repo/.bzr -type d | xargs chmod g+s") ;
186 system ("chmod -R g+wX,o+rX-w $tmp_repo/.bzr") ;
187 system ("chgrp $unix_group $tmp_repo/.bzr") ;
189 system ("mkdir -p $repo") ;
190 system ("chgrp $unix_group $repo") ;
191 system ("chmod g+s $repo") ;
192 system ("mv $tmp_repo/.bzr $repo/.bzr");
196 if ($project->enableAnonSCM()) {
197 system ("chmod o+rX-w $repo") ;
199 system ("chmod o-rwx $repo") ;
203 function updateRepositoryList ($params) {
204 $groups = $this->getGroups () ;
206 $dir = '/var/lib/gforge/plugins/scmbzr/public-repositories' ;
208 $oldlist = array () ;
209 $dh = opendir ($dir) ;
210 while (($file = readdir($dh)) !== false) {
211 if ($file != '.' && $file != '..') {
218 $newlist = array () ;
219 foreach ($groups as $project) {
220 if ($this->browserDisplayable ($project)) {
221 $newlist[] = $project->getUnixName() ;
226 $dellist = array () ;
227 $createlist = array () ;
229 while (count ($oldlist) > 0 && count ($newlist) > 0) {
233 $createlist[] = array_shift ($newlist) ;
235 $dellist[] = array_shift ($oldlist) ;
237 array_shift ($newlist) ;
238 array_shift ($oldlist) ;
241 $dellist = array_merge ($dellist, $oldlist) ;
242 $createlist = array_merge ($createlist, $newlist) ;
244 foreach ($dellist as $del) {
245 unlink ($dir . '/' . $del) ;
247 foreach ($createlist as $create) {
248 symlink (forge_get_config('repos_path', 'scmbzr') . '/' . $create, $dir . '/' . $create) ;
252 function gatherStats ($params) {
253 $project = $this->checkParams ($params) ;
258 if (! $project->usesPlugin ($this->name)) {
262 if ($params['mode'] == 'day') {
265 $year = $params ['year'] ;
266 $month = $params ['month'] ;
267 $day = $params ['day'] ;
268 $month_string = sprintf( "%04d%02d", $year, $month );
269 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
270 $end_time = $start_time + 86400;
272 $date = sprintf ("%04d-%02d-%02d", $year, $month, $day);
276 $usr_updates = array () ;
277 $usr_adds = array () ;
279 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
280 $repo = $toprepo . '/' . $project->getUnixName() ;
282 $branch = $this->findMainBranch ($project) ;
289 $pipe = popen ("bzr log file://$repo/$branch --long --verbose 2> /dev/null", 'r' ) ;
291 // cleaning stats_cvs_* table for the current day
292 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
293 array ($month_string,
295 $project->getID())) ;
297 echo "Error while cleaning stats_cvs_group\n" ;
302 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
303 array ($month_string,
305 $project->getID())) ;
307 echo "Error while cleaning stats_cvs_user\n" ;
312 // Analyzing history stream
313 $sep = '------------------------------------------------------------' ;
320 while (! feof ($pipe) &&
321 $line = rtrim (fgets ($pipe))) {
323 if ($curdate == $date) {
324 $adds = $adds + $curadds ;
325 $updates = $updates + $curupdates ;
327 if ($curdate != '' && $curdate < $date) {
337 } elseif (preg_match( '/^revno: ([0-9]+)$/', $line, $matches)) {
338 $currev = $matches[1] ;
339 } elseif (preg_match( '/^committer: (.*)$/', $line, $matches)) {
340 $curuser = $matches[1] ;
341 } elseif (preg_match( '/^timestamp: ... (\d\d\d\d-\d\d-\d\d)/', $line, $matches)) {
342 $curdate = $matches[1] ;
343 } elseif (preg_match( '/^modified:/', $line, $matches)) {
344 $state = 'modified' ;
345 } elseif (preg_match( '/^renamed:/', $line, $matches)) {
347 } elseif (preg_match( '/^removed:/', $line, $matches)) {
349 } elseif (preg_match( '/^added/', $line, $matches)) {
362 if ($curdate == $date) {
363 $adds = $adds + $curadds ;
364 $updates = $updates + $curupdates ;
367 // inserting group results in stats_cvs_groups
368 if ($updates > 0 || $adds > 0) {
369 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
370 array ($month_string,
376 echo "Error while inserting into stats_cvs_group\n" ;
382 // building the user list
383 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
385 foreach ( $user_list as $user ) {
386 // trying to get user id from user name
387 $u = &user_get_object_by_name ($user) ;
389 $user_id = $u->getID();
394 $uu = $usr_updates[$user] ? $usr_updates[$user] : 0 ;
395 $ua = $usr_adds[$user] ? $usr_adds[$user] : 0 ;
396 if ($uu > 0 || $ua > 0) {
397 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
398 array ($month_string,
404 echo "Error while inserting into stats_cvs_user\n" ;
414 function findMainBranch ($project) {
415 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
416 $repo = $toprepo . '/' . $project->getUnixName() ;
420 foreach ($this->main_branch_names as $bname) {
421 system ("bzr ls file://$repo/$bname > /dev/null 2>&1", $code) ;
430 function generateSnapshots ($params) {
434 $project = $this->checkParams ($params) ;
439 $group_name = $project->getUnixName() ;
441 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar'.util_get_compressed_file_extension();
442 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar'.util_get_compressed_file_extension();
444 if (! $project->usesPlugin ($this->name)) {
448 if (! $project->enableAnonSCM()) {
454 $toprepo = forge_get_config('repos_path', 'scmbzr') ;
455 $repo = $toprepo . '/' . $project->getUnixName() ;
457 if (!is_dir ($repo) || !is_file ("$repo/format")) {
463 $tmp = trim (`mktemp -d`) ;
467 $today = date ('Y-m-d') ;
468 $branch = $this->findMainBranch ($project) ;
470 system ("bzr export --root=$group_name-scm-$today --format=tar - $repo/$bname |".forge_get_config('compression_method')."> $tmp/snapshot") ;
471 chmod ("$tmp/snapshot", 0644) ;
472 copy ("$tmp/snapshot", $snapshot) ;
473 unlink ("$tmp/snapshot") ;
474 system ("rm -rf $tmp/$dir") ;
479 system ("tar cCf $toprepo - ".$project->getUnixName() ."|".forge_get_config('compression_method')."> $tmp/tarball") ;
480 chmod ("$tmp/tarball", 0644) ;
481 copy ("$tmp/tarball", $tarball) ;
482 unlink ("$tmp/tarball") ;
483 system ("rm -rf $tmp") ;
489 // c-file-style: "bsd"