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 (2fd1d85cb8d898a3ab8a0e2b2b3aef7daa053dfb) (2,154B) (mode 100644) [raw]
<?php
// This is called by cron, and is persistent.
// It takes care of repository init/clone
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 . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/repo.inc.php");

rg_log_set_file("/var/log/rocketgit/q.log");

// locking
$lock = "/var/run/rocketgit/q.lock";
rg_lock_or_exit($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);
}

// Limit the number of runs, so we can upgrade the software and this script will
// restart.
$runs = 1;
while ($runs-- > 0) {
	// check machine load - if too big we will delay
	while (1) {
		$load = rg_load();
		if ($load < 10)
			break;

		rg_log("\tLoad too big!");
		sleep(10);
	}

	rg_log("Check to create not-yet-created repos...");
	// Ordered by master to create masters first
	$sql = "SELECT repo_id, master, name FROM repos"
		. " WHERE deleted = 0"
		. " AND git_dir_done = 0"
		. " ORDER BY 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))) {
		rg_log("\tProcess repo " . $row['name'] . "...");

		$dst = rg_repo_name2base($row['name']) . $row['name'] . ".git";
		if ($row['master'] == 0) {
			$r = rg_git_init($dst);
			if ($r === FALSE) {
				rg_log("\tCannot init master!");
			} else {
				rg_repo_git_done($db, $row['repo_id']);
			}
		} else {
			$mi = rg_repo_info($db, $row['master'], "");
			if ($mi['exists'] != 1) {
				rg_log("\tCannot find master!");
			} else {
				$src = rg_repo_name2base($mi['name']) . $mi['name'] . ".git";
				$r = rg_git_clone($src, $dst);
				if ($r === FALSE) {
					rg_log("\tCould not create repo!");
				} else {
					rg_repo_git_done($db, $row['repo_id']);
				}
			}
		}
	}
	rg_sql_free_result($res);

	sleep(10);
}

$_diff = sprintf("%u", microtime(TRUE) - $_s);
rg_log("Done in " . $_diff . "s!");
?>
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