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 (25937f387024661593f41fc77923c4156476910d) (4,537B) (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");
require_once("helpers.inc.php");
require_once("http.inc.php");

rg_log_set_file("git2.log");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'git2';
$rg_cache_enable = TRUE;

system("./git2.sh &>git2.sh.log");
$_err = file_get_contents('git2.sh.log');
@unlink('git2.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_log('Created user ' . $rg_ui['uid']);
rg_test_create_repo($db, $rg_ui, $repo);
rg_log('Created repo ' . $repo['repo_id']);
$r = test_login($test_url, $rg_ui, $good_sid);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}


rg_log('');
rg_log_enter('Uploading a key...');
rg_test_upload_ssh_key($db, $rg_ui, "git2", $good_sid);
rg_log_exit();


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"
	. " && export GIT_SSH_COMMAND=\"ssh -v -o IdentityFile=../keys/git2 -o IdentitiesOnly=yes\""
	. " && git push origin master", '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('out: ' . $r['errmsg']);
	rg_log("Seems I cannot push master! err=$err");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Trying to push tags...');
$r = rg_exec("cd git2"
	. " && export GIT_SSH_COMMAND=\"ssh -v -o IdentityFile=../keys/git2 -o IdentitiesOnly=yes\""
	. " && git push --tags origin", '', 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("Load master from .git: $commit");

rg_log('');
rg_log_enter('Checking on web that everything is OK...');
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$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("Cookie: sid=" . $good_sid);
$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("Cannot see the tag: " . print_r($r, TRUE));
	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"
	. " && export GIT_SSH_COMMAND=\"ssh -v -o IdentityFile=../keys/git2 -o IdentitiesOnly=yes\""
	. " && git checkout -b branch22"
	. " && git push origin branch22", '', 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"
	. " && export GIT_SSH_COMMAND=\"ssh -v -o IdentityFile=../keys/git2 -o IdentitiesOnly=yes\""
	. " && git push origin :branch22", '', 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("Cookie: sid=" . $good_sid);
$r = do_req($test_url . '/user/' . rawurlencode($rg_ui['username'])
	. '/' . rawurlencode($repo['name']) . '/source', $data, $headers);
	rg_log_ml($r['body']);
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