]>
Commit | Line | Data |
---|---|---|
62e09ce9 CR |
1 | /* |
2 | * Builtin "git tag" | |
3 | * | |
4 | * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>, | |
5 | * Carlos Rica <jasampler@gmail.com> | |
6 | * Based on git-tag.sh and mktag.c by Linus Torvalds. | |
7 | */ | |
8 | ||
9 | #include "cache.h" | |
b2141fc1 | 10 | #include "config.h" |
62e09ce9 CR |
11 | #include "builtin.h" |
12 | #include "refs.h" | |
13 | #include "tag.h" | |
14 | #include "run-command.h" | |
39686585 | 15 | #include "parse-options.h" |
ffc4b801 JK |
16 | #include "diff.h" |
17 | #include "revision.h" | |
2f47eae2 | 18 | #include "gpg-interface.h" |
ae7706b9 | 19 | #include "sha1-array.h" |
d96e3c15 | 20 | #include "column.h" |
ac4cc866 | 21 | #include "ref-filter.h" |
39686585 CR |
22 | |
23 | static const char * const git_tag_usage[] = { | |
9c9b4f2f | 24 | N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"), |
c88bba18 | 25 | N_("git tag -d <tagname>..."), |
ac3f5a34 | 26 | N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]" |
5242860f | 27 | "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"), |
07d347cf | 28 | N_("git tag -v [--format=<format>] <tagname>..."), |
39686585 CR |
29 | NULL |
30 | }; | |
62e09ce9 | 31 | |
d96e3c15 | 32 | static unsigned int colopts; |
61c2fe0c | 33 | static int force_sign_annotate; |
ae7706b9 | 34 | |
df094741 | 35 | static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format) |
ca516999 | 36 | { |
ac4cc866 | 37 | struct ref_array array; |
df094741 | 38 | char *to_free = NULL; |
ca516999 | 39 | int i; |
62e09ce9 | 40 | |
ac4cc866 | 41 | memset(&array, 0, sizeof(array)); |
32c35cfb | 42 | |
ac4cc866 KN |
43 | if (filter->lines == -1) |
44 | filter->lines = 0; | |
ae7706b9 | 45 | |
df094741 KN |
46 | if (!format) { |
47 | if (filter->lines) { | |
48 | to_free = xstrfmt("%s %%(contents:lines=%d)", | |
17938f17 | 49 | "%(align:15)%(refname:lstrip=2)%(end)", |
df094741 KN |
50 | filter->lines); |
51 | format = to_free; | |
52 | } else | |
17938f17 | 53 | format = "%(refname:lstrip=2)"; |
62e09ce9 CR |
54 | } |
55 | ||
b7cc53e9 | 56 | verify_ref_format(format); |
008ed7df | 57 | filter->with_commit_tag_algo = 1; |
b7cc53e9 KN |
58 | filter_refs(&array, filter, FILTER_REFS_TAGS); |
59 | ref_array_sort(sorting, &array); | |
62e09ce9 | 60 | |
b7cc53e9 KN |
61 | for (i = 0; i < array.nr; i++) |
62 | show_ref_array_item(array.items[i], format, 0); | |
63 | ref_array_clear(&array); | |
64 | free(to_free); | |
9ef176b5 | 65 | |
62e09ce9 CR |
66 | return 0; |
67 | } | |
68 | ||
e317cfaf | 69 | typedef int (*each_tag_name_fn)(const char *name, const char *ref, |
07d347cf | 70 | const unsigned char *sha1, const void *cb_data); |
62e09ce9 | 71 | |
07d347cf LP |
72 | static int for_each_tag_name(const char **argv, each_tag_name_fn fn, |
73 | const void *cb_data) | |
62e09ce9 CR |
74 | { |
75 | const char **p; | |
7f897b6f | 76 | struct strbuf ref = STRBUF_INIT; |
62e09ce9 CR |
77 | int had_error = 0; |
78 | unsigned char sha1[20]; | |
79 | ||
80 | for (p = argv; *p; p++) { | |
7f897b6f JK |
81 | strbuf_reset(&ref); |
82 | strbuf_addf(&ref, "refs/tags/%s", *p); | |
83 | if (read_ref(ref.buf, sha1)) { | |
d08ebf99 | 84 | error(_("tag '%s' not found."), *p); |
62e09ce9 CR |
85 | had_error = 1; |
86 | continue; | |
87 | } | |
7f897b6f | 88 | if (fn(*p, ref.buf, sha1, cb_data)) |
62e09ce9 CR |
89 | had_error = 1; |
90 | } | |
7f897b6f | 91 | strbuf_release(&ref); |
62e09ce9 CR |
92 | return had_error; |
93 | } | |
94 | ||
95 | static int delete_tag(const char *name, const char *ref, | |
07d347cf | 96 | const unsigned char *sha1, const void *cb_data) |
62e09ce9 | 97 | { |
755b49ae | 98 | if (delete_ref(NULL, ref, sha1, 0)) |
62e09ce9 | 99 | return 1; |
d08ebf99 | 100 | printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
62e09ce9 CR |
101 | return 0; |
102 | } | |
103 | ||
104 | static int verify_tag(const char *name, const char *ref, | |
07d347cf | 105 | const unsigned char *sha1, const void *cb_data) |
62e09ce9 | 106 | { |
07d347cf LP |
107 | int flags; |
108 | const char *fmt_pretty = cb_data; | |
109 | flags = GPG_VERIFY_VERBOSE; | |
110 | ||
111 | if (fmt_pretty) | |
112 | flags = GPG_VERIFY_OMIT_STATUS; | |
113 | ||
114 | if (gpg_verify_tag(sha1, name, flags)) | |
115 | return -1; | |
116 | ||
117 | if (fmt_pretty) | |
118 | pretty_print_ref(name, sha1, fmt_pretty); | |
119 | ||
120 | return 0; | |
62e09ce9 CR |
121 | } |
122 | ||
fd17f5b5 | 123 | static int do_sign(struct strbuf *buffer) |
62e09ce9 | 124 | { |
2f47eae2 | 125 | return sign_buffer(buffer, buffer, get_signing_key()); |
62e09ce9 CR |
126 | } |
127 | ||
128 | static const char tag_template[] = | |
d78f340e | 129 | N_("\nWrite a message for tag:\n %s\n" |
eff80a9f | 130 | "Lines starting with '%c' will be ignored.\n"); |
d3e05983 KS |
131 | |
132 | static const char tag_template_nocleanup[] = | |
d78f340e | 133 | N_("\nWrite a message for tag:\n %s\n" |
eff80a9f JH |
134 | "Lines starting with '%c' will be kept; you may remove them" |
135 | " yourself if you want to.\n"); | |
62e09ce9 | 136 | |
b7cc53e9 KN |
137 | /* Parse arg given and add it the ref_sorting array */ |
138 | static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail) | |
b150794d | 139 | { |
b7cc53e9 KN |
140 | struct ref_sorting *s; |
141 | int len; | |
b150794d | 142 | |
b7cc53e9 KN |
143 | s = xcalloc(1, sizeof(*s)); |
144 | s->next = *sorting_tail; | |
145 | *sorting_tail = s; | |
b150794d | 146 | |
b7cc53e9 KN |
147 | if (*arg == '-') { |
148 | s->reverse = 1; | |
149 | arg++; | |
b150794d | 150 | } |
b7cc53e9 KN |
151 | if (skip_prefix(arg, "version:", &arg) || |
152 | skip_prefix(arg, "v:", &arg)) | |
153 | s->version = 1; | |
b150794d | 154 | |
b7cc53e9 KN |
155 | len = strlen(arg); |
156 | s->atom = parse_ref_filter_atom(arg, arg+len); | |
b150794d JK |
157 | |
158 | return 0; | |
159 | } | |
160 | ||
ef90d6d4 | 161 | static int git_tag_config(const char *var, const char *value, void *cb) |
62e09ce9 | 162 | { |
b150794d | 163 | int status; |
b7cc53e9 | 164 | struct ref_sorting **sorting_tail = (struct ref_sorting **)cb; |
b150794d JK |
165 | |
166 | if (!strcmp(var, "tag.sort")) { | |
167 | if (!value) | |
168 | return config_error_nonbool(var); | |
b7cc53e9 | 169 | parse_sorting_string(value, sorting_tail); |
b150794d JK |
170 | return 0; |
171 | } | |
172 | ||
173 | status = git_gpg_config(var, value, cb); | |
2f47eae2 JH |
174 | if (status) |
175 | return status; | |
61c2fe0c LA |
176 | if (!strcmp(var, "tag.forcesignannotated")) { |
177 | force_sign_annotate = git_config_bool(var, value); | |
178 | return 0; | |
179 | } | |
180 | ||
59556548 | 181 | if (starts_with(var, "column.")) |
d96e3c15 | 182 | return git_column_config(var, value, "tag", &colopts); |
ef90d6d4 | 183 | return git_default_config(var, value, cb); |
62e09ce9 CR |
184 | } |
185 | ||
bab8118a MH |
186 | static void write_tag_body(int fd, const unsigned char *sha1) |
187 | { | |
188 | unsigned long size; | |
189 | enum object_type type; | |
e10dfb62 | 190 | char *buf, *sp; |
bab8118a MH |
191 | |
192 | buf = read_sha1_file(sha1, &type, &size); | |
193 | if (!buf) | |
194 | return; | |
195 | /* skip header */ | |
196 | sp = strstr(buf, "\n\n"); | |
197 | ||
198 | if (!sp || !size || type != OBJ_TAG) { | |
199 | free(buf); | |
200 | return; | |
201 | } | |
202 | sp += 2; /* skip the 2 LFs */ | |
e10dfb62 | 203 | write_or_die(fd, sp, parse_signature(sp, buf + size - sp)); |
bab8118a MH |
204 | |
205 | free(buf); | |
206 | } | |
207 | ||
3927bbe9 JK |
208 | static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result) |
209 | { | |
210 | if (sign && do_sign(buf) < 0) | |
d08ebf99 | 211 | return error(_("unable to sign the tag")); |
3927bbe9 | 212 | if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0) |
d08ebf99 | 213 | return error(_("unable to write tag file")); |
3927bbe9 JK |
214 | return 0; |
215 | } | |
216 | ||
d3e05983 KS |
217 | struct create_tag_options { |
218 | unsigned int message_given:1; | |
219 | unsigned int sign; | |
220 | enum { | |
221 | CLEANUP_NONE, | |
222 | CLEANUP_SPACE, | |
223 | CLEANUP_ALL | |
224 | } cleanup_mode; | |
225 | }; | |
226 | ||
62e09ce9 | 227 | static void create_tag(const unsigned char *object, const char *tag, |
d3e05983 | 228 | struct strbuf *buf, struct create_tag_options *opt, |
bd46c9a9 | 229 | unsigned char *prev, unsigned char *result) |
62e09ce9 CR |
230 | { |
231 | enum object_type type; | |
b0ceab98 | 232 | struct strbuf header = STRBUF_INIT; |
3927bbe9 | 233 | char *path = NULL; |
62e09ce9 CR |
234 | |
235 | type = sha1_object_info(object, NULL); | |
e317cfaf | 236 | if (type <= OBJ_NONE) |
d08ebf99 | 237 | die(_("bad object type.")); |
62e09ce9 | 238 | |
b0ceab98 JK |
239 | strbuf_addf(&header, |
240 | "object %s\n" | |
241 | "type %s\n" | |
242 | "tag %s\n" | |
243 | "tagger %s\n\n", | |
244 | sha1_to_hex(object), | |
245 | typename(type), | |
246 | tag, | |
247 | git_committer_info(IDENT_STRICT)); | |
62e09ce9 | 248 | |
d3e05983 | 249 | if (!opt->message_given) { |
62e09ce9 CR |
250 | int fd; |
251 | ||
252 | /* write the template message before editing: */ | |
a4f34cbb | 253 | path = git_pathdup("TAG_EDITMSG"); |
62e09ce9 CR |
254 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
255 | if (fd < 0) | |
d08ebf99 | 256 | die_errno(_("could not create file '%s'"), path); |
bab8118a | 257 | |
eff80a9f | 258 | if (!is_null_sha1(prev)) { |
bab8118a | 259 | write_tag_body(fd, prev); |
eff80a9f JH |
260 | } else { |
261 | struct strbuf buf = STRBUF_INIT; | |
262 | strbuf_addch(&buf, '\n'); | |
263 | if (opt->cleanup_mode == CLEANUP_ALL) | |
d78f340e | 264 | strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char); |
eff80a9f | 265 | else |
d78f340e | 266 | strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char); |
eff80a9f JH |
267 | write_or_die(fd, buf.buf, buf.len); |
268 | strbuf_release(&buf); | |
269 | } | |
62e09ce9 CR |
270 | close(fd); |
271 | ||
7198203a SB |
272 | if (launch_editor(path, buf, NULL)) { |
273 | fprintf(stderr, | |
d08ebf99 | 274 | _("Please supply the message using either -m or -F option.\n")); |
7198203a SB |
275 | exit(1); |
276 | } | |
62e09ce9 | 277 | } |
62e09ce9 | 278 | |
d3e05983 | 279 | if (opt->cleanup_mode != CLEANUP_NONE) |
63af4a84 | 280 | strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); |
62e09ce9 | 281 | |
d3e05983 | 282 | if (!opt->message_given && !buf->len) |
d08ebf99 | 283 | die(_("no tag message?")); |
62e09ce9 | 284 | |
b0ceab98 JK |
285 | strbuf_insert(buf, 0, header.buf, header.len); |
286 | strbuf_release(&header); | |
62e09ce9 | 287 | |
d3e05983 | 288 | if (build_tag_object(buf, opt->sign, result) < 0) { |
3927bbe9 | 289 | if (path) |
d08ebf99 | 290 | fprintf(stderr, _("The tag message has been left in %s\n"), |
3927bbe9 JK |
291 | path); |
292 | exit(128); | |
293 | } | |
294 | if (path) { | |
691f1a28 | 295 | unlink_or_warn(path); |
3927bbe9 JK |
296 | free(path); |
297 | } | |
62e09ce9 CR |
298 | } |
299 | ||
df8512ed CW |
300 | static void create_reflog_msg(const unsigned char *sha1, struct strbuf *sb) |
301 | { | |
302 | enum object_type type; | |
303 | struct commit *c; | |
304 | char *buf; | |
305 | unsigned long size; | |
306 | int subject_len = 0; | |
307 | const char *subject_start; | |
308 | ||
309 | char *rla = getenv("GIT_REFLOG_ACTION"); | |
310 | if (rla) { | |
311 | strbuf_addstr(sb, rla); | |
312 | } else { | |
c3027be9 | 313 | strbuf_addstr(sb, "tag: tagging "); |
df8512ed CW |
314 | strbuf_add_unique_abbrev(sb, sha1, DEFAULT_ABBREV); |
315 | } | |
316 | ||
317 | strbuf_addstr(sb, " ("); | |
318 | type = sha1_object_info(sha1, NULL); | |
319 | switch (type) { | |
320 | default: | |
c3027be9 | 321 | strbuf_addstr(sb, "object of unknown type"); |
df8512ed CW |
322 | break; |
323 | case OBJ_COMMIT: | |
324 | if ((buf = read_sha1_file(sha1, &type, &size)) != NULL) { | |
325 | subject_len = find_commit_subject(buf, &subject_start); | |
326 | strbuf_insert(sb, sb->len, subject_start, subject_len); | |
327 | } else { | |
c3027be9 | 328 | strbuf_addstr(sb, "commit object"); |
df8512ed CW |
329 | } |
330 | free(buf); | |
331 | ||
332 | if ((c = lookup_commit_reference(sha1)) != NULL) | |
333 | strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT))); | |
334 | break; | |
335 | case OBJ_TREE: | |
c3027be9 | 336 | strbuf_addstr(sb, "tree object"); |
df8512ed CW |
337 | break; |
338 | case OBJ_BLOB: | |
c3027be9 | 339 | strbuf_addstr(sb, "blob object"); |
df8512ed CW |
340 | break; |
341 | case OBJ_TAG: | |
c3027be9 | 342 | strbuf_addstr(sb, "other tag object"); |
df8512ed CW |
343 | break; |
344 | } | |
345 | strbuf_addch(sb, ')'); | |
346 | } | |
347 | ||
bd46c9a9 JH |
348 | struct msg_arg { |
349 | int given; | |
350 | struct strbuf buf; | |
351 | }; | |
352 | ||
353 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) | |
354 | { | |
355 | struct msg_arg *msg = opt->value; | |
356 | ||
357 | if (!arg) | |
358 | return -1; | |
359 | if (msg->buf.len) | |
360 | strbuf_addstr(&(msg->buf), "\n\n"); | |
361 | strbuf_addstr(&(msg->buf), arg); | |
362 | msg->given = 1; | |
363 | return 0; | |
364 | } | |
365 | ||
4f0accd6 MS |
366 | static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) |
367 | { | |
368 | if (name[0] == '-') | |
8d9c5010 | 369 | return -1; |
4f0accd6 MS |
370 | |
371 | strbuf_reset(sb); | |
372 | strbuf_addf(sb, "refs/tags/%s", name); | |
373 | ||
8d9c5010 | 374 | return check_refname_format(sb->buf, 0); |
4f0accd6 MS |
375 | } |
376 | ||
62e09ce9 CR |
377 | int cmd_tag(int argc, const char **argv, const char *prefix) |
378 | { | |
f285a2d7 | 379 | struct strbuf buf = STRBUF_INIT; |
4f0accd6 | 380 | struct strbuf ref = STRBUF_INIT; |
df8512ed | 381 | struct strbuf reflog_msg = STRBUF_INIT; |
62e09ce9 | 382 | unsigned char object[20], prev[20]; |
62e09ce9 | 383 | const char *object_ref, *tag; |
d3e05983 KS |
384 | struct create_tag_options opt; |
385 | char *cleanup_arg = NULL; | |
144c76fa | 386 | int create_reflog = 0; |
ac4cc866 | 387 | int annotate = 0, force = 0; |
61c2fe0c | 388 | int cmdmode = 0, create_tag_object = 0; |
dbd0f5c7 | 389 | const char *msgfile = NULL, *keyid = NULL; |
bd46c9a9 | 390 | struct msg_arg msg = { 0, STRBUF_INIT }; |
e5074bfe RS |
391 | struct ref_transaction *transaction; |
392 | struct strbuf err = STRBUF_INIT; | |
ac4cc866 | 393 | struct ref_filter filter; |
b7cc53e9 | 394 | static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; |
df094741 | 395 | const char *format = NULL; |
3bb16a8b | 396 | int icase = 0; |
39686585 | 397 | struct option options[] = { |
e6b722db | 398 | OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'), |
ac4cc866 | 399 | { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"), |
c88bba18 | 400 | N_("print <n> lines of each tag message"), |
39686585 | 401 | PARSE_OPT_OPTARG, NULL, 1 }, |
e6b722db JH |
402 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'), |
403 | OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'), | |
39686585 | 404 | |
c88bba18 | 405 | OPT_GROUP(N_("Tag creation options")), |
d5d09d47 | 406 | OPT_BOOL('a', "annotate", &annotate, |
c88bba18 NTND |
407 | N_("annotated tag, needs a message")), |
408 | OPT_CALLBACK('m', "message", &msg, N_("message"), | |
409 | N_("tag message"), parse_msg_arg), | |
410 | OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), | |
d5d09d47 | 411 | OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), |
c88bba18 NTND |
412 | OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"), |
413 | N_("how to strip spaces and #comments from message")), | |
e703d711 | 414 | OPT_STRING('u', "local-user", &keyid, N_("key-id"), |
c88bba18 NTND |
415 | N_("use another key to sign the tag")), |
416 | OPT__FORCE(&force, N_("replace the tag if exists")), | |
98c32bd8 | 417 | OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")), |
dd059c6c JK |
418 | |
419 | OPT_GROUP(N_("Tag listing options")), | |
c88bba18 | 420 | OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")), |
ac4cc866 | 421 | OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")), |
ac3f5a34 | 422 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")), |
ac4cc866 | 423 | OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")), |
ac3f5a34 | 424 | OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")), |
5242860f KN |
425 | OPT_MERGED(&filter, N_("print only tags that are merged")), |
426 | OPT_NO_MERGED(&filter, N_("print only tags that are not merged")), | |
b7cc53e9 KN |
427 | OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"), |
428 | N_("field name to sort on"), &parse_opt_ref_sorting), | |
32c35cfb | 429 | { |
ac4cc866 | 430 | OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"), |
1e0c3b68 ÆAB |
431 | N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT, |
432 | parse_opt_object_name, (intptr_t) "HEAD" | |
ae7706b9 | 433 | }, |
df094741 | 434 | OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), |
3bb16a8b | 435 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
39686585 CR |
436 | OPT_END() |
437 | }; | |
438 | ||
56b43607 KN |
439 | setup_ref_filter_porcelain_msg(); |
440 | ||
b7cc53e9 | 441 | git_config(git_tag_config, sorting_tail); |
62e09ce9 | 442 | |
d3e05983 | 443 | memset(&opt, 0, sizeof(opt)); |
ac4cc866 KN |
444 | memset(&filter, 0, sizeof(filter)); |
445 | filter.lines = -1; | |
d3e05983 | 446 | |
37782920 | 447 | argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); |
62e09ce9 | 448 | |
be15f505 | 449 | if (keyid) { |
d3e05983 | 450 | opt.sign = 1; |
2f47eae2 | 451 | set_signing_key(keyid); |
be15f505 | 452 | } |
61c2fe0c LA |
453 | create_tag_object = (opt.sign || annotate || msg.given || msgfile); |
454 | ||
6a338149 ÆAB |
455 | if (!cmdmode) { |
456 | if (argc == 0) | |
457 | cmdmode = 'l'; | |
ac3f5a34 | 458 | else if (filter.with_commit || filter.no_commit || |
6a338149 ÆAB |
459 | filter.points_at.nr || filter.merge_commit || |
460 | filter.lines != -1) | |
461 | cmdmode = 'l'; | |
462 | } | |
c1a41b9d | 463 | |
61c2fe0c | 464 | if ((create_tag_object || force) && (cmdmode != 0)) |
6fa8342b ST |
465 | usage_with_options(git_tag_usage, options); |
466 | ||
d96e3c15 | 467 | finalize_colopts(&colopts, -1); |
ac4cc866 | 468 | if (cmdmode == 'l' && filter.lines != -1) { |
d96e3c15 NTND |
469 | if (explicitly_enable_column(colopts)) |
470 | die(_("--column and -n are incompatible")); | |
471 | colopts = 0; | |
472 | } | |
b7cc53e9 KN |
473 | if (!sorting) |
474 | sorting = ref_default_sorting(); | |
3bb16a8b NTND |
475 | sorting->ignore_case = icase; |
476 | filter.ignore_case = icase; | |
e6b722db | 477 | if (cmdmode == 'l') { |
d96e3c15 NTND |
478 | int ret; |
479 | if (column_active(colopts)) { | |
480 | struct column_options copts; | |
481 | memset(&copts, 0, sizeof(copts)); | |
482 | copts.padding = 2; | |
483 | run_column_filter(colopts, &copts); | |
484 | } | |
ac4cc866 | 485 | filter.name_patterns = argv; |
df094741 | 486 | ret = list_tags(&filter, sorting, format); |
d96e3c15 NTND |
487 | if (column_active(colopts)) |
488 | stop_column_filter(); | |
489 | return ret; | |
490 | } | |
ac4cc866 | 491 | if (filter.lines != -1) |
6a338149 | 492 | die(_("-n option is only allowed in list mode")); |
ac4cc866 | 493 | if (filter.with_commit) |
6a338149 | 494 | die(_("--contains option is only allowed in list mode")); |
ac3f5a34 ÆAB |
495 | if (filter.no_commit) |
496 | die(_("--no-contains option is only allowed in list mode")); | |
ac4cc866 | 497 | if (filter.points_at.nr) |
6a338149 | 498 | die(_("--points-at option is only allowed in list mode")); |
5242860f | 499 | if (filter.merge_commit) |
6a338149 | 500 | die(_("--merged and --no-merged options are only allowed in list mode")); |
e6b722db | 501 | if (cmdmode == 'd') |
07d347cf LP |
502 | return for_each_tag_name(argv, delete_tag, NULL); |
503 | if (cmdmode == 'v') { | |
504 | if (format) | |
505 | verify_ref_format(format); | |
506 | return for_each_tag_name(argv, verify_tag, format); | |
507 | } | |
39686585 | 508 | |
bd46c9a9 JH |
509 | if (msg.given || msgfile) { |
510 | if (msg.given && msgfile) | |
d08ebf99 | 511 | die(_("only one -F or -m option is allowed.")); |
bd46c9a9 JH |
512 | if (msg.given) |
513 | strbuf_addbuf(&buf, &(msg.buf)); | |
39686585 CR |
514 | else { |
515 | if (!strcmp(msgfile, "-")) { | |
387e7e19 | 516 | if (strbuf_read(&buf, 0, 1024) < 0) |
d08ebf99 | 517 | die_errno(_("cannot read '%s'"), msgfile); |
387e7e19 | 518 | } else { |
39686585 | 519 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
d08ebf99 | 520 | die_errno(_("could not open or read '%s'"), |
d824cbba | 521 | msgfile); |
62e09ce9 | 522 | } |
62e09ce9 | 523 | } |
62e09ce9 CR |
524 | } |
525 | ||
39686585 | 526 | tag = argv[0]; |
62e09ce9 | 527 | |
39686585 CR |
528 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
529 | if (argc > 2) | |
d08ebf99 | 530 | die(_("too many params")); |
62e09ce9 CR |
531 | |
532 | if (get_sha1(object_ref, object)) | |
d08ebf99 | 533 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
62e09ce9 | 534 | |
4f0accd6 | 535 | if (strbuf_check_tag_ref(&ref, tag)) |
d08ebf99 | 536 | die(_("'%s' is not a valid tag name."), tag); |
62e09ce9 | 537 | |
c6893323 | 538 | if (read_ref(ref.buf, prev)) |
62e09ce9 CR |
539 | hashclr(prev); |
540 | else if (!force) | |
d08ebf99 | 541 | die(_("tag '%s' already exists"), tag); |
62e09ce9 | 542 | |
d3e05983 KS |
543 | opt.message_given = msg.given || msgfile; |
544 | ||
545 | if (!cleanup_arg || !strcmp(cleanup_arg, "strip")) | |
546 | opt.cleanup_mode = CLEANUP_ALL; | |
547 | else if (!strcmp(cleanup_arg, "verbatim")) | |
548 | opt.cleanup_mode = CLEANUP_NONE; | |
549 | else if (!strcmp(cleanup_arg, "whitespace")) | |
550 | opt.cleanup_mode = CLEANUP_SPACE; | |
551 | else | |
552 | die(_("Invalid cleanup mode %s"), cleanup_arg); | |
553 | ||
df8512ed CW |
554 | create_reflog_msg(object, &reflog_msg); |
555 | ||
61c2fe0c LA |
556 | if (create_tag_object) { |
557 | if (force_sign_annotate && !annotate) | |
558 | opt.sign = 1; | |
d3e05983 | 559 | create_tag(object, tag, &buf, &opt, prev, object); |
61c2fe0c | 560 | } |
62e09ce9 | 561 | |
e5074bfe RS |
562 | transaction = ref_transaction_begin(&err); |
563 | if (!transaction || | |
564 | ref_transaction_update(transaction, ref.buf, object, prev, | |
144c76fa | 565 | create_reflog ? REF_FORCE_CREATE_REFLOG : 0, |
df8512ed | 566 | reflog_msg.buf, &err) || |
db7516ab | 567 | ref_transaction_commit(transaction, &err)) |
e5074bfe RS |
568 | die("%s", err.buf); |
569 | ref_transaction_free(transaction); | |
3ae851e6 | 570 | if (force && !is_null_sha1(prev) && hashcmp(prev, object)) |
d08ebf99 | 571 | printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV)); |
62e09ce9 | 572 | |
e5074bfe | 573 | strbuf_release(&err); |
fd17f5b5 | 574 | strbuf_release(&buf); |
4f0accd6 | 575 | strbuf_release(&ref); |
df8512ed | 576 | strbuf_release(&reflog_msg); |
62e09ce9 CR |
577 | return 0; |
578 | } |