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 / git2.php (2dd04903f536288f8a81f7720eea8ab6573f340e) (4,740B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$test_normal = TRUE;

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

rg_log_set_file("git2.log");

require_once("common.php");

$_testns = 'git2';


prepare_http();

system("./git2_helper.sh &>git2_helper.sh.log");
$_err = file_get_contents('git2_helper.sh.log');
@unlink('git2_helper.sh.log');
if (!file_exists("git2/.git/refs/heads/group1/branch1")) {
	rg_log("Something wrong generating the git2 tree: " . $_err);
	exit(1);
}

// test rg_git_refs
$refs = rg_git_refs("git2/.git");
if (strcmp($refs['tag'][0], "unannot1") != 0) {
	rg_log("Could not create an un-annotated tag: " . print_r($refs, TRUE));
	exit(1);
}
if (strcmp($refs['tag'][1], "v1.0") != 0) {
	rg_log("Could not create an annotated tag: " . print_r($refs, TRUE));
	exit(1);
}
if (strcmp($refs['branch'][0], "group1/branch1") != 0) {
	rg_log("Could not create branch group1/branch1: " . print_r($refs, TRUE));
	exit(1);
}


rg_test_create_user($db, $rg_ui);
rg_test_create_repo($db, $rg_ui, $repo);
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}


rg_test_upload_ssh_key($db, $rg_ui, 'git2', $kn);
putenv('GIT_SSH_COMMAND=ssh -o ControlMaster=no -o IdentityFile=' . __DIR__ . '/keys/' . $kn
	. ' -o IdentitiesOnly=yes');

rg_log('');
rg_log_enter('Trying to push master...');
$remote = 'ssh://rocketgit@' . $rg_ssh_host . ':' .$rg_ssh_port
	. '/user/' . escapeshellarg($rg_ui['username'])
	. '/' . escapeshellarg($repo['name']);
$r = rg_exec("cd git2 && git remote add origin $remote"
	. " && git push origin master",
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('out: ' . print_r($r, TRUE));
	rg_log('Seems I cannot push master!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Go back one commit and try to push (must fail)...');
$r = rg_exec("cd git2 && git reset --hard HEAD^1"
	. " && git push origin master",
	'', FALSE, FALSE, FALSE);
if ($r['ok'] == 1) {
	rg_log('Seems I can push master and I must not!');
	exit(1);
}
if (!strstr($r['stderr'], 'tip of your current branch is behind')) {
	rg_log('The remote error message is not about being behind!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Go back one commit and try to push (force)...');
$r = rg_exec("cd git2 && git push --force origin master",
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('out: ' . $r['errmsg']);
	rg_log("Seems I cannot force push the master! err=$err");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Trying to push tags...');
$r = rg_exec("cd git2 && git push --tags",
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('error: ' . $r['errmsg']);
	rg_log("Seems I cannot push tags!");
	exit(1);
}
rg_log_exit();


$commit = trim(file_get_contents('git2/.git/refs/heads/master'));
rg_log("Loaded master from .git: $commit");

rg_log('');
rg_log_enter('Checking on web that everything is OK...');
$data = array();
$headers = array();
$r = do_req($test_url . '/user/' . rawurlencode($rg_ui['username'])
	. '/' . rawurlencode($repo['name']) . '/source/log/commit/'
	. $commit, $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load master commit!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Checking on web that tag is OK...');
$data = array();
$headers = array();
$r = do_req($test_url . '/user/' . rawurlencode($rg_ui['username'])
	. '/' . rawurlencode($repo['name'])
	. '/source/tree/tag/unannot1/blob/a', $data, $headers);
if (strstr($r['body'], 'a signature') === FALSE) {
	rg_log_ml("r: " . print_r($r, TRUE));
	rg_log_ml('Cannot see \'a signature\'!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Delete a branch must make it disappear also from web...');
// Pushing a branch
$r = rg_exec("cd git2"
	. " && git checkout -b branch22"
	. " && git push origin branch22",
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('out: ' . $r['errmsg']);
	rg_log("Seems I cannot push branch22! err=$err");
	exit(1);
}
// Deleting the branch
$r = rg_exec("cd git2 && git push origin :branch22",
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('out: ' . $r['errmsg']);
	rg_log("Seems I cannot delete branch22! err=$err");
	exit(1);
}
// Now, the branch should not be visible anymore
$headers = array();
$r = do_req($test_url . '/user/' . rawurlencode($rg_ui['username'])
	. '/' . rawurlencode($repo['name']) . '/source', $data, $headers);
if (strstr($r['body'], 'branch22') !== FALSE) {
	rg_log_ml('Seems the branch is still on the web!');
	exit(1);
}
rg_log_exit();


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