BarryServer : Git

All the code for all my projects
// BarryServer : Git / barryserver-git / blob / a6603d573a369feda0bc772d59bc48c3d1397442 / blob.php

// Related

barryserver-git

Barry Splitting into more pages + commit pages a6603d5 (2 years, 3 months ago)
<?php require_once $_SERVER["DOCUMENT_ROOT"]."/config.php"; ?>
<?php $SIDE_LINKS = array(); ?>
<?php
	ob_start();
	if (!isset($_GET["repo"])) {header("Location: /");die();}
	$repo = $_GET["repo"]??"";
	if (isset($_GET["repo"])) {
		$PAGE_TITLE = $repo;
	}

	if (!is_dir(GIT_DIR."/".$repo.".git")) { ?>
<?php require_once $_SERVER["DOCUMENT_ROOT"]."/head.php"; ?>
<main>
	<h1>Repository not found</h1>
	<article>The repository you were looking for was not found on this server. Please check the URL and look for errors. If there are no errors, then the repository does not exist. Please use the index to reach any repositories you wish to access.</article>
</main>
<?php require_once $_SERVER["DOCUMENT_ROOT"]."/foot.php"; ?>
<?php	die();}
	$branch = $_GET["object"]??DEFAULT_BRANCH;
	$path = $_GET["path"]??".";
	chdir(GIT_DIR."/".$repo.".git");
	if ($branch === "") {
		header("Location: /".$repo."/tree/");
		die();
	} else {
		if ($path == ".") {
			header("Location: /".$repo."/tree/".$branch);
			die();
		}
		$files = shell_exec("git ls-tree --full-name ".escapeshellarg($branch)." ".escapeshellarg($path));
		if (substr($path,-1) !== "/" && $path !== "") {
			if (explode(" ",explode("\n",$files)[0])[1] === "tree") {
				header("Location: /".$repo."/tree/".$branch."/".$path."/");
				die();
			}
		}
		if (substr($path,-1) === "/" && $path !== "") {
			if (count(explode("\n",$files)) > 1) {
				header("Location: /".$repo."/tree/".$branch."/".$path);
				die();
			}
		}
		if (substr($path,-1) === "/" && $path !== "") {
			if (explode(" ",explode("\n",shell_exec("git ls-tree --full-name ".escapeshellarg($branch)." ".escapeshellarg(substr($path,0,-1))))[0])[1] === "blob") {
				header("Location: /".$repo."/blob/".$branch."/".substr($path,0,-1));
				die();
			}
		}
		$SIDE_LINKS["git clone"] = "git://".$_SERVER["SERVER_NAME"]."/".$repo.".git";
		$SIDE_LINKS["Raw View"] = "/".$repo."/raw/".$branch."/".$path;
		$SIDE_LINKS["Show History"] = "/".$repo."/commits/".$branch."/".$path;
		$PAGE_TITLE = basename($path)." @ ".$PAGE_TITLE;
		require_once $_SERVER["DOCUMENT_ROOT"]."/head.php"; ?>
<main>
	<h1><?= $repo ?></h1>
<?php           $lastcommit = shell_exec("git log --summary --quiet --oneline --abbrev-commit --format=format:'<b>%an</b> %s <span><a href=\"/".escapeshellcmd($repo)."/commit/%H/".escapeshellcmd($path)."\">%h</a> (%ar)</span>' -n 1 ".escapeshellarg($branch)." -- ".escapeshellarg($path));
		echo "<span class=\"last-commit\">".$lastcommit."</span>";
		if (shell_exec("git ls-tree --full-name ".escapeshellarg($branch)." ".escapeshellarg($path))) {
			$content = shell_exec("git cat-file -p ".explode("\t",explode(" ",explode("\n",$files)[0])[2])[0]);
			if (isset($_GET["raw"])) {
				ob_clean();
				header("Content-type: text/plain");
				echo $content;
				die();
			}
			echo "<pre class=\"prettyprint\">\n".htmlspecialchars($content)."</pre>\n";
		} else {
			header("Location: .");
			die();
		}
	}
?>
</main>
<?php require_once $_SERVER["DOCUMENT_ROOT"]."/foot.php"; ?>