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 / update (4f71c9a16141166e162dfd798cedfd6f75d4b738) (3,125B) (mode 100755) [raw]
#!/usr/bin/php
<?php

//
// This is called by 'update' hook
// Inspired by update.sample in git package
// It is executed before each ref is updated.
//

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

$_start = microtime(TRUE);

$rg_log_sid = getenv('ROCKETGIT_LOG_SID');

$conf = getenv("ROCKETGIT_CONF_FILE");
if (empty($conf))
	$conf = "/etc/rocketgit/config.php";
require_once($conf);

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

rg_prof_start("hook-update");

rg_log_set_file($rg_log_dir . "/hook_update.log");

$a = $rg;
$a['login_uid'] = sprintf("%u", getenv("ROCKETGIT_LOGIN_UID"));
$a['login_url'] = getenv('ROCKETGIT_LOGIN_URL');
$a['repo_id'] = sprintf("%u", getenv("ROCKETGIT_REPO_ID"));
$a['ip'] = getenv("ROCKETGIT_IP");
$a['namespace'] = getenv("GIT_NAMESPACE");
$a['repo_path'] = getenv("ROCKETGIT_REPO_PATH");
$a['repo_name'] = getenv("ROCKETGIT_REPO_NAME");
$a['repo_uid'] = sprintf("%u", getenv("ROCKETGIT_REPO_UID"));
$a['repo_clone_url_http'] = getenv('ROCKETGIT_REPO_CLONE_URL');
$a['log_sid'] = $rg_log_sid;

rg_log("Start a=" . rg_array2string($a));
rg_log("_SERVER: " . rg_array2string($_SERVER));

rg_sql_app('rg-hook-update-' . $a['log_sid']);
$db = rg_sql_open($rg_sql);

if ($a['login_uid'] > 0) {
	$a['ui'] = rg_user_info($db, $a['login_uid'], '', '');
	if ($a['ui']['ok'] != 1)
		rg_git_fatal('internal error');
	if ($a['ui']['exists'] != 1)
		rg_git_fatal('user does not exists');
} else {
	$a['ui'] = array(
		'uid' => 0,
		'username' => 'anonymous user',
		'organization' => 0
	);
}

umask(0077);

//echo rg_git_pack("\x02Test message on band 2\n");
//echo rg_git_pack("\x03Test message on band 3\n");

$a['refname'] = rg_git_reference($_SERVER['argv'][1]);
if ($a['refname'] === FALSE)
	rg_git_fatal('Invalid reference: ' . rg_git_error() . '!');

$a['old_rev'] = rg_git_rev(@$_SERVER['argv'][2]);
$a['new_rev'] = rg_git_rev(@$_SERVER['argv'][3]);
rg_log("refname=" . $a['refname'] . " old_rev=" . $a['old_rev']
	. " new_rev=" . $a['new_rev']);

if (empty($a['refname']) || empty($a['old_rev']) || empty($a['new_rev']))
	rg_git_fatal("Invalid parameters: ref=[" . $a['refname']
		. "] old=[" . $a['old_rev'] . "] new=[" . $a['new_rev'] . "]!");

if (strcmp($a['new_rev'], $rg_git_zero) == 0)
	$a['new_rev_type'] = rg_git_type($a['old_rev']);
else
	$a['new_rev_type'] = rg_git_type($a['new_rev']);
rg_log("new_rev_type=" . $a['new_rev_type']);

if (strncmp($a['refname'], "refs/tags/", 10) == 0) {
	rg_git_update_tag($db, $a);
} else if (strncmp($a['refname'], "refs/heads/", 11) == 0) {
	rg_git_update_branch($db, $a);
} else if (strncmp($a['refname'], "refs/remotes/", 13) == 0) {
	rg_git_fatal("No action for remotes for now!");
} else {
	$m = "Unknown refname type provided [" . $a['refname'] . "]";
	rg_internal_error($m);
	rg_git_fatal($m);
}

rg_prof_end("hook-update");
rg_prof_log();
?>
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