address review cooment · QuantStack/git2cpp@3aa613e · GitHub
Skip to content

Commit 3aa613e

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

4 files changed

Lines changed: 89 additions & 25 deletions

File tree

src/subcommand/log_subcommand.cpp

Lines changed: 3 additions & 1 deletion

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

0 commit comments

Comments
 (0)