Skip to content

Commit b08b8ca

Browse files
committed
fix memry issue and add test
1 parent 16cfe1c commit b08b8ca

File tree

6 files changed

+77
-34
lines changed

6 files changed

+77
-34
lines changed

src/subcommand/log_subcommand.cpp

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
// #include <iostream>
2-
// #include <ostream>
3-
// #include <string>
4-
1+
#include <format>
52
#include <git2.h>
63
#include <git2/revwalk.h>
74
#include <git2/types.h>
5+
#include <string_view>
86

97
#include "log_subcommand.hpp"
108
#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)
9+
#include "../wrapper/commit_wrapper.hpp"
1710

1811
log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
1912
{
@@ -26,7 +19,7 @@ log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
2619
sub->callback([this]() { this->run(); });
2720
};
2821

29-
void print_time(git_time intime, const char *prefix)
22+
void print_time(git_time intime, std::string prefix)
3023
{
3124
char sign, out[32];
3225
struct tm *intm;
@@ -37,7 +30,9 @@ void print_time(git_time intime, const char *prefix)
3730
if (offset < 0) {
3831
sign = '-';
3932
offset = -offset;
40-
} else {
33+
}
34+
else
35+
{
4136
sign = '+';
4237
}
4338

@@ -49,38 +44,37 @@ void print_time(git_time intime, const char *prefix)
4944
intm = gmtime(&t);
5045
strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
5146

52-
printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes);
47+
std::cout << prefix << out << " " << sign << std::format("{:02d}", hours) << std::format("{:02d}", minutes) <<std::endl;
5348
}
5449

5550
void print_commit(const commit_wrapper& commit, std::string m_format_flag)
5651
{
57-
// TODO: put in commit_wrapper ?
58-
char buf[GIT_OID_SHA1_HEXSIZE + 1];
59-
int i, count;
60-
61-
git_oid_tostr(buf, sizeof(buf), &commit.oid());
62-
// TODO end
52+
std::string buf = commit.commit_oid_tostr();
6353

6454
signature_wrapper author = signature_wrapper::get_commit_author(commit);
6555
signature_wrapper committer = signature_wrapper::get_commit_committer(commit);
6656

6757
std::cout << "\033[0;33m" << "commit " << buf << "\033[0m" << std::endl;
68-
std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl;
69-
if (m_format_flag=="full")
70-
{
71-
std::cout << "Commit:\t" << committer.name() << " " << committer.email() << std::endl;
72-
}
73-
else if (m_format_flag=="fuller")
58+
if (m_format_flag=="fuller")
7459
{
75-
print_time(author.when(), "AuthorDate:\t");
76-
std::cout << "Commit:\t" << committer.name() << " " << committer.email() << std::endl;
77-
print_time(committer.when(), "CommitDate:\t");
60+
std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl;
61+
print_time(author.when(), "AuthorDate: ");
62+
std::cout << "Commit:\t " << committer.name() << " " << committer.email() << std::endl;
63+
print_time(committer.when(), "CommitDate: ");
7864
}
7965
else
8066
{
81-
print_time(author.when(), "Date:\t");
67+
std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl;
68+
if (m_format_flag=="full")
69+
{
70+
std::cout << "Commit:\t" << committer.name() << " " << committer.email() << std::endl;
71+
}
72+
else
73+
{
74+
print_time(author.when(), "Date:\t");
75+
}
8276
}
83-
std::cout << git_commit_message(commit) << "\n" << std::endl;
77+
std::cout << "\n " << git_commit_message(commit) << "\n" << std::endl;
8478
}
8579

8680
void log_subcommand::run()

src/wrapper/commit_wrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ const git_oid& commit_wrapper::oid() const
2020
{
2121
return *git_commit_id(p_resource);
2222
}
23+
24+
std::string commit_wrapper::commit_oid_tostr() const
25+
{
26+
char buf[GIT_OID_SHA1_HEXSIZE + 1];
27+
return git_oid_tostr(buf, sizeof(buf), &this->oid());
28+
}

src/wrapper/commit_wrapper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class commit_wrapper : public wrapper_base<git_commit>
1919
operator git_object*() const noexcept;
2020

2121
const git_oid& oid() const;
22+
std::string commit_oid_tostr() const;
2223

2324
private:
2425

src/wrapper/signature_wrapper.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
signature_wrapper::~signature_wrapper()
66
{
7-
git_signature_free(p_resource);
7+
if (m_ownership)
8+
{
9+
git_signature_free(p_resource);
10+
}
811
p_resource=nullptr;
912
}
1013

@@ -35,12 +38,14 @@ signature_wrapper signature_wrapper::get_commit_author(const commit_wrapper& cw)
3538
{
3639
signature_wrapper author;
3740
author.p_resource = const_cast<git_signature*>(git_commit_author(cw));
41+
author.m_ownership = false;
3842
return author;
3943
}
4044

4145
signature_wrapper signature_wrapper::get_commit_committer(const commit_wrapper& cw)
4246
{
43-
signature_wrapper author;
44-
author.p_resource = const_cast<git_signature*>(git_commit_committer(cw));
45-
return author;
47+
signature_wrapper committer;
48+
committer.p_resource = const_cast<git_signature*>(git_commit_committer(cw));
49+
committer.m_ownership = false;
50+
return committer;
4651
}

src/wrapper/signature_wrapper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ class signature_wrapper : public wrapper_base<git_signature>
3131
private:
3232

3333
signature_wrapper() = default;
34+
bool m_ownership=true;
3435
};

test/test_log.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import subprocess
2+
3+
import pytest
4+
5+
@pytest.mark.parametrize("format_flag", ["", "--format=full", "--format=fuller"])
6+
def test_log(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, format_flag):
7+
assert (tmp_path / "xtl").exists()
8+
xtl_path = tmp_path / "xtl"
9+
10+
p = xtl_path / "mook_file.txt"
11+
p.write_text('')
12+
13+
cmd_add = [git2cpp_path, 'add', "mook_file.txt"]
14+
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
15+
assert p_add.returncode == 0
16+
17+
cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"]
18+
p_commit = subprocess.run(cmd_commit, cwd=xtl_path, text=True)
19+
assert p_commit.returncode == 0
20+
21+
cmd_log = [git2cpp_path, 'log']
22+
if format_flag != "":
23+
cmd_log.append(format_flag)
24+
p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
25+
assert p_log.returncode == 0
26+
assert "Jane Doe" in p_log.stdout
27+
assert "test commit" in p_log.stdout
28+
29+
if format_flag == "":
30+
assert "Commit" not in p_log.stdout
31+
else:
32+
assert "Commit" in p_log.stdout
33+
if format_flag == "--format=full":
34+
assert "Date" not in p_log.stdout
35+
else:
36+
assert "CommitDate" in p_log.stdout

0 commit comments

Comments
 (0)