how can I svn commit from my php - php

I have been trying to get my php to be able to create sql procedure files. My page does everything just as it needs to. It even writes to file correctly, but now I need it to add the file to svn and then also commit. I have been trying
system("svn commit", $trash);
but I cannot seem to get it to work. I have been able to get
system("svn add ".$fileName);
to work and i get common "A................fileName added" or what ever it says. I think that when I try to commit the file svn is waiting for authentication or something, I think this because I don't get anything back in the $trash...
All help is appreciated, thanks.

There is a PECL extension for working with SVN:
svn_commit — Sends changes from the local working copy to the repository
Example from Manual:
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, 'Bob');
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, 'abc123');
var_dump(svn_commit('Log message of Bob\'s commit', array(realpath('calculator'))));
There is also a PEAR package:
http://pear.php.net/package/VersionControl_SVN
VersionControl_SVN is a simple OO-style interface for Subversion, the free/open-source version control system. VersionControl_SVN can be used to manage trees of source code, text files, image files -- just about any collection of files.

You can go with a full svn commit command line like :
svn commit --username USER --password PASS
Further options available at : http://svnbook.red-bean.com/en/1.1/re06.html

Related

Git for windows seems to automatically fall back to credential helper "manager" if everything else fails. How do I prevent this?

I'm trying to integrate a custom web page with git. In my PHP scripts, I use the option "-c credential.helper="store --file=..." so that the web page does not stop and wait for a password to be input. The User ID is specifically designated for automated tasks like this. As part of the web interface, I have some code that will update the credentials file when the password expires.
During development, when I issue the git commands at the windows command prompt (the "terminal" for you *NIX readers :) they all work fine. However, when I put the commands inside my PHP script and run it via the web server, they fail. I've managed to capture the output of the git task, and it's waiting for someone to type in a user ID.
It seems that git on windows will automatically drop back to the "manager" helper, if all other mechanisms fail. The following sequence of commands illustrates this:
C:\TFS\Train>git config --get --show-origin credential.helper
C:\TFS\Train>type .git\.git_web_credentials
http://Promote5:[.....]#tocgnxt1pv%3a8080
C:\TFS\Train>git -c credential.helper="store --file=C:\TFS\Train\.git\.git_credentials" tag -a -m "Testing tags from the command line" Who_created_this_tag2
C:\TFS\Train>git show Who_created_this_tag2
tag Who_created_this_tag2
Tagger: JimHyslop <jim.hyslop#xxx.xxx>
Date: Wed Dec 6 18:13:27 2017 -0500
[... remainder of output elided ...]
C:\TFS\Train>git --version
git version 2.13.0.windows.1
As you can see, the "tagger" line indicates that I'm the one who applied the tag.
I've even tried deleting the credentials file completely, but it still falls back to using my identity.
Is there any way to suppress this automatic fall back to using my Windows credentials? It's making it very difficult to debug my PHP script: I never know from the command prompt whether the command succeeded because it was able to use the credentials file, or because it fell back to using my own ID (which the web service cannot do).
Edit to add: The password is actually in the credentials file, I masked it out. The file was initially created by entering the ID and password from a Cygwin shell, which is running a slightly newer version of git: 2.15.0. I'm running a WAMP stack for my web service.
After more investigation, I realized that the 'tagger' line is pulled from the configuration information under the [user] section. The user information shown in the log is unrelated to the authorization/credentials method.
The real question is: why is git ignoring the command-line option "-c credential.helper="? Or, if it's not ignoring the option, how can I figure out exactly what is failing with the credential helper?
For the record, the solution I came up with was to write a custom credential manager, as outlined at git-scm.com

moodle - database connection failed when running a script

So I am setting up a moodle plugin that syncs groups defined in child courses into a meta course. I managed to install it based on the following instruction.
Installation
Copy the metagroups folder into your Moodle /local directory and visit
your Admin Notification page to complete the installation.
After installation you may need to synchronize existing meta-course groups, to do this run the cli/sync.php script (use the --help switch for further instructions on script usage).
So when i tried to run th sync.php script using this command in there terminal
php -f sync.php
it shows me an error message
I found that the line in the script that is causing this issue is the following line
require_once(DIR . '/../../../config.php');
my config.php file looks like this:
I have no idea why this is happening, as all moodle functionality seems to be working, which means database connection is the way it should be. Does anyone know this is happening and a way around this. Thanks in advance.
By the way, my moodle version is 2.6.3 running in localhost on a mac OSX 10.9
I had the same problem, but running moodle with MAMP.
The simple fix for me was just to add the php path for MAMP (for whichever PHP version I required):
export PATH=/Applications/MAMP/bin/php/php5.6.40/bin/:$PATH
…and then run whatever PHP script I needed!
Just a guess but I'm wondering if its picking up a different config.php
Try specifying the folder for the require_once
require_once('/Applications/XAMMP/moodlewebroot/config.php');
If that works then check the check the output of
echo DIR . '/../../../config.php'
Although it's more common to use dirname() in Moodle eg:
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
Also some of the command line functions expect to have the dirroot set up in config.
$CFG->dirroot = '/Applications/XAMMP/moodlewebroot';

Using PHP to write git hooks

I want to use PHP writing git hooks but have some problem.
I use Windows 7 LAMP packet and git bash.
So, if I run the next script (pre-commit hook) through git shell:
https://gist.github.com/713716
it works fine and there is 123 on the screen. But if I use:
git commit
I have the next error:
error: cannot spawn .git/hooks/pre-commit: No such file or directory
So, what the problem is?
I rarely work on Windows systems but it might have something to do with either making sure the pre-commit file is executable by every user (per Phil's comment), or something to do with git invoking a php command.
Looks like this problem may exist elsewhere but not easily reproduceable.
It's janky, but it might work if you create a new file somewhere with a php extension and invoke that file from the pre-commit hook.
#pre-commit
C:/WebServers/usr/local/php5/php C:/path/to/123.php
#123.php
<?php echo 123 ?>

how to run a batch file in php under xampp on windows

I'm trying to write a little php to update an svn repo on a server running xampplite under windows. (This is a development server, not a production one.)
Here's my php:
<?php
passthru("update.bat");
// I also tried exec() & putting the svn command in directly
?>
update.bat is sitting in the same folder as the php script
Here's the content of update.bat:
svn up c:\path\to\my\repo
When I run the batch file by itself, it works. When I run it via php, I get this printed to the browser:
C:\path\to\script\folder>svn up c:\path\to\my\repo
which looks good, but the project isn't updated.
Adding the username and password to the batch made the difference. Here's the new update.bat:
svn up --username <usr> --password <pwd> c:\path\to\the\repo
Try this tip on php.net/function.exec
The other option is to manually compile the php svn extension (there's no Windows DLL), but you also need the svn libraries first.

How can I get the Subversion revision number in PHP?

I want to have my PHP application labeled with the revision number which it uses, but I don't want to use CruiseControl or update a file and upload it every time. How should I do it?
SVN keywords is not a good solution. As others pointed out adding $Revision$ in a file only affects the specific file, which may not change for a long time.
Remembering to "edit" a file (by adding or removing a blank line) before every commit is pointless. You could as well just type the revision by hand.
One good way to do it (that I know of) is to have an automated deployment process (which is always a good thing) and using the command svnversion. Here is what I do:
Wherever I need the revision I do an include: <?php include 'version.php'; ?>. This "version.php" file only has the revision number. Moreover it is not part of the repository (it set to be ignored). Here is how I create it:
1) On projects where SVN is installed on the server, I also use it for deployment. Getting the latest version to the server I have a script that among other things does the following (it runs on the server):
cd /var/www/project
svn update
rm version.php
svnversion > version.php
2) On projects where SVN is not installed my deployment script is more complex: it creates the version.php file locally, zips the code, uploads and extracts it
Assuming your webroot is a checked-out copy of the subversion tree, you could parse the /.svn/entries file and hook out the revision number (4th line here)...
In PHP:
$svn = File('.svn/entries');
$svnrev = $svn[3];
unset($svn);
This is how I got it to work.
If your server is setup to allow shell_exec AND you have SVN installed just run:
$revision = `svnversion`;
or
$revision = shell_exec('svnversion');
From this answer:
You can do it by adding the following
anywhere in your code
$Id:$
So for example Jeff did:
<div id="svnrevision">svn revision: $Id:$</div>
and when checked in the
server replaced $Id:$ with the current
revision number. I also found this reference.
There is also $Date:$, $Rev:$,
$Revision:$
Bit late now, but use a Subversion post-commit hook. In your repository's hooks folder, create a shell script like this one:
#!/bin/bash
REPOS="$1"
REV="$2"
cd /web/root
rm -f /web/root/templates/base.html
/usr/bin/svn update
/bin/sed -i s/REVISION/$REV/ /web/root/templates/base.html
This particular example assumes your live site is in /web/root and the development code is held elsewhere. When you commit a dev change, the script deletes the prior live template (to avoid conflict messages), runs the update and replaces occurrences of REVISION in the template with the actual revision number.
More on hooks here
In most cases the code on the server would actually contain an "Export" of the code, not a checkout, and therefore not contain the .svn folders. At least that's the setup I see most often. Do others actually check out their code onto the web server?
You can get close with SVN Keywords. Add $Revision$ where you want the revision to show, but that will only show the last revision that particular file was changed, so you would have to make a change to the file each time. Getting the global revision number isn't possible without some sort of external script, or a post-commit hook.
You could also do it like this:
$status = #shell_exec('svnversion '.realpath(__FILE__));
if ( preg_match('/\d+/', $status, $match) ) {
echo 'Revision: '.$match[0];
}
The easiest way is to use the Subversion "Keyword Substitution". There is a guide here in the SVN book (Version Control with Subversion).
You'll basically just have to add the text $Rev$ somewhere in your file.
Then enable the keyword in your repository. On checkout SVN will substitute the revision number into the file.
See my response to the similar question "Mark" svn export with revision.
If you capture the revision number when you export you can use:
svn export /path/to/repository | grep ^Exported > revision.txt
To strip everything but the revision number, you can pipe it through this sed command:
svn export /path/to/repository | grep ^Exported | sed 's/^[^0-9]\+\([0-9]\+\).*/\1/' > revision.txt
$svn_rev=file_get_contents('/path.to.repository/db/current');
Another possibility to do this is to run a cron that executes the steps described in the "Deploy Process" (assuming it is a *nix/FreeBSD server).
If performance is an issue, then you could do:
exec('svn info /path/to/repository', $output);
$svn_ver = (int) trim(substr($output[4], strpos($output[4], ':')));
This of course depends on your having done a checkout, and the presence of the svn command.

Categories