Code forensics  0.1
Generate historical information about code changes
git_interface.hpp
Go to the documentation of this file.
1 #ifndef GIT_INTERFACE_HPP
4 #define GIT_INTERFACE_HPP
5 
6 #include <git2.h>
7 #include "common.hpp"
8 #include <fmt/core.h>
9 
10 #include <iostream>
11 
13 namespace git {
14 struct exception : public std::exception {
15  Str message;
16  inline exception(int error, const char* funcname) {
17  const git_error* e = git_error_last();
18  message = fmt::format(
19  "Error {}/{} while calling {}: {}",
20  error,
21  funcname,
22  e->klass,
23  e->message);
24  }
25 
26  const char* what() const noexcept override { return message.c_str(); }
27 };
28 } // namespace git
29 
30 
31 // NOLINTNEXTLINE
32 #define __GIT_THROW_EXCEPTION(code, function) \
33  throw git::exception(code, function);
34 
35 namespace git {
36 #include "gitwrap.hpp"
37 }
38 
39 
41 inline Str oid_tostr(git_oid oid) {
42  std::array<char, GIT_OID_HEXSZ + 1> result;
43  git_oid_tostr(result.data(), sizeof(result), &oid);
44  return Str{result.data(), result.size() - 1};
45 }
46 
47 template <>
48 struct fmt::formatter<git_oid> : fmt::formatter<Str> {
49  auto format(CR<git_oid> date, fmt::format_context& ctx) const {
50  return fmt::formatter<Str>::format(oid_tostr(date), ctx);
51  }
52 };
53 
56 inline void tree_walk(
57  const git_tree* tree,
58  git_treewalk_mode mode,
59  Func<int(const char*, const git_tree_entry*)> callback
60 ) {
67  using CB = decltype(callback);
68  CB* allocated = new CB;
69  *allocated = std::move(callback);
71  tree,
72  mode,
73  [](const char* root,
74  const git_tree_entry* entry,
75  void* payload) -> int {
76  CB* impl = static_cast<CB*>(payload);
77  try {
78  auto __result = (*impl)(root, entry);
79  return __result;
80  } catch (...) { throw; }
81  },
82  allocated);
83 }
84 
85 
88  using file_cb_t = Func<int(const git_diff_delta*, float)>;
89 
91 
93  using binary_cb_t = Func<
94  int(const git_diff_delta*, const git_diff_binary*)>;
95 
97 
101  using hunk_cb_t = Func<
102  int(const git_diff_delta*, const git_diff_hunk*)>;
103 
105 
109  using line_cb_t = Func<int(
110  const git_diff_delta*,
111  const git_diff_hunk*,
112  const git_diff_line*)>;
113 
115 };
116 
117 inline void diff_foreach(
118  git_diff* diff,
119  CR<DiffForeachParams> foreach) {
122  diff,
123  [](const git_diff_delta* delta, float progress, void* payload) {
124  auto l = static_cast<DiffForeachParams*>(payload);
125  if (l->file_cb) {
126  return l->file_cb(delta, progress);
127  } else {
128  return 0;
129  }
130  },
131  [](const git_diff_delta* delta,
132  const git_diff_binary* binary,
133  void* payload) {
134  auto l = static_cast<DiffForeachParams*>(payload);
135  if (l->binary_cb) {
136  return l->binary_cb(delta, binary);
137  } else {
138  return 0;
139  }
140  },
141  [](const git_diff_delta* delta,
142  const git_diff_hunk* hunk,
143  void* payload) {
144  auto l = static_cast<DiffForeachParams*>(payload);
145  if (l->hunk_cb) {
146  return l->hunk_cb(delta, hunk);
147  } else {
148  return 0;
149  }
150  },
151  [](const git_diff_delta* delta,
152  const git_diff_hunk* hunk,
153  const git_diff_line* line,
154  void* payload) {
155  auto l = static_cast<DiffForeachParams*>(payload);
156  if (l->line_cb) {
157  return l->line_cb(delta, hunk, line);
158  } else {
159  return 0;
160  }
161  },
162  (void*)&foreach);
163 }
164 
165 namespace std {
167 template <>
168 struct hash<git_oid> {
169  inline std::size_t operator()(const git_oid& it) const {
170  return std::hash<Str>()(
171  Str(reinterpret_cast<const char*>(&it.id[0]), sizeof(it.id)));
172  }
173 };
174 } // namespace std
175 
177 inline bool operator==(CR<git_oid> lhs, CR<git_oid> rhs) {
178  return git::oid_cmp(&lhs, &rhs) == 0;
179 }
180 
181 inline bool operator<(CR<git_oid> lhs, CR<git_oid> rhs) {
182  return git::oid_cmp(&lhs, &rhs) < 0;
183 }
184 
185 
186 inline std::ostream& operator<<(std::ostream& out, git_oid const& oid) {
187  out << oid_tostr(oid);
188  return out;
189 }
190 
191 
192 #endif // GIT_INTERFACE_HPP
DiffForeachParams::line_cb
line_cb_t line_cb
Definition: git_interface.hpp:114
DiffForeachParams::hunk_cb_t
Func< int(const git_diff_delta *, const git_diff_hunk *)> hunk_cb_t
Definition: git_interface.hpp:102
DiffForeachParams::hunk_cb
hunk_cb_t hunk_cb
Definition: git_interface.hpp:104
DiffForeachParams::binary_cb
binary_cb_t binary_cb
Definition: git_interface.hpp:96
operator<<
std::ostream & operator<<(std::ostream &out, git_oid const &oid)
Definition: git_interface.hpp:186
operator==
bool operator==(CR< git_oid > lhs, CR< git_oid > rhs)
Compare git OID for equality.
Definition: git_interface.hpp:177
git::exception::exception
exception(int error, const char *funcname)
Definition: git_interface.hpp:16
gitwrap.hpp
diff_foreach
void diff_foreach(git_diff *diff, CR< DiffForeachParams > foreach)
Definition: git_interface.hpp:117
fmt::formatter< git_oid >::format
auto format(CR< git_oid > date, fmt::format_context &ctx) const
Definition: git_interface.hpp:49
DiffForeachParams::file_cb
file_cb_t file_cb
Definition: git_interface.hpp:90
tree_walk
void tree_walk(const git_tree *tree, git_treewalk_mode mode, Func< int(const char *, const git_tree_entry *)> callback)
Iterate over the git tree in specified order using provided callback.
Definition: git_interface.hpp:56
DiffForeachParams::binary_cb_t
Func< int(const git_diff_delta *, const git_diff_binary *)> binary_cb_t
Optional callback to make for binary files.
Definition: git_interface.hpp:94
std::hash< git_oid >::operator()
std::size_t operator()(const git_oid &it) const
Definition: git_interface.hpp:169
oid_tostr
Str oid_tostr(git_oid oid)
Convert git ID object to it's string representation.
Definition: git_interface.hpp:41
operator<
bool operator<(CR< git_oid > lhs, CR< git_oid > rhs)
Definition: git_interface.hpp:181
oid_cmp
int oid_cmp(const git_oid *a, const git_oid *b)
Definition: gitwrap.hpp:151
DiffForeachParams
Definition: git_interface.hpp:86
git::exception::what
const char * what() const noexcept override
Definition: git_interface.hpp:26
std
Definition: git_interface.hpp:165
git::exception::message
Str message
Definition: git_interface.hpp:15
DiffForeachParams::line_cb_t
Func< int(const git_diff_delta *, const git_diff_hunk *, const git_diff_line *)> line_cb_t
Definition: git_interface.hpp:112
DiffForeachParams::file_cb_t
Func< int(const git_diff_delta *, float)> file_cb_t
Callback function to make per file in the diff.
Definition: git_interface.hpp:88
git
C++ wrapper for the libgit2 library.
Definition: git_interface.hpp:13
git::exception
Definition: git_interface.hpp:14