Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add revwalk wrapper
  • Loading branch information
SandrineP committed Jan 6, 2026
commit 70ee6b976fb4ad8d17d1ab0a2ee02ce4ce4fcb89
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/wrapper/remote_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/revwalk_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/revwalk_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.cpp
Expand Down
9 changes: 3 additions & 6 deletions src/subcommand/log_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,19 @@ void log_subcommand::run()
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);
revwalk_wrapper walker = repo.new_walker();
walker.push_head();

terminal_pager pager;

std::size_t i=0;
git_oid commit_oid;
while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
while (!walker.next(commit_oid) && i<m_max_count_flag)
{
commit_wrapper commit = repo.find_commit(commit_oid);
print_commit(commit, m_format_flag);
++i;
}

git_revwalk_free(walker);

pager.show();
}
12 changes: 4 additions & 8 deletions src/subcommand/revlist_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "revlist_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"
// #include <ios>
// #include <stdexcept>
#include "../wrapper/revwalk_wrapper.hpp"

revlist_subcommand::revlist_subcommand(const libgit2_object&, CLI::App& app)
{
Expand Down Expand Up @@ -30,20 +29,17 @@ void revlist_subcommand::run()
start_commit_oid = start_commit.oid();
}

git_revwalk* walker;
git_revwalk_new(&walker, repo);
git_revwalk_push_head(walker);
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 (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
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;
}

git_revwalk_free(walker);
}
14 changes: 7 additions & 7 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#include <iostream>

#include "../utils/git_exception.hpp"
#include "../wrapper/index_wrapper.hpp"
#include "../wrapper/object_wrapper.hpp"
#include "../wrapper/commit_wrapper.hpp"
#include "../wrapper/remote_wrapper.hpp"
#include <git2/repository.h>
#include <git2/remote.h>
#include "../wrapper/repository_wrapper.hpp"

repository_wrapper::~repository_wrapper()
Expand Down Expand Up @@ -56,6 +49,13 @@ bool repository_wrapper::is_shallow() const
return git_repository_is_shallow(*this);
}

revwalk_wrapper repository_wrapper::new_walker()
{
git_revwalk* walker;
throw_if_error(git_revwalk_new(&walker, *this));
return revwalk_wrapper(walker);
}

// Head

bool repository_wrapper::is_head_unborn() const
Expand Down
3 changes: 3 additions & 0 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../wrapper/object_wrapper.hpp"
#include "../wrapper/refs_wrapper.hpp"
#include "../wrapper/remote_wrapper.hpp"
#include "../wrapper/revwalk_wrapper.hpp"
#include "../wrapper/signature_wrapper.hpp"
#include "../wrapper/wrapper_base.hpp"

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

revwalk_wrapper new_walker();

// Head
bool is_head_unborn() const;
reference_wrapper head() const;
Expand Down
31 changes: 31 additions & 0 deletions src/wrapper/revwalk_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <git2/index.h>
#include <git2/types.h>

#include "revwalk_wrapper.hpp"
#include "../utils/git_exception.hpp"

revwalk_wrapper::revwalk_wrapper(git_revwalk* walker)
: base_type(walker)
{
}

revwalk_wrapper::~revwalk_wrapper()
{
git_revwalk_free(p_resource);
p_resource=nullptr;
}

void revwalk_wrapper::push_head()
{
throw_if_error(git_revwalk_push_head(*this));
}

void revwalk_wrapper::push(git_oid& commit_oid)
{
throw_if_error(git_revwalk_push(*this, &commit_oid));
}

int revwalk_wrapper::next(git_oid& commit_oid)
{
return git_revwalk_next(&commit_oid, *this);
}
29 changes: 29 additions & 0 deletions src/wrapper/revwalk_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <git2.h>
#include <git2/types.h>

#include "../wrapper/wrapper_base.hpp"
#include "../wrapper/commit_wrapper.hpp"

class revwalk_wrapper : public wrapper_base<git_revwalk>
{
public:

using base_type = wrapper_base<git_revwalk>;

~revwalk_wrapper();

revwalk_wrapper(revwalk_wrapper&&) noexcept = default;
revwalk_wrapper& operator=(revwalk_wrapper&&) noexcept = default;

void push_head();
void push(git_oid& commit_oid);
int next(git_oid& commit_oid);

private:

revwalk_wrapper(git_revwalk* walker);

friend class repository_wrapper;
};
3 changes: 2 additions & 1 deletion test/test_revlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ def test_revlist(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch):
"rev-list",
"35955995424eb9699bb604b988b5270253b1fccc",
"--max-count",
"4",
"2",
]
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)
assert p.returncode == 0
assert "da1754dd6" in p.stdout
assert "2da8e13ef" not in p.stdout
Loading