Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Restructures code to have single Git instance

Author Dave Jarvis <email>
Date 2026-02-09 21:53:16 GMT-0800
Commit ec459c6a0b77e10a305ad33c9f5bd4bb35b95e3e
Parent c5ace60
Delta 50 lines added, 32 lines removed, 18-line increase
CommitsPage.php
private $hash;
- public function __construct($allRepos, $currentRepo, $git, $hash) {
- parent::__construct($allRepos);
+ public function __construct(array $repositories, array $currentRepo, Git $git, string $hash) {
+ parent::__construct($repositories);
$this->currentRepo = $currentRepo;
$this->git = $git;
$this->hash = $hash;
$this->title = $currentRepo['name'];
}
public function render() {
$this->renderLayout(function() {
+ // Use local private $git
$main = $this->git->getMainBranch();
+
if (!$main) {
echo '<div class="empty-state"><h3>No branches</h3><p>Empty repository.</p></div>';
FilePage.php
private $hash;
- public function __construct( $allRepos, $currentRepo, $git, $hash ) {
- parent::__construct( $allRepos );
+ public function __construct(array $repositories, array $currentRepo, Git $git, string $hash = '') {
+ parent::__construct($repositories);
$this->currentRepo = $currentRepo;
$this->git = $git;
$this->hash = $hash;
$this->title = $currentRepo['name'];
}
public function render() {
$this->renderLayout( function() {
+ // Use the injected private Git instance
$main = $this->git->getMainBranch();
if( !$main ) {
echo '<div class="empty-state"><h3>No branches</h3></div>';
-
return;
}
$target = $this->hash ?: $main['hash'];
$entries = [];
+ // Use the injected private Git instance
$this->git->walk( $target, function( $file ) use ( &$entries ) {
$entries[] = $file;
usort( $entries, function( $a, $b ) {
-
return $a->compare( $b );
} );
private function renderBlob( $targetHash ) {
$repoParam = '&repo=' . urlencode( $this->currentRepo['safe_name'] );
+
+ // Use the injected private Git instance
$size = $this->git->getObjectSize( $targetHash );
$buffer = '';
+ // Use the injected private Git instance
$this->git->stream( $targetHash, function( $d ) use ( &$buffer ) {
if( strlen( $buffer ) < 12 ) $buffer .= $d;
} else {
$content = '';
+ // Use the injected private Git instance
$this->git->stream( $targetHash, function( $d ) use ( &$content ) { $content .= $d; } );
echo '<div class="blob-content"><pre class="blob-code">' . htmlspecialchars( $content ) . '</pre></div>';
Git.php
public function __construct( string $repoPath ) {
- $this->path = rtrim( $repoPath, '/' );
- $this->objPath = $this->path . '/objects';
- $this->packFiles = glob( "{$this->objPath}/pack/*.idx" ) ?: [];
+ $this->setRepository($repoPath);
}
public function __destruct() {
foreach( $this->fileHandles as $handle ) {
if( is_resource( $handle ) ) {
fclose( $handle );
}
}
+ }
+
+ public function setRepository($repoPath) {
+ $this->path = rtrim( $repoPath, '/' );
+ $this->objPath = $this->path . '/objects';
+ $this->packFiles = glob( "{$this->objPath}/pack/*.idx" ) ?: [];
}
HomePage.php
<?php
class HomePage extends BasePage {
+ private $git;
+
+ public function __construct(array $repositories, Git $git) {
+ parent::__construct($repositories);
+ $this->git = $git;
+ }
+
public function render() {
$this->renderLayout(function() {
echo '<h2>Repositories</h2>';
if (empty($this->repositories)) {
- echo '<div class="empty-state">No repositories found in ' . htmlspecialchars(Config::getReposPath()) . '</div>';
+ echo '<div class="empty-state">No repositories found.</div>';
return;
}
private function renderRepoCard($repo) {
- $git = new Git($repo['path']);
- $main = $git->getMainBranch();
+ // REUSE: Re-target the single Git instance
+ $this->git->setRepository($repo['path']);
+
+ $main = $this->git->getMainBranch();
$stats = ['branches' => 0, 'tags' => 0];
- $git->eachBranch(function() use (&$stats) { $stats['branches']++; });
- $git->eachTag(function() use (&$stats) { $stats['tags']++; });
+ $this->git->eachBranch(function() use (&$stats) { $stats['branches']++; });
+ $this->git->eachTag(function() use (&$stats) { $stats['tags']++; });
echo '<a href="?repo=' . urlencode($repo['safe_name']) . '" class="repo-card">';
echo '<h3>' . htmlspecialchars($repo['name']) . '</h3>';
if ($main) echo '<p>Branch: ' . htmlspecialchars($main['name']) . '</p>';
echo '<p>' . $stats['branches'] . ' branches, ' . $stats['tags'] . ' tags</p>';
if ($main) {
- $git->history('HEAD', 1, function($c) use ($repo) {
+ $this->git->history('HEAD', 1, function($c) use ($repo) {
$renderer = new HtmlFileRenderer($repo['safe_name']);
echo '<p class="repo-card-time">';
Router.php
require_once 'Views.php';
require_once 'RepositoryList.php';
+require_once 'Git.php';
class Router {
- private $repositories;
+ private $repositories = [];
+ private $git;
- public function __construct(array $repositories) {
- $this->repositories = $repositories;
+ public function __construct(string $reposPath) {
+ $this->git = new Git($reposPath);
+
+ $list = new RepositoryList($reposPath);
+ $list->eachRepository(function($repo) {
+ $this->repositories[] = $repo;
+ });
}
if (!$currentRepo) {
- return new HomePage($this->repositories);
+ return new HomePage($this->repositories, $this->git);
}
- $git = new Git($currentRepo['path']);
+ $this->git->setRepository($currentRepo['path']);
if ($action === 'raw') {
- return new RawPage($git, $hash);
+ return new RawPage($this->git, $hash);
}
if ($action === 'commits') {
- return new CommitsPage($this->repositories, $currentRepo, $git, $hash);
+ return new CommitsPage($this->repositories, $currentRepo, $this->git, $hash);
}
- return new FilePage($this->repositories, $currentRepo, $git, $hash);
+ return new FilePage($this->repositories, $currentRepo, $this->git, $hash);
}
index.php
<?php
-require_once 'Git.php';
require_once 'Config.php';
require_once 'Page.php';
require_once 'Router.php';
-require_once 'RepositoryList.php';
Config::init();
-
-$repositories = [];
-$list = new RepositoryList(Config::getReposPath());
-$list->eachRepository(function($repo) use (&$repositories) {
- $repositories[] = $repo;
-});
-$router = new Router($repositories);
+$router = new Router(Config::getReposPath());
$page = $router->route();
$page->render();