From c1d9c0e685bb294421f38f3e046648013bf05263 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Fri, 26 Dec 2025 15:28:10 +0100 Subject: [PATCH] Add rev-list --- CMakeLists.txt | 2 ++ src/main.cpp | 4 ++- src/subcommand/revlist_subcommand.cpp | 49 +++++++++++++++++++++++++++ src/subcommand/revlist_subcommand.hpp | 20 +++++++++++ test/test_revlist.py | 19 +++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/subcommand/revlist_subcommand.cpp create mode 100644 src/subcommand/revlist_subcommand.hpp create mode 100644 test/test_revlist.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 1165dbd..90b0194 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/subcommand/remote_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp + ${GIT2CPP_SOURCE_DIR}/subcommand/revlist_subcommand.cpp + ${GIT2CPP_SOURCE_DIR}/subcommand/revlist_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp diff --git a/src/main.cpp b/src/main.cpp index bbdbcef..9a3012b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "subcommand/reset_subcommand.hpp" #include "subcommand/status_subcommand.hpp" #include "subcommand/revparse_subcommand.hpp" +#include "subcommand/revlist_subcommand.hpp" int main(int argc, char** argv) { @@ -45,7 +46,8 @@ int main(int argc, char** argv) merge_subcommand merge(lg2_obj, app); push_subcommand push(lg2_obj, app); remote_subcommand remote(lg2_obj, app); - revparse_subcommand rev(lg2_obj, app); + revparse_subcommand revparse(lg2_obj, app); + revlist_subcommand revlist(lg2_obj, app); app.require_subcommand(/* min */ 0, /* max */ 1); diff --git a/src/subcommand/revlist_subcommand.cpp b/src/subcommand/revlist_subcommand.cpp new file mode 100644 index 0000000..09fbfa8 --- /dev/null +++ b/src/subcommand/revlist_subcommand.cpp @@ -0,0 +1,49 @@ +#include "revlist_subcommand.hpp" +#include "../wrapper/repository_wrapper.hpp" +// #include +// #include + +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("", m_commit, ""); + sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to commits."); + + sub->callback([this]() { this->run(); }); +} + +void revlist_subcommand::run() +{ + if (m_commit.empty()) + { + throw std::runtime_error("usage: git rev-list [] ... [--] [...]"); // 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(); + } + + git_revwalk* walker; + git_revwalk_new(&walker, repo); + git_revwalk_push_head(walker); + + std::size_t i=0; + git_oid commit_oid; + char buf[GIT_OID_SHA1_HEXSIZE + 1]; + while (!git_revwalk_next(&commit_oid, walker) && i +#include + +#include "../utils/common.hpp" + +class revlist_subcommand +{ +public: + + explicit revlist_subcommand(const libgit2_object&, CLI::App& app); + void run(); + +private: + + std::string m_commit; + int m_max_count_flag=std::numeric_limits::max(); + +}; diff --git a/test/test_revlist.py b/test/test_revlist.py new file mode 100644 index 0000000..302bd5b --- /dev/null +++ b/test/test_revlist.py @@ -0,0 +1,19 @@ +import subprocess + +import pytest + + +def test_revlist(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch): + assert (tmp_path / "xtl").exists() + xtl_path = tmp_path / "xtl" + + cmd = [ + git2cpp_path, + "rev-list", + "35955995424eb9699bb604b988b5270253b1fccc", + "--max-count", + "4", + ] + p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True) + assert p.returncode == 0 + assert "da1754dd6" in p.stdout