Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 44 additions & 15 deletions src/subcommand/log_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, fuller or oneline");
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(); });
};
Expand Down Expand Up @@ -166,6 +167,7 @@ void print_refs(const commit_refs& refs)
return;
}

std::cout << termcolor::yellow;
std::cout << " (";

bool first = true;
Expand All @@ -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)
{
Expand Down Expand Up @@ -212,17 +214,49 @@ 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();
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand All @@ -247,11 +281,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))
Expand Down Expand Up @@ -287,7 +316,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;
}

Expand Down
10 changes: 9 additions & 1 deletion src/subcommand/log_subcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <limits>

#include "../utils/common.hpp"
#include "../wrapper/commit_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"


class log_subcommand
Expand All @@ -14,7 +16,13 @@ class log_subcommand
void run();

private:

void print_commit(repository_wrapper& repo, const commit_wrapper& commit);

std::string m_format_flag;
int m_max_count_flag=std::numeric_limits<int>::max();
// bool m_oneline_flag = false;
size_t m_abbrev = 7;
bool m_abbrev_commit_flag = false;
bool m_no_abbrev_commit_flag = false;
bool m_oneline_flag = false;
};
64 changes: 50 additions & 14 deletions src/subcommand/revparse_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
#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\".");
auto* rev_opt = 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, rev_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");
}
else if (opt == rev_opt)
{
m_queries_in_order.push_back("is_rev");
}
}
});

sub->callback([this]() { this->run(); });
}
Expand All @@ -18,16 +36,34 @@ 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)
size_t i = 0;
if (!m_queries_in_order.empty())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed now as the following for-loop will do nothing if m_queries_in_order is empty.

{
std::cout << std::boolalpha << repo.is_shallow() << std::endl;
}
else
{
std::cout << "revparse only supports --is-bare-repository and --is-shallow-repository for now" << std::endl;
for (const auto& q : m_queries_in_order)
{
if (q == "is_bare")
{
std::cout << std::boolalpha << repo.is_bare() << std::endl;
}
else if (q == "is_shallow")
{
std::cout << std::boolalpha << repo.is_shallow() << std::endl;
}
else if (q == "is_rev")
{
const auto& rev = m_revisions[i];
auto obj = repo.revparse_single(rev.c_str());

if (!obj.has_value())
{
throw git_exception("bad revision '" + rev + "'", git2cpp_error_code::BAD_ARGUMENT);
}

auto oid = obj.value().oid();
std::cout << git_oid_tostr_s(&oid) << std::endl;
i += 1;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using else here would avoid the need for the previous return statement

return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed any more.

}
5 changes: 5 additions & 0 deletions src/subcommand/revparse_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <string>
#include <vector>

#include <CLI/CLI.hpp>

#include "../utils/common.hpp"
Expand All @@ -13,6 +16,8 @@ class revparse_subcommand

private:

std::vector<std::string> m_revisions;
std::vector<std::string> m_queries_in_order;
bool m_is_bare_repository_flag = false;
bool m_is_shallow_repository_flag = false;
};
13 changes: 10 additions & 3 deletions src/subcommand/tag_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <git2.h>

#include "../subcommand/tag_subcommand.hpp"
#include "../wrapper/commit_wrapper.hpp"
#include "../wrapper/tag_wrapper.hpp"

tag_subcommand::tag_subcommand(const libgit2_object&, CLI::App& app)
{
Expand All @@ -12,6 +10,7 @@ tag_subcommand::tag_subcommand(const libgit2_object&, CLI::App& app)
sub->add_flag("-f,--force", m_force_flag, "Replace an existing tag with the given name (instead of failing)");
sub->add_option("-d,--delete", m_delete, "Delete existing tags with the given names.");
sub->add_option("-n", m_num_lines, "<num> specifies how many lines from the annotation, if any, are printed when using -l. Implies --list.");
sub->add_flag("-a,--annotate", m_annotate_flag, "Make an annotated tag.");
sub->add_option("-m,--message", m_message, "Tag message for annotated tags");
sub->add_option("<tagname>", m_tag_name, "Tag name");
sub->add_option("<commit>", m_target, "Target commit (defaults to HEAD)");
Expand Down Expand Up @@ -217,10 +216,18 @@ void tag_subcommand::run()
{
delete_tag(repo);
}
else if (m_list_flag || (m_tag_name.empty() && m_message.empty()))
else if (m_list_flag || (m_tag_name.empty() && m_message.empty() && !m_annotate_flag))
{
list_tags(repo);
}
else if (m_annotate_flag)
{
if (m_message.empty())
{
throw git_exception("error: -a/--annotate requires -m/--message", git2cpp_error_code::BAD_ARGUMENT);
}
create_tag(repo);
}
else if (!m_message.empty())
{
create_tag(repo);
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/tag_subcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ class tag_subcommand
std::string m_target;
bool m_list_flag = false;
bool m_force_flag = false;
bool m_annotate_flag = false;
int m_num_lines = 0;
};
Loading
Loading