git repos / sago_web_git

src/util.h

browsing at commit = 118f7b3050759438e47061859b2ee856af44ebfc

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 <cstdint>
#include <ctime>
#include <string>
#include <vector>

namespace util {

// True if every character is a lowercase hex digit and the length is plausible
// for an abbreviated or full git object id.
bool is_hex_oid(const std::string &s);

// Human readable byte size, e.g. "1.2 KiB".
std::string human_size(uint64_t bytes);

// UTC timestamp formatted as "YYYY-MM-DD HH:MM".
std::string format_time(std::time_t t);

// Last path component of a '/'-separated path.
std::string basename(const std::string &path);

// Everything before the last '/', or "" if there is no '/'.
std::string parent_path(const std::string &path);

// Remove leading and trailing '/' characters.
std::string trim_slashes(const std::string &path);

// Replaces every character outside [A-Za-z0-9._-] with '_', so the result is
// safe to use as a single filesystem path component.
std::string sanitize_filename(const std::string &name);

// Whether author/committer/tagger email addresses may be shown. False unless
// the SHOW_EMAILS environment variable is "1". Evaluated once.
bool show_emails();

// True for paths ending in a markdown extension (.md, .markdown), case
// insensitive.
bool is_markdown_path(const std::string &path);

// MIME type for a recognised image path by extension (e.g. "image/png"), or
// "" when the path is not a recognised image. Case insensitive.
std::string image_mime_type(const std::string &path);

// Canonical clone URLs for a GitHub repository. Both strings are "" when the
// remote does not point at a recognised GitHub host.
struct GitHubClone {
    std::string ssh;   // git@<host>:owner/repo.git
    std::string https; // https://<host>/owner/repo.git
};

// Parses a git remote URL (scp-like, ssh://, https://, git://) and, when it
// points at github.com or one of the configured GitHub Enterprise hosts in
// `enterprise_hosts`, returns the canonical ssh and https clone URLs for that
// host. Host comparison is case insensitive.
GitHubClone github_clone_urls(const std::string &remote_url,
                              const std::vector<std::string> &enterprise_hosts);

} // namespace util