Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add log subcommand
  • Loading branch information
SandrineP committed Aug 26, 2025
commit a509df8a79a64a4b6d1dad051bf33063015e5871
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/subcommand/commit_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/log_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/log_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "subcommand/clone_subcommand.hpp"
#include "subcommand/commit_subcommand.hpp"
#include "subcommand/init_subcommand.hpp"
#include "subcommand/log_subcommand.hpp"
#include "subcommand/reset_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"

Expand All @@ -33,6 +34,7 @@ int main(int argc, char** argv)
clone_subcommand clone(lg2_obj, app);
commit_subcommand commit(lg2_obj, app);
reset_subcommand reset(lg2_obj, app);
log_subcommand log(lg2_obj, app);

app.require_subcommand(/* min */ 0, /* max */ 1);

Expand Down
88 changes: 88 additions & 0 deletions src/subcommand/log_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// #include <iostream>
// #include <ostream>
// #include <string>

#include <git2.h>
#include <git2/revwalk.h>
#include <git2/types.h>

#include "log_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"

// TODO: put in another file
/** Size (in bytes) of a raw/binary sha1 oid */
#define GIT_OID_SHA1_SIZE 20
/** Size (in bytes) of a hex formatted sha1 oid */
#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)

log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
{
auto *sub = app.add_subcommand("log", "Shows commit logs");

sub->add_flag("-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->callback([this]() { this->run(); });
};

void print_time(git_time intime, const char *prefix)
{
char sign, out[32];
struct tm *intm;
int offset, hours, minutes;
time_t t;

offset = intime.offset;
if (offset < 0) {
sign = '-';
offset = -offset;
} else {
sign = '+';
}

hours = offset / 60;
minutes = offset % 60;

t = (time_t)intime.time + (intime.offset * 60);

intm = gmtime(&t);
strftime(out, sizeof(out), "%a %b %e %T %Y", intm);

printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes);
}

void print_commit(const commit_wrapper& commit)
{
// TODO: put in commit_wrapper ?
char buf[GIT_OID_SHA1_HEXSIZE + 1];
int i, count;

git_oid_tostr(buf, sizeof(buf), &commit.oid());
// TODO end

signature_wrapper author = signature_wrapper::get_commit_author(commit);

std::cout << "commit " << buf << std::endl;
std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl;
print_time(author.when(), "Date:\t");
std::cout << git_commit_message(commit) << "\n" << std::endl;
}

void log_subcommand::run()
{
auto directory = get_current_git_path();
auto bare = false;
auto repo = repository_wrapper::init(directory, bare);
// auto branch_name = repo.head().short_name();

git_revwalk* walker;
git_revwalk_new(&walker, repo);
Comment thread
JohanMabille marked this conversation as resolved.
git_revwalk_push_head(walker);

git_oid commit_oid;
while (!git_revwalk_next(&commit_oid, walker))
{
commit_wrapper commit = repo.find_commit(commit_oid);
print_commit(commit);
}
}
18 changes: 18 additions & 0 deletions src/subcommand/log_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <CLI/CLI.hpp>

#include "../utils/common.hpp"


class log_subcommand
{
public:

explicit log_subcommand(const libgit2_object&, CLI::App& app);
void run();

private:
int m_max_count_flag;
// bool m_oneline_flag = false;
};
29 changes: 29 additions & 0 deletions src/wrapper/signature_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,39 @@ signature_wrapper::~signature_wrapper()
p_resource=nullptr;
}

std::string_view signature_wrapper::name() const
{
return p_resource->name;
}

std::string_view signature_wrapper::email() const
{
return p_resource->email;
}

git_time signature_wrapper::when() const
{
return p_resource->when;
}

signature_wrapper::author_committer_signatures signature_wrapper::get_default_signature_from_env(repository_wrapper& rw)
{
signature_wrapper author;
signature_wrapper committer;
throw_if_error(git_signature_default_from_env(&(author.p_resource), &(committer.p_resource), rw));
return {std::move(author), std::move(committer)};
}

signature_wrapper signature_wrapper::get_commit_author(const commit_wrapper& cw)
{
signature_wrapper author;
author.p_resource = const_cast<git_signature*>(git_commit_author(cw));
return author;
}

signature_wrapper signature_wrapper::get_commit_committer(const commit_wrapper& cw)
{
signature_wrapper author;
author.p_resource = const_cast<git_signature*>(git_commit_committer(cw));
return author;
}
8 changes: 8 additions & 0 deletions src/wrapper/signature_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include <utility>
#include <string_view>

#include <git2.h>

#include "../wrapper/wrapper_base.hpp"

class commit_wrapper;
class repository_wrapper;

class signature_wrapper : public wrapper_base<git_signature>
Expand All @@ -18,7 +20,13 @@ class signature_wrapper : public wrapper_base<git_signature>
signature_wrapper(signature_wrapper&&) = default;
signature_wrapper& operator=(signature_wrapper&&) = default;

std::string_view name() const;
std::string_view email() const;
git_time when() const;

static author_committer_signatures get_default_signature_from_env(repository_wrapper&);
static signature_wrapper get_commit_author(const commit_wrapper&);
static signature_wrapper get_commit_committer(const commit_wrapper&);

private:

Expand Down