Skip to content

Instantly share code, notes, and snippets.

@afshin-hoseini
Created August 18, 2016 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afshin-hoseini/d7419be4868b71a1c0020f11c9547da3 to your computer and use it in GitHub Desktop.
Save afshin-hoseini/d7419be4868b71a1c0020f11c9547da3 to your computer and use it in GitHub Desktop.
Git web hook puller
<?php
//______________________________________________
function gitInit() {
$currentDir = getcwd();
exec('cd '.$currentDir);
$result = exec('git init');
echo 'res: '.$result;
}
//____________________________________________
function logHook() {
$headers = array();
foreach($_SERVER as $key => $value) {
if(substr($key, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
}
}
$info = "HEADERS:\n\n";
foreach($headers as $key => $value) {
$info .= ($key.":".$value."\n");
}
$info .= "\n\nBODY:\n\n".file_get_contents('php://input');
$file = fopen(getcwd()."/hookLog.txt","a");
fwrite($file,"\n____________\n".$info);
fclose($file);
}
//___________________________
function pullRepository() {
//Send 200 as http status code to close the connection and release gitlab
ignore_user_abort(true);
set_time_limit(0);
ob_start();
// do initial processing here
http_response_code(200);
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
$headers = array();
foreach($_SERVER as $key => $value) {
if(substr($key, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
}
}
$isPushHook = false;
foreach($headers as $key => $value) {
if($key == "X-Gitlab-Event" && $value == "Push Hook") {
$isPushHook = true;
break;
}
}
if($isPushHook == true) {
$currentDir = getcwd();
$repoDir = $currentDir."/testRepo";
// $file = fopen($currentDir."/TESTFILE.txt","a");
// fwrite($file,"\n____________\nTEST");
// fclose($file);
//exec('cd '.$currentDir);
chdir($repoDir);
//$result = exec("git pull https://afshin.hoseini:Afshin1990@gitlab.com/afshin.hoseini/testRepo.git develop");
exec("git config --global user.name \"afshin.hoseini\"");
exec("git config --global user.email \"afshin.hoseini@gmail.com\"");
exec("git checkout develop");
$result = [];
exec("git pull https://afshin.hoseini:Afshin1990@gitlab.com/afshin.hoseini/testRepo.git develop", $result);
$content = "";
foreach($result as $key=>$value) {
$content .= ($key.":".$value."\n");
}
$file = fopen(getcwd()."/pullLog.txt","a");
fwrite($file,"\n____________\n".$content);
fclose($file);
}
}
//___________________________
function cloneRepo() {
$currentDir = getcwd();
exec('cd '.$currentDir);
exec("git config --global user.name \"afshin.hoseini\"");
exec("git config --global user.email \"afshin.hoseini@gmail.com\"");
$result = exec('git clone https://afshin.hoseini:Afshin1990@gitlab.com/afshin.hoseini/testRepo.git --branch develop');
echo 'res: '.$result;
}
//___________________________
if(isset($_GET['clone'])) {
cloneRepo();
}
else {
logHook();
pullRepository();
}
echo 'HELLO';
?>
@afshin-hoseini
Copy link
Author

Care about chdir function while using git commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment