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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);

Expand Down
49 changes: 49 additions & 0 deletions src/subcommand/revlist_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "revlist_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"
// #include <ios>
// #include <stdexcept>

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();
}

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<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);
}
20 changes: 20 additions & 0 deletions src/subcommand/revlist_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <CLI/CLI.hpp>
#include <string>

#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<int>::max();

};
19 changes: 19 additions & 0 deletions test/test_revlist.py
Original file line number Diff line number Diff line change
@@ -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