SlHelpers
SlGit::Repo Class Reference

The most important Git class. More...

#include <Repo.h>

Public Member Functions

bool checkout (const std::string &branch) const noexcept
 Checkout a branch.
 
bool checkout (const Reference &reference) const noexcept
 Checkout a Reference reference.
 
bool checkoutTree (const Tree &tree, unsigned int strategy=GIT_CHECKOUT_SAFE) const noexcept
 Update index and files to match tree.
 
std::optional< std::string > catFile (const std::string &branch, const std::string &file) const noexcept
 Cat a file in a branch. More...
 
std::variant< Blob, Commit, Tag, Tree, std::monostate > revparseSingle (const std::string &rev) const noexcept
 Parse rev as either blob, commit, tag, or tree.
 
std::optional< BlobblobCreateFromWorkDir (const std::filesystem::path &file) const noexcept
 Create a new Blob from file in workdir.
 
std::optional< BlobblobCreateFromDisk (const std::filesystem::path &file) const noexcept
 Create a new Blob from file.
 
std::optional< BlobblobCreateFromBuffer (const std::string &buf) const noexcept
 Create a new Blob from string buf.
 
std::optional< BlobblobLookup (const git_oid &oid) const noexcept
 Get a Blob corresponding to oid.
 
std::optional< BlobblobLookup (const TreeEntry &tentry) const noexcept
 Get a Blob corresponding to tentry.
 
std::optional< BlobblobRevparseSingle (const std::string &rev) const noexcept
 Parse rev as blob.
 
std::optional< CommitcommitLookup (const git_oid &oid) const noexcept
 Get a Commit corresponding to oid.
 
std::optional< CommitcommitCreate (const Signature &author, const Signature &committer, const std::string &msg, const Tree &tree, const std::vector< const Commit *> &parents={}) const noexcept
 Create a new Commit.
 
std::optional< CommitcommitCreateCheckout (const Signature &author, const Signature &committer, const std::string &msg, const Tree &tree, unsigned int strategy=GIT_CHECKOUT_SAFE, const std::vector< const Commit *> &parents={}) const noexcept
 Create a new Commit and move to it.
 
std::optional< CommitcommitHead () const noexcept
 Get a Commit corresponding to HEAD.
 
std::optional< CommitcommitRevparseSingle (const std::string &rev) const noexcept
 Parse rev as commit.
 
std::optional< Diffdiff (const Commit &commit1, const Commit &commit2, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of commit1 and commit2. More...
 
std::optional< Diffdiff (const Tree &tree1, const Tree &tree2, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of tree1 and tree2. More...
 
std::optional< DiffdiffCached (const Commit &commit, const Index &index, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of a commit and index. More...
 
std::optional< DiffdiffCached (const Tree &tree, const Index &index, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of a tree and index. More...
 
std::optional< DiffdiffCached (const Commit &commit, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of a commit and repo's index.
 
std::optional< DiffdiffCached (const Tree &tree, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of a tree and repo's index.
 
std::optional< DiffdiffWorkdir (const Index &index, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of an index and workdir. More...
 
std::optional< DiffdiffWorkdir (const Commit &commit, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of a commit and workdir.
 
std::optional< DiffdiffWorkdir (const Tree &tree, const git_diff_options *opts=nullptr) const noexcept
 Create a new Diff of a tree and workdir.
 
std::optional< Indexindex () const noexcept
 Get repository's index.
 
std::optional< RemoteremoteCreate (const std::string &name, const std::string &url) const noexcept
 Create a new Remote called name located at url.
 
std::optional< RemoteremoteLookup (const std::string &name) const noexcept
 Get a Remote called name.
 
std::optional< ReferencerefLookup (const std::string &name) const noexcept
 Get a Reference called exactly name (like refs/heads/master)
 
std::optional< ReferencerefDWIM (const std::string &name) const noexcept
 Get a Reference called name (like master)
 
std::optional< ReferencerefCreateDirect (const std::string &name, const git_oid &oid, bool force=false) const noexcept
 Create a new direct Reference to oid called name.
 
std::optional< ReferencerefCreateSymbolic (const std::string &name, const std::string &target, bool force=false) const noexcept
 Create a new symbolic Reference to target called name.
 
std::optional< RevWalkrevWalkCreate () const noexcept
 Create a new RevWalk.
 
std::optional< TagtagCreate (const std::string &tagName, const Object &target, const Signature &tagger, const std::string &message, bool force=false) const noexcept
 Create a new Tag called tagName, pointing at target.
 
std::optional< TagtagLookup (const git_oid &oid) const noexcept
 Get a Tag corresponding to oid.
 
std::optional< TagtagLookup (const TreeEntry &tentry) const noexcept
 Get a Tag corresponding to tentry.
 
std::optional< TagtagRevparseSingle (const std::string &rev) const noexcept
 Parse rev as tag.
 
std::optional< TreetreeLookup (const git_oid &oid) const noexcept
 Get a Tree corresponding to oid.
 
std::optional< TreetreeLookup (const TreeEntry &tentry) const noexcept
 Get a Tree corresponding to tentry.
 
std::optional< TreetreeRevparseSingle (const std::string &rev) const noexcept
 Parse rev as tree.
 
std::optional< TreeBuildertreeBuilderCreate (const Tree *source=nullptr) const noexcept
 Create a new TreeBuilder.
 
std::filesystem::path path () const noexcept
 Get the path to .git.
 
std::filesystem::path workDir () const noexcept
 Get the path to sources.
 
GitTy * repo () const noexcept
 Get the underlying libgit2 pointer.
 
 operator GitTy * () const noexcept
 Get the underlying libgit2 pointer.
 

Static Public Member Functions

static std::optional< Repoinit (const std::filesystem::path &path, bool bare=false, const std::string &originUrl="") noexcept
 init Init an empty repository More...
 
static std::optional< Repoclone (const std::filesystem::path &path, const std::string &url, FetchCallbacks &fc, const std::string &branch="", const unsigned int &depth=0, bool tags=true) noexcept
 clone Clone (and open) an existing repository More...
 
static std::optional< Repoclone (const std::filesystem::path &path, const std::string &url, const std::string &branch="", const unsigned int &depth=0, bool tags=true) noexcept
 clone Clone (and open) an existing repository More...
 
static std::optional< Repoopen (const std::filesystem::path &path=".") noexcept
 Open an existing repository. More...
 
static bool update (const std::filesystem::path &path, const std::string &remote="origin")
 Update/fetch remote remote in repository at path. More...
 
static auto & lastError () noexcept
 Return the last error string if some (from git_last_error())
 
static auto lastClass () noexcept
 Return the last error class (from git_last_error())
 
static auto lastErrno () noexcept
 Return the last error number (returned from git_* functions)
 

Friends

class Diff
 
class Index
 
class PathSpec
 
class Remote
 
class RevWalk
 
class Signature
 
class Tag
 
class Tree
 
class TreeBuilder
 

Detailed Description

The most important Git class.

It is the starting point to work with a git repository using this library.

auto repo = Repo::init("git_repo");
repo->remoteCreate("origin", "ssh://some_host/repo");
auto repo2 = Repo::clone("git", "https://github.com/git/git");
auto HEAD_SHA = repo2->commitHead()->idStr();

Member Function Documentation

◆ catFile()

std::optional<std::string> SlGit::Repo::catFile ( const std::string &  branch,
const std::string &  file 
) const
noexcept

Cat a file in a branch.

Parameters
branchBranch where to look
fileFile to get content of
Returns
File content.

Kind of:

git cat-file

◆ clone() [1/2]

static std::optional<Repo> SlGit::Repo::clone ( const std::filesystem::path &  path,
const std::string &  url,
FetchCallbacks fc,
const std::string &  branch = "",
const unsigned int &  depth = 0,
bool  tags = true 
)
staticnoexcept

clone Clone (and open) an existing repository

Parameters
pathPath where to create the repository
urlURL where to download from
fcFetch callbacks, see FetchCallbacks
branchBranch to download (or empty string)
depthHow deep to download (or zero to download whole history)
tagsDownload also tags?
Returns
Repo on success, nullopt otherwise.
git clone

◆ clone() [2/2]

static std::optional<Repo> SlGit::Repo::clone ( const std::filesystem::path &  path,
const std::string &  url,
const std::string &  branch = "",
const unsigned int &  depth = 0,
bool  tags = true 
)
inlinestaticnoexcept

clone Clone (and open) an existing repository

Parameters
pathPath where to create the repository
urlURL where to download from
branchBranch to download (or empty string)
depthHow deep to download (or zero to download whole history)
tagsDownload also tags?
Returns
Repo on success, nullopt otherwise.
git clone

◆ diff() [1/2]

std::optional<Diff> SlGit::Repo::diff ( const Commit commit1,
const Commit commit2,
const git_diff_options *  opts = nullptr 
) const
noexcept

Create a new Diff of commit1 and commit2.

git diff commit1..commit2

◆ diff() [2/2]

std::optional<Diff> SlGit::Repo::diff ( const Tree tree1,
const Tree tree2,
const git_diff_options *  opts = nullptr 
) const
noexcept

Create a new Diff of tree1 and tree2.

git diff tree1..tree2

◆ diffCached() [1/2]

std::optional<Diff> SlGit::Repo::diffCached ( const Commit commit,
const Index index,
const git_diff_options *  opts = nullptr 
) const
noexcept

Create a new Diff of a commit and index.

git diff --cached commit

◆ diffCached() [2/2]

std::optional<Diff> SlGit::Repo::diffCached ( const Tree tree,
const Index index,
const git_diff_options *  opts = nullptr 
) const
noexcept

Create a new Diff of a tree and index.

git diff --cached tree

◆ diffWorkdir()

std::optional<Diff> SlGit::Repo::diffWorkdir ( const Index index,
const git_diff_options *  opts = nullptr 
) const
noexcept

Create a new Diff of an index and workdir.

git diff

◆ init()

static std::optional<Repo> SlGit::Repo::init ( const std::filesystem::path &  path,
bool  bare = false,
const std::string &  originUrl = "" 
)
staticnoexcept

init Init an empty repository

Parameters
pathPath where to create the repository
bareShould this be bare repo (no sources, only git files)
originUrlSet origin URL to this (or empty string)
Returns
Repo on success, nullopt otherwise.
git init

◆ open()

static std::optional<Repo> SlGit::Repo::open ( const std::filesystem::path &  path = ".")
staticnoexcept

Open an existing repository.

Parameters
pathPath to the repository
Returns
Repo on success, nullopt otherwise.

◆ update()

static bool SlGit::Repo::update ( const std::filesystem::path &  path,
const std::string &  remote = "origin" 
)
static

Update/fetch remote remote in repository at path.

Parameters
pathPath to an existing git repository
remoteRemote to update
Returns
true on success.
git remote-update

The documentation for this class was generated from the following file: