forked from QuantStack/git2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_subcommand.cpp
More file actions
295 lines (246 loc) · 8.07 KB
/
log_subcommand.cpp
File metadata and controls
295 lines (246 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <format>
#include <git2.h>
#include <git2/oid.h>
#include <git2/refs.h>
#include <git2/types.h>
#include <sstream>
#include <string_view>
#include <vector>
#include <termcolor/termcolor.hpp>
#include "log_subcommand.hpp"
#include "../utils/terminal_pager.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/commit_wrapper.hpp"
log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
{
auto *sub = app.add_subcommand("log", "Shows commit logs");
sub->add_flag("--format", m_format_flag, "Pretty-print the contents of the commit logs in a given format, where <format> can be one of full and fuller");
sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
// sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --pretty=oneline --abbrev-commit used together.");
sub->callback([this]() { this->run(); });
};
void print_time(git_time intime, std::string prefix)
{
char sign, out[32];
struct tm *intm;
int offset, hours, minutes;
time_t t;
offset = intime.offset;
if (offset < 0) {
sign = '-';
offset = -offset;
}
else
{
sign = '+';
}
hours = offset / 60;
minutes = offset % 60;
t = (time_t)intime.time + (intime.offset * 60);
intm = gmtime(&t);
strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
std::cout << prefix << out << " " << sign << std::format("{:02d}", hours) << std::format("{:02d}", minutes) <<std::endl;
}
std::vector<std::string> get_tags_for_commit(repository_wrapper& repo, const git_oid& commit_oid)
{
std::vector<std::string> tags;
git_strarray tag_names = {0};
if (git_tag_list(&tag_names, repo) != 0)
{
return tags;
}
for (size_t i = 0; i < tag_names.count; i++)
{
std::string tag_name = tag_names.strings[i];
std::string ref_name = "refs/tags/" + std::string(tag_name);
reference_wrapper tag_ref = repo.find_reference(ref_name);
object_wrapper peeled = tag_ref.peel<object_wrapper>();
if (git_oid_equal(&peeled.oid(), &commit_oid))
{
tags.push_back(std::string(tag_name));
}
}
git_strarray_dispose(&tag_names); // TODO: refactor git_strarray_wrapper to use it here
return tags;
}
std::vector<std::string> get_branches_for_commit(repository_wrapper& repo, git_branch_t type, const git_oid& commit_oid, const std::string exclude_branch)
{
std::vector<std::string> branches;
auto branch_iter = repo.iterate_branches(type);
while (auto branch = branch_iter.next())
{
const git_oid* branch_target = nullptr;
git_reference* ref = branch.value();
if (git_reference_type(ref) == GIT_REFERENCE_DIRECT)
{
branch_target = git_reference_target(ref);
}
else if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC)
{
git_reference* resolved = nullptr;
if (git_reference_resolve(&resolved, ref) == 0)
{
branch_target = git_reference_target(resolved);
git_reference_free(resolved);
}
}
if (branch_target && git_oid_equal(branch_target, &commit_oid))
{
std::string branch_name(branch->name());
if (type == GIT_BRANCH_LOCAL)
{
if (branch_name != exclude_branch)
{
branches.push_back(branch_name);
}
}
else
{
branches.push_back(branch_name);
}
}
}
return branches;
}
struct commit_refs
{
std::string head_branch;
std::vector<std::string> tags;
std::vector<std::string> local_branches;
std::vector<std::string> remote_branches;
bool has_refs() const {
return !head_branch.empty() || !tags.empty() ||
!local_branches.empty() || !remote_branches.empty();
}
};
commit_refs get_refs_for_commit(repository_wrapper& repo, const git_oid& commit_oid)
{
commit_refs refs;
if (!repo.is_head_unborn())
{
auto head = repo.head();
auto head_taget = head.target();
if (git_oid_equal(head_taget, &commit_oid))
{
refs.head_branch = head.short_name();
}
}
refs.tags = get_tags_for_commit(repo, commit_oid);
refs.local_branches = get_branches_for_commit(repo, GIT_BRANCH_LOCAL, commit_oid, refs.head_branch);
refs.remote_branches = get_branches_for_commit(repo, GIT_BRANCH_REMOTE, commit_oid, "");
return refs;
}
void print_refs(const commit_refs& refs)
{
if (!refs.has_refs())
{
return;
}
std::cout << " (";
bool first = true;
if (!refs.head_branch.empty())
{
std::cout << termcolor::bold << termcolor::cyan << "HEAD" << termcolor::reset
<< termcolor::yellow << " -> " << termcolor::reset
<< termcolor::bold << termcolor::green << refs.head_branch << termcolor::reset
<< termcolor::yellow;
first = false;
}
for (const auto& tag :refs.tags)
{
if (!first)
{
std::cout << ", ";
}
std::cout << termcolor::bold << "tag: " << tag << termcolor::reset << termcolor::yellow;
first = false;
}
for (const auto& remote : refs.remote_branches)
{
if (!first)
{
std::cout << ", ";
}
std::cout << termcolor::bold << termcolor::red << remote << termcolor::reset << termcolor::yellow;
first = false;
}
for (const auto& local : refs.local_branches)
{
if (!first)
{
std::cout << ", ";
}
std::cout << termcolor::bold << termcolor::green << local << termcolor::reset << termcolor::yellow;
first = false;
}
std::cout << ")" << termcolor::reset;
}
void print_commit(repository_wrapper& repo, const commit_wrapper& commit, std::string m_format_flag)
{
std::string buf = commit.commit_oid_tostr();
signature_wrapper author = signature_wrapper::get_commit_author(commit);
signature_wrapper committer = signature_wrapper::get_commit_committer(commit);
stream_colour_fn colour = termcolor::yellow;
std::cout << colour << "commit " << buf;
commit_refs refs = get_refs_for_commit(repo, commit.oid());
print_refs(refs);
std::cout << termcolor::reset << std::endl;
if (m_format_flag=="fuller")
{
std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl;
print_time(author.when(), "AuthorDate: ");
std::cout << "Commit:\t " << committer.name() << " " << committer.email() << std::endl;
print_time(committer.when(), "CommitDate: ");
}
else
{
std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl;
if (m_format_flag=="full")
{
std::cout << "Commit:\t" << committer.name() << " " << committer.email() << std::endl;
}
else
{
print_time(author.when(), "Date:\t");
}
}
std::string message = commit.message();
while (!message.empty() && message.back() == '\n')
{
message.pop_back();
}
std::istringstream message_stream(message);
std::string line;
while (std::getline(message_stream, line))
{
std::cout << "\n " << line;
}
std::cout << std::endl;
}
void log_subcommand::run()
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
// auto branch_name = repo.head().short_name();
if (repo.is_head_unborn())
{
std::cout << "fatal: your current branch 'main' does not have any commits yet" << std::endl;
return;
}
revwalk_wrapper walker = repo.new_walker();
walker.push_head();
terminal_pager pager;
std::size_t i=0;
git_oid commit_oid;
while (!walker.next(commit_oid) && i<m_max_count_flag)
{
if (i != 0)
{
std::cout << std::endl;
}
commit_wrapper commit = repo.find_commit(commit_oid);
print_commit(repo, commit, m_format_flag);
++i;
}
pager.show();
}