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.
13 #include "run-command.h"
15 static const char builtin_tag_usage
[] =
16 "git-tag [-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg> | -F <file>] <tagname> [<head>]";
18 static char signingkey
[1000];
20 void launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
22 const char *editor
, *terminal
;
24 editor
= getenv("GIT_EDITOR");
25 if (!editor
&& editor_program
)
26 editor
= editor_program
;
28 editor
= getenv("VISUAL");
30 editor
= getenv("EDITOR");
32 terminal
= getenv("TERM");
33 if (!editor
&& (!terminal
|| !strcmp(terminal
, "dumb"))) {
35 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
36 "Please supply the message using either -m or -F option.\n");
43 if (strcmp(editor
, ":")) {
44 const char *args
[] = { editor
, path
, NULL
};
46 if (run_command_v_opt_cd_env(args
, 0, NULL
, env
))
47 die("There was a problem with the editor %s.", editor
);
50 if (strbuf_read_file(buffer
, path
, 0) < 0)
51 die("could not read message file '%s': %s",
52 path
, strerror(errno
));
60 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
62 static int show_reference(const char *refname
, const unsigned char *sha1
,
63 int flag
, void *cb_data
)
65 struct tag_filter
*filter
= cb_data
;
67 if (!fnmatch(filter
->pattern
, refname
, 0)) {
70 enum object_type type
;
75 printf("%s\n", refname
);
78 printf("%-15s ", refname
);
80 buf
= read_sha1_file(sha1
, &type
, &size
);
85 sp
= strstr(buf
, "\n\n");
90 /* only take up to "lines" lines, and strip the signature */
92 i
< filter
->lines
&& sp
< buf
+ size
&&
93 prefixcmp(sp
, PGP_SIGNATURE
"\n");
97 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
98 len
= eol
? eol
- sp
: size
- (sp
- buf
);
99 fwrite(sp
, len
, 1, stdout
);
111 static int list_tags(const char *pattern
, int lines
)
113 struct tag_filter filter
;
118 filter
.pattern
= pattern
;
119 filter
.lines
= lines
;
121 for_each_tag_ref(show_reference
, (void *) &filter
);
126 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
127 const unsigned char *sha1
);
129 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
134 unsigned char sha1
[20];
136 for (p
= argv
; *p
; p
++) {
137 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
139 error("tag name too long: %.*s...", 50, *p
);
143 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
144 error("tag '%s' not found.", *p
);
148 if (fn(*p
, ref
, sha1
))
154 static int delete_tag(const char *name
, const char *ref
,
155 const unsigned char *sha1
)
157 if (delete_ref(ref
, sha1
))
159 printf("Deleted tag '%s'\n", name
);
163 static int verify_tag(const char *name
, const char *ref
,
164 const unsigned char *sha1
)
166 const char *argv_verify_tag
[] = {"git-verify-tag",
167 "-v", "SHA1_HEX", NULL
};
168 argv_verify_tag
[2] = sha1_to_hex(sha1
);
170 if (run_command_v_opt(argv_verify_tag
, 0))
171 return error("could not verify the tag '%s'", name
);
175 static int do_sign(struct strbuf
*buffer
)
177 struct child_process gpg
;
183 if (strlcpy(signingkey
, git_committer_info(1),
184 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
185 return error("committer info too long.");
186 bracket
= strchr(signingkey
, '>');
191 /* When the username signingkey is bad, program could be terminated
192 * because gpg exits without reading and then write gets SIGPIPE. */
193 signal(SIGPIPE
, SIG_IGN
);
195 memset(&gpg
, 0, sizeof(gpg
));
201 args
[2] = signingkey
;
204 if (start_command(&gpg
))
205 return error("could not run gpg.");
207 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
209 finish_command(&gpg
);
210 return error("gpg did not accept the tag data");
214 len
= strbuf_read(buffer
, gpg
.out
, 1024);
216 if (finish_command(&gpg
) || !len
|| len
< 0)
217 return error("gpg failed to sign the tag");
220 return error("could not read the entire signature from gpg.");
225 static const char tag_template
[] =
228 "# Write a tag message\n"
231 static int git_tag_config(const char *var
, const char *value
)
233 if (!strcmp(var
, "user.signingkey")) {
235 die("user.signingkey without value");
236 if (strlcpy(signingkey
, value
, sizeof(signingkey
))
237 >= sizeof(signingkey
))
238 die("user.signingkey value too long");
242 return git_default_config(var
, value
);
245 static void write_tag_body(int fd
, const unsigned char *sha1
)
248 enum object_type type
;
249 char *buf
, *sp
, *eob
;
252 buf
= read_sha1_file(sha1
, &type
, &size
);
256 sp
= strstr(buf
, "\n\n");
258 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
262 sp
+= 2; /* skip the 2 LFs */
263 eob
= strstr(sp
, "\n" PGP_SIGNATURE
"\n");
267 len
= buf
+ size
- sp
;
268 write_or_die(fd
, sp
, len
);
273 static void create_tag(const unsigned char *object
, const char *tag
,
274 struct strbuf
*buf
, int message
, int sign
,
275 unsigned char *prev
, unsigned char *result
)
277 enum object_type type
;
278 char header_buf
[1024];
281 type
= sha1_object_info(object
, NULL
);
282 if (type
<= OBJ_NONE
)
283 die("bad object type.");
285 header_len
= snprintf(header_buf
, sizeof(header_buf
),
293 git_committer_info(1));
295 if (header_len
> sizeof(header_buf
) - 1)
296 die("tag header too big.");
302 /* write the template message before editing: */
303 path
= xstrdup(git_path("TAG_EDITMSG"));
304 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
306 die("could not create file '%s': %s",
307 path
, strerror(errno
));
309 if (!is_null_sha1(prev
))
310 write_tag_body(fd
, prev
);
312 write_or_die(fd
, tag_template
, strlen(tag_template
));
315 launch_editor(path
, buf
, NULL
);
323 if (!message
&& !buf
->len
)
324 die("no tag message?");
326 strbuf_insert(buf
, 0, header_buf
, header_len
);
328 if (sign
&& do_sign(buf
) < 0)
329 die("unable to sign the tag");
330 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
331 die("unable to write tag file");
334 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
337 unsigned char object
[20], prev
[20];
338 int annotate
= 0, sign
= 0, force
= 0, lines
= 0, message
= 0;
340 const char *object_ref
, *tag
;
342 struct ref_lock
*lock
;
344 git_config(git_tag_config
);
345 strbuf_init(&buf
, 0);
347 for (i
= 1; i
< argc
; i
++) {
348 const char *arg
= argv
[i
];
352 if (!strcmp(arg
, "-a")) {
356 if (!strcmp(arg
, "-s")) {
361 if (!strcmp(arg
, "-f")) {
365 if (!strcmp(arg
, "-n")) {
366 if (i
+ 1 == argc
|| *argv
[i
+ 1] == '-')
370 lines
= isdigit(*argv
[++i
]) ?
374 if (!strcmp(arg
, "-m")) {
378 die("option -m needs an argument.");
380 die("only one -F or -m option is allowed.");
381 strbuf_addstr(&buf
, argv
[i
]);
385 if (!strcmp(arg
, "-F")) {
389 die("option -F needs an argument.");
391 die("only one -F or -m option is allowed.");
393 if (!strcmp(argv
[i
], "-")) {
394 if (strbuf_read(&buf
, 0, 1024) < 0)
395 die("cannot read %s", argv
[i
]);
397 if (strbuf_read_file(&buf
, argv
[i
], 1024) < 0)
398 die("could not open or read '%s': %s",
399 argv
[i
], strerror(errno
));
404 if (!strcmp(arg
, "-u")) {
409 die("option -u needs an argument.");
410 if (strlcpy(signingkey
, argv
[i
], sizeof(signingkey
))
411 >= sizeof(signingkey
))
412 die("argument to option -u too long");
415 if (!strcmp(arg
, "-l"))
416 return list_tags(argv
[i
+ 1], lines
);
417 if (!strcmp(arg
, "-d"))
418 return for_each_tag_name(argv
+ i
+ 1, delete_tag
);
419 if (!strcmp(arg
, "-v"))
420 return for_each_tag_name(argv
+ i
+ 1, verify_tag
);
421 usage(builtin_tag_usage
);
426 usage(builtin_tag_usage
);
427 return list_tags(NULL
, lines
);
431 object_ref
= i
< argc
? argv
[i
] : "HEAD";
433 die("too many params");
435 if (get_sha1(object_ref
, object
))
436 die("Failed to resolve '%s' as a valid ref.", object_ref
);
438 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
439 die("tag name too long: %.*s...", 50, tag
);
440 if (check_ref_format(ref
))
441 die("'%s' is not a valid tag name.", tag
);
443 if (!resolve_ref(ref
, prev
, 1, NULL
))
446 die("tag '%s' already exists", tag
);
449 create_tag(object
, tag
, &buf
, message
, sign
, prev
, object
);
451 lock
= lock_any_ref_for_update(ref
, prev
, 0);
453 die("%s: cannot lock the ref", ref
);
454 if (write_ref_sha1(lock
, object
, NULL
) < 0)
455 die("%s: cannot update the ref", ref
);
457 strbuf_release(&buf
);