|
| 1 | +// #include <iostream> |
| 2 | +// #include <ostream> |
| 3 | +// #include <string> |
| 4 | + |
| 5 | +#include <git2.h> |
| 6 | +#include <git2/revwalk.h> |
| 7 | +#include <git2/types.h> |
| 8 | + |
| 9 | +#include "log_subcommand.hpp" |
| 10 | +#include "../wrapper/repository_wrapper.hpp" |
| 11 | + |
| 12 | +// TODO: put in another file |
| 13 | +/** Size (in bytes) of a raw/binary sha1 oid */ |
| 14 | +#define GIT_OID_SHA1_SIZE 20 |
| 15 | +/** Size (in bytes) of a hex formatted sha1 oid */ |
| 16 | +#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) |
| 17 | + |
| 18 | +log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app) |
| 19 | +{ |
| 20 | + auto *sub = app.add_subcommand("log", "Shows commit logs"); |
| 21 | + |
| 22 | + sub->add_flag("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits."); |
| 23 | + // sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --pretty=oneline --abbrev-commit used together."); |
| 24 | + |
| 25 | + sub->callback([this]() { this->run(); }); |
| 26 | +}; |
| 27 | + |
| 28 | +void print_time(git_time intime, const char *prefix) |
| 29 | +{ |
| 30 | + char sign, out[32]; |
| 31 | + struct tm *intm; |
| 32 | + int offset, hours, minutes; |
| 33 | + time_t t; |
| 34 | + |
| 35 | + offset = intime.offset; |
| 36 | + if (offset < 0) { |
| 37 | + sign = '-'; |
| 38 | + offset = -offset; |
| 39 | + } else { |
| 40 | + sign = '+'; |
| 41 | + } |
| 42 | + |
| 43 | + hours = offset / 60; |
| 44 | + minutes = offset % 60; |
| 45 | + |
| 46 | + t = (time_t)intime.time + (intime.offset * 60); |
| 47 | + |
| 48 | + intm = gmtime(&t); |
| 49 | + strftime(out, sizeof(out), "%a %b %e %T %Y", intm); |
| 50 | + |
| 51 | + printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes); |
| 52 | +} |
| 53 | + |
| 54 | +void print_commit(const commit_wrapper& commit) |
| 55 | +{ |
| 56 | + // TODO: put in commit_wrapper ? |
| 57 | + char buf[GIT_OID_SHA1_HEXSIZE + 1]; |
| 58 | + int i, count; |
| 59 | + |
| 60 | + git_oid_tostr(buf, sizeof(buf), &commit.oid()); |
| 61 | + // TODO end |
| 62 | + |
| 63 | + signature_wrapper author = signature_wrapper::get_commit_author(commit); |
| 64 | + |
| 65 | + std::cout << "commit " << buf << std::endl; |
| 66 | + std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl; |
| 67 | + print_time(author.when(), "Date:\t"); |
| 68 | + std::cout << git_commit_message(commit) << "\n" << std::endl; |
| 69 | +} |
| 70 | + |
| 71 | +void log_subcommand::run() |
| 72 | +{ |
| 73 | + auto directory = get_current_git_path(); |
| 74 | + auto bare = false; |
| 75 | + auto repo = repository_wrapper::init(directory, bare); |
| 76 | + // auto branch_name = repo.head().short_name(); |
| 77 | + |
| 78 | + git_revwalk* walker; |
| 79 | + git_revwalk_new(&walker, repo); |
| 80 | + git_revwalk_push_head(walker); |
| 81 | + |
| 82 | + git_oid commit_oid; |
| 83 | + while (!git_revwalk_next(&commit_oid, walker)) |
| 84 | + { |
| 85 | + commit_wrapper commit = repo.find_commit(commit_oid); |
| 86 | + print_commit(commit); |
| 87 | + } |
| 88 | +} |
0 commit comments