6 * The rest Copyright 2002-2005 (c) GForge Team
9 * This file is part of GForge.
11 * GForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 require dirname(__FILE__).'/../../env.inc.php';
27 require_once $gfwww.'include/pre.php';
28 require $gfcommon.'include/cron_utils.php';
29 require_once $gfcommon.'mail/MailingList.class.php';
30 require_once $gfcommon.'mail/MailingListFactory.class.php';
32 require_once $gfcommon.'include/SCMPlugin.class.php' ;
36 setup_plugin_manager () ;
39 // Some OSs do not allow root to do a commit
40 // so having this script do a proper checkout/commit is not possible
42 $scmcvs_proper_commit_loginfo=false;
45 * Retrieve a file into a temporary directory from a CVS server
47 * @param String $repos Repository Name
48 * @param String $file File Name
50 * return String the FileName in the working repository
52 function checkout_cvs_file($repos,$file) {
53 //echo "$repos,$file\n";
54 global $scmcvs_proper_commit_loginfo;
55 if (!$scmcvs_proper_commit_loginfo) {
56 return $repos.'/'.$file;
58 $actual_dir = getcwd();
59 $tempdirname = tempnam("/tmp","cvstracker");
62 if (!unlink($tempdirname))
65 // Create the temporary directory and returns its name.
66 if (!mkdir($tempdirname))
69 if (!chdir($tempdirname))
71 system("cvs -d ".$repos." co ".$file);
74 return $tempdirname."/".$file;
78 * commit_cvs_file commit a file to the repository
80 * @param String $repos Repository
81 * @param String $file to commit
82 * @param String $message to commit
84 function commit_cvs_file($repos,$file,$message="Automatic updated by cvstracker") {
85 global $scmcvs_proper_commit_loginfo;
86 if (!$scmcvs_proper_commit_loginfo) {
89 $actual_dir = getcwd();
90 chdir(dirname($file));
91 system("cvs -d ".$repos." ci -m \"".$message."\" ".basename($file));
92 // unlink (basename($file));
97 * release_cvs_file - Remove the file that was checked out from cvs
98 * @see checkout_cvs_file
100 function release_cvs_file($file) {
101 global $scmcvs_proper_commit_loginfo;
102 if (!$scmcvs_proper_commit_loginfo) {
105 // $file is something like /tmp/(tmp_dir)/path/to/file
106 // we must delete /tmp/tmp_dir
107 if (!preg_match("/^(\\/tmp\\/[^\\/]*)\\/.* /", $file, $result)) { // Make sure the dir is under /tmp
108 echo "Trying to release a directory not in /tmp. Skipping...";
113 // this shouldn't happen... but add it as a security checke
114 if (util_is_root_dir($dir)) {
115 echo "Trying to delete root dir. Skipping...";
118 $dir = escapeshellarg($dir);
119 system("rm -rf ".$dir);
122 function cvs_write_file($filePath, $content, $append=1) {
124 $file = fopen($filePath, 'a');
126 $file = fopen($filePath, 'w');
128 flock($file, LOCK_EX);
129 if(!empty($content)) {
130 fwrite($file, $content);
132 flock($file, LOCK_UN);
137 *add_sync_mail write to /CVSROOT/loginfo unix_name-commits@lists.gforge.company.com
139 *@param $unix_group_name Name Group
143 function add_sync_mail($unix_group_name) {
145 global $cvsdir_prefix, $cvs_binary_version;
146 $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/loginfo';
148 if (!$loginfo_file) {
149 echo "Couldn't get loginfo for $unix_group_name";
153 $content = file_get_contents ($loginfo_file);
154 if ( strstr($content, "syncmail") == FALSE) {
155 // echo $unix_group_name.":Syncmail not found in loginfo.Adding\n";
156 if ( $cvs_binary_version == "1.11" ) {
157 $pathsyncmail = "DEFAULT ".
158 forge_get_config('plugins_path')."/cvssyncmail/bin/syncmail -u %{sVv} ".
159 $unix_group_name."-commits@".forge_get_config('lists_host');
161 $pathsyncmail = "DEFAULT ".
162 forge_get_config('plugins_path')."/cvssyncmail/bin/syncmail -u %p %{sVv} ".
163 $unix_group_name."-commits@".forge_get_config('lists_host');
165 $content = "\n#BEGIN Added by cvs.php script\n".
166 $pathsyncmail. "\n#END Added by cvs.php script\n";
167 $loginfo_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/loginfo');
168 if(is_file($loginfo_file)){
169 //echo $unix_group_name.":About to write the lines\n";
170 cvs_write_file($loginfo_file, $content, 1);
172 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$loginfo_file);
173 release_cvs_file($loginfo_file);
175 // echo "Syncmail Found!\n";
180 * Function to add cvstracker lines to a loginfo file
181 * @param string the unix_group_name
184 function add_cvstracker($unix_group_name) {
185 global $cvsdir_prefix, $cvs_binary_version;
186 $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/loginfo';
188 if (!$loginfo_file) {
189 echo "Couldn't get loginfo for $unix_group_name";
193 $content = file_get_contents ($loginfo_file);
194 if ( strstr($content, "cvstracker") == FALSE) {
195 $content = "\n# BEGIN added by gforge-plugin-cvstracker";
196 if ( $cvs_binary_version == "1.11" ) {
197 $content .= "\nALL ( php -q -d include_path=".ini_get('include_path').
198 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
199 ".$unix_group_name." %{sVv} )";
200 } else { //it's version 1.12
201 $content .= "\nALL ( php -q -d include_path=".ini_get('include_path').
202 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
205 $content .= "\n# END added by gforge-plugin-cvstracker";
207 $loginfo_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/loginfo');
208 if(is_file($loginfo_file)){
209 //echo $unix_group_name.":About to write the lines\n";
210 cvs_write_file($loginfo_file, $content, 1);
212 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$loginfo_file);
213 release_cvs_file($loginfo_file);
215 // echo "cvstracker Found!\n";
218 // now make sure that if cvs version is 1.12, "UseNewInfoFmtStrings=yes" line
219 // MUST be present in CVSROOT/config, or else cvstracker won't work
220 if ($cvs_binary_version == "1.12") {
221 $config_file = $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/config';
222 if (!is_file($config_file)) {
223 echo "Couldn't get CVSROOT/config for $unix_group_name";
227 $content = file_get_contents($config_file);
228 if (!preg_match("/UseNewInfoFmtStrings=yes/i", $content)) {
229 // file must be modified
230 $config_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/config');
231 if (is_file($config_file)) {
232 $lines = file($config_file);
234 foreach ($lines as $line) {
235 if (!preg_match("/UseNewInfoFmtStrings/i", $line)) { // maybe it was set to "no"?
236 $newlines[] = trim($line);
239 $newlines[] = "UseNewInfoFmtStrings=yes"; // add the required line at the end
241 $content = implode("\n", $newlines);
242 cvs_write_file($config_file, $content, 0);
243 commit_cvs_file($cvsdir_prefix."/".$unix_group_name, $config_file);
244 release_cvs_file($config_file);
249 function add_acl_check($unix_group_name) {
250 global $cvsdir_prefix, $cvs_binary_version;
252 $commitinfofile = $cvsdir_prefix."/".$unix_group_name.'/CVSROOT/commitinfo';
254 $content = file_get_contents ($commitinfofile);
255 if ( strstr($content, "aclcheck") == FALSE) {
257 $commitinfofile = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/commitinfo');
258 if ( $cvs_binary_version == "1.11" ) {
259 $aclcheck = "\n#BEGIN adding cvs acl check".
260 "\nALL php -q -d include_path=".ini_get('include_path').
261 " ".forge_get_config('plugins_path')."/scmcvs/bin/aclcheck.php ".$cvsdir_prefix."/".$unix_group_name.
262 "\n#END adding cvs acl check\n";
263 } else { //it's version 1.12
264 $aclcheck = "\n#BEGIN adding cvs acl check".
265 "\nALL php -q -d include_path=".ini_get('include_path').
266 " ".forge_get_config('plugins_path')."/scmcvs/bin/aclcheck.php %r %p ".
267 "\n#END adding cvs acl check\n";
272 cvs_write_file($commitinfofile, $aclcheck, 1);
273 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$commitinfofile);
274 release_cvs_file($commitinfofile);
276 // echo "cvstracker Found!\n";
280 function update_cvs_repositories() {
281 global $cvsdir_prefix;
284 $res = db_query_params ('select groups.group_id,groups.unix_group_name,groups.enable_anonscm,groups.enable_pserver
285 FROM groups, plugins, group_plugin
286 WHERE groups.status != $1
287 AND groups.group_id=group_plugin.group_id
288 AND group_plugin.plugin_id=plugins.plugin_id
289 AND plugins.plugin_name=$2',
294 // Move CVS trees for deleted groups
296 $res8 = db_query_params ('SELECT unix_group_name FROM deleted_groups WHERE isdeleted = 0;',
299 $rows = db_numrows($res8);
300 for($k = 0; $k < $rows; $k++) {
301 $deleted_group_name = db_result($res8,$k,'unix_group_name');
303 if(!is_dir($cvsdir_prefix."/.deleted"))
304 system("mkdir ".$cvsdir_prefix."/.deleted");
306 system("mv -f $cvsdir_prefix/$deleted_group_name/ $cvsdir_prefix/.deleted/");
307 system("chown -R root:root $cvsdir_prefix/.deleted/$deleted_group_name");
308 system("chmod -R o-rwx $cvsdir_prefix/.deleted/$deleted_group_name");
311 $res9 = db_query_params ('UPDATE deleted_groups set isdeleted = 1 WHERE unix_group_name = $1',
312 array ($deleted_group_name));
323 Loop through and create/update each repository for every project
324 that uses SCMCVS plugin
330 if(is_dir($cvsdir_prefix)) {
331 update_cvs_repositories();
333 if(is_file($cvsdir_prefix)) {
334 $err .= "$cvsdir_prefix exists but is a file\n";
337 if (mkdir($cvsdir_prefix)) {
338 //need to update group permissions using chmod
339 update_cvs_repositories();
341 $err .= "unable to make $cvsdir_prefix directory\n";