Skip to content

Commit 343b3f1

Browse files
committed
address review comments
1 parent 1d4a228 commit 343b3f1

File tree

8 files changed

+53
-108
lines changed

8 files changed

+53
-108
lines changed

src/subcommand/tag_subcommand.cpp

Lines changed: 31 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -27,73 +27,26 @@ void print_list_lines(const std::string& message, int num_lines)
2727
return;
2828
}
2929

30-
size_t pos = 0;
31-
int num = num_lines - 1; // TODO: check with git re. "- 1"
30+
auto lines = split_input_at_newlines(message);
3231

33-
/** first line - headline */
34-
size_t newline_pos = message.find('\n', pos);
35-
if (newline_pos != std::string::npos)
36-
{
37-
std::cout << message.substr(pos, newline_pos - pos);
38-
pos = newline_pos;
39-
}
40-
else
41-
{
42-
std::cout << message << std::endl;
43-
return;
44-
}
45-
46-
/** skip over new lines */
47-
while (pos < message.length() && message[pos] == '\n')
48-
{
49-
pos++;
50-
}
51-
52-
std::cout << std::endl;
32+
// header
33+
std::cout << lines[0];
5334

54-
/** print just headline? */
55-
if (num == 0)
56-
{
57-
return;
58-
}
59-
if (pos < message.length() && pos + 1 < message.length())
35+
// other lines
36+
if (num_lines <= 1 || lines.size() <= 2)
6037
{
6138
std::cout << std::endl;
6239
}
63-
64-
/** print individual commit/tag lines */
65-
while (pos < message.length() && num >= 2)
40+
else
6641
{
67-
std::cout << " ";
68-
69-
newline_pos = message.find('\n', pos);
70-
if (newline_pos != std::string::npos)
42+
for (size_t i = 1; i < lines.size() ; i++)
7143
{
72-
std::cout << message.substr(pos, newline_pos - pos);
73-
pos = newline_pos;
44+
if (i < num_lines)
45+
{
46+
std::cout << "\n\t\t" << lines[i];
47+
}
7448
}
75-
else
76-
{
77-
std::cout << message.substr(pos);
78-
break;
79-
}
80-
81-
// Handle consecutive newlines
82-
if (pos + 1 < message.length() &&
83-
message[pos] == '\n' && message[pos + 1] == '\n')
84-
{
85-
num--;
86-
std::cout << std::endl;
87-
}
88-
89-
while (pos < message.length() && message[pos] == '\n')
90-
{
91-
pos++;
92-
}
93-
94-
std::cout << std::endl;
95-
num--;
96-
}
49+
}
9750
}
9851

9952
// Tag listing: Print an actual tag object
@@ -172,9 +125,9 @@ void tag_subcommand::list_tags(repository_wrapper& repo)
172125
std::string pattern = m_tag_name.empty() ? "*" : m_tag_name;
173126
auto tag_names = repo.tag_list_match(pattern);
174127

175-
for (size_t i = 0; i < tag_names.size(); i++)
128+
for (const auto& tag_name: tag_names)
176129
{
177-
each_tag(repo, tag_names[i], m_num_lines);
130+
each_tag(repo, tag_name, m_num_lines);
178131
}
179132
}
180133

@@ -201,7 +154,7 @@ void tag_subcommand::delete_tag(repository_wrapper& repo)
201154
std::cout << "Deleted tag '" << m_delete << "' (was " << oid_str << ")" << std::endl;
202155
}
203156

204-
void tag_subcommand::create_lightweight_tag(repository_wrapper& repo)
157+
std::optional<object_wrapper> tag_subcommand::get_target_obj(repository_wrapper& repo)
205158
{
206159
if (m_tag_name.empty())
207160
{
@@ -216,54 +169,43 @@ void tag_subcommand::create_lightweight_tag(repository_wrapper& repo)
216169
throw git_exception("Unable to resolve target: " + target, git2cpp_error_code::GENERIC_ERROR);
217170
}
218171

219-
git_oid oid;
220-
size_t force = m_force_flag ? 1 : 0;
221-
int error = git_tag_create_lightweight(&oid, repo, m_tag_name.c_str(), target_obj.value(), force);
172+
return target_obj;
173+
}
222174

175+
void tag_subcommand::handle_error(int error)
176+
{
223177
if (error < 0)
224178
{
225179
if (error == GIT_EEXISTS)
226180
{
227181
throw git_exception("tag '" + m_tag_name + "' already exists", git2cpp_error_code::FILESYSTEM_ERROR);
228182
}
229-
throw git_exception("Unable to create lightweight tag", error);
183+
throw git_exception("Unable to create annotated tag", error);
230184
}
231185
}
232186

233-
void tag_subcommand::create_tag(repository_wrapper& repo)
187+
void tag_subcommand::create_lightweight_tag(repository_wrapper& repo)
234188
{
235-
if (m_tag_name.empty())
236-
{
237-
throw git_exception("Tag name required", git2cpp_error_code::GENERIC_ERROR);
238-
}
189+
auto target_obj = tag_subcommand::get_target_obj(repo);
239190

240-
if (m_message.empty())
241-
{
242-
throw git_exception("Message required for annotated tag (use -m)", git2cpp_error_code::GENERIC_ERROR);
243-
}
191+
git_oid oid;
192+
size_t force = m_force_flag ? 1 : 0;
193+
int error = git_tag_create_lightweight(&oid, repo, m_tag_name.c_str(), target_obj.value(), force);
244194

245-
std::string target = m_target.empty() ? "HEAD" : m_target;
195+
handle_error(error);
196+
}
246197

247-
auto target_obj = repo.revparse_single(target);
248-
if (!target_obj.has_value())
249-
{
250-
throw git_exception("Unable to resolve target: " + target, git2cpp_error_code::GENERIC_ERROR);
251-
}
198+
void tag_subcommand::create_tag(repository_wrapper& repo)
199+
{
200+
auto target_obj = tag_subcommand::get_target_obj(repo);
252201

253202
auto tagger = signature_wrapper::get_default_signature_from_env(repo);
254203

255204
git_oid oid;
256205
size_t force = m_force_flag ? 1 : 0;
257206
int error = git_tag_create(&oid, repo, m_tag_name.c_str(), target_obj.value(), tagger.first, m_message.c_str(), force);
258207

259-
if (error < 0)
260-
{
261-
if (error == GIT_EEXISTS)
262-
{
263-
throw git_exception("tag '" + m_tag_name + "' already exists", git2cpp_error_code::FILESYSTEM_ERROR);
264-
}
265-
throw git_exception("Unable to create annotated tag", error);
266-
}
208+
handle_error(error);
267209
}
268210

269211
void tag_subcommand::run()

src/subcommand/tag_subcommand.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class tag_subcommand
1919
void delete_tag(repository_wrapper& repo);
2020
void create_lightweight_tag(repository_wrapper& repo);
2121
void create_tag(repository_wrapper& repo);
22+
std::optional<object_wrapper> get_target_obj(repository_wrapper& repo);
23+
void handle_error(int error);
2224

2325
std::string m_delete;
2426
std::string m_message;

src/utils/common.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sstream>
55
#include <unistd.h>
66
#include <map>
7+
#include <ranges>
78

89
#include <git2.h>
910

@@ -121,3 +122,12 @@ std::string read_file(const std::string& path)
121122
buffer << file.rdbuf();
122123
return buffer.str();
123124
}
125+
126+
std::vector<std::string> split_input_at_newlines(std::string_view str)
127+
{
128+
auto split = str | std::ranges::views::split('\n')
129+
| std::ranges::views::transform([](auto&& range) {
130+
return std::string(range.begin(), range.end());
131+
});
132+
return std::vector<std::string>{split.begin(), split.end()};
133+
}

src/utils/common.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ class git_strarray_wrapper
6868
};
6969

7070
std::string read_file(const std::string& path);
71+
72+
std::vector<std::string> split_input_at_newlines(std::string_view str);

src/utils/terminal_pager.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "ansi_code.hpp"
1313
#include "output.hpp"
1414
#include "terminal_pager.hpp"
15+
#include "common.hpp"
1516

1617
terminal_pager::terminal_pager()
1718
: m_rows(0), m_columns(0), m_start_row_index(0)
@@ -167,7 +168,7 @@ void terminal_pager::show()
167168
{
168169
release_cout();
169170

170-
split_input_at_newlines(m_stringbuf.view());
171+
m_lines = split_input_at_newlines(m_stringbuf.view());
171172

172173
update_terminal_size();
173174
if (m_rows == 0 || m_lines.size() <= m_rows - 1)
@@ -196,15 +197,6 @@ void terminal_pager::show()
196197
m_start_row_index = 0;
197198
}
198199

199-
void terminal_pager::split_input_at_newlines(std::string_view str)
200-
{
201-
auto split = str | std::ranges::views::split('\n')
202-
| std::ranges::views::transform([](auto&& range) {
203-
return std::string(range.begin(), range.end());
204-
});
205-
m_lines = std::vector<std::string>{split.begin(), split.end()};
206-
}
207-
208200
void terminal_pager::update_terminal_size()
209201
{
210202
struct winsize size;

src/utils/terminal_pager.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ class terminal_pager
4949

5050
void scroll(bool up, bool page);
5151

52-
void split_input_at_newlines(std::string_view str);
53-
5452
void update_terminal_size();
5553

5654

src/wrapper/repository_wrapper.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,7 @@ std::vector<std::string> repository_wrapper::tag_list_match(std::string pattern)
513513
git_strarray tag_names;
514514
throw_if_error(git_tag_list_match(&tag_names, pattern.c_str(), *this));
515515

516-
std::vector<std::string> result;
517-
for (size_t i = 0; i < tag_names.count; ++i)
518-
{
519-
result.emplace_back(tag_names.strings[i]);
520-
}
516+
std::vector<std::string> result(tag_names.strings, tag_names.strings + tag_names.count);
521517

522518
git_strarray_dispose(&tag_names);
523519
return result;

test/test_tag.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_tag_list_with_message_lines(xtl_clone, commit_env_config, git2cpp_path,
175175
xtl_path = tmp_path / "xtl"
176176

177177
# Create an annotated tag with a message
178-
create_cmd = [git2cpp_path, 'tag', '-m', 'First line\nSecond line\nThird line', 'v1.0.0']
178+
create_cmd = [git2cpp_path, 'tag', '-m', 'First line\nSecond line\nThird line\nForth line', 'v1.0.0']
179179
p_create = subprocess.run(create_cmd, capture_output=True, cwd=xtl_path, text=True)
180180
assert p_create.returncode == 0
181181

@@ -184,7 +184,10 @@ def test_tag_list_with_message_lines(xtl_clone, commit_env_config, git2cpp_path,
184184
p_list = subprocess.run(list_cmd, capture_output=True, cwd=xtl_path, text=True)
185185
assert p_list.returncode == 0
186186
assert 'v1.0.0' in p_list.stdout
187-
# TODO: another assert after checking what should be printed with git
187+
assert 'First line' in p_list.stdout
188+
assert 'Second line' in p_list.stdout
189+
assert 'Third line' in p_list.stdout
190+
assert 'Forth line' not in p_list.stdout
188191

189192

190193
@pytest.mark.parametrize("force_flag", ["-f", "--force"])

0 commit comments

Comments
 (0)