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 / cron.php (50a58d84a0bd00a71d541d63c58d46c1596f7ce7) (3,167B) (mode 100644) [raw]
<?php
// This is called by cron
error_reporting(E_ALL);
ini_set("track_errors", "On");

$now = time();

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 . "/keys.inc.php");

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

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

rg_log("Start...");

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

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

if (date("H") == 0) {
	rg_log("Compute repository sizes if dirty...");
	// delete 'dirty' files
	$sql = "SELECT a.*, b.username, b.organization"
		. " FROM repos a, users b"
		. " WHERE a.uid = b.uid";
	$res = rg_sql_query($db, $sql);
	if ($res === FALSE) {
		// TODO: rg_internal_error? it must notify me in case of problems
		rg_log("Cannot run query (" . rg_sql_error() . ")!");
	} else {
		while (($row = rg_sql_fetch_array($res))) {
			$prefix = ($row['organization'] == 1) ? "" : "/user";

			rg_log("Processing $prefix/" . $row['username']
				. "/" . $row['name'] . "...");
			$_rr = array("prefix" => $prefix,
				"user" => $row['username'],
				"repo" => $row['name']);
			$repo_path = rg_repo_name2base($_rr) . $row['name'] . ".git";
			$disk_used_mb = rg_repo_disk_mb($repo_path);
			$sql = "UPDATE repos SET disk_used_mb = $disk_used_mb"
				. " WHERE repo_id = " . $row['repo_id'];
			$res2 = rg_sql_query($db, $sql);
			if ($res2 === FALSE) {
				rg_log("Cannot run query!");
			} else {
				if (file_exists($repo_path . "/rocketgit/dirty"))
					@unlink($repo_path . "/rocketgit/dirty");
				rg_sql_free_result($res2);
			}
		}
		rg_sql_free_result($res);
	}

	rg_log("Compute repository sizes per user...");
	$sql = "SELECT SUM(disk_used_mb) AS disk_used_mb, uid FROM repos"
		. " GROUP BY uid";
	$res = rg_sql_query($db, $sql);
	if ($res === FALSE) {
		rg_log("Cannot run query (" . rg_sql_error() . ")!");
	} else {
		while (($row = rg_sql_fetch_array($res))) {
			$sql = "UPDATE users"
				. " SET disk_used_mb = " . $row['disk_used_mb']
				. " WHERE uid = " . $row['uid'];
			$res2 = rg_sql_query($db, $sql);
			rg_sql_free_result($res2);
		}
		rg_sql_free_result($res);
	}
}

// TODO
//rg_log("Sending notifications...");

if (date("H") == 0) {
	rg_log("Clean old forget_pass entries...");
	$sql = "DELETE FROM forgot_pass WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	rg_sql_free_result($res);
}

if (date("H") == 1) {
	rg_log("Clean old tokens...");
	$sql = "DELETE FROM tokens WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	rg_sql_free_result($res);
}

if (date("H") == 1) {
	rg_log("Clean old sess entries...");
	$sql = "DELETE FROM sess WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	rg_sql_free_result($res);
}

rg_keys_regen($db);

// Arhive deleted repositories
if (date("H") == 23) {
	//TODO: rg_log("Delete repositories...");
}

rg_log("Done!");
?>
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