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 (d636337f57ec12ba2ab311061851e1f93781ad37) (2,275B) (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/rg/config.php");

$INC = dirname(__FILE__) . "/../inc";
require_once($INC . "/log.inc.php");
require_once($INC . "/db.inc.php");
require_once($INC . "/repo.inc.php");

rg_log_set_file("/tmp/rg_q.log");

// locking
$lock = "/var/run/rg/q.lock";
rg_lock_or_exit($lock);

$rg_sql_debug = $rg_db_debug;

rg_log("Start: euid=" . posix_geteuid() . "...");

$db = rg_sql_open($rg_db);
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!");
		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_log("Make hooks links...");
		symlink("/BIG1T/sync1/Dev/rg/scripts/hooks_update", $dst . "/hooks/upate");
	}
	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