|
| 1 | +#include "revlist_subcommand.hpp" |
| 2 | +#include "../wrapper/repository_wrapper.hpp" |
| 3 | +#include "../wrapper/revwalk_wrapper.hpp" |
| 4 | + |
| 5 | +revlist_subcommand::revlist_subcommand(const libgit2_object&, CLI::App& app) |
| 6 | +{ |
| 7 | + auto* sub = app.add_subcommand("rev-list", "Lists commit objects in reverse chronological order"); |
| 8 | + |
| 9 | + sub->add_option("<commit>", m_commit, ""); |
| 10 | + sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits."); |
| 11 | + |
| 12 | + sub->callback([this]() { this->run(); }); |
| 13 | +} |
| 14 | + |
| 15 | +void revlist_subcommand::run() |
| 16 | +{ |
| 17 | + if (m_commit.empty()) |
| 18 | + { |
| 19 | + throw std::runtime_error("usage: git rev-list [<options>] <commit>... [--] [<path>...]"); // TODO: add help info |
| 20 | + } |
| 21 | + |
| 22 | + auto directory = get_current_git_path(); |
| 23 | + auto repo = repository_wrapper::open(directory); |
| 24 | + git_oid start_commit_oid; |
| 25 | + int not_sha1 = git_oid_fromstrp(&start_commit_oid, m_commit.c_str()); |
| 26 | + if (not_sha1) |
| 27 | + { |
| 28 | + commit_wrapper start_commit = repo.find_commit(m_commit); |
| 29 | + start_commit_oid = start_commit.oid(); |
| 30 | + } |
| 31 | + |
| 32 | + revwalk_wrapper walker = repo.new_walker(); |
| 33 | + walker.push(start_commit_oid); |
| 34 | + |
| 35 | + std::size_t i=0; |
| 36 | + git_oid commit_oid; |
| 37 | + char buf[GIT_OID_SHA1_HEXSIZE + 1]; |
| 38 | + while (!walker.next(commit_oid) && i<m_max_count_flag) |
| 39 | + { |
| 40 | + git_oid_fmt(buf, &commit_oid); |
| 41 | + buf[GIT_OID_SHA1_HEXSIZE] = '\0'; |
| 42 | + std::cout << buf << std::endl; |
| 43 | + ++i; |
| 44 | + } |
| 45 | +} |
0 commit comments