-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrevlist_subcommand.cpp
More file actions
53 lines (45 loc) · 1.63 KB
/
revlist_subcommand.cpp
File metadata and controls
53 lines (45 loc) · 1.63 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
#include "revlist_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/revwalk_wrapper.hpp"
revlist_subcommand::revlist_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("rev-list", "Lists commit objects in reverse chronological order");
sub->add_option("<commit>", m_commit, "");
sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
sub->callback(
[this]()
{
this->run();
}
);
}
void revlist_subcommand::run()
{
if (m_commit.empty())
{
throw std::runtime_error("usage: git rev-list [<options>] <commit>... [--] [<path>...]"); // TODO:
// add help
// info
}
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
git_oid start_commit_oid;
int not_sha1 = git_oid_fromstrp(&start_commit_oid, m_commit.c_str());
if (not_sha1)
{
commit_wrapper start_commit = repo.find_commit(m_commit);
start_commit_oid = start_commit.oid();
}
revwalk_wrapper walker = repo.new_walker();
walker.push(start_commit_oid);
std::size_t i = 0;
git_oid commit_oid;
char buf[GIT_OID_SHA1_HEXSIZE + 1];
while (!walker.next(commit_oid) && i < m_max_count_flag)
{
git_oid_fmt(buf, &commit_oid);
buf[GIT_OID_SHA1_HEXSIZE] = '\0';
std::cout << buf << std::endl;
++i;
}
}