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
100 lines (83 loc) · 2.96 KB
/
log_subcommand.cpp
File metadata and controls
100 lines (83 loc) · 2.96 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
#include <format>
#include <git2.h>
#include <git2/revwalk.h>
#include <git2/types.h>
#include <string_view>
#include "log_subcommand.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;
}
void print_commit(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);
std::cout << "\033[0;33m" << "commit " << buf << "\033[0m" << 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::cout << "\n " << git_commit_message(commit) << "\n" << 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();
git_revwalk* walker;
git_revwalk_new(&walker, repo);
git_revwalk_push_head(walker);
std::size_t i=0;
git_oid commit_oid;
while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
{
commit_wrapper commit = repo.find_commit(commit_oid);
print_commit(commit, m_format_flag);
++i;
}
git_revwalk_free(walker);
}