Skip to content

Commit bf9dbf8

Browse files
committed
add --max-count
1 parent 1b02641 commit bf9dbf8

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/subcommand/log_subcommand.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
1313
auto *sub = app.add_subcommand("log", "Shows commit logs");
1414

1515
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");
16-
// sub->add_flag("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
16+
sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
1717
// sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --pretty=oneline --abbrev-commit used together.");
1818

1919
sub->callback([this]() { this->run(); });
@@ -88,11 +88,13 @@ void log_subcommand::run()
8888
git_revwalk_new(&walker, repo);
8989
git_revwalk_push_head(walker);
9090

91+
std::size_t i=0;
9192
git_oid commit_oid;
92-
while (!git_revwalk_next(&commit_oid, walker))
93+
while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag)
9394
{
9495
commit_wrapper commit = repo.find_commit(commit_oid);
9596
print_commit(commit, m_format_flag);
97+
i+=1;
9698
}
9799

98100
git_revwalk_free(walker);

src/subcommand/log_subcommand.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <CLI/CLI.hpp>
4+
#include <cstddef>
5+
#include <limits>
46

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

@@ -14,6 +16,6 @@ class log_subcommand
1416

1517
private:
1618
std::string m_format_flag;
17-
// int m_max_count_flag;
19+
int m_max_count_flag=std::numeric_limits<int>::max();
1820
// bool m_oneline_flag = false;
1921
};

test/test_log.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,21 @@ def test_log(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, format_
3434
assert "Date" not in p_log.stdout
3535
else:
3636
assert "CommitDate" in p_log.stdout
37+
38+
39+
@pytest.mark.parametrize("max_count_flag", ["", "-n", "--max-count"])
40+
def test_max_count(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, max_count_flag):
41+
assert (tmp_path / "xtl").exists()
42+
xtl_path = tmp_path / "xtl"
43+
44+
cmd_log = [git2cpp_path, 'log']
45+
if max_count_flag != "":
46+
cmd_log.append(max_count_flag)
47+
cmd_log.append("2")
48+
p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
49+
assert p_log.returncode == 0
50+
51+
if max_count_flag == "":
52+
assert p_log.stdout.count("Author") > 2
53+
else:
54+
assert p_log.stdout.count("Author") == 2

0 commit comments

Comments
 (0)