git repos / sago_web_git

src/application.h

raw · blame · history

/*
MIT License

Copyright (c) 2026 Poul Sander

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#pragma once

#include <cppcms/application.h>

#include <memory>
#include <string>
#include <vector>

class RepoScanner;
class GitRepo;

// Serves the multi-repository git frontend. One instance per worker thread;
// the shared RepoScanner is read-only after construction.
class GitApp : public cppcms::application {
public:
    GitApp(cppcms::service &srv, const RepoScanner *scanner);

    // Defaults every response to UTF-8 before dispatching; handlers that emit
    // other content types (raw files, static assets) override it themselves.
    void main(std::string url) override;

private:
    void frontpage();
    void about();
    void repos_list();
    void repo_home(std::string name);
    void tree(std::string name, std::string path);
    void raw(std::string name, std::string path);
    void blame(std::string name, std::string path);
    void log(std::string name);
    void object(std::string name, std::string oid);
    void tags(std::string name);
    void tag(std::string name, std::string tag_name);
    void tag_archive(std::string name, std::string tag_name);
    void branches(std::string name);
    void commit(std::string name, std::string oid);
    void commit_patch(std::string name, std::string oid);
    void static_file(std::string rel);

    // Serves Git's Smart HTTP protocol (clone/fetch) by proxying to
    // `git http-backend`. `endpoint` is "info/refs" or "git-upload-pack".
    void git_http(std::string name, std::string endpoint);

    // Opens the named repo, or writes a 404 and returns nullptr.
    std::unique_ptr<GitRepo> open(const std::string &name);

    // Resolves the commit to display from the "id" query parameter (or HEAD).
    // Throws GitError on an invalid id.
    std::string commit_from_request(const GitRepo &repo);

    void show_error(int code, const std::string &message);

    const RepoScanner *scanner_;
    std::string static_root_;
    std::string cache_root_; // where generated tag archives are cached
    std::vector<std::string> github_hosts_; // extra GitHub Enterprise hostnames
    bool allow_clone_ = false; // serve git clone over HTTP (ALLOW_GIT_CLONE)
};