FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
git2cpp/src/subcommand/tag_subcommand.cpp at 0.0.12 · QuantStack/git2cpp · GitHub
QuantStack
git2cpp
Repository navigation
Code
Issues
6
(6)
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
git2cpp
/
src
/
subcommand
/
tag_subcommand.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (228 loc) · 6.86 KB
Breadcrumbs
git2cpp
/
src
/
subcommand
/
tag_subcommand.cpp
Copy path
File metadata and controls
263 lines (228 loc) · 6.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
include
"
../subcommand/tag_subcommand.hpp
"
#
include
<
git2.h
>
tag_subcommand::tag_subcommand
(
const
libgit2_object&,
CLI
::App& app)
{
auto
* sub = app.
add_subcommand
(
"
tag
"
,
"
Create, list, delete or verify tags
"
);
sub->
add_flag
(
"
-l,--list
"
, m_list_flag,
"
List tags. With optional <pattern>.
"
);
sub->
add_flag
(
"
-f,--force
"
, m_force_flag,
"
Replace an existing tag with the given name (instead of failing)
"
);
sub->
add_option
(
"
-d,--delete
"
, m_delete,
"
Delete existing tags with the given names.
"
);
sub->
add_option
(
"
-n
"
,
m_num_lines,
"
<num> specifies how many lines from the annotation, if any, are printed when using -l. Implies --list.
"
);
sub->
add_flag
(
"
-a,--annotate
"
, m_annotate_flag,
"
Make an annotated tag.
"
);
sub->
add_option
(
"
-m,--message
"
, m_message,
"
Tag message for annotated tags
"
);
sub->
add_option
(
"
<tagname>
"
, m_tag_name,
"
Tag name
"
);
sub->
add_option
(
"
<commit>
"
, m_target,
"
Target commit (defaults to HEAD)
"
);
sub->
callback
(
[
this
]()
{
this
->
run
();
}
);
}
namespace
{
//
Tag listing: Print individual message lines
void
print_list_lines
(
const
std::string& message,
int
num_lines)
{
if
(message.
empty
())
{
return
;
}
auto
lines =
split_input_at_newlines
(message);
//
header
std::cout << lines[
0
];
//
other lines
if
(num_lines <=
1
|| lines.
size
() <=
2
)
{
std::cout << std::endl;
}
else
{
for
(
size_t
i =
1
; i < lines.
size
(); i++)
{
if
(i < num_lines)
{
std::cout <<
"
\n\t\t
"
<< lines[i];
}
}
}
}
//
Tag listing: Print an actual tag object
void
print_tag
(git_tag* tag,
int
num_lines)
{
std::cout << std::left <<
std::setw
(
16
) <<
git_tag_name
(tag);
if
(num_lines)
{
std::string msg =
git_tag_message
(tag);
if
(!msg.
empty
())
{
print_list_lines
(msg, num_lines);
}
else
{
std::cout << std::endl;
}
}
else
{
std::cout << std::endl;
}
}
//
Tag listing: Print a commit (target of a lightweight tag)
void
print_commit
(git_commit* commit, std::string name,
int
num_lines)
{
std::cout << std::left <<
std::setw
(
16
) << name;
if
(num_lines)
{
std::string msg =
git_commit_message
(commit);
if
(!msg.
empty
())
{
print_list_lines
(msg, num_lines);
}
else
{
std::cout << std::endl;
}
}
else
{
std::cout << std::endl;
}
}
//
Tag listing: Lookup tags based on ref name and dispatch to print
void
each_tag
(repository_wrapper& repo,
const
std::string& name,
int
num_lines)
{
auto
obj = repo.
revparse_single
(name);
if
(obj.
has_value
())
{
switch
(
git_object_type
(obj.
value
()))
{
case
GIT_OBJECT_TAG
:
print_tag
(obj.
value
(), num_lines);
break
;
case
GIT_OBJECT_COMMIT
:
print_commit
(obj.
value
(), name, num_lines);
break
;
default
:
std::cout << name << std::endl;
}
}
else
{
std::cout << name << std::endl;
}
}
}
void
tag_subcommand::list_tags
(repository_wrapper& repo)
{
std::string pattern = m_tag_name.
empty
() ?
"
*
"
: m_tag_name;
auto
tag_names = repo.
tag_list_match
(pattern);
for
(
size_t
i =
0u
; i < tag_names.
size
(); ++i)
{
each_tag
(repo, tag_names[i], m_num_lines);
}
}
void
tag_subcommand::delete_tag
(repository_wrapper& repo)
{
if
(m_delete.
empty
())
{
throw
git_exception
(
"
Name required for tag deletion.
"
, git2cpp_error_code::
GENERIC_ERROR
);
}
auto
obj = repo.
revparse_single
(m_delete);
if
(!obj.
has_value
())
{
throw
git_exception
(
"
error: tag '
"
+ m_delete +
"
' not found.
"
, git2cpp_error_code::
GENERIC_ERROR
);
}
git_buf abbrev_oid =
GIT_BUF_INIT
;
throw_if_error
(
git_object_short_id
(&abbrev_oid, obj.
value
()));
std::string
oid_str
(abbrev_oid.
ptr
);
git_buf_dispose
(&abbrev_oid);
throw_if_error
(
git_tag_delete
(repo, m_delete.
c_str
()));
std::cout <<
"
Deleted tag '
"
<< m_delete <<
"
' (was
"
<< oid_str <<
"
)
"
<< std::endl;
}
std::optional<object_wrapper>
tag_subcommand::get_target_obj
(repository_wrapper& repo)
{
if
(m_tag_name.
empty
())
{
throw
git_exception
(
"
Tag name required
"
, git2cpp_error_code::
GENERIC_ERROR
);
}
std::string target = m_target.
empty
() ?
"
HEAD
"
: m_target;
auto
target_obj = repo.
revparse_single
(target);
if
(!target_obj.
has_value
())
{
throw
git_exception
(
"
Unable to resolve target:
"
+ target, git2cpp_error_code::
GENERIC_ERROR
);
}
return
target_obj;
}
void
tag_subcommand::handle_error
(
int
error)
{
if
(error <
0
)
{
if
(error ==
GIT_EEXISTS
)
{
throw
git_exception
(
"
tag '
"
+ m_tag_name +
"
' already exists
"
, git2cpp_error_code::
FILESYSTEM_ERROR
);
}
throw
git_exception
(
"
Unable to create annotated tag
"
, error);
}
}
void
tag_subcommand::create_lightweight_tag
(repository_wrapper& repo)
{
auto
target_obj =
tag_subcommand::get_target_obj
(repo);
git_oid oid;
size_t
force = m_force_flag ?
1
:
0
;
int
error =
git_tag_create_lightweight
(&oid, repo, m_tag_name.
c_str
(), target_obj.
value
(), force);
handle_error
(error);
}
void
tag_subcommand::create_tag
(repository_wrapper& repo)
{
auto
target_obj =
tag_subcommand::get_target_obj
(repo);
auto
tagger =
signature_wrapper::get_default_signature_from_env
(repo);
git_oid oid;
size_t
force = m_force_flag ?
1
:
0
;
int
error =
git_tag_create
(
&oid,
repo,
m_tag_name.
c_str
(),
target_obj.
value
(),
tagger.
first
,
m_message.
c_str
(),
force
);
handle_error
(error);
}
void
tag_subcommand::run
()
{
auto
directory =
get_current_git_path
();
auto
repo =
repository_wrapper::open
(directory);
if
(!m_delete.
empty
())
{
delete_tag
(repo);
}
else
if
(m_list_flag || (m_tag_name.
empty
() && m_message.
empty
() && !m_annotate_flag))
{
list_tags
(repo);
}
else
if
(m_annotate_flag)
{
if
(m_message.
empty
())
{
throw
git_exception
(
"
error: -a/--annotate requires -m/--message
"
, git2cpp_error_code::
BAD_ARGUMENT
);
}
create_tag
(repo);
}
else
if
(!m_message.
empty
())
{
create_tag
(repo);
}
else
if
(!m_tag_name.
empty
())
{
create_lightweight_tag
(repo);
}
else
{
list_tags
(repo);
}
}
Back
|
FazBrowse Home
|
New Git URL