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> / inc / git.inc.php (e711d13a75371b148c0f8af0f70920d62d8dd32f) (3,098B) (mode 100644) [raw]
<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");

$rg_git_error = "";

function rg_git_set_error($str)
{
	global $rg_git_error;

	rg_log("\tError: $str");
	$rg_git_error = $str;
}

function rg_git_error()
{
	global $rg_git_error;
	return $rg_git_error;
}

function rg_git_install_hooks($dst)
{
	rg_log("git_install_hooks: dst=$dst...");

	rg_log("\tNot yet implemented!");
	return TRUE;
}

function rg_git_init($dst)
{
	rg_log("git_init: dst=$dst...");

	$dir = dirname($dst);
	if (!file_exists($dir)) {
		$r = @mkdir($dir, 0755, TRUE);
		if ($r === FALSE) {
			rg_log("\tCannot create dir [$dir] ($php_errormsg)!");
			return FALSE;
		}
	}

	if (file_exists($dst . "/rg")) {
		rg_log("\tGit repo was created OK. Skip cloning.");
	} else {
		$cmd = "git init --bare '" . escapeshellcmd($dst) . "'";
		rg_log("\texec $cmd...");
		$a = exec($cmd, $output, $err);
		if ($err != 0) {
			rg_log("\tError $err (" . implode("|", $output) . " ($a)!");
			return FALSE;
		}

		if (!@mkdir($dst . "/rg")) {
			rg_log("\tCannot create rg dir ($php_errormsg)!");
			return FALSE;
		}
	}

	if (rg_git_install_hooks($dst) === FALSE)
		return FALSE;

	return TRUE;
}

function rg_git_clone($src, $dst)
{
	rg_log("git_clone: src=$src, dst=$dst...");

	$dir = dirname($dst);
	if (!file_exists($dir)) {
		$r = @mkdir($dir, 0755, TRUE);
		if ($r === FALSE) {
			rg_log("\tCannot create dir [$dir] ($php_errormsg)!");
			return FALSE;
		}
	}

	if (file_exists($dst . "/rg")) {
		rg_log("\tGit repo was created OK. Skip cloning.");
	} else {
		$cmd = "git clone --bare '" . escapeshellcmd($src) . "'"
			. " '" . escapeshellcmd($dst) . "'";
		rg_log("\texec $cmd...");
		$a = exec($cmd);
		if ($err != 0) {
			rg_log("\tError $err (" . implode("|", $output) . " ($a)!");
			return FALSE;
		}

		if (!@mkdir($dst . "/rg")) {
			rg_log("\tCannot create rg dir ($php_errormsg)!");
			return FALSE;
		}
	}

	if (rg_git_install_hooks($dst) === FALSE)
		return FALSE;

	return TRUE;
}

/*
 * Returns type for an object
 */
function rg_git_type($obj)
{
	$cmd = "git cat-file -t '" . escapeshellcmd($obj) . "'";
	rg_log("\texec $cmd...");
	$a = exec($cmd, $output, $err);
	if ($err != 0) {
		rg_log("\tError $err (" . implode("|", $output) . " ($a)!");
		return FALSE;
	}

	return $a;
}

/*
 * Corrects a ref
 */
function rg_git_ref($s)
{
	return preg_replace("/[^a-zA-Z0-9^~]/", "", $s);
}

// Check a ref if is OK
// TODO: Unit testing
function rg_git_ref_ok($ref)
{
	$cmd = "git rev-parse --verify --quiet " . escapeshellcmd($ref);
	rg_log("\texec $cmd...");
	$a = exec($cmd, $output, $err);
	if ($err != 0) {
		rg_log("\tError $err (" . implode("|", $output) . " ($a)!");
		return FALSE;
	}

	return TRUE;
}

// returns TRUE if bad whitespace detected
// TODO: Unit testing
function rg_git_bad_whitespace($old, $new)
{
	$cmd = "git diff --check --quiet " . escapeshellcmd($old) . " "
		. escapeshellcmd($new);
	rg_log("\texec $cmd...");
	$a = exec($cmd, $output, $err);
	if ($err != 0) {
		rg_log("\tError $err (" . implode("|", $output) . " ($a)!");
		return FALSE;
	}

	return 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