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