← Back
mainapp/controllers/BranchController.php
<?php

class BranchController
{
  private array $cfg;
  public function __construct(array $cfg){ $this->cfg=$cfg; }

  public function show(int $repoId): void {
    $db = db();
    $uid = require_auth();

    if (!repo_can_read($db, $repoId, $uid)) { http_response_code(403); exit("403"); }
    $repo = repo_get($db, $repoId);

    $st = $db->prepare("SELECT name, head_commit_id FROM branches WHERE repo_id=? ORDER BY name ASC");
    $st->execute([$repoId]);
    $branches = $st->fetchAll();

    $canWrite = repo_can_write($db, $repoId, $uid);
    $isOwner = ((int)$repo['owner_id'] === (int)$uid);

    $title = $repo['owner_username'] . "/" . $repo['name'] . " / Branches";
    require __DIR__ . '/../views/repos/branches.php';
  }

  public function create(int $repoId): void {
    $db = db();
    $uid = require_auth();
    csrf_verify();

    if (!repo_can_write($db, $repoId, $uid)) { http_response_code(403); exit("403"); }
    $repo = repo_get($db, $repoId);

    $from = safe_branch($_POST['from_branch'] ?? $repo['default_branch']);
    $new  = safe_branch($_POST['new_branch'] ?? '');

    $blobs = new BlobStore($this->cfg['storage_base']);
    $engine = new RepoEngine($blobs);
    $engine->createBranch($db, $repoId, $from, $new);

    flash_set('ok', "Branch created: $new");
    redirect("/r/$repoId/branches");
  }
}