main • app/controllers/RepoController.php
<?php
class RepoController
{
private array $cfg;
public function __construct(array $cfg){ $this->cfg=$cfg; }
public function list(): void {
$uid = require_auth();
$db = db();
// owner repos
$st = $db->prepare("SELECT r.*, u.username as owner_username
FROM repositories r
JOIN users u ON u.id=r.owner_id
WHERE r.owner_id=?
ORDER BY r.id DESC");
$st->execute([$uid]);
$owned = $st->fetchAll();
// collaborator repos
$st = $db->prepare("SELECT r.*, u.username as owner_username, c.role as collab_role
FROM repo_collaborators c
JOIN repositories r ON r.id=c.repo_id
JOIN users u ON u.id=r.owner_id
WHERE c.user_id=?
ORDER BY r.id DESC");
$st->execute([$uid]);
$collab = $st->fetchAll();
$title = "Your Repositories";
require __DIR__ . '/../views/repos/list.php';
}
public function showCreate(): void {
require_auth();
$title = "Create Repo";
require __DIR__ . '/../views/repos/create.php';
}
public function create(): void {
$uid = require_auth();
csrf_verify();
$db = db();
$storage = $this->cfg['storage_base'];
$blobs = new BlobStore($storage);
$engine = new RepoEngine($blobs);
$name = trim($_POST['name'] ?? '');
$vis = $_POST['visibility'] ?? 'public';
$desc = trim($_POST['description'] ?? '') ?: null;
$repoId = $engine->createRepo($db, $uid, $name, $vis, $desc);
flash_set('ok', 'Repository created.');
redirect("/r/$repoId");
}
public function show(int $repoId): void {
$db = db();
$uid = auth_user_id(); // can be null for public repos
if (!repo_can_read($db, $repoId, $uid)) { http_response_code(403); exit("403 Forbidden"); }
$repo = repo_get($db, $repoId);
$branch = safe_branch($_GET['branch'] ?? $repo['default_branch']);
$title = $repo['owner_username'] . "/" . $repo['name'];
$st = $db->prepare("SELECT name, head_commit_id FROM branches WHERE repo_id=? ORDER BY name ASC");
$st->execute([$repoId]);
$branches = $st->fetchAll();
$st = $db->prepare("SELECT c.*, u.username as author_name
FROM commits c JOIN users u ON u.id=c.author_id
WHERE c.repo_id=? AND c.branch_name=?
ORDER BY c.id DESC LIMIT 25");
$st->execute([$repoId, $branch]);
$commits = $st->fetchAll();
$canWrite = repo_can_write($db, $repoId, $uid);
$isOwner = ($uid && (int)$repo['owner_id'] === (int)$uid);
require __DIR__ . '/../views/repos/show.php';
}
}