Skip to content

Commit 3aa613e

Browse files
committed
address review cooment
1 parent 87be596 commit 3aa613e

File tree

4 files changed

+89
-25
lines changed

4 files changed

+89
-25
lines changed

src/subcommand/log_subcommand.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
1616
{
1717
auto *sub = app.add_subcommand("log", "Shows commit logs");
1818

19-
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");
19+
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");
2020
sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits.");
2121
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.");
2222
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.");
@@ -230,7 +230,9 @@ void log_subcommand::print_commit(repository_wrapper& repo, const commit_wrapper
230230

231231
std::string message = commit.message();
232232
while (!message.empty() && message.back() == '\n')
233+
{
233234
message.pop_back();
235+
}
234236

235237
if (oneline)
236238
{

src/subcommand/revparse_subcommand.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ revparse_subcommand::revparse_subcommand(const libgit2_object&, CLI::App& app)
88

99
auto* bare_opt = sub->add_flag("--is-bare-repository", m_is_bare_repository_flag, "When the repository is bare print \"true\", otherwise \"false\".");
1010
auto* shallow_opt = sub->add_flag("--is-shallow-repository", m_is_shallow_repository_flag, "When the repository is shallow print \"true\", otherwise \"false\".");
11+
auto* rev_opt = sub->add_option("<rev>", m_revisions, "Revision(s) to parse (e.g. HEAD, main, HEAD~1, dae86e, ...)");
1112

12-
sub->add_option("<rev>", m_revisions, "Revision(s) to parse (e.g. HEAD, main, HEAD~1, dae86e, ...)");
13-
14-
sub->parse_complete_callback([this, sub, bare_opt, shallow_opt]() {
13+
sub->parse_complete_callback([this, sub, bare_opt, shallow_opt, rev_opt]() {
1514
for (CLI::Option* opt : sub->parse_order())
1615
{
1716
if (opt == bare_opt)
@@ -22,6 +21,10 @@ revparse_subcommand::revparse_subcommand(const libgit2_object&, CLI::App& app)
2221
{
2322
m_queries_in_order.push_back("is_shallow");
2423
}
24+
else if (opt == rev_opt)
25+
{
26+
m_queries_in_order.push_back("is_rev");
27+
}
2528
}
2629
});
2730

@@ -33,6 +36,7 @@ void revparse_subcommand::run()
3336
auto directory = get_current_git_path();
3437
auto repo = repository_wrapper::open(directory);
3538

39+
size_t i = 0;
3640
if (!m_queries_in_order.empty())
3741
{
3842
for (const auto& q : m_queries_in_order)
@@ -41,31 +45,25 @@ void revparse_subcommand::run()
4145
{
4246
std::cout << std::boolalpha << repo.is_bare() << std::endl;
4347
}
44-
if (q == "is_shallow")
48+
else if (q == "is_shallow")
4549
{
4650
std::cout << std::boolalpha << repo.is_shallow() << std::endl;
4751
}
48-
}
49-
return;
50-
}
52+
else if (q == "is_rev")
53+
{
54+
const auto& rev = m_revisions[i];
55+
auto obj = repo.revparse_single(rev.c_str());
5156

52-
if (!m_revisions.empty())
53-
{
54-
for (const auto& rev : m_revisions)
55-
{
56-
auto obj = repo.revparse_single(rev.c_str());
57+
if (!obj.has_value())
58+
{
59+
throw git_exception("bad revision '" + rev + "'", git2cpp_error_code::BAD_ARGUMENT);
60+
}
5761

58-
if (!obj.has_value())
59-
{
60-
throw git_exception("bad revision '" + rev + "'", git2cpp_error_code::BAD_ARGUMENT);
61-
return;
62+
auto oid = obj.value().oid();
63+
std::cout << git_oid_tostr_s(&oid) << std::endl;
64+
i += 1;
6265
}
63-
64-
auto oid = obj.value().oid();
65-
std::cout << git_oid_tostr_s(&oid) << std::endl;
6666
}
67-
return;
6867
}
69-
70-
std::cout << "revparse only supports --is-bare-repository, --is-shallow-repository and parsing revisions for now" << std::endl;
68+
return;
7169
}

test/test_revparse.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,55 @@ def test_revparse_multiple_revs(repo_init_with_commit, git2cpp_path, tmp_path):
7070
assert p.returncode == 0
7171

7272
lines = p.stdout.splitlines()
73+
print()
7374
assert len(lines) == 2
7475
assert all(len(x) == 40 for x in lines)
7576
assert lines[0] != lines[1]
77+
78+
79+
def test_revparse_multiple_opts(git2cpp_path, tmp_path, run_in_tmp_path):
80+
"""Test the options are printed in order"""
81+
url = "https://github.com/xtensor-stack/xtl.git"
82+
cmd = [git2cpp_path, "clone", "--depth", "2", url]
83+
p = subprocess.run(cmd, capture_output=True, text=True, cwd=tmp_path)
84+
assert p.returncode == 0
85+
assert (tmp_path / "xtl").exists()
86+
87+
xtl_path = tmp_path / "xtl"
88+
89+
p = subprocess.run(
90+
[
91+
git2cpp_path,
92+
"rev-parse",
93+
"HEAD",
94+
"--is-shallow-repository",
95+
"--is-bare-repository",
96+
"HEAD~1",
97+
],
98+
capture_output=True,
99+
text=True,
100+
cwd=xtl_path,
101+
)
102+
assert p.returncode == 0
103+
104+
lines = p.stdout.splitlines()
105+
assert len(lines) == 4
106+
assert len(lines[0]) == 40
107+
assert len(lines[3]) == 40
108+
assert lines[0] != lines[1]
109+
assert "true" in lines[1]
110+
assert "false" in lines[2]
111+
112+
113+
def test_revparse_errors(repo_init_with_commit, git2cpp_path, tmp_path):
114+
assert (tmp_path / "initial.txt").exists()
115+
116+
rev_cmd = [git2cpp_path, "rev-parse", "HEAD~1"]
117+
p_rev = subprocess.run(rev_cmd, capture_output=True, text=True, cwd=tmp_path)
118+
assert p_rev.returncode == 129
119+
assert "bad revision" in p_rev.stderr
120+
121+
opt_cmd = [git2cpp_path, "rev-parse", "--parseopt"]
122+
p_opt = subprocess.run(opt_cmd, capture_output=True, text=True, cwd=tmp_path)
123+
assert p_opt.returncode != 0
124+
assert "The following argument was not expected:" in p_opt.stderr

test/test_tag.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def test_tag_delete_nonexistent(repo_init_with_commit, git2cpp_path, tmp_path):
135135
assert "not found" in p_delete.stderr
136136

137137

138-
@pytest.mark.parametrize("list_flag", ["-l", "--list"])
139-
def test_tag_list_with_flag(
138+
@pytest.mark.parametrize("list_flag", ["", "-l", "--list"])
139+
def test_tag_list(
140140
repo_init_with_commit, commit_env_config, git2cpp_path, tmp_path, list_flag
141141
):
142142
"""Test listing tags with -l or --list flag."""
@@ -350,3 +350,18 @@ def test_tag_annotate_flag_requires_message(
350350
p = subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, text=True)
351351
assert p.returncode != 0
352352
assert "requires -m" in p.stderr
353+
354+
355+
def test_tag_errors(repo_init_with_commit, commit_env_config, git2cpp_path, tmp_path):
356+
assert (tmp_path / "initial.txt").exists()
357+
358+
# Test that -a/--annotate without -m fails.
359+
create_cmd = [git2cpp_path, "tag", "-a", "v1.0.0"]
360+
p_create = subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, text=True)
361+
assert p_create.returncode != 0
362+
assert "requires -m" in p_create.stderr
363+
364+
# Test that command fails when no message
365+
del_cmd = [git2cpp_path, "tag", "-d"]
366+
p_del = subprocess.run(del_cmd, capture_output=True, cwd=tmp_path, text=True)
367+
assert p_del.returncode != 0

0 commit comments

Comments
 (0)