Skip to content

Commit c1d9c0e

Browse files
committed
Add rev-list
1 parent 1cd32b6 commit c1d9c0e

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ set(GIT2CPP_SRC
6464
${GIT2CPP_SOURCE_DIR}/subcommand/remote_subcommand.hpp
6565
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp
6666
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp
67+
${GIT2CPP_SOURCE_DIR}/subcommand/revlist_subcommand.cpp
68+
${GIT2CPP_SOURCE_DIR}/subcommand/revlist_subcommand.hpp
6769
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.cpp
6870
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.hpp
6971
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "subcommand/reset_subcommand.hpp"
2020
#include "subcommand/status_subcommand.hpp"
2121
#include "subcommand/revparse_subcommand.hpp"
22+
#include "subcommand/revlist_subcommand.hpp"
2223

2324
int main(int argc, char** argv)
2425
{
@@ -45,7 +46,8 @@ int main(int argc, char** argv)
4546
merge_subcommand merge(lg2_obj, app);
4647
push_subcommand push(lg2_obj, app);
4748
remote_subcommand remote(lg2_obj, app);
48-
revparse_subcommand rev(lg2_obj, app);
49+
revparse_subcommand revparse(lg2_obj, app);
50+
revlist_subcommand revlist(lg2_obj, app);
4951

5052
app.require_subcommand(/* min */ 0, /* max */ 1);
5153

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "revlist_subcommand.hpp"
2+
#include "../wrapper/repository_wrapper.hpp"
3+
// #include <ios>
4+
// #include <stdexcept>
5+
6+
revlist_subcommand::revlist_subcommand(const libgit2_object&, CLI::App& app)
7+
{
8+
auto* sub = app.add_subcommand("rev-list", "Lists commit objects in reverse chronological order");
9+
10+
sub->add_option("<commit>", m_commit, "");
11+
sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
12+
13+
sub->callback([this]() { this->run(); });
14+
}
15+
16+
void revlist_subcommand::run()
17+
{
18+
if (m_commit.empty())
19+
{
20+
throw std::runtime_error("usage: git rev-list [<options>] <commit>... [--] [<path>...]"); // TODO: add help info
21+
}
22+
23+
auto directory = get_current_git_path();
24+
auto repo = repository_wrapper::open(directory);
25+
git_oid start_commit_oid;
26+
int not_sha1 = git_oid_fromstrp(&start_commit_oid, m_commit.c_str());
27+
if (not_sha1)
28+
{
29+
commit_wrapper start_commit = repo.find_commit(m_commit);
30+
start_commit_oid = start_commit.oid();
31+
}
32+
33+
git_revwalk* walker;
34+
git_revwalk_new(&walker, repo);
35+
git_revwalk_push_head(walker);
36+
37+
std::size_t i=0;
38+
git_oid commit_oid;
39+
char buf[GIT_OID_SHA1_HEXSIZE + 1];
40+
while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
41+
{
42+
git_oid_fmt(buf, &commit_oid);
43+
buf[GIT_OID_SHA1_HEXSIZE] = '\0';
44+
std::cout << buf << std::endl;
45+
++i;
46+
}
47+
48+
git_revwalk_free(walker);
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <CLI/CLI.hpp>
4+
#include <string>
5+
6+
#include "../utils/common.hpp"
7+
8+
class revlist_subcommand
9+
{
10+
public:
11+
12+
explicit revlist_subcommand(const libgit2_object&, CLI::App& app);
13+
void run();
14+
15+
private:
16+
17+
std::string m_commit;
18+
int m_max_count_flag=std::numeric_limits<int>::max();
19+
20+
};

test/test_revlist.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import subprocess
2+
3+
import pytest
4+
5+
6+
def test_revlist(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch):
7+
assert (tmp_path / "xtl").exists()
8+
xtl_path = tmp_path / "xtl"
9+
10+
cmd = [
11+
git2cpp_path,
12+
"rev-list",
13+
"35955995424eb9699bb604b988b5270253b1fccc",
14+
"--max-count",
15+
"4",
16+
]
17+
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)
18+
assert p.returncode == 0
19+
assert "da1754dd6" in p.stdout

0 commit comments

Comments
 (0)