BarryServer : Git

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

// Related

barryserver-git

Barry Display README from currently directory acd7cb2 (1 year, 10 months ago)
<?php require_once $_SERVER["DOCUMENT_ROOT"]."/config.php"; ?>
<?php $SIDE_LINKS = array(); ?>
<?php
	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");
	$commits = shell_exec("git rev-list --all --count");
	if ($commits == 0) {
		$SIDE_LINKS["git clone"] = "git://".$_SERVER["SERVER_NAME"]."/".$repo.".git";
		require_once $_SERVER["DOCUMENT_ROOT"]."/head.php";
		echo "<main><h1>".$repo."</h1>\n";
		echo "No commits yet</main>";
		require_once $_SERVER["DOCUMENT_ROOT"]."/foot.php";
		die();
	}
	if ($branch === "") {
		$SIDE_LINKS["git clone"] = "git://".$_SERVER["SERVER_NAME"]."/".$repo.".git";
		require_once $_SERVER["DOCUMENT_ROOT"]."/head.php";
		echo "<main><h1>".$repo."</h1>\n";
		$branches = shell_exec("git show-branch");
		foreach (explode("\n",$branches) as $cbranch) {
			echo preg_replace("/\[(.+)\] (.+)/","<a href=\"/".$repo."/tree/$1\" class=\"branch\">$1</a><br>",$cbranch);
		}
	} else {
		$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."/blob/".$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";
		if ($path == ".") {$SIDE_LINKS["Branches"] = "/".$repo."/tree/";}
		$SIDE_LINKS["Show History"] = "/".$repo."/commits/".$branch."/".$path;
		if ($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>";
		$folderstxt = "";
		if ($path !== ".") {$folderstxt .= "		<a href=\"../\" class=\"folder\">../</a><br>\n";}
		$filestxt = "";
		foreach (explode("\n",$files) as $file) {
			$name = explode(" ",$file);
			if ($name[1] == "tree") {
				$folderstxt .= "		<a href=\"/".$repo."/tree/".$branch."/".explode("\t",$name[2])[1]."/\" class=\"folder\">".end(explode("/",explode("\t",$name[2])[1]))."/</a><br>\n";
			}
			if ($name[1] == "blob") {
				$filestxt .= "		<a href=\"/".$repo."/blob/".$branch."/".explode("\t",$name[2])[1]."\" class=\"file\">".end(explode("/",explode("\t",$name[2])[1]))."</a><br>\n";
			}
		}
		echo $folderstxt;
		echo $filestxt;
		if ($readmefile = shell_exec("git ls-tree --full-name ".escapeshellarg($branch)." ".escapeshellarg($path)."/README.")) {
			$readme = shell_exec("git cat-file -p ".explode("\t",explode(" ",explode("\n",$readmefile)[0])[2])[0]." | md2html");
			echo "\n		</main><main>".$readme;
		}
	}
?>
</main>
<?php
if ($path === "." && $branch) {
$desc = file_get_contents(GIT_DIR."/".$repo.".git/description");
echo "<main class=\"aside\"><h1>About</h1>".$desc."</main>";
}
?>
<?php require_once $_SERVER["DOCUMENT_ROOT"]."/foot.php"; ?>