xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / hooks / pre-receive (d7d3035cd1c822a5601ba5c5ac5103ea36dd7773) (1,726B) (mode 100755) [raw]
#!/usr/bin/php
<?php

//
// pre-receive hook
// If one check fails, receiving is denied.
// Executed before any ref is updated and before fast-forward checks.
//

error_reporting(E_ALL);
ini_set("track_errors", "On");

$_start = microtime(TRUE);

require_once("/etc/rocketgit/config.php");

$INC = $rg_scripts . "/inc";
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/repo.inc.php");

rg_log_set_file("/var/log/rocketgit/hook_pre-receive.log");

$uid = @sprintf("%u", getenv("ROCKETGIT_UID"));
$rights = getenv("ROCKETGIT_RIGHTS");

rg_log("Start uid=$uid, rights=[$rights]...");
rg_log("_SERVER: " . print_r($_SERVER, TRUE));

umask(0022);


$f = @fopen("php://stdin", "r");
if ($f === FALSE) {
	rg_log("Error: Cannot open stdin!");
	rg_fatal("Internal error!");
}
while (($set = fgets($f))) {
	$set = trim($set);
	if (empty($set))
		continue;

	$x = explode(" ", $set);
	$old_rev = @rg_git_rev($x[0]);
	$new_rev = @rg_git_rev($x[1]);
	$refname = @rg_git_reference($x[2]);
	rg_log("refname=$refname old_rev=$old_rev new_rev=$new_rev.");

	if (empty($refname) || empty($old_rev) || empty($new_rev))
		rg_fatal("Invalid parameters [$old_rev $new_rev $refname]!");

	if (rg_rights_allow($rights, "P") === FALSE)
		rg_fatal("Push is not allowed in this repository!");
}
fclose($f);


$diff = sprintf("%u", (microtime(TRUE) - $_start) * 1000);
rg_log("Took " . $diff . "ms.");

@file_put_contents($repo_path . "/rg/hook-pre-receive",
	"repo: " . $repo . " ($repo_path)"
	. "\nat: " . sprintf("%u", $_start)
	. "\nuid: " . $uid
	. "\npara: $refname $old_rev $new_rev"
	. "\nTook: " . $diff . "ms"
	. "\t_SERVER: " . print_r($_SERVER, TRUE));
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master