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