tag.c 16 KB

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