-
Notifications
You must be signed in to change notification settings - Fork 5
Add --abbrev-commit, --format=oneline and --oneline to the log subcommand
#118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,16 +11,17 @@ | |
|
|
||
| #include "log_subcommand.hpp" | ||
| #include "../utils/terminal_pager.hpp" | ||
| #include "../wrapper/repository_wrapper.hpp" | ||
| #include "../wrapper/commit_wrapper.hpp" | ||
|
|
||
| log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app) | ||
| { | ||
| auto *sub = app.add_subcommand("log", "Shows commit logs"); | ||
|
|
||
| sub->add_flag("--format", m_format_flag, "Pretty-print the contents of the commit logs in a given format, where <format> can be one of full and fuller"); | ||
| sub->add_option("--format", m_format_flag, "Pretty-print the contents of the commit logs in a given format, where <format> can be one of full and fuller"); | ||
| sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits."); | ||
| // sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --pretty=oneline --abbrev-commit used together."); | ||
| sub->add_flag("--abbrev-commit", m_abbrev_commit_flag, "Instead of showing the full 40-byte hexadecimal commit object name, show a prefix that names the object uniquely. --abbrev=<n> (which also modifies diff output, if it is displayed) option can be used to specify the minimum length of the prefix."); | ||
| sub->add_option("--abbrev", m_abbrev, "Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header lines, show the shortest prefix that is at least <n> hexdigits long that uniquely refers the object."); | ||
| sub->add_flag("--no-abbrev-commit", m_no_abbrev_commit_flag, "Show the full 40-byte hexadecimal commit object name. This negates --abbrev-commit, either explicit or implied by other options such as --oneline."); | ||
| sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --format=oneline --abbrev-commit used together."); | ||
|
|
||
| sub->callback([this]() { this->run(); }); | ||
| }; | ||
|
|
@@ -166,6 +167,7 @@ void print_refs(const commit_refs& refs) | |
| return; | ||
| } | ||
|
|
||
| std::cout << termcolor::yellow; | ||
| std::cout << " ("; | ||
|
|
||
| bool first = true; | ||
|
|
@@ -179,7 +181,7 @@ void print_refs(const commit_refs& refs) | |
| first = false; | ||
| } | ||
|
|
||
| for (const auto& tag :refs.tags) | ||
| for (const auto& tag : refs.tags) | ||
| { | ||
| if (!first) | ||
| { | ||
|
|
@@ -212,17 +214,47 @@ void print_refs(const commit_refs& refs) | |
| std::cout << ")" << termcolor::reset; | ||
| } | ||
|
|
||
| void print_commit(repository_wrapper& repo, const commit_wrapper& commit, std::string m_format_flag) | ||
| void log_subcommand::print_commit(repository_wrapper& repo, const commit_wrapper& commit) | ||
| { | ||
| std::string buf = commit.commit_oid_tostr(); | ||
| const bool abbrev_commit = (m_abbrev_commit_flag || m_oneline_flag) && !m_no_abbrev_commit_flag; | ||
| const bool oneline = (m_format_flag == "oneline") || m_oneline_flag; | ||
|
|
||
| std::string sha = commit.commit_oid_tostr(); | ||
|
|
||
| if (abbrev_commit && m_abbrev <= sha.size()) | ||
| { | ||
| sha = sha.substr(0, m_abbrev); | ||
| } | ||
|
|
||
| commit_refs refs = get_refs_for_commit(repo, commit.oid()); | ||
|
|
||
| std::string message = commit.message(); | ||
| while (!message.empty() && message.back() == '\n') | ||
| message.pop_back(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add surrounding curly braces |
||
|
|
||
| if (oneline) | ||
| { | ||
| std::string subject; | ||
| { | ||
| std::istringstream s(message); | ||
| std::getline(s, subject); | ||
| } | ||
|
|
||
| std::cout << termcolor::yellow << sha << termcolor::reset; | ||
| print_refs(refs); | ||
| if (!subject.empty()) | ||
| { | ||
| std::cout << " " << subject; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| signature_wrapper author = signature_wrapper::get_commit_author(commit); | ||
| signature_wrapper committer = signature_wrapper::get_commit_committer(commit); | ||
|
|
||
| stream_colour_fn colour = termcolor::yellow; | ||
| std::cout << colour << "commit " << buf; | ||
| std::cout << colour << "commit " << sha << termcolor::reset; | ||
|
|
||
| commit_refs refs = get_refs_for_commit(repo, commit.oid()); | ||
| print_refs(refs); | ||
|
|
||
| std::cout << termcolor::reset << std::endl; | ||
|
|
@@ -247,11 +279,6 @@ void print_commit(repository_wrapper& repo, const commit_wrapper& commit, std::s | |
| } | ||
| } | ||
|
|
||
| std::string message = commit.message(); | ||
| while (!message.empty() && message.back() == '\n') | ||
| { | ||
| message.pop_back(); | ||
| } | ||
| std::istringstream message_stream(message); | ||
| std::string line; | ||
| while (std::getline(message_stream, line)) | ||
|
|
@@ -287,7 +314,7 @@ void log_subcommand::run() | |
| std::cout << std::endl; | ||
| } | ||
| commit_wrapper commit = repo.find_commit(commit_oid); | ||
| print_commit(repo, commit, m_format_flag); | ||
| print_commit(repo, commit); | ||
| ++i; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,29 @@ | ||
| #include "revparse_subcommand.hpp" | ||
| #include "../utils/git_exception.hpp" | ||
| #include "../wrapper/repository_wrapper.hpp" | ||
| #include <ios> | ||
| #include <stdexcept> | ||
|
|
||
| revparse_subcommand::revparse_subcommand(const libgit2_object&, CLI::App& app) | ||
| { | ||
| auto* sub = app.add_subcommand("rev-parse", "Pick out and message parameters"); | ||
|
|
||
| sub->add_flag("--is-bare-repository", m_is_bare_repository_flag); | ||
| sub->add_flag("--is-shallow-repository", m_is_shallow_repository_flag); | ||
| auto* bare_opt = sub->add_flag("--is-bare-repository", m_is_bare_repository_flag, "When the repository is bare print \"true\", otherwise \"false\"."); | ||
| auto* shallow_opt = sub->add_flag("--is-shallow-repository", m_is_shallow_repository_flag, "When the repository is shallow print \"true\", otherwise \"false\"."); | ||
|
|
||
| sub->add_option("<rev>", m_revisions, "Revision(s) to parse (e.g. HEAD, main, HEAD~1, dae86e, ...)"); | ||
|
|
||
| sub->parse_complete_callback([this, sub, bare_opt, shallow_opt]() { | ||
| for (CLI::Option* opt : sub->parse_order()) | ||
| { | ||
| if (opt == bare_opt) | ||
| { | ||
| m_queries_in_order.push_back("is_bare"); | ||
| } | ||
| else if (opt == shallow_opt) | ||
| { | ||
| m_queries_in_order.push_back("is_shallow"); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| sub->callback([this]() { this->run(); }); | ||
| } | ||
|
|
@@ -18,16 +33,39 @@ void revparse_subcommand::run() | |
| auto directory = get_current_git_path(); | ||
| auto repo = repository_wrapper::open(directory); | ||
|
|
||
| if (m_is_bare_repository_flag) | ||
| { | ||
| std::cout << std::boolalpha << repo.is_bare() << std::endl; | ||
| } | ||
| else if (m_is_shallow_repository_flag) | ||
| if (!m_queries_in_order.empty()) | ||
|
ianthomas23 marked this conversation as resolved.
Outdated
|
||
| { | ||
| std::cout << std::boolalpha << repo.is_shallow() << std::endl; | ||
| for (const auto& q : m_queries_in_order) | ||
| { | ||
| if (q == "is_bare") | ||
| { | ||
| std::cout << std::boolalpha << repo.is_bare() << std::endl; | ||
| } | ||
| if (q == "is_shallow") | ||
| { | ||
| std::cout << std::boolalpha << repo.is_shallow() << std::endl; | ||
| } | ||
| } | ||
| return; | ||
|
ianthomas23 marked this conversation as resolved.
Outdated
|
||
| } | ||
| else | ||
|
|
||
| if (!m_revisions.empty()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use |
||
| { | ||
| std::cout << "revparse only supports --is-bare-repository and --is-shallow-repository for now" << std::endl; | ||
| for (const auto& rev : m_revisions) | ||
| { | ||
| auto obj = repo.revparse_single(rev.c_str()); | ||
|
|
||
| if (!obj.has_value()) | ||
| { | ||
| throw git_exception("bad revision '" + rev + "'", git2cpp_error_code::BAD_ARGUMENT); | ||
| return; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for return after throwing |
||
| } | ||
|
|
||
| auto oid = obj.value().oid(); | ||
| std::cout << git_oid_tostr_s(&oid) << std::endl; | ||
| } | ||
| return; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
|
|
||
| std::cout << "revparse only supports --is-bare-repository, --is-shallow-repository and parsing revisions for now" << std::endl; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.