Skip to content

Commit a509df8

Browse files
committed
add log subcommand
1 parent 9351a2f commit a509df8

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ set(GIT2CPP_SRC
5151
${GIT2CPP_SOURCE_DIR}/subcommand/commit_subcommand.hpp
5252
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp
5353
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp
54+
${GIT2CPP_SOURCE_DIR}/subcommand/log_subcommand.cpp
55+
${GIT2CPP_SOURCE_DIR}/subcommand/log_subcommand.hpp
5456
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp
5557
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp
5658
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "subcommand/clone_subcommand.hpp"
1111
#include "subcommand/commit_subcommand.hpp"
1212
#include "subcommand/init_subcommand.hpp"
13+
#include "subcommand/log_subcommand.hpp"
1314
#include "subcommand/reset_subcommand.hpp"
1415
#include "subcommand/status_subcommand.hpp"
1516

@@ -33,6 +34,7 @@ int main(int argc, char** argv)
3334
clone_subcommand clone(lg2_obj, app);
3435
commit_subcommand commit(lg2_obj, app);
3536
reset_subcommand reset(lg2_obj, app);
37+
log_subcommand log(lg2_obj, app);
3638

3739
app.require_subcommand(/* min */ 0, /* max */ 1);
3840

src/subcommand/log_subcommand.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// #include <iostream>
2+
// #include <ostream>
3+
// #include <string>
4+
5+
#include <git2.h>
6+
#include <git2/revwalk.h>
7+
#include <git2/types.h>
8+
9+
#include "log_subcommand.hpp"
10+
#include "../wrapper/repository_wrapper.hpp"
11+
12+
// TODO: put in another file
13+
/** Size (in bytes) of a raw/binary sha1 oid */
14+
#define GIT_OID_SHA1_SIZE 20
15+
/** Size (in bytes) of a hex formatted sha1 oid */
16+
#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
17+
18+
log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
19+
{
20+
auto *sub = app.add_subcommand("log", "Shows commit logs");
21+
22+
sub->add_flag("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
23+
// sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --pretty=oneline --abbrev-commit used together.");
24+
25+
sub->callback([this]() { this->run(); });
26+
};
27+
28+
void print_time(git_time intime, const char *prefix)
29+
{
30+
char sign, out[32];
31+
struct tm *intm;
32+
int offset, hours, minutes;
33+
time_t t;
34+
35+
offset = intime.offset;
36+
if (offset < 0) {
37+
sign = '-';
38+
offset = -offset;
39+
} else {
40+
sign = '+';
41+
}
42+
43+
hours = offset / 60;
44+
minutes = offset % 60;
45+
46+
t = (time_t)intime.time + (intime.offset * 60);
47+
48+
intm = gmtime(&t);
49+
strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
50+
51+
printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes);
52+
}
53+
54+
void print_commit(const commit_wrapper& commit)
55+
{
56+
// TODO: put in commit_wrapper ?
57+
char buf[GIT_OID_SHA1_HEXSIZE + 1];
58+
int i, count;
59+
60+
git_oid_tostr(buf, sizeof(buf), &commit.oid());
61+
// TODO end
62+
63+
signature_wrapper author = signature_wrapper::get_commit_author(commit);
64+
65+
std::cout << "commit " << buf << std::endl;
66+
std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl;
67+
print_time(author.when(), "Date:\t");
68+
std::cout << git_commit_message(commit) << "\n" << std::endl;
69+
}
70+
71+
void log_subcommand::run()
72+
{
73+
auto directory = get_current_git_path();
74+
auto bare = false;
75+
auto repo = repository_wrapper::init(directory, bare);
76+
// auto branch_name = repo.head().short_name();
77+
78+
git_revwalk* walker;
79+
git_revwalk_new(&walker, repo);
80+
git_revwalk_push_head(walker);
81+
82+
git_oid commit_oid;
83+
while (!git_revwalk_next(&commit_oid, walker))
84+
{
85+
commit_wrapper commit = repo.find_commit(commit_oid);
86+
print_commit(commit);
87+
}
88+
}

src/subcommand/log_subcommand.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <CLI/CLI.hpp>
4+
5+
#include "../utils/common.hpp"
6+
7+
8+
class log_subcommand
9+
{
10+
public:
11+
12+
explicit log_subcommand(const libgit2_object&, CLI::App& app);
13+
void run();
14+
15+
private:
16+
int m_max_count_flag;
17+
// bool m_oneline_flag = false;
18+
};

src/wrapper/signature_wrapper.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,39 @@ signature_wrapper::~signature_wrapper()
88
p_resource=nullptr;
99
}
1010

11+
std::string_view signature_wrapper::name() const
12+
{
13+
return p_resource->name;
14+
}
15+
16+
std::string_view signature_wrapper::email() const
17+
{
18+
return p_resource->email;
19+
}
20+
21+
git_time signature_wrapper::when() const
22+
{
23+
return p_resource->when;
24+
}
25+
1126
signature_wrapper::author_committer_signatures signature_wrapper::get_default_signature_from_env(repository_wrapper& rw)
1227
{
1328
signature_wrapper author;
1429
signature_wrapper committer;
1530
throw_if_error(git_signature_default_from_env(&(author.p_resource), &(committer.p_resource), rw));
1631
return {std::move(author), std::move(committer)};
1732
}
33+
34+
signature_wrapper signature_wrapper::get_commit_author(const commit_wrapper& cw)
35+
{
36+
signature_wrapper author;
37+
author.p_resource = const_cast<git_signature*>(git_commit_author(cw));
38+
return author;
39+
}
40+
41+
signature_wrapper signature_wrapper::get_commit_committer(const commit_wrapper& cw)
42+
{
43+
signature_wrapper author;
44+
author.p_resource = const_cast<git_signature*>(git_commit_committer(cw));
45+
return author;
46+
}

src/wrapper/signature_wrapper.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

33
#include <utility>
4+
#include <string_view>
45

56
#include <git2.h>
67

78
#include "../wrapper/wrapper_base.hpp"
89

10+
class commit_wrapper;
911
class repository_wrapper;
1012

1113
class signature_wrapper : public wrapper_base<git_signature>
@@ -18,7 +20,13 @@ class signature_wrapper : public wrapper_base<git_signature>
1820
signature_wrapper(signature_wrapper&&) = default;
1921
signature_wrapper& operator=(signature_wrapper&&) = default;
2022

23+
std::string_view name() const;
24+
std::string_view email() const;
25+
git_time when() const;
26+
2127
static author_committer_signatures get_default_signature_from_env(repository_wrapper&);
28+
static signature_wrapper get_commit_author(const commit_wrapper&);
29+
static signature_wrapper get_commit_committer(const commit_wrapper&);
2230

2331
private:
2432

0 commit comments

Comments
 (0)