Skip to content

Instantly share code, notes, and snippets.

@tovic tovic/git.php
Last active Dec 29, 2018

Embed
What would you like to do?
GitHub Repository Browser API
<?php
if (!isset($_GET['r'])) {
echo '<p style="color:red;">Missing `r` parameter.</p>';
exit;
}
// <https://developer.github.com/apps/building-oauth-apps/creating-an-oauth-app>
$user = '03d2df6cd302*******'; // client ID
$token = '4716bf841e847b86e616467a0f618e4d4*******'; // client secret
$repo = $_GET['r'];
$type = (int) ($_GET['k'] ?? 0);
function fetch($url, $agent = null) {
$agent = $agent ?? 'Mecha/2.2.0 (+https://mecha-cms.com)';
if (extension_loaded('curl')) {
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_FAILONERROR => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPGET => true,
CURLOPT_MAXREDIRS => 2,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_USERAGENT => $agent
]);
$out = curl_exec($curl);
curl_close($curl);
} else {
$out = file_get_contents($url, false, stream_context_create([
'http' => [
'method' => 'GET',
// <https://tools.ietf.org/html/rfc7231#section-5.5.3>
'header' => 'User-Agent: ' . $agent
]
]));
}
return $out !== false ? json_decode($out, true) : [];
}
$meta = fetch('https://api.github.com/repos/' . $repo . '?client_id=' . $user . '&client_secret=' . $token);
$data = fetch('https://api.github.com/repos/' . $repo . '/git/' . ($type === 0 ? 'trees' : 'blobs') . '/' . ($_GET['v'] ?? 'master') . '?client_id=' . $user . '&client_secret=' . $token);
// var_dump($meta, $data);
?>
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<title><?php echo $meta['full_name']; ?></title>
<meta name="description" content="<?php echo $meta['description']; ?>">
<?php if ($type === 1): ?>
<link href="//fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/zenburn.min.css" rel="stylesheet">
<?php endif; ?>
<style>*{margin:0;padding:0}html,body{height:100%;min-height:100%;overflow:auto;font:normal normal 13px/1.5 Helmet,FreeSans,sans-serif}body{position:relative}pre{position:absolute;top:0;right:0;bottom:0;left:0;overflow:auto;white-space:pre;font-family:"Roboto Mono",Consolas,"Courier New",Courier,"Nimbus Mono L",monospace;font-size:11px}pre code,pre .hljs{padding:20px;font:inherit;min-height:100%;box-sizing:border-box}ul{padding:20px 20px 20px 40px}li{list-style:disc}</style>
</head>
<body>
<?php if ($type === 1): ?>
<pre><code><?php echo htmlentities(base64_decode($data['content'])); ?></code></pre>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script>hljs.initHighlighting();</script>
<?php elseif ($type === 0): ?>
<ul>
<?php foreach ($data['tree'] as $v): ?>
<?php $n = basename(__FILE__); ?>
<?php if ($v['type'] === 'tree'): ?>
<li><a href="/<?php echo $n; ?>?r=<?php echo $repo; ?>&amp;v=<?php echo $v['sha']; ?>&amp;k=0"><?php echo $v['path']; ?>/</a></li>
<?php else: ?>
<li><a href="/<?php echo $n; ?>?r=<?php echo $repo; ?>&amp;v=<?php echo $v['sha']; ?>&amp;k=1" target="_blank"><?php echo $v['path']; ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>Error: <code>k</code> value.</p>
<?php endif; ?>
</body>
</html>
@tovic

This comment has been minimized.

Copy link
Owner Author

commented Dec 28, 2018

Usage

http://127.0.0.1/git.php?r=:user/:repo
  • :user → your user name
  • :repo → your repo name

Example

http://127.0.0.1/git.php?r=mecha-cms/genome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.