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 static void launch_editor(const char *path
, char **buffer
, unsigned long *len
)
22 const char *editor
, *terminal
;
23 struct child_process child
;
27 editor
= getenv("GIT_EDITOR");
28 if (!editor
&& editor_program
)
29 editor
= editor_program
;
31 editor
= getenv("VISUAL");
33 editor
= getenv("EDITOR");
35 terminal
= getenv("TERM");
36 if (!editor
&& (!terminal
|| !strcmp(terminal
, "dumb"))) {
38 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
39 "Please supply the message using either -m or -F option.\n");
46 memset(&child
, 0, sizeof(child
));
52 if (run_command(&child
))
53 die("There was a problem with the editor %s.", editor
);
55 fd
= open(path
, O_RDONLY
);
57 die("could not open '%s': %s", path
, strerror(errno
));
58 if (read_fd(fd
, buffer
, len
)) {
60 die("could not read message file '%s': %s",
61 path
, strerror(errno
));
71 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
73 static int show_reference(const char *refname
, const unsigned char *sha1
,
74 int flag
, void *cb_data
)
76 struct tag_filter
*filter
= cb_data
;
78 if (!fnmatch(filter
->pattern
, refname
, 0)) {
81 enum object_type type
;
86 printf("%s\n", refname
);
89 printf("%-15s ", refname
);
91 sp
= buf
= read_sha1_file(sha1
, &type
, &size
);
99 while (sp
+ 1 < buf
+ size
&&
100 !(sp
[0] == '\n' && sp
[1] == '\n'))
102 /* only take up to "lines" lines, and strip the signature */
104 i
< filter
->lines
&& sp
< buf
+ size
&&
105 prefixcmp(sp
, PGP_SIGNATURE
"\n");
109 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
110 len
= eol
? eol
- sp
: size
- (sp
- buf
);
111 fwrite(sp
, len
, 1, stdout
);
123 static int list_tags(const char *pattern
, int lines
)
125 struct tag_filter filter
;
131 /* prepend/append * to the shell pattern: */
132 newpattern
= xmalloc(strlen(pattern
) + 3);
133 sprintf(newpattern
, "*%s*", pattern
);
135 filter
.pattern
= newpattern
;
136 filter
.lines
= lines
;
138 for_each_tag_ref(show_reference
, (void *) &filter
);
145 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
146 const unsigned char *sha1
);
148 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
153 unsigned char sha1
[20];
155 for (p
= argv
; *p
; p
++) {
156 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
158 error("tag name too long: %.*s...", 50, *p
);
162 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
163 error("tag '%s' not found.", *p
);
167 if (fn(*p
, ref
, sha1
))
173 static int delete_tag(const char *name
, const char *ref
,
174 const unsigned char *sha1
)
176 if (delete_ref(ref
, sha1
))
178 printf("Deleted tag '%s'\n", name
);
182 static int verify_tag(const char *name
, const char *ref
,
183 const unsigned char *sha1
)
185 const char *argv_verify_tag
[] = {"git-verify-tag",
186 "-v", "SHA1_HEX", NULL
};
187 argv_verify_tag
[2] = sha1_to_hex(sha1
);
189 if (run_command_v_opt(argv_verify_tag
, 0))
190 return error("could not verify the tag '%s'", name
);
194 static ssize_t
do_sign(char *buffer
, size_t size
, size_t max
)
196 struct child_process gpg
;
202 if (strlcpy(signingkey
, git_committer_info(1),
203 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
204 return error("committer info too long.");
205 bracket
= strchr(signingkey
, '>');
210 memset(&gpg
, 0, sizeof(gpg
));
216 args
[2] = signingkey
;
219 if (start_command(&gpg
))
220 return error("could not run gpg.");
222 write_or_die(gpg
.in
, buffer
, size
);
225 len
= read_in_full(gpg
.out
, buffer
+ size
, max
- size
);
227 finish_command(&gpg
);
229 if (len
== max
- size
)
230 return error("could not read the entire signature from gpg.");
233 /* strip CR from the line endings */
236 for (i
= j
= 0; i
< len
; i
++)
237 if (buffer
[size
+ i
] != '\r') {
239 buffer
[size
+ j
] = buffer
[size
+ i
];
249 static const char tag_template
[] =
252 "# Write a tag message\n"
255 static int git_tag_config(const char *var
, const char *value
)
257 if (!strcmp(var
, "user.signingkey")) {
259 die("user.signingkey without value");
260 if (strlcpy(signingkey
, value
, sizeof(signingkey
))
261 >= sizeof(signingkey
))
262 die("user.signingkey value too long");
266 return git_default_config(var
, value
);
269 #define MAX_SIGNATURE_LENGTH 1024
270 /* message must be NULL or allocated, it will be reallocated and freed */
271 static void create_tag(const unsigned char *object
, const char *tag
,
272 char *message
, int sign
, unsigned char *result
)
274 enum object_type type
;
275 char header_buf
[1024], *buffer
= NULL
;
276 int header_len
, max_size
;
277 unsigned long size
= 0;
279 type
= sha1_object_info(object
, NULL
);
280 if (type
<= OBJ_NONE
)
281 die("bad object type.");
283 header_len
= snprintf(header_buf
, sizeof(header_buf
),
291 git_committer_info(1));
293 if (header_len
> sizeof(header_buf
) - 1)
294 die("tag header too big.");
300 /* write the template message before editing: */
301 path
= xstrdup(git_path("TAG_EDITMSG"));
302 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
304 die("could not create file '%s': %s",
305 path
, strerror(errno
));
306 write_or_die(fd
, tag_template
, strlen(tag_template
));
309 launch_editor(path
, &buffer
, &size
);
316 size
= strlen(message
);
319 size
= stripspace(buffer
, size
, 1);
321 if (!message
&& !size
)
322 die("no tag message?");
324 /* insert the header and add the '\n' if needed: */
325 max_size
= header_len
+ size
+ (sign
? MAX_SIGNATURE_LENGTH
: 0) + 1;
326 buffer
= xrealloc(buffer
, max_size
);
328 buffer
[size
++] = '\n';
329 memmove(buffer
+ header_len
, buffer
, size
);
330 memcpy(buffer
, header_buf
, header_len
);
334 size
= do_sign(buffer
, size
, max_size
);
336 die("unable to sign the tag");
339 if (write_sha1_file(buffer
, size
, tag_type
, result
) < 0)
340 die("unable to write tag file");
344 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
346 unsigned char object
[20], prev
[20];
347 int annotate
= 0, sign
= 0, force
= 0, lines
= 0;
348 char *message
= NULL
;
350 const char *object_ref
, *tag
;
352 struct ref_lock
*lock
;
354 git_config(git_tag_config
);
356 for (i
= 1; i
< argc
; i
++) {
357 const char *arg
= argv
[i
];
361 if (!strcmp(arg
, "-a")) {
365 if (!strcmp(arg
, "-s")) {
370 if (!strcmp(arg
, "-f")) {
374 if (!strcmp(arg
, "-n")) {
375 if (i
+ 1 == argc
|| *argv
[i
+ 1] == '-')
379 lines
= isdigit(*argv
[++i
]) ?
383 if (!strcmp(arg
, "-m")) {
387 die("option -m needs an argument.");
389 die("only one -F or -m option is allowed.");
390 message
= xstrdup(argv
[i
]);
393 if (!strcmp(arg
, "-F")) {
400 die("option -F needs an argument.");
402 die("only one -F or -m option is allowed.");
404 if (!strcmp(argv
[i
], "-"))
407 fd
= open(argv
[i
], O_RDONLY
);
409 die("could not open '%s': %s",
410 argv
[i
], strerror(errno
));
413 message
= xmalloc(len
);
414 if (read_fd(fd
, &message
, &len
)) {
416 die("cannot read %s", argv
[i
]);
420 if (!strcmp(arg
, "-u")) {
425 die("option -u needs an argument.");
426 if (strlcpy(signingkey
, argv
[i
], sizeof(signingkey
))
427 >= sizeof(signingkey
))
428 die("argument to option -u too long");
431 if (!strcmp(arg
, "-l"))
432 return list_tags(argv
[i
+ 1], lines
);
433 if (!strcmp(arg
, "-d"))
434 return for_each_tag_name(argv
+ i
+ 1, delete_tag
);
435 if (!strcmp(arg
, "-v"))
436 return for_each_tag_name(argv
+ i
+ 1, verify_tag
);
437 usage(builtin_tag_usage
);
442 usage(builtin_tag_usage
);
443 return list_tags(NULL
, lines
);
447 object_ref
= i
< argc
? argv
[i
] : "HEAD";
449 die("too many params");
451 if (get_sha1(object_ref
, object
))
452 die("Failed to resolve '%s' as a valid ref.", object_ref
);
454 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
455 die("tag name too long: %.*s...", 50, tag
);
456 if (check_ref_format(ref
))
457 die("'%s' is not a valid tag name.", tag
);
459 if (!resolve_ref(ref
, prev
, 1, NULL
))
462 die("tag '%s' already exists", tag
);
465 create_tag(object
, tag
, message
, sign
, object
);
467 lock
= lock_any_ref_for_update(ref
, prev
, 0);
469 die("%s: cannot lock the ref", ref
);
470 if (write_ref_sha1(lock
, object
, NULL
) < 0)
471 die("%s: cannot update the ref", ref
);