@@ -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
269211void tag_subcommand::run ()
0 commit comments