SlHelpers
Commit.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <optional>
6 #include <string>
7 
8 #include <git2.h>
9 
10 #include "Helpers.h"
11 #include "Object.h"
12 
13 namespace SlGit {
14 
15 class Repo;
16 class Tree;
17 
21 class Commit : public TypedObject<git_commit> {
22  using GitTy = git_commit;
23 
24  friend class Repo;
25 public:
26  Commit() = delete;
27 
29  std::optional<Commit> parent(unsigned int nth = 0) const noexcept;
31  std::optional<Commit> ancestor(unsigned int nth = 0) const noexcept;
32 
34  std::optional<Tree> tree() const noexcept;
35 
37  const git_oid *treeId() const noexcept { return git_commit_tree_id(commit()); }
39  std::string treeIdStr() const noexcept { return Helpers::oidToStr(*treeId()); }
41  std::string messageEncoding() const noexcept {
42  return git_commit_message_encoding(commit());
43  }
45  std::string message() const noexcept { return git_commit_message(commit()); }
47  std::string summary() const noexcept { return git_commit_summary(commit()); }
49  git_time_t time() const noexcept { return git_commit_time(commit()); }
51  int timeOffset() const noexcept { return git_commit_time_offset(commit()); }
53  const git_signature *committer() const noexcept { return git_commit_committer(commit()); }
55  const git_signature *author() const noexcept { return git_commit_author(commit()); }
57  std::string rawHeader() const noexcept { return git_commit_raw_header(commit()); }
58 
60  unsigned int parentCount() const noexcept { return git_commit_parentcount(commit()); }
62  const git_oid *parentId(unsigned int nth) const noexcept {
63  return git_commit_parent_id(commit(), nth);
64  }
65 
67  std::optional<std::string> catFile(const std::string &file) const noexcept;
68 
70  GitTy *commit() const noexcept { return typed(); }
72  operator GitTy *() const noexcept { return commit(); }
73 private:
74  friend class Tag;
75  explicit Commit(const Repo &repo, GitTy *commit) noexcept :
76  TypedObject(repo, commit) { }
77 };
78 
79 }
Definition: Blob.h:11
Git Object class – base for Blob, Commit, Tag, Tree.
Definition: Object.h:65
const git_oid * treeId() const noexcept
Get the OID of the tree of this Commit.
Definition: Commit.h:37
std::optional< Commit > ancestor(unsigned int nth=0) const noexcept
Get the nth generation ancestor of this Commit.
const git_signature * committer() const noexcept
Get the committer of this Commit.
Definition: Commit.h:53
std::optional< Tree > tree() const noexcept
Get the Tree of this Commit.
int timeOffset() const noexcept
Get the timezone offset of this Commit.
Definition: Commit.h:51
GitTy * commit() const noexcept
Get the stored pointer to libgit2&#39;s git_commit.
Definition: Commit.h:70
std::string message() const noexcept
Get the commit message of this Commit.
Definition: Commit.h:45
std::string rawHeader() const noexcept
Get a raw Commit header.
Definition: Commit.h:57
static std::string oidToStr(const git_oid &id)
Convert OID id to string.
Definition: Helpers.h:20
The most important Git class.
Definition: Repo.h:45
unsigned int parentCount() const noexcept
Get count of parents.
Definition: Commit.h:60
Commit is a representation of a git commit.
Definition: Commit.h:21
std::optional< Commit > parent(unsigned int nth=0) const noexcept
Get the nth parent of this Commit.
std::string treeIdStr() const noexcept
Get the SHA of the tree of this Commit.
Definition: Commit.h:39
git_time_t time() const noexcept
Get the time of this Commit.
Definition: Commit.h:49
std::optional< std::string > catFile(const std::string &file) const noexcept
Cat a file in this Commit&#39;s tree.
const git_oid * parentId(unsigned int nth) const noexcept
Get OID of the nth parent.
Definition: Commit.h:62
std::string summary() const noexcept
Get the summary line of this Commit.
Definition: Commit.h:47
git_commit * typed() const noexcept
Get the stored pointer typed to one of libgit2&#39;s types.
Definition: Object.h:83
std::string messageEncoding() const noexcept
Get the commit message encoding of this Commit.
Definition: Commit.h:41
Tag is a representation of a git tag.
Definition: Tag.h:23
const Repo & repo() const
Get the Repo this Object lives in.
Definition: Object.h:38
const git_signature * author() const noexcept
Get the author of this Commit.
Definition: Commit.h:55