| 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 | #define USE_THE_REPOSITORY_VARIABLE |
| 10 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 11 | |
| 12 | #include "builtin.h" |
| 13 | #include "advice.h" |
| 14 | #include "config.h" |
| 15 | #include "editor.h" |
| 16 | #include "environment.h" |
| 17 | #include "gettext.h" |
| 18 | #include "hex.h" |
| 19 | #include "refs.h" |
| 20 | #include "object-file.h" |
| 21 | #include "object-name.h" |
| 22 | #include "odb.h" |
| 23 | #include "path.h" |
| 24 | #include "tag.h" |
| 25 | #include "parse-options.h" |
| 26 | #include "diff.h" |
| 27 | #include "revision.h" |
| 28 | #include "gpg-interface.h" |
| 29 | #include "oid-array.h" |
| 30 | #include "column.h" |
| 31 | #include "ref-filter.h" |
| 32 | #include "date.h" |
| 33 | #include "write-or-die.h" |
| 34 | #include "object-file-convert.h" |
| 35 | #include "trailer.h" |
| 36 | |
| 37 | static const char * const git_tag_usage[] = { |
| 38 | N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n" |
| 39 | " [(--trailer <token>[(=|:)<value>])...]\n" |
| 40 | " <tagname> [<commit> | <object>]"), |
| 41 | N_("git tag -d <tagname>..."), |
| 42 | N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n" |
| 43 | " [--points-at <object>] [--column[=<options>] | --no-column]\n" |
| 44 | " [--create-reflog] [--sort=<key>] [--format=<format>]\n" |
| 45 | " [--merged <commit>] [--no-merged <commit>] [<pattern>...]"), |
| 46 | N_("git tag -v [--format=<format>] <tagname>..."), |
| 47 | NULL |
| 48 | }; |
| 49 | |
| 50 | static unsigned int colopts; |
| 51 | static int force_sign_annotate; |
| 52 | static int config_sign_tag = -1; /* unspecified */ |
| 53 | |
| 54 | static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, |
| 55 | struct ref_format *format) |
| 56 | { |
| 57 | char *to_free = NULL; |
| 58 | |
| 59 | if (filter->lines == -1) |
| 60 | filter->lines = 0; |
| 61 | |
| 62 | if (!format->format) { |
| 63 | if (filter->lines) { |
| 64 | to_free = xstrfmt("%s %%(contents:lines=%d)", |
| 65 | "%(align:15)%(refname:lstrip=2)%(end)", |
| 66 | filter->lines); |
| 67 | format->format = to_free; |
| 68 | } else |
| 69 | format->format = "%(refname:lstrip=2)"; |
| 70 | } |
| 71 | |
| 72 | if (verify_ref_format(format)) |
| 73 | die(_("unable to parse format string")); |
| 74 | filter->with_commit_tag_algo = 1; |
| 75 | filter_and_format_refs(filter, FILTER_REFS_TAGS, sorting, format); |
| 76 | |
| 77 | free(to_free); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | typedef int (*each_tag_name_fn)(const char *name, const char *ref, |
| 83 | const struct object_id *oid, void *cb_data); |
| 84 | |
| 85 | static int for_each_tag_name(const char **argv, each_tag_name_fn fn, |
| 86 | void *cb_data) |
| 87 | { |
| 88 | const char **p; |
| 89 | struct strbuf ref = STRBUF_INIT; |
| 90 | int had_error = 0; |
| 91 | struct object_id oid; |
| 92 | |
| 93 | for (p = argv; *p; p++) { |
| 94 | strbuf_reset(&ref); |
| 95 | strbuf_addf(&ref, "refs/tags/%s", *p); |
| 96 | if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) { |
| 97 | error(_("tag '%s' not found."), *p); |
| 98 | had_error = 1; |
| 99 | continue; |
| 100 | } |
| 101 | if (fn(*p, ref.buf, &oid, cb_data)) |
| 102 | had_error = 1; |
| 103 | } |
| 104 | strbuf_release(&ref); |
| 105 | return had_error; |
| 106 | } |
| 107 | |
| 108 | static int collect_tags(const char *name UNUSED, const char *ref, |
| 109 | const struct object_id *oid, void *cb_data) |
| 110 | { |
| 111 | struct string_list *ref_list = cb_data; |
| 112 | |
| 113 | string_list_append(ref_list, ref); |
| 114 | ref_list->items[ref_list->nr - 1].util = oiddup(oid); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int delete_tags(const char **argv) |
| 119 | { |
| 120 | int result; |
| 121 | struct string_list refs_to_delete = STRING_LIST_INIT_DUP; |
| 122 | struct string_list_item *item; |
| 123 | |
| 124 | result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete); |
| 125 | if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF)) |
| 126 | result = 1; |
| 127 | |
| 128 | for_each_string_list_item(item, &refs_to_delete) { |
| 129 | const char *name = item->string; |
| 130 | struct object_id *oid = item->util; |
| 131 | if (!refs_ref_exists(get_main_ref_store(the_repository), name)) |
| 132 | printf(_("Deleted tag '%s' (was %s)\n"), |
| 133 | item->string + 10, |
| 134 | repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV)); |
| 135 | |
| 136 | free(oid); |
| 137 | } |
| 138 | string_list_clear(&refs_to_delete, 0); |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | static int verify_tag(const char *name, const char *ref UNUSED, |
| 143 | const struct object_id *oid, void *cb_data) |
| 144 | { |
| 145 | int flags; |
| 146 | struct ref_format *format = cb_data; |
| 147 | flags = GPG_VERIFY_VERBOSE; |
| 148 | |
| 149 | if (format->format) |
| 150 | flags = GPG_VERIFY_OMIT_STATUS; |
| 151 | |
| 152 | if (gpg_verify_tag(the_repository, oid, name, flags)) |
| 153 | return -1; |
| 154 | |
| 155 | if (format->format) |
| 156 | pretty_print_ref(name, oid, NULL, format); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int do_sign(struct strbuf *buffer, struct object_id **compat_oid, |
| 162 | struct object_id *compat_oid_buf) |
| 163 | { |
| 164 | const struct git_hash_algo *compat = the_repository->compat_hash_algo; |
| 165 | struct strbuf sig = STRBUF_INIT, compat_sig = STRBUF_INIT; |
| 166 | struct strbuf compat_buf = STRBUF_INIT; |
| 167 | char *keyid = get_signing_key(); |
| 168 | int ret = -1; |
| 169 | |
| 170 | if (sign_buffer(buffer, &sig, keyid, 0)) |
| 171 | goto out; |
| 172 | |
| 173 | if (compat) { |
| 174 | const struct git_hash_algo *algo = the_repository->hash_algo; |
| 175 | |
| 176 | if (convert_object_file(the_repository ,&compat_buf, algo, compat, |
| 177 | buffer->buf, buffer->len, OBJ_TAG, 1)) |
| 178 | goto out; |
| 179 | if (sign_buffer(&compat_buf, &compat_sig, keyid, 0)) |
| 180 | goto out; |
| 181 | add_header_signature(&compat_buf, &sig, algo); |
| 182 | strbuf_addbuf(&compat_buf, &compat_sig); |
| 183 | hash_object_file(compat, compat_buf.buf, compat_buf.len, |
| 184 | OBJ_TAG, compat_oid_buf); |
| 185 | *compat_oid = compat_oid_buf; |
| 186 | } |
| 187 | |
| 188 | if (compat_sig.len) |
| 189 | add_header_signature(buffer, &compat_sig, compat); |
| 190 | |
| 191 | strbuf_addbuf(buffer, &sig); |
| 192 | ret = 0; |
| 193 | out: |
| 194 | strbuf_release(&sig); |
| 195 | strbuf_release(&compat_sig); |
| 196 | strbuf_release(&compat_buf); |
| 197 | free(keyid); |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static const char tag_template[] = |
| 202 | N_("\nWrite a message for tag:\n %s\n" |
| 203 | "Lines starting with '%s' will be ignored.\n"); |
| 204 | |
| 205 | static const char tag_template_nocleanup[] = |
| 206 | N_("\nWrite a message for tag:\n %s\n" |
| 207 | "Lines starting with '%s' will be kept; you may remove them" |
| 208 | " yourself if you want to.\n"); |
| 209 | |
| 210 | static int git_tag_config(const char *var, const char *value, |
| 211 | const struct config_context *ctx, void *cb) |
| 212 | { |
| 213 | if (!strcmp(var, "tag.gpgsign")) { |
| 214 | config_sign_tag = git_config_bool(var, value); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | if (!strcmp(var, "tag.sort")) { |
| 219 | if (!value) |
| 220 | return config_error_nonbool(var); |
| 221 | string_list_append(cb, value); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | if (!strcmp(var, "tag.forcesignannotated")) { |
| 226 | force_sign_annotate = git_config_bool(var, value); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | if (starts_with(var, "column.")) |
| 231 | return git_column_config(var, value, "tag", &colopts); |
| 232 | |
| 233 | if (git_color_config(var, value, cb) < 0) |
| 234 | return -1; |
| 235 | |
| 236 | return git_default_config(var, value, ctx, cb); |
| 237 | } |
| 238 | |
| 239 | static void write_tag_body(int fd, const struct object_id *oid) |
| 240 | { |
| 241 | size_t size; |
| 242 | enum object_type type; |
| 243 | char *buf, *sp, *orig; |
| 244 | struct strbuf payload = STRBUF_INIT; |
| 245 | struct strbuf signature = STRBUF_INIT; |
| 246 | |
| 247 | orig = buf = odb_read_object(the_repository->objects, oid, &type, &size); |
| 248 | if (!buf) |
| 249 | return; |
| 250 | if (parse_signature(buf, size, &payload, &signature)) { |
| 251 | buf = payload.buf; |
| 252 | size = payload.len; |
| 253 | } |
| 254 | /* skip header */ |
| 255 | sp = strstr(buf, "\n\n"); |
| 256 | |
| 257 | if (!sp || !size || type != OBJ_TAG) { |
| 258 | free(buf); |
| 259 | return; |
| 260 | } |
| 261 | sp += 2; /* skip the 2 LFs */ |
| 262 | write_or_die(fd, sp, buf + size - sp); |
| 263 | |
| 264 | free(orig); |
| 265 | strbuf_release(&payload); |
| 266 | strbuf_release(&signature); |
| 267 | } |
| 268 | |
| 269 | static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result) |
| 270 | { |
| 271 | struct object_id *compat_oid = NULL, compat_oid_buf; |
| 272 | if (sign && do_sign(buf, &compat_oid, &compat_oid_buf) < 0) |
| 273 | return error(_("unable to sign the tag")); |
| 274 | if (odb_write_object_ext(the_repository->objects, buf->buf, |
| 275 | buf->len, OBJ_TAG, result, compat_oid, 0) < 0) |
| 276 | return error(_("unable to write tag file")); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | struct create_tag_options { |
| 281 | unsigned int message_given:1; |
| 282 | unsigned int use_editor:1; |
| 283 | unsigned int sign; |
| 284 | enum { |
| 285 | CLEANUP_NONE, |
| 286 | CLEANUP_SPACE, |
| 287 | CLEANUP_ALL |
| 288 | } cleanup_mode; |
| 289 | }; |
| 290 | |
| 291 | static const char message_advice_nested_tag[] = |
| 292 | N_("You have created a nested tag. The object referred to by your new tag is\n" |
| 293 | "already a tag. If you meant to tag the object that it points to, use:\n" |
| 294 | "\n" |
| 295 | "\tgit tag -f %s %s^{}"); |
| 296 | |
| 297 | static void create_tag(const struct object_id *object, const char *object_ref, |
| 298 | const char *tag, |
| 299 | struct strbuf *buf, struct create_tag_options *opt, |
| 300 | struct object_id *prev, struct object_id *result, |
| 301 | struct strvec *trailer_args, char *path) |
| 302 | { |
| 303 | enum object_type type; |
| 304 | struct strbuf header = STRBUF_INIT; |
| 305 | int should_edit; |
| 306 | |
| 307 | type = odb_read_object_info(the_repository->objects, object, NULL); |
| 308 | if (type <= OBJ_NONE) |
| 309 | die(_("bad object type.")); |
| 310 | |
| 311 | if (type == OBJ_TAG) |
| 312 | advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag), |
| 313 | tag, object_ref); |
| 314 | |
| 315 | strbuf_addf(&header, |
| 316 | "object %s\n" |
| 317 | "type %s\n" |
| 318 | "tag %s\n" |
| 319 | "tagger %s\n\n", |
| 320 | oid_to_hex(object), |
| 321 | type_name(type), |
| 322 | tag, |
| 323 | git_committer_info(IDENT_STRICT)); |
| 324 | |
| 325 | should_edit = opt->use_editor || !opt->message_given; |
| 326 | if (should_edit || trailer_args->nr) { |
| 327 | int fd; |
| 328 | |
| 329 | /* write the template message before editing: */ |
| 330 | fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 331 | |
| 332 | if (opt->message_given && buf->len) { |
| 333 | strbuf_complete(buf, '\n'); |
| 334 | write_or_die(fd, buf->buf, buf->len); |
| 335 | strbuf_reset(buf); |
| 336 | } else if (!is_null_oid(prev)) { |
| 337 | write_tag_body(fd, prev); |
| 338 | } else { |
| 339 | struct strbuf buf = STRBUF_INIT; |
| 340 | strbuf_addch(&buf, '\n'); |
| 341 | if (opt->cleanup_mode == CLEANUP_ALL) |
| 342 | strbuf_commented_addf(&buf, comment_line_str, |
| 343 | _(tag_template), tag, comment_line_str); |
| 344 | else |
| 345 | strbuf_commented_addf(&buf, comment_line_str, |
| 346 | _(tag_template_nocleanup), tag, comment_line_str); |
| 347 | write_or_die(fd, buf.buf, buf.len); |
| 348 | strbuf_release(&buf); |
| 349 | } |
| 350 | close(fd); |
| 351 | |
| 352 | if (trailer_args->nr && amend_file_with_trailers(path, trailer_args)) |
| 353 | die(_("unable to pass trailers to --trailers")); |
| 354 | |
| 355 | if (should_edit) { |
| 356 | if (launch_editor(path, buf, NULL)) { |
| 357 | fprintf(stderr, |
| 358 | _("Please supply the message using either -m or -F option.\n")); |
| 359 | exit(1); |
| 360 | } |
| 361 | } else if (trailer_args->nr) { |
| 362 | strbuf_reset(buf); |
| 363 | if (strbuf_read_file(buf, path, 0) < 0) |
| 364 | die_errno(_("failed to read '%s'"), path); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (opt->cleanup_mode != CLEANUP_NONE) |
| 369 | strbuf_stripspace(buf, |
| 370 | opt->cleanup_mode == CLEANUP_ALL ? comment_line_str : NULL); |
| 371 | |
| 372 | if (!opt->message_given && !buf->len) |
| 373 | die(_("no tag message?")); |
| 374 | |
| 375 | strbuf_insert(buf, 0, header.buf, header.len); |
| 376 | strbuf_release(&header); |
| 377 | |
| 378 | if (build_tag_object(buf, opt->sign, result) < 0) { |
| 379 | if (path) |
| 380 | fprintf(stderr, _("The tag message has been left in %s\n"), |
| 381 | path); |
| 382 | exit(128); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb) |
| 387 | { |
| 388 | enum object_type type; |
| 389 | struct commit *c; |
| 390 | char *buf; |
| 391 | size_t size; |
| 392 | int subject_len = 0; |
| 393 | const char *subject_start; |
| 394 | |
| 395 | char *rla = getenv("GIT_REFLOG_ACTION"); |
| 396 | if (rla) { |
| 397 | strbuf_addstr(sb, rla); |
| 398 | } else { |
| 399 | strbuf_addstr(sb, "tag: tagging "); |
| 400 | strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV); |
| 401 | } |
| 402 | |
| 403 | strbuf_addstr(sb, " ("); |
| 404 | type = odb_read_object_info(the_repository->objects, oid, NULL); |
| 405 | switch (type) { |
| 406 | default: |
| 407 | strbuf_addstr(sb, "object of unknown type"); |
| 408 | break; |
| 409 | case OBJ_COMMIT: |
| 410 | if ((buf = odb_read_object(the_repository->objects, oid, &type, &size))) { |
| 411 | subject_len = find_commit_subject(buf, &subject_start); |
| 412 | strbuf_insert(sb, sb->len, subject_start, subject_len); |
| 413 | } else { |
| 414 | strbuf_addstr(sb, "commit object"); |
| 415 | } |
| 416 | free(buf); |
| 417 | |
| 418 | if ((c = lookup_commit_reference(the_repository, oid))) |
| 419 | strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT))); |
| 420 | break; |
| 421 | case OBJ_TREE: |
| 422 | strbuf_addstr(sb, "tree object"); |
| 423 | break; |
| 424 | case OBJ_BLOB: |
| 425 | strbuf_addstr(sb, "blob object"); |
| 426 | break; |
| 427 | case OBJ_TAG: |
| 428 | strbuf_addstr(sb, "other tag object"); |
| 429 | break; |
| 430 | } |
| 431 | strbuf_addch(sb, ')'); |
| 432 | } |
| 433 | |
| 434 | struct msg_arg { |
| 435 | int given; |
| 436 | struct strbuf buf; |
| 437 | }; |
| 438 | |
| 439 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) |
| 440 | { |
| 441 | struct msg_arg *msg = opt->value; |
| 442 | |
| 443 | BUG_ON_OPT_NEG(unset); |
| 444 | |
| 445 | if (!arg) |
| 446 | return -1; |
| 447 | if (msg->buf.len) |
| 448 | strbuf_addstr(&(msg->buf), "\n\n"); |
| 449 | strbuf_addstr(&(msg->buf), arg); |
| 450 | msg->given = 1; |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int cmd_tag(int argc, |
| 455 | const char **argv, |
| 456 | const char *prefix, |
| 457 | struct repository *repo UNUSED) |
| 458 | { |
| 459 | struct strbuf buf = STRBUF_INIT; |
| 460 | struct strbuf ref = STRBUF_INIT; |
| 461 | struct strbuf reflog_msg = STRBUF_INIT; |
| 462 | struct object_id object, prev; |
| 463 | const char *object_ref, *tag; |
| 464 | struct create_tag_options opt; |
| 465 | char *cleanup_arg = NULL; |
| 466 | int create_reflog = 0; |
| 467 | int annotate = 0, force = 0; |
| 468 | int cmdmode = 0, create_tag_object = 0; |
| 469 | char *msgfile = NULL; |
| 470 | const char *keyid = NULL; |
| 471 | struct msg_arg msg = { .buf = STRBUF_INIT }; |
| 472 | struct ref_transaction *transaction; |
| 473 | struct strbuf err = STRBUF_INIT; |
| 474 | struct ref_filter filter = REF_FILTER_INIT; |
| 475 | struct ref_sorting *sorting; |
| 476 | struct string_list sorting_options = STRING_LIST_INIT_DUP; |
| 477 | struct ref_format format = REF_FORMAT_INIT; |
| 478 | struct strvec trailer_args = STRVEC_INIT; |
| 479 | int icase = 0; |
| 480 | int edit_flag = 0; |
| 481 | struct option options[] = { |
| 482 | OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'), |
| 483 | { |
| 484 | .type = OPTION_INTEGER, |
| 485 | .short_name = 'n', |
| 486 | .value = &filter.lines, |
| 487 | .precision = sizeof(filter.lines), |
| 488 | .argh = N_("n"), |
| 489 | .help = N_("print <n> lines of each tag message"), |
| 490 | .flags = PARSE_OPT_OPTARG, |
| 491 | .defval = 1, |
| 492 | }, |
| 493 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'), |
| 494 | OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'), |
| 495 | |
| 496 | OPT_GROUP(N_("Tag creation options")), |
| 497 | OPT_BOOL('a', "annotate", &annotate, |
| 498 | N_("annotated tag, needs a message")), |
| 499 | OPT_CALLBACK_F('m', "message", &msg, N_("message"), |
| 500 | N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg), |
| 501 | OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), |
| 502 | OPT_STRVEC(0, "trailer", &trailer_args, N_("trailer"), |
| 503 | N_("add custom trailer(s)")), |
| 504 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")), |
| 505 | OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), |
| 506 | OPT_CLEANUP(&cleanup_arg), |
| 507 | OPT_STRING('u', "local-user", &keyid, N_("key-id"), |
| 508 | N_("use another key to sign the tag")), |
| 509 | OPT__FORCE(&force, N_("replace the tag if exists"), 0), |
| 510 | OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")), |
| 511 | |
| 512 | OPT_GROUP(N_("Tag listing options")), |
| 513 | OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")), |
| 514 | OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")), |
| 515 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")), |
| 516 | OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")), |
| 517 | OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")), |
| 518 | OPT_MERGED(&filter, N_("print only tags that are merged")), |
| 519 | OPT_NO_MERGED(&filter, N_("print only tags that are not merged")), |
| 520 | OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty, |
| 521 | N_("do not output a newline after empty formatted refs")), |
| 522 | OPT_REF_SORT(&sorting_options), |
| 523 | { |
| 524 | .type = OPTION_CALLBACK, |
| 525 | .long_name = "points-at", |
| 526 | .value = &filter.points_at, |
| 527 | .argh = N_("object"), |
| 528 | .help = N_("print only tags of the object"), |
| 529 | .flags = PARSE_OPT_LASTARG_DEFAULT, |
| 530 | .callback = parse_opt_object_name, |
| 531 | .defval = (intptr_t) "HEAD", |
| 532 | }, |
| 533 | OPT_STRING( 0 , "format", &format.format, N_("format"), |
| 534 | N_("format to use for the output")), |
| 535 | OPT__COLOR(&format.use_color, N_("respect format colors")), |
| 536 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
| 537 | OPT_END() |
| 538 | }; |
| 539 | int ret = 0; |
| 540 | const char *only_in_list = NULL; |
| 541 | char *path = NULL; |
| 542 | |
| 543 | setup_ref_filter_porcelain_msg(); |
| 544 | |
| 545 | /* |
| 546 | * Try to set sort keys from config. If config does not set any, |
| 547 | * fall back on default (refname) sorting. |
| 548 | */ |
| 549 | repo_config(the_repository, git_tag_config, &sorting_options); |
| 550 | if (!sorting_options.nr) |
| 551 | string_list_append(&sorting_options, "refname"); |
| 552 | |
| 553 | memset(&opt, 0, sizeof(opt)); |
| 554 | filter.lines = -1; |
| 555 | opt.sign = -1; |
| 556 | |
| 557 | argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); |
| 558 | |
| 559 | if (!cmdmode) { |
| 560 | if (argc == 0) |
| 561 | cmdmode = 'l'; |
| 562 | else if (filter.with_commit || filter.no_commit || |
| 563 | filter.reachable_from || filter.unreachable_from || |
| 564 | filter.points_at.nr || filter.lines != -1) |
| 565 | cmdmode = 'l'; |
| 566 | } |
| 567 | |
| 568 | if (cmdmode == 'l') |
| 569 | setup_auto_pager("tag", 1); |
| 570 | |
| 571 | if (trailer_args.nr) |
| 572 | trailer_config_init(); |
| 573 | |
| 574 | if (opt.sign == -1) |
| 575 | opt.sign = cmdmode ? 0 : config_sign_tag > 0; |
| 576 | |
| 577 | if (keyid) { |
| 578 | opt.sign = 1; |
| 579 | set_signing_key(keyid); |
| 580 | } |
| 581 | create_tag_object = (opt.sign || annotate || msg.given || msgfile || |
| 582 | edit_flag || trailer_args.nr); |
| 583 | |
| 584 | if ((create_tag_object || force) && (cmdmode != 0)) |
| 585 | usage_with_options(git_tag_usage, options); |
| 586 | |
| 587 | finalize_colopts(&colopts, -1); |
| 588 | if (cmdmode == 'l' && filter.lines != -1) { |
| 589 | if (explicitly_enable_column(colopts)) |
| 590 | die(_("options '%s' and '%s' cannot be used together"), "--column", "-n"); |
| 591 | colopts = 0; |
| 592 | } |
| 593 | sorting = ref_sorting_options(&sorting_options); |
| 594 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
| 595 | filter.ignore_case = icase; |
| 596 | if (cmdmode == 'l') { |
| 597 | if (column_active(colopts)) { |
| 598 | struct column_options copts; |
| 599 | memset(&copts, 0, sizeof(copts)); |
| 600 | copts.padding = 2; |
| 601 | if (run_column_filter(colopts, &copts)) |
| 602 | die(_("could not start 'git column'")); |
| 603 | } |
| 604 | filter.name_patterns = argv; |
| 605 | ret = list_tags(&filter, sorting, &format); |
| 606 | if (column_active(colopts)) |
| 607 | stop_column_filter(); |
| 608 | goto cleanup; |
| 609 | } |
| 610 | if (filter.lines != -1) |
| 611 | only_in_list = "-n"; |
| 612 | else if (filter.with_commit) |
| 613 | only_in_list = "--contains"; |
| 614 | else if (filter.no_commit) |
| 615 | only_in_list = "--no-contains"; |
| 616 | else if (filter.points_at.nr) |
| 617 | only_in_list = "--points-at"; |
| 618 | else if (filter.reachable_from) |
| 619 | only_in_list = "--merged"; |
| 620 | else if (filter.unreachable_from) |
| 621 | only_in_list = "--no-merged"; |
| 622 | if (only_in_list) |
| 623 | die(_("the '%s' option is only allowed in list mode"), only_in_list); |
| 624 | if (cmdmode == 'd') { |
| 625 | ret = delete_tags(argv); |
| 626 | goto cleanup; |
| 627 | } |
| 628 | if (cmdmode == 'v') { |
| 629 | if (format.format && verify_ref_format(&format)) |
| 630 | usage_with_options(git_tag_usage, options); |
| 631 | ret = for_each_tag_name(argv, verify_tag, &format); |
| 632 | goto cleanup; |
| 633 | } |
| 634 | |
| 635 | if (msg.given || msgfile) { |
| 636 | if (msg.given && msgfile) |
| 637 | die(_("options '%s' and '%s' cannot be used together"), "-F", "-m"); |
| 638 | if (msg.given) |
| 639 | strbuf_addbuf(&buf, &(msg.buf)); |
| 640 | else { |
| 641 | if (!strcmp(msgfile, "-")) { |
| 642 | if (strbuf_read(&buf, 0, 1024) < 0) |
| 643 | die_errno(_("cannot read '%s'"), msgfile); |
| 644 | } else { |
| 645 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
| 646 | die_errno(_("could not open or read '%s'"), |
| 647 | msgfile); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | tag = argv[0]; |
| 653 | |
| 654 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
| 655 | if (argc > 2) |
| 656 | die(_("too many arguments")); |
| 657 | |
| 658 | if (repo_get_oid(the_repository, object_ref, &object)) |
| 659 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
| 660 | |
| 661 | if (check_tag_ref(&ref, tag)) |
| 662 | die(_("'%s' is not a valid tag name."), tag); |
| 663 | |
| 664 | if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev)) |
| 665 | oidclr(&prev, the_repository->hash_algo); |
| 666 | else if (!force) |
| 667 | die(_("tag '%s' already exists"), tag); |
| 668 | |
| 669 | opt.message_given = msg.given || msgfile; |
| 670 | opt.use_editor = edit_flag; |
| 671 | |
| 672 | if (!cleanup_arg || !strcmp(cleanup_arg, "strip")) |
| 673 | opt.cleanup_mode = CLEANUP_ALL; |
| 674 | else if (!strcmp(cleanup_arg, "verbatim")) |
| 675 | opt.cleanup_mode = CLEANUP_NONE; |
| 676 | else if (!strcmp(cleanup_arg, "whitespace")) |
| 677 | opt.cleanup_mode = CLEANUP_SPACE; |
| 678 | else |
| 679 | die(_("Invalid cleanup mode %s"), cleanup_arg); |
| 680 | |
| 681 | create_reflog_msg(&object, &reflog_msg); |
| 682 | |
| 683 | if (create_tag_object) { |
| 684 | if (force_sign_annotate && !annotate) |
| 685 | opt.sign = 1; |
| 686 | path = repo_git_path(the_repository, "TAG_EDITMSG"); |
| 687 | create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object, |
| 688 | &trailer_args, path); |
| 689 | } |
| 690 | |
| 691 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
| 692 | 0, &err); |
| 693 | if (!transaction || |
| 694 | ref_transaction_update(transaction, ref.buf, &object, &prev, |
| 695 | NULL, NULL, |
| 696 | create_reflog ? REF_FORCE_CREATE_REFLOG : 0, |
| 697 | reflog_msg.buf, &err) || |
| 698 | ref_transaction_commit(transaction, &err)) { |
| 699 | if (path) |
| 700 | fprintf(stderr, |
| 701 | _("The tag message has been left in %s\n"), |
| 702 | path); |
| 703 | die("%s", err.buf); |
| 704 | } |
| 705 | if (path) { |
| 706 | unlink_or_warn(path); |
| 707 | free(path); |
| 708 | } |
| 709 | ref_transaction_free(transaction); |
| 710 | if (force && !is_null_oid(&prev) && !oideq(&prev, &object)) |
| 711 | printf(_("Updated tag '%s' (was %s)\n"), tag, |
| 712 | repo_find_unique_abbrev(the_repository, &prev, DEFAULT_ABBREV)); |
| 713 | |
| 714 | cleanup: |
| 715 | ref_sorting_release(sorting); |
| 716 | ref_filter_clear(&filter); |
| 717 | strbuf_release(&buf); |
| 718 | strbuf_release(&ref); |
| 719 | strbuf_release(&reflog_msg); |
| 720 | strbuf_release(&msg.buf); |
| 721 | strbuf_release(&err); |
| 722 | strvec_clear(&trailer_args); |
| 723 | free(msgfile); |
| 724 | return ret; |
| 725 | } |