Using PHP to write git hooks - php

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 ?>

Related

Unable to run bash commands in php script

I'm trying to use a script to automate some syncing stuff with the website's corresponding github.
The offending code is
<html>
<head><title>github_sync script</title></head>
<body><p>Github sync script working... Although you're probably a github webhook anyways</p><body>
<?php
`mkdir test_dir`;
`cd github_sync/`;
`git clone https://github.com/frczyxw/my-website.git`;
`cd my-website/`;
`rm sixdegrees/`;
`cp -R * ../../`;
?>
</html>
The script is placed in the public_html/beta/directory. I can log into and navigate through the ftp and launch the script by clicking on it fine (the body text displays), but upon rechecking the ftp directory, I find that public_html/beta/test_dir has not appeared, nor does /public_html/beta/github_sync/ have anything in it (I created it manually previous to running the script).
The server should be running php5.3, and I am hosting the website vie Bluehost
OK, so I've solved the problem-
The server was configured correctly- however it turns out that to run all of these commands in sequence and in the same context (i.e. cd into github_sync make a git clone in that folder) the commands need to be executed all at once, like
`cd github_sync/;git clone wsgzsgg; rm -rf...`
Thnaks to everyone who tried to help

Execute PHP from GIT post-update hook

I am using GIT on my server and I am trying to get a PHP file to be executed each time I update my repository. I'm trying to use my post-update hook to achieve this.
this is the code I tried:
#!/bin/sh
echo
echo "**** Pulling changes into Prime [Hub's post-update hook]"
echo
cd $HOME/www || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info
php /path/to/directory/file.php
I can't seem to get the PHP to execute. Anyone able to shine any light on this?
exec never returns. Anything you put after the exec call is dead code.
Remove the exec, or place it before your php line if that's the last thing that needs to be done. (And after doing error checking if necessary obviously.)
So for instance
...
git-update-server-info
exec php /path/to/directory/file.php
Or just simply
...
git-update-server-info
php /path/to/directory/file.php
(or move the statements around if your php script can be called before the git command.)

how can I svn commit from my 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

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