git repos / sago_web_git

src/util.h

browsing at commit = 55fa1ac0b398c748c7e2421b855a1606790cafc9

raw · blame · history

#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