6 * The rest Copyright 2002-2005 (c) GForge Team
7 * http://fusionforge.org/
9 * This file is part of FusionForge.
11 * FusionForge 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 * FusionForge 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 along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require dirname(__FILE__).'/../../env.inc.php';
27 require_once $gfcommon.'include/pre.php';
28 require $gfcommon.'include/cron_utils.php';
29 require $gfcommon.'include/utils.php';
30 require_once $gfcommon.'mail/MailingList.class.php';
31 require_once $gfcommon.'mail/MailingListFactory.class.php';
33 require_once $gfcommon.'include/SCMPlugin.class.php' ;
37 setup_plugin_manager () ;
40 // Some OSs do not allow root to do a commit
41 // so having this script do a proper checkout/commit is not possible
43 $scmcvs_proper_commit_loginfo=false;
46 * Retrieve a file into a temporary directory from a CVS server
48 * @param String $repos Repository Name
49 * @param String $file File Name
51 * return String the FileName in the working repository
53 function checkout_cvs_file($repos,$file) {
54 //echo "$repos,$file\n";
55 global $scmcvs_proper_commit_loginfo;
56 if (!$scmcvs_proper_commit_loginfo) {
57 return $repos.'/'.$file;
59 $actual_dir = getcwd();
60 $tempdirname = tempnam("/tmp","cvstracker");
63 if (!unlink($tempdirname))
66 // Create the temporary directory and returns its name.
67 if (!mkdir($tempdirname))
70 if (!chdir($tempdirname))
72 system("cvs -d ".$repos." co ".$file);
75 return $tempdirname."/".$file;
79 * commit_cvs_file commit a file to the repository
81 * @param String $repos Repository
82 * @param String $file to commit
83 * @param String $message to commit
85 function commit_cvs_file($repos,$file,$message="Automatic updated by cvstracker") {
86 global $scmcvs_proper_commit_loginfo;
87 if (!$scmcvs_proper_commit_loginfo) {
90 $actual_dir = getcwd();
91 chdir(dirname($file));
92 system("cvs -d ".$repos." ci -m \"".$message."\" ".basename($file));
93 // unlink (basename($file));
98 * release_cvs_file - Remove the file that was checked out from cvs
99 * @see checkout_cvs_file
101 function release_cvs_file($file) {
102 global $scmcvs_proper_commit_loginfo;
103 if (!$scmcvs_proper_commit_loginfo) {
106 // $file is something like /tmp/(tmp_dir)/path/to/file
107 // we must delete /tmp/tmp_dir
108 if (!preg_match("/^(\\/tmp\\/[^\\/]*)\\/.* /", $file, $result)) { // Make sure the dir is under /tmp
109 echo "Trying to release a directory not in /tmp. Skipping...";
114 // this shouldn't happen... but add it as a security checke
115 if (util_is_root_dir($dir)) {
116 echo "Trying to delete root dir. Skipping...";
119 $dir = escapeshellarg($dir);
120 system("rm -rf ".$dir);
123 function cvs_write_file($filePath, $content, $append=1) {
125 $file = fopen($filePath, 'a');
127 $file = fopen($filePath, 'w');
129 flock($file, LOCK_EX);
130 if(!empty($content)) {
131 fwrite($file, $content);
133 flock($file, LOCK_UN);
138 *add_sync_mail write to /CVSROOT/loginfo unix_name-commits@lists.gforge.company.com
140 *@param $unix_group_name Name Group
144 function add_sync_mail($unix_group_name) {
146 global $cvsdir_prefix ;
147 $cvs_binary_version = get_cvs_binary_version () ;
148 $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/loginfo';
150 if (!$loginfo_file) {
151 echo "Couldn't get loginfo for $unix_group_name";
155 $content = file_get_contents ($loginfo_file);
156 if ( strstr($content, "syncmail") == FALSE) {
157 // echo $unix_group_name.":Syncmail not found in loginfo.Adding\n";
158 if ( $cvs_binary_version == "1.11" ) {
159 $pathsyncmail = "DEFAULT ".
160 forge_get_config('plugins_path')."/cvssyncmail/bin/syncmail -u %{sVv} ".
161 $unix_group_name."-commits@".forge_get_config('lists_host');
163 $pathsyncmail = "DEFAULT ".
164 forge_get_config('plugins_path')."/cvssyncmail/bin/syncmail -u %p %{sVv} ".
165 $unix_group_name."-commits@".forge_get_config('lists_host');
167 $content = "\n#BEGIN Added by cvs.php script\n".
168 $pathsyncmail. "\n#END Added by cvs.php script\n";
169 $loginfo_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/loginfo');
170 if(is_file($loginfo_file)){
171 //echo $unix_group_name.":About to write the lines\n";
172 cvs_write_file($loginfo_file, $content, 1);
174 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$loginfo_file);
175 release_cvs_file($loginfo_file);
177 // echo "Syncmail Found!\n";
182 * Function to add cvstracker lines to a loginfo file
183 * @param string the unix_group_name
186 function add_cvstracker($unix_group_name) {
187 global $cvsdir_prefix;
188 $cvs_binary_version = get_cvs_binary_version () ;
189 $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/loginfo';
191 if (!$loginfo_file) {
192 echo "Couldn't get loginfo for $unix_group_name";
196 $content = file_get_contents ($loginfo_file);
197 if ( strstr($content, "cvstracker") == FALSE) {
198 $content = "\n# BEGIN added by gforge-plugin-cvstracker";
199 if ( $cvs_binary_version == "1.11" ) {
200 $content .= "\nALL ( php -q -d include_path=".ini_get('include_path').
201 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
202 ".$unix_group_name." %{sVv} )";
203 } else { //it's version 1.12
204 $content .= "\nALL ( php -q -d include_path=".ini_get('include_path').
205 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
208 $content .= "\n# END added by gforge-plugin-cvstracker";
210 $loginfo_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/loginfo');
211 if(is_file($loginfo_file)){
212 //echo $unix_group_name.":About to write the lines\n";
213 cvs_write_file($loginfo_file, $content, 1);
215 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$loginfo_file);
216 release_cvs_file($loginfo_file);
218 // echo "cvstracker Found!\n";
221 // now make sure that if cvs version is 1.12, "UseNewInfoFmtStrings=yes" line
222 // MUST be present in CVSROOT/config, or else cvstracker won't work
223 if ($cvs_binary_version == "1.12") {
224 $config_file = $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/config';
225 if (!is_file($config_file)) {
226 echo "Couldn't get CVSROOT/config for $unix_group_name";
230 $content = file_get_contents($config_file);
231 if (!preg_match("/UseNewInfoFmtStrings=yes/i", $content)) {
232 // file must be modified
233 $config_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/config');
234 if (is_file($config_file)) {
235 $lines = file($config_file);
237 foreach ($lines as $line) {
238 if (!preg_match("/UseNewInfoFmtStrings/i", $line)) { // maybe it was set to "no"?
239 $newlines[] = trim($line);
242 $newlines[] = "UseNewInfoFmtStrings=yes"; // add the required line at the end
244 $content = implode("\n", $newlines);
245 cvs_write_file($config_file, $content, 0);
246 commit_cvs_file($cvsdir_prefix."/".$unix_group_name, $config_file);
247 release_cvs_file($config_file);
252 function add_acl_check($unix_group_name) {
253 global $cvsdir_prefix;
254 $cvs_binary_version = get_cvs_binary_version () ;
256 $commitinfofile = $cvsdir_prefix."/".$unix_group_name.'/CVSROOT/commitinfo';
258 $content = file_get_contents ($commitinfofile);
259 if ( strstr($content, "aclcheck") == FALSE) {
261 $commitinfofile = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/commitinfo');
262 if ( $cvs_binary_version == "1.11" ) {
263 $aclcheck = "\n#BEGIN adding cvs acl check".
264 "\nALL php -q -d include_path=".ini_get('include_path').
265 " ".forge_get_config('plugins_path')."/scmcvs/bin/aclcheck.php ".$cvsdir_prefix."/".$unix_group_name.
266 "\n#END adding cvs acl check\n";
267 } else { //it's version 1.12
268 $aclcheck = "\n#BEGIN adding cvs acl check".
269 "\nALL php -q -d include_path=".ini_get('include_path').
270 " ".forge_get_config('plugins_path')."/scmcvs/bin/aclcheck.php %r %p ".
271 "\n#END adding cvs acl check\n";
276 cvs_write_file($commitinfofile, $aclcheck, 1);
277 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$commitinfofile);
278 release_cvs_file($commitinfofile);
280 // echo "cvstracker Found!\n";
284 function update_cvs_repositories() {
285 global $cvsdir_prefix;
289 // Move CVS trees for deleted groups
291 $res8 = db_query_params ('SELECT unix_group_name FROM deleted_groups WHERE isdeleted = 0;',
294 $rows = db_numrows($res8);
295 for($k = 0; $k < $rows; $k++) {
296 $deleted_group_name = db_result($res8,$k,'unix_group_name');
298 if(!is_dir($cvsdir_prefix."/.deleted"))
299 system("mkdir ".$cvsdir_prefix."/.deleted");
301 system("mv -f $cvsdir_prefix/$deleted_group_name/ $cvsdir_prefix/.deleted/");
302 system("chown -R root:root $cvsdir_prefix/.deleted/$deleted_group_name");
303 system("chmod -R o-rwx $cvsdir_prefix/.deleted/$deleted_group_name");
306 $res9 = db_query_params ('UPDATE deleted_groups set isdeleted = 1 WHERE unix_group_name = $1',
307 array ($deleted_group_name));
318 Loop through and create/update each repository for every project
319 that uses SCMCVS plugin
325 if(is_dir($cvsdir_prefix)) {
326 update_cvs_repositories();
328 if(is_file($cvsdir_prefix)) {
329 $err .= "$cvsdir_prefix exists but is a file\n";
332 if (mkdir($cvsdir_prefix)) {
333 //need to update group permissions using chmod
334 update_cvs_repositories();
336 $err .= "unable to make $cvsdir_prefix directory\n";