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> / scripts / q.php (b5c39933f7c7ca72085b9798632d0c08a7afb39d) (3,182B) (mode 100644) [raw]
<?php
// This is called by cron, and is persistent.
// It takes care of repository init/clone, merge request to db etc.
error_reporting(E_ALL);
ini_set("track_errors", "On");

$now = time();
$_s = microtime(TRUE);

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

$INC = dirname(__FILE__) . "/../inc";
require_once($INC . "/init.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/struct.inc.php");
require_once($INC . "/repo.inc.php");
require_once($INC . "/prof.inc.php");
require_once($INC . "/mr.inc.php");

rg_prof_start("MAIN");

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

// locking
rg_lock_or_exit("q.lock");

rg_log("Start...");

$db = rg_sql_open($rg_sql);
if ($db === FALSE) {
	rg_log("Cannot connect to database!");
	// TODO: inform admin - already by e-mail?
	exit(1);
}

$r = rg_sql_struct_update($db, 0);
if ($r !== TRUE)
	exit(1);

$original_mtime = filemtime(__FILE__);

// Check our mtime so we can upgrade the software and this script will restart.
while (TRUE) {
	clearstatcache();
	$mtime = filemtime(__FILE__);
	rg_log("mtime=$mtime, original_mtime=$original_mtime");
	if ($mtime != $original_mtime) {
		rg_log("File changed. Exiting...");
		break;
	}

	// check machine load - if too big we will delay
	$load = rg_load();
	if ($load > 10) {
   		rg_log("\tLoad too big!");
		sleep(10);
		continue;
	}

	rg_mr_queue_process($db);

	rg_log("Check to create not-yet-created repos...");
	// Ordered by master to create masters first
	$sql = "SELECT a.repo_id, a.master, a.name, b.username, b.organization"
		. " FROM repos a, users b"
		. " WHERE a.uid = b.uid"
		. " AND a.deleted = 0"
		. " AND a.git_dir_done = 0"
		. " ORDER BY a.master";
	$res = rg_sql_query($db, $sql);
	if ($res === FALSE) {
		rg_log("\tCannot query (" . rg_sql_error() . ")!");
		exit(1);
	}
	while (($row = rg_sql_fetch_array($res))) {
		$rr = array(
			"prefix" => ($row['organization'] == 1) ? "" : "/user",
			"user" => $row['username'],
			"repo" => $row['name']);

		rg_log("\tProcessing rr: " . rg_array2string($rr));

		$dst = rg_repo_name2base($rr) . $row['name'] . ".git";
		if ($row['master'] == 0) {
			$r = rg_git_init($dst);
			if ($r === FALSE) {
				rg_log("\tCannot init master (" . rg_git_error() . ")!");
				continue;
			}

			rg_repo_git_done($db, $row['repo_id']);
		} else {
			$rr = array("repo_id" => $row['master']);
			$mi = rg_repo_info($db, $rr);
			if ($mi['exists'] != 1) {
				rg_log("\tCannot find master (" . rg_repo_error() . ")!");
				continue;
			}

			$ui = rg_user_info($db, $mi['uid'], "", "");
			if ($ui['ok'] != 1) {
				rg_log("\tCannot lookup uid "
					. $mi['uid'] . " (" . rg_user_error() . ")!");
				continue;
			}

			$rr = array(
				"prefix" => ($mi['organization'] == 1) ? "" : "/user",
				"user" => $ui['username'],
				"repo" => $mi['name']);
			$src = rg_repo_name2base($rr) . $mi['name'] . ".git";
			$r = rg_git_clone($src, $dst);
			if ($r === FALSE) {
				rg_log("\tCould not create repo (" . rg_git_error() . ")!");
				continue;
			}

			rg_repo_git_done($db, $row['repo_id']);
		}
	}
	rg_sql_free_result($res);

	sleep(1);
}

rg_prof_end("MAIN");
rg_prof_log("rg_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