<?php
/**
Webhook script for bitbucket
@author da.chen@outlook.com
@version 20170406

How to use:

1) goto your repository and change owner to www-data
chown -R www-data:www-data repository/
chmod -R g+s repository/

2) edit your repository/.git/config and add bitbucket user password to the origin url
https://<usr>:<pwd>/xx/repository

3) in bitbucket config add webhook
http://<this_host>/webhook/webhook.php?repo=<your_repority_name>

*/
if(!isset($_GET['repo'])) {
    die("repository not specified");
}
$repo = $_GET['repo'];
$repo_root = '/var/git/';

echo "current user: <br />";
system("id");
echo "<br />--- start of webhook output ---<br /><pre>";
//system( 'cd '.$repo_root.$repo.'/ && /usr/bin/git reset --hard HEAD && /usr/bin/git pull 2>&1', $retval);
//system( 'cd '.$repo_root.$repo.'/ && /usr/bin/git reset --hard HEAD && /usr/bin/git pull origin master 2>&1', $retval);
system( 'cd '.$repo_root.$repo.'/ && /usr/bin/git pull 2>&1', $retval);
echo "</pre><br />--- end of webhook output ---<br />";
echo "return value: " .$retval;
?>
