Skip to content

Commit 70ee6b9

Browse files
committed
Add revwalk wrapper
1 parent c1d9c0e commit 70ee6b9

File tree

8 files changed

+81
-22
lines changed

8 files changed

+81
-22
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ set(GIT2CPP_SRC
9898
${GIT2CPP_SOURCE_DIR}/wrapper/remote_wrapper.hpp
9999
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.cpp
100100
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.hpp
101+
${GIT2CPP_SOURCE_DIR}/wrapper/revwalk_wrapper.cpp
102+
${GIT2CPP_SOURCE_DIR}/wrapper/revwalk_wrapper.hpp
101103
${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.cpp
102104
${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.hpp
103105
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.cpp

src/subcommand/log_subcommand.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,19 @@ void log_subcommand::run()
8787
auto repo = repository_wrapper::open(directory);
8888
// auto branch_name = repo.head().short_name();
8989

90-
git_revwalk* walker;
91-
git_revwalk_new(&walker, repo);
92-
git_revwalk_push_head(walker);
90+
revwalk_wrapper walker = repo.new_walker();
91+
walker.push_head();
9392

9493
terminal_pager pager;
9594

9695
std::size_t i=0;
9796
git_oid commit_oid;
98-
while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
97+
while (!walker.next(commit_oid) && i<m_max_count_flag)
9998
{
10099
commit_wrapper commit = repo.find_commit(commit_oid);
101100
print_commit(commit, m_format_flag);
102101
++i;
103102
}
104103

105-
git_revwalk_free(walker);
106-
107104
pager.show();
108105
}

src/subcommand/revlist_subcommand.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "revlist_subcommand.hpp"
22
#include "../wrapper/repository_wrapper.hpp"
3-
// #include <ios>
4-
// #include <stdexcept>
3+
#include "../wrapper/revwalk_wrapper.hpp"
54

65
revlist_subcommand::revlist_subcommand(const libgit2_object&, CLI::App& app)
76
{
@@ -30,20 +29,17 @@ void revlist_subcommand::run()
3029
start_commit_oid = start_commit.oid();
3130
}
3231

33-
git_revwalk* walker;
34-
git_revwalk_new(&walker, repo);
35-
git_revwalk_push_head(walker);
32+
revwalk_wrapper walker = repo.new_walker();
33+
walker.push(start_commit_oid);
3634

3735
std::size_t i=0;
3836
git_oid commit_oid;
3937
char buf[GIT_OID_SHA1_HEXSIZE + 1];
40-
while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
38+
while (!walker.next(commit_oid) && i<m_max_count_flag)
4139
{
4240
git_oid_fmt(buf, &commit_oid);
4341
buf[GIT_OID_SHA1_HEXSIZE] = '\0';
4442
std::cout << buf << std::endl;
4543
++i;
4644
}
47-
48-
git_revwalk_free(walker);
4945
}

src/wrapper/repository_wrapper.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#include <iostream>
22

3-
#include "../utils/git_exception.hpp"
4-
#include "../wrapper/index_wrapper.hpp"
5-
#include "../wrapper/object_wrapper.hpp"
6-
#include "../wrapper/commit_wrapper.hpp"
7-
#include "../wrapper/remote_wrapper.hpp"
8-
#include <git2/repository.h>
9-
#include <git2/remote.h>
103
#include "../wrapper/repository_wrapper.hpp"
114

125
repository_wrapper::~repository_wrapper()
@@ -56,6 +49,13 @@ bool repository_wrapper::is_shallow() const
5649
return git_repository_is_shallow(*this);
5750
}
5851

52+
revwalk_wrapper repository_wrapper::new_walker()
53+
{
54+
git_revwalk* walker;
55+
throw_if_error(git_revwalk_new(&walker, *this));
56+
return revwalk_wrapper(walker);
57+
}
58+
5959
// Head
6060

6161
bool repository_wrapper::is_head_unborn() const

src/wrapper/repository_wrapper.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../wrapper/object_wrapper.hpp"
1515
#include "../wrapper/refs_wrapper.hpp"
1616
#include "../wrapper/remote_wrapper.hpp"
17+
#include "../wrapper/revwalk_wrapper.hpp"
1718
#include "../wrapper/signature_wrapper.hpp"
1819
#include "../wrapper/wrapper_base.hpp"
1920

@@ -36,6 +37,8 @@ class repository_wrapper : public wrapper_base<git_repository>
3637
bool is_bare() const;
3738
bool is_shallow() const;
3839

40+
revwalk_wrapper new_walker();
41+
3942
// Head
4043
bool is_head_unborn() const;
4144
reference_wrapper head() const;

src/wrapper/revwalk_wrapper.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <git2/index.h>
2+
#include <git2/types.h>
3+
4+
#include "revwalk_wrapper.hpp"
5+
#include "../utils/git_exception.hpp"
6+
7+
revwalk_wrapper::revwalk_wrapper(git_revwalk* walker)
8+
: base_type(walker)
9+
{
10+
}
11+
12+
revwalk_wrapper::~revwalk_wrapper()
13+
{
14+
git_revwalk_free(p_resource);
15+
p_resource=nullptr;
16+
}
17+
18+
void revwalk_wrapper::push_head()
19+
{
20+
throw_if_error(git_revwalk_push_head(*this));
21+
}
22+
23+
void revwalk_wrapper::push(git_oid& commit_oid)
24+
{
25+
throw_if_error(git_revwalk_push(*this, &commit_oid));
26+
}
27+
28+
int revwalk_wrapper::next(git_oid& commit_oid)
29+
{
30+
return git_revwalk_next(&commit_oid, *this);
31+
}

src/wrapper/revwalk_wrapper.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <git2.h>
4+
#include <git2/types.h>
5+
6+
#include "../wrapper/wrapper_base.hpp"
7+
#include "../wrapper/commit_wrapper.hpp"
8+
9+
class revwalk_wrapper : public wrapper_base<git_revwalk>
10+
{
11+
public:
12+
13+
using base_type = wrapper_base<git_revwalk>;
14+
15+
~revwalk_wrapper();
16+
17+
revwalk_wrapper(revwalk_wrapper&&) noexcept = default;
18+
revwalk_wrapper& operator=(revwalk_wrapper&&) noexcept = default;
19+
20+
void push_head();
21+
void push(git_oid& commit_oid);
22+
int next(git_oid& commit_oid);
23+
24+
private:
25+
26+
revwalk_wrapper(git_revwalk* walker);
27+
28+
friend class repository_wrapper;
29+
};

test/test_revlist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def test_revlist(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch):
1212
"rev-list",
1313
"35955995424eb9699bb604b988b5270253b1fccc",
1414
"--max-count",
15-
"4",
15+
"2",
1616
]
1717
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)
1818
assert p.returncode == 0
1919
assert "da1754dd6" in p.stdout
20+
assert "2da8e13ef" not in p.stdout

0 commit comments

Comments
 (0)