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> / tests / git.php (cc0d9ab541470af1f301e3fd7f8ed16a54199f22) (6,912B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/git.inc.php");

rg_log_set_file("git.log");

$rg_no_db = TRUE;
require_once("common.php");

$rg_git_debug = 100;
$patch_limit = rg_git_patch_limit($db);


rg_log('');
rg_log_enter('Testing rg_git_merge_base on an empty repo');
$r = rg_exec('mkdir -p temp_repos'
	. ' && rm -rf temp_repos/git_empty'
	. ' && mkdir temp_repos/git_empty'
	. ' && cd temp_repos/git_empty'
	. ' && git init',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Could not create repo: ' . $r['errmsg'] . '!');
	exit(1);
}
$r = rg_git_merge_base('temp_repos/git_empty', 'master',
	'3fa5ae7e933e33308a6f3ec70edcf2499c1914ac');
if ($r !== $rg_git_empty) {
	rg_log('Test must return ' . $rg_git_empty . ' but returned: ' . $r);
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Testing rg_git_log with a rename');
$r = rg_exec('mkdir -p temp_repos'
	. ' && rm -rf temp_repos/git_rename'
	. ' && mkdir temp_repos/git_rename'
	. ' && cd temp_repos/git_rename'
	. ' && git init'
	. ' && seq 0 1000 > a'
	. ' && echo yyy > b && git add a b && git commit -a -m "aaa"'
	. ' && cp a aclone && git add aclone'
	. ' && git mv b b2'
	. ' && git commit -a -m "bbb"',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Could not exec repo script: ' . $r['errmsg'] . '!');
	exit(1);
}
$r = rg_git_log('temp_repos/git_rename/.git', 1, '', '', TRUE, $patch_limit);
if (@strcmp($r[0]['files']['aclone']['file_from'], 'a') != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][aclone][file_from] is not "a" ('
		. @$r[0]['files']['aclone']['file_from'] . ')!');
	exit(1);
}
if (@$r[0]['files']['aclone']['lines_add'] != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][aclone][lines_add] is not 0 ('
		. @$r[0]['files']['aclone']['lines_add'] . ')!');
	exit(1);
}
if (@!strstr($r[0]['files']['aclone']['flags'], 'C')) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][aclone][flags] does not containc "C" ('
		. @$r[0]['files']['aclone']['flags'] . ')!');
	exit(1);
}
if (@strcmp($r[0]['files']['b2']['file_from'], 'b') != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][b2][file_from] is not "b" ('
		. @$r[0]['files']['b2']['file_from'] . ')!');
	exit(1);
}
if (@$r[0]['files']['b2']['lines_add'] != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][b2][lines_add] is not 0 ('
		. @$r[0]['files']['b2']['lines_add'] . ')!');
	exit(1);
}
if (@!strstr($r[0]['files']['b2']['flags'], 'R')) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][b2][flags] does not containc "C" ('
		. @$r[0]['files']['b2']['flags'] . ')!');
	exit(1);
}
system("rm -rf temp_repos/git_rename");
rg_log_exit();


rg_log('');
rg_log_enter('Testing rg_git_log for binary files');
$r = rg_exec('rm -rf temp_repos/git_bin'
	. ' && mkdir temp_repos/git_bin'
	. ' && cd temp_repos/git_bin'
	. ' && git init'
	. ' && seq 0 100 | while read a; do echo -en "\x`printf "%02x" ${a}`"; done > a'
	. ' && git add a && git commit -a -m "aaa"'
	. ' && echo -e \x0ff\x03\x90 > a && git commit -a -m "bbb"',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Could not exec repo script: ' . $r['errmsg'] . '!');
	exit(1);
}
$r = rg_git_log('temp_repos/git_bin/.git', 0, '', '', TRUE, $patch_limit);
if (@$r[0]['vars']['lines_add'] != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][vars][lines_add] is not 0 ('
		. @$r[0]['vars']['lines_add'] . ')!');
	exit(1);
}
if (@$r[0]['vars']['lines_del'] != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][vars][lines_del] is not 0 ('
		. @$r[0]['vars']['lines_del'] . ')!');
	exit(1);
}
if (@strcmp($r[0]['files']['a']['mode'], '100644') != 0) {
	rg_log_ml(print_r($r, TRUE));
	rg_log('git_bin: r[0][files][a][mode] is not 100644 ('
		. @$r[0]['files']['a']['mode'] . ')!');
	exit(1);
}
system("rm -rf temp_repos/git_bin");
rg_log_exit();


rg_log('');
rg_log_enter('Testing git_reference...');
$s = "refs";
$e = $s;
$r = rg_git_reference($s);
if (strcmp($r, $e) != 0) {
	rg_log("git_reference($s) is not working");
	exit(1);
}

$s = "refs/heads/x";
$e = $s;
$r = rg_git_reference($s);
if (strcmp($r, $e) != 0) {
	rg_log('git_reference($s) is not working (!= ' . $e . ')');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter("[*] Checking if git-init works...");
$r = rg_git_init("git.tmp");
if ($r !== TRUE) {
	rg_log("Cannot run rg_git_init (" . rg_git_error() . ")!");
	exit(1);
}
rg_log_exit();



/*
rg_log("[*] Populate repo...");
file_put_contents("git.tmp/a", "aaa");
system("cd git.tmp; git commit -a -m \"aa\"; git checkout -n b1");
// This is a bare repo! Could not work!
*/

rg_log("[*] Testing rg_git_refs...");
$refs = rg_git_refs("git.tmp");
//TODO print_r($refs);

system("rm -rf git.tmp");


rg_log('');
rg_log_enter('Testing rg_git_log with a big diff...');
$r = rg_exec('mkdir -p temp_repos && cd temp_repos'
	. ' && rm -rf git_big_diff && mkdir git_big_diff && cd git_big_diff'
	. ' && git init'
	. ' && echo -e "line1\nline2\nline3" > a'
	. ' && echo -e "aaaa" > b'
	. ' && git add a b'
	. ' && git commit -a -m "aaa"'
	. ' && echo -e "\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx" > a'
	. ' && echo -e "bbbb" > b'
	. ' && git commit -a -m "bbb"'
	. ' && echo -e "c" > a'
	. ' && echo -e "cccc" > b'
	. ' && git commit -a -m "ccc"'
	. ' && echo -e "\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx" > a'
	. ' && echo -e "dddd" > b'
	. ' && git commit -a -m "ddd"'
	. ' && echo -e "e" > a'
	. ' && echo -e "eeee" > b'
	. ' && git commit -a -m "eee"'
	. ' && echo -e "f" > a'
	. ' && echo -e "ffff" > b'
	. ' && git commit -a -m "fff"'
	. ' && echo -e "g" > a'
	. ' && echo -e "gggg" > b'
	. ' && git commit -a -m "ggg"',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Could not exec repo script: ' . $r['errmsg'] . '!');
	exit(1);
}
$r = rg_git_log('temp_repos/git_big_diff/.git', 0, '', '', TRUE /*also_patch*/,
	5 /*patch_limit*/);
if (empty($r[1]['files']['b']['chunks'])) {
	rg_log_ml(@print_r($r[1], TRUE));
	rg_log('r[1][files][b][chunks] must not be empty!');
	exit(1);
}
if (@$r[2]['vars']['lines_add'] != 2) {
	rg_log_ml(@print_r($r[2], TRUE));
	rg_log('lines_add is != 2 (' . @$r[2]['vars']['lines_add'] . ')');
	exit(1);
}
if (@$r[2]['files']['a']['oversize_diff'] != 1) {
	rg_log_ml('r[2]: ' . @print_r($r[2], TRUE));
	rg_log('r[2][files][a][oversize_diff] is not 1 ('
		. @@$r[2]['files']['a']['oversize_diff'] . ')!');
	exit(1);
}
if (!empty($r[2]['files']['a']['chunks'])) {
	rg_log_ml('r[2]: ' . @print_r($r[2], TRUE));
	rg_log('r[2][files][a][chunks] is not empty and file a has a big diff!');
	exit(1);
}
system('rm -rf temp_repos/git_big_diff');
rg_log_exit();


// TODO: test with one good/bad commit and with an empty one


rg_log('OK!');
?>
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