@matheusbsbs / matheus-bagliano / commits / 62e09ce998
Fork 0

Make git tag a builtin.

This replaces the script "git-tag.sh" with "builtin-tag.c". The existing test suite for "git tag" guarantees the compatibility with the features provided by the script version. There are some minor changes in the behaviour of "git tag" here: "git tag -v" now can get more than one tag to verify, like "git tag -d" does, "git tag" with no arguments prints all tags, more like "git branch" does, and "git tag -n" also prints all tags with annotations (without needing -l). Tests and documentation were also updated to reflect these changes. The program is currently calling the script "git verify-tag" for verify. This can be changed porting it to C and calling its functions directly from builtin-tag.c. Signed-off-by: Carlos Rica <jasampler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Rica committed Jul 20, 2007 at 01:42 UTC 62e09ce998dd7f6b844deb650101c743a5c4ce50
7 files changed +538 -13
Documentation/git-tag.txt
+5 -3
@@ -12,7 +12,7 @@ SYNOPSIS
12 'git-tag' [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <name> [<head>]
13 'git-tag' -d <name>...
14 'git-tag' [-n [<num>]] -l [<pattern>]
15 -'git-tag' -v <name>
15 +'git-tag' -v <name>...
16
17 DESCRIPTION
18 -----------
@@ -23,7 +23,7 @@ Unless `-f` is given, the tag must not yet exist in
23
24 If one of `-a`, `-s`, or `-u <key-id>` is passed, the command
25 creates a 'tag' object, and requires the tag message. Unless
26 -`-m <msg>` is given, an editor is started for the user to type
26 +`-m <msg>` or `-F <file>` is given, an editor is started for the user to type
27 in the tag message.
28
29 Otherwise just the SHA1 object name of the commit object is
@@ -59,15 +59,17 @@ OPTIONS
59 Delete existing tags with the given names.
60
61 -v::
62 - Verify the gpg signature of given the tag
62 + Verify the gpg signature of the given tag names.
63
64 -n <num>::
65 <num> specifies how many lines from the annotation, if any,
66 are printed when using -l.
67 The default is not to print any annotation lines.
68 + If no number is given to `-n`, only the first line is printed.
69
70 -l <pattern>::
71 List tags with names that match the given pattern (or all if no pattern is given).
72 + Typing "git tag" without arguments, also lists all tags.
73
74 -m <msg>::
75 Use the given tag message (instead of prompting)
Makefile
+2 -1
@@ -206,7 +206,7 @@ SCRIPT_SH = \
206 git-pull.sh git-rebase.sh git-rebase--interactive.sh \
207 git-repack.sh git-request-pull.sh git-reset.sh \
208 git-sh-setup.sh \
209 - git-tag.sh git-verify-tag.sh \
209 + git-verify-tag.sh \
210 git-am.sh \
211 git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
212 git-merge-resolve.sh git-merge-ours.sh \
@@ -361,6 +361,7 @@ BUILTIN_OBJS = \
361 builtin-show-branch.o \
362 builtin-stripspace.o \
363 builtin-symbolic-ref.o \
364 + builtin-tag.o \
365 builtin-tar-tree.o \
366 builtin-unpack-objects.o \
367 builtin-update-index.o \
builtin-tag.c new
+450
@@ -0,0 +1,450 @@
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"
10 +#include "builtin.h"
11 +#include "refs.h"
12 +#include "tag.h"
13 +#include "run-command.h"
14 +
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>]";
17 +
18 +static char signingkey[1000];
19 +
20 +static void launch_editor(const char *path, char **buffer, unsigned long *len)
21 +{
22 + const char *editor, *terminal;
23 + struct child_process child;
24 + const char *args[3];
25 + int fd;
26 +
27 + editor = getenv("VISUAL");
28 + if (!editor)
29 + editor = getenv("EDITOR");
30 +
31 + terminal = getenv("TERM");
32 + if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
33 + fprintf(stderr,
34 + "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
35 + "Please supply the message using either -m or -F option.\n");
36 + exit(1);
37 + }
38 +
39 + if (!editor)
40 + editor = "vi";
41 +
42 + memset(&child, 0, sizeof(child));
43 + child.argv = args;
44 + args[0] = editor;
45 + args[1] = path;
46 + args[2] = NULL;
47 +
48 + if (run_command(&child))
49 + die("There was a problem with the editor %s.", editor);
50 +
51 + fd = open(path, O_RDONLY);
52 + if (fd < 0)
53 + die("could not open '%s': %s", path, strerror(errno));
54 + if (read_fd(fd, buffer, len)) {
55 + free(*buffer);
56 + die("could not read message file '%s': %s",
57 + path, strerror(errno));
58 + }
59 + close(fd);
60 +}
61 +
62 +struct tag_filter {
63 + const char *pattern;
64 + int lines;
65 +};
66 +
67 +#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
68 +
69 +static int show_reference(const char *refname, const unsigned char *sha1,
70 + int flag, void *cb_data)
71 +{
72 + struct tag_filter *filter = cb_data;
73 +
74 + if (!fnmatch(filter->pattern, refname, 0)) {
75 + int i;
76 + unsigned long size;
77 + enum object_type type;
78 + char *buf, *sp, *eol;
79 + size_t len;
80 +
81 + if (!filter->lines) {
82 + printf("%s\n", refname);
83 + return 0;
84 + }
85 + printf("%-15s ", refname);
86 +
87 + sp = buf = read_sha1_file(sha1, &type, &size);
88 + if (!buf || !size)
89 + return 0;
90 + /* skip header */
91 + while (sp + 1 < buf + size &&
92 + !(sp[0] == '\n' && sp[1] == '\n'))
93 + sp++;
94 + /* only take up to "lines" lines, and strip the signature */
95 + for (i = 0, sp += 2; i < filter->lines && sp < buf + size &&
96 + prefixcmp(sp, PGP_SIGNATURE "\n");
97 + i++) {
98 + if (i)
99 + printf("\n ");
100 + eol = memchr(sp, '\n', size - (sp - buf));
101 + len = eol ? eol - sp : size - (sp - buf);
102 + fwrite(sp, len, 1, stdout);
103 + if (!eol)
104 + break;
105 + sp = eol + 1;
106 + }
107 + putchar('\n');
108 + free(buf);
109 + }
110 +
111 + return 0;
112 +}
113 +
114 +static int list_tags(const char *pattern, int lines)
115 +{
116 + struct tag_filter filter;
117 + char *newpattern;
118 +
119 + if (pattern == NULL)
120 + pattern = "";
121 +
122 + /* prepend/append * to the shell pattern: */
123 + newpattern = xmalloc(strlen(pattern) + 3);
124 + sprintf(newpattern, "*%s*", pattern);
125 +
126 + filter.pattern = newpattern;
127 + filter.lines = lines;
128 +
129 + for_each_tag_ref(show_reference, (void *) &filter);
130 +
131 + free(newpattern);
132 +
133 + return 0;
134 +}
135 +
136 +typedef int (*func_tag)(const char *name, const char *ref,
137 + const unsigned char *sha1);
138 +
139 +static int do_tag_names(const char **argv, func_tag fn)
140 +{
141 + const char **p;
142 + char ref[PATH_MAX];
143 + int had_error = 0;
144 + unsigned char sha1[20];
145 +
146 + for (p = argv; *p; p++) {
147 + if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
148 + >= sizeof(ref)) {
149 + error("tag name too long: %.*s...", 50, *p);
150 + had_error = 1;
151 + continue;
152 + }
153 + if (!resolve_ref(ref, sha1, 1, NULL)) {
154 + error("tag '%s' not found.", *p);
155 + had_error = 1;
156 + continue;
157 + }
158 + if (fn(*p, ref, sha1))
159 + had_error = 1;
160 + }
161 + return had_error;
162 +}
163 +
164 +static int delete_tag(const char *name, const char *ref,
165 + const unsigned char *sha1)
166 +{
167 + if (delete_ref(ref, sha1))
168 + return 1;
169 + printf("Deleted tag '%s'\n", name);
170 + return 0;
171 +}
172 +
173 +static int verify_tag(const char *name, const char *ref,
174 + const unsigned char *sha1)
175 +{
176 + const char *argv_verify_tag[] = {"git-verify-tag",
177 + "-v", "SHA1_HEX", NULL};
178 + argv_verify_tag[2] = sha1_to_hex(sha1);
179 +
180 + if (run_command_v_opt(argv_verify_tag, 0))
181 + return error("could not verify the tag '%s'", name);
182 + return 0;
183 +}
184 +
185 +static ssize_t do_sign(char *buffer, size_t size, size_t max)
186 +{
187 + struct child_process gpg;
188 + const char *args[4];
189 + char *bracket;
190 + int len;
191 +
192 + if (!*signingkey) {
193 + if (strlcpy(signingkey, git_committer_info(1),
194 + sizeof(signingkey)) >= sizeof(signingkey))
195 + return error("committer info too long.");
196 + bracket = strchr(signingkey, '>');
197 + if (bracket)
198 + bracket[1] = '\0';
199 + }
200 +
201 + memset(&gpg, 0, sizeof(gpg));
202 + gpg.argv = args;
203 + gpg.in = -1;
204 + gpg.out = -1;
205 + args[0] = "gpg";
206 + args[1] = "-bsau";
207 + args[2] = signingkey;
208 + args[3] = NULL;
209 +
210 + if (start_command(&gpg))
211 + return error("could not run gpg.");
212 +
213 + write_or_die(gpg.in, buffer, size);
214 + close(gpg.in);
215 + gpg.close_in = 0;
216 + len = read_in_full(gpg.out, buffer + size, max - size);
217 +
218 + finish_command(&gpg);
219 +
220 + if (len == max - size)
221 + return error("could not read the entire signature from gpg.");
222 +
223 + return size + len;
224 +}
225 +
226 +static const char tag_template[] =
227 + "\n"
228 + "#\n"
229 + "# Write a tag message\n"
230 + "#\n";
231 +
232 +static int git_tag_config(const char *var, const char *value)
233 +{
234 + if (!strcmp(var, "user.signingkey")) {
235 + if (!value)
236 + die("user.signingkey without value");
237 + if (strlcpy(signingkey, value, sizeof(signingkey))
238 + >= sizeof(signingkey))
239 + die("user.signingkey value too long");
240 + return 0;
241 + }
242 +
243 + return git_default_config(var, value);
244 +}
245 +
246 +#define MAX_SIGNATURE_LENGTH 1024
247 +/* message must be NULL or allocated, it will be reallocated and freed */
248 +static void create_tag(const unsigned char *object, const char *tag,
249 + char *message, int sign, unsigned char *result)
250 +{
251 + enum object_type type;
252 + char header_buf[1024], *buffer;
253 + int header_len, max_size;
254 + unsigned long size;
255 +
256 + type = sha1_object_info(object, NULL);
257 + if (type <= 0)
258 + die("bad object type.");
259 +
260 + header_len = snprintf(header_buf, sizeof(header_buf),
261 + "object %s\n"
262 + "type %s\n"
263 + "tag %s\n"
264 + "tagger %s\n\n",
265 + sha1_to_hex(object),
266 + typename(type),
267 + tag,
268 + git_committer_info(1));
269 +
270 + if (header_len >= sizeof(header_buf))
271 + die("tag header too big.");
272 +
273 + if (!message) {
274 + char *path;
275 + int fd;
276 +
277 + /* write the template message before editing: */
278 + path = xstrdup(git_path("TAG_EDITMSG"));
279 + fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
280 + if (fd < 0)
281 + die("could not create file '%s': %s",
282 + path, strerror(errno));
283 + write_or_die(fd, tag_template, strlen(tag_template));
284 + close(fd);
285 +
286 + launch_editor(path, &buffer, &size);
287 +
288 + unlink(path);
289 + free(path);
290 + }
291 + else {
292 + buffer = message;
293 + size = strlen(message);
294 + }
295 +
296 + size = stripspace(buffer, size, 1);
297 +
298 + if (!message && !size)
299 + die("no tag message?");
300 +
301 + /* insert the header and add the '\n' if needed: */
302 + max_size = header_len + size + (sign ? MAX_SIGNATURE_LENGTH : 0) + 1;
303 + buffer = xrealloc(buffer, max_size);
304 + if (size)
305 + buffer[size++] = '\n';
306 + memmove(buffer + header_len, buffer, size);
307 + memcpy(buffer, header_buf, header_len);
308 + size += header_len;
309 +
310 + if (sign) {
311 + size = do_sign(buffer, size, max_size);
312 + if (size < 0)
313 + die("unable to sign the tag");
314 + }
315 +
316 + if (write_sha1_file(buffer, size, tag_type, result) < 0)
317 + die("unable to write tag file");
318 + free(buffer);
319 +}
320 +
321 +int cmd_tag(int argc, const char **argv, const char *prefix)
322 +{
323 + unsigned char object[20], prev[20];
324 + int annotate = 0, sign = 0, force = 0, lines = 0;
325 + char *message = NULL;
326 + char ref[PATH_MAX];
327 + const char *object_ref, *tag;
328 + int i;
329 + struct ref_lock *lock;
330 +
331 + git_config(git_tag_config);
332 +
333 + for (i = 1; i < argc; i++) {
334 + const char *arg = argv[i];
335 +
336 + if (arg[0] != '-')
337 + break;
338 + if (!strcmp(arg, "-a")) {
339 + annotate = 1;
340 + continue;
341 + }
342 + if (!strcmp(arg, "-s")) {
343 + annotate = 1;
344 + sign = 1;
345 + continue;
346 + }
347 + if (!strcmp(arg, "-f")) {
348 + force = 1;
349 + continue;
350 + }
351 + if (!strcmp(arg, "-n")) {
352 + if (i + 1 == argc || *argv[i + 1] == '-')
353 + /* no argument */
354 + lines = 1;
355 + else
356 + lines = isdigit(*argv[++i]) ?
357 + atoi(argv[i]) : 1;
358 + continue;
359 + }
360 + if (!strcmp(arg, "-m")) {
361 + annotate = 1;
362 + i++;
363 + if (i == argc)
364 + die("option -m needs an argument.");
365 + message = xstrdup(argv[i]);
366 + continue;
367 + }
368 + if (!strcmp(arg, "-F")) {
369 + unsigned long len;
370 + int fd;
371 +
372 + annotate = 1;
373 + i++;
374 + if (i == argc)
375 + die("option -F needs an argument.");
376 +
377 + if (!strcmp(argv[i], "-"))
378 + fd = 0;
379 + else {
380 + fd = open(argv[i], O_RDONLY);
381 + if (fd < 0)
382 + die("could not open '%s': %s",
383 + argv[i], strerror(errno));
384 + }
385 + len = 1024;
386 + message = xmalloc(len);
387 + if (read_fd(fd, &message, &len)) {
388 + free(message);
389 + die("cannot read %s", argv[i]);
390 + }
391 + continue;
392 + }
393 + if (!strcmp(arg, "-u")) {
394 + annotate = 1;
395 + sign = 1;
396 + i++;
397 + if (i == argc)
398 + die("option -u needs an argument.");
399 + if (strlcpy(signingkey, argv[i], sizeof(signingkey))
400 + >= sizeof(signingkey))
401 + die("argument to option -u too long");
402 + continue;
403 + }
404 + if (!strcmp(arg, "-l")) {
405 + return list_tags(argv[i + 1], lines);
406 + }
407 + if (!strcmp(arg, "-d")) {
408 + return do_tag_names(argv + i + 1, delete_tag);
409 + }
410 + if (!strcmp(arg, "-v")) {
411 + return do_tag_names(argv + i + 1, verify_tag);
412 + }
413 + usage(builtin_tag_usage);
414 + }
415 +
416 + if (i == argc) {
417 + if (annotate)
418 + usage(builtin_tag_usage);
419 + return list_tags(NULL, lines);
420 + }
421 + tag = argv[i++];
422 +
423 + object_ref = i < argc ? argv[i] : "HEAD";
424 + if (i + 1 < argc)
425 + die("too many params");
426 +
427 + if (get_sha1(object_ref, object))
428 + die("Failed to resolve '%s' as a valid ref.", object_ref);
429 +
430 + if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) >= sizeof(ref))
431 + die("tag name too long: %.*s...", 50, tag);
432 + if (check_ref_format(ref))
433 + die("'%s' is not a valid tag name.", tag);
434 +
435 + if (!resolve_ref(ref, prev, 1, NULL))
436 + hashclr(prev);
437 + else if (!force)
438 + die("tag '%s' already exists", tag);
439 +
440 + if (annotate)
441 + create_tag(object, tag, message, sign, object);
442 +
443 + lock = lock_any_ref_for_update(ref, prev, 0);
444 + if (!lock)
445 + die("%s: cannot lock the ref", ref);
446 + if (write_ref_sha1(lock, object, NULL) < 0)
447 + die("%s: cannot update the ref", ref);
448 +
449 + return 0;
450 +}
builtin.h
+1
@@ -70,6 +70,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);
70 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
71 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
72 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
73 +extern int cmd_tag(int argc, const char **argv, const char *prefix);
74 extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
75 extern int cmd_unpack_objects(int argc, const char **argv, const char *prefix);
76 extern int cmd_update_index(int argc, const char **argv, const char *prefix);
contrib/examples/git-tag.sh renamed
git.c
+1
@@ -363,6 +363,7 @@ static void handle_internal_command(int argc, const char **argv)
363 { "show", cmd_show, RUN_SETUP | USE_PAGER },
364 { "stripspace", cmd_stripspace },
365 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
366 + { "tag", cmd_tag, RUN_SETUP },
367 { "tar-tree", cmd_tar_tree },
368 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
369 { "update-index", cmd_update_index, RUN_SETUP },
t/t7004-tag.sh
+79 -9
@@ -5,7 +5,7 @@
5
6 test_description='git-tag
7
8 -Basic tests for operations with tags.'
8 +Tests for operations with tags.'
9
10 . ./test-lib.sh
11
@@ -16,11 +16,15 @@ tag_exists () {
16 }
17
18 # todo: git tag -l now returns always zero, when fixed, change this test
19 -test_expect_success 'listing all tags in an empty tree should succeed' \
20 - 'git tag -l'
19 +test_expect_success 'listing all tags in an empty tree should succeed' '
20 + git tag -l &&
21 + git tag
22 +'
23
22 -test_expect_success 'listing all tags in an empty tree should output nothing' \
23 - 'test `git-tag -l | wc -l` -eq 0'
24 +test_expect_success 'listing all tags in an empty tree should output nothing' '
25 + test `git-tag -l | wc -l` -eq 0 &&
26 + test `git-tag | wc -l` -eq 0
27 +'
28
29 test_expect_failure 'looking for a tag in an empty tree should fail' \
30 'tag_exists mytag'
@@ -49,11 +53,15 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
53 git tag mytag
54 '
55
52 -test_expect_success 'listing all tags if one exists should succeed' \
53 - 'git-tag -l'
56 +test_expect_success 'listing all tags if one exists should succeed' '
57 + git-tag -l &&
58 + git-tag
59 +'
60
55 -test_expect_success 'listing all tags if one exists should output that tag' \
56 - 'test `git-tag -l` = mytag'
61 +test_expect_success 'listing all tags if one exists should output that tag' '
62 + test `git-tag -l` = mytag &&
63 + test `git-tag` = mytag
64 +'
65
66 # pattern matching:
67
@@ -165,6 +173,8 @@ test_expect_success 'listing all tags should print them ordered' '
173 git tag v1.0 &&
174 git tag t210 &&
175 git tag -l > actual &&
176 + git diff expect actual &&
177 + git tag > actual &&
178 git diff expect actual
179 '
180
@@ -264,6 +274,10 @@ test_expect_failure \
274 'trying to verify a non-annotated and non-signed tag should fail' \
275 'git-tag -v non-annotated-tag'
276
277 +test_expect_failure \
278 + 'trying to verify many non-annotated or unknown tags, should fail' \
279 + 'git-tag -v unknown-tag1 non-annotated-tag unknown-tag2'
280 +
281 # creating annotated tags:
282
283 get_tag_msg () {
@@ -306,6 +320,18 @@ test_expect_success \
320 git diff expect actual
321 '
322
323 +cat >inputmsg <<EOF
324 +A message from the
325 +standard input
326 +EOF
327 +get_tag_header stdin-annotated-tag $commit commit $time >expect
328 +cat inputmsg >>expect
329 +test_expect_success 'creating an annotated tag with -F - should succeed' '
330 + git-tag -F - stdin-annotated-tag <inputmsg &&
331 + get_tag_msg stdin-annotated-tag >actual &&
332 + git diff expect actual
333 +'
334 +
335 # blank and empty messages:
336
337 get_tag_header empty-annotated-tag $commit commit $time >expect
@@ -551,6 +577,12 @@ test_expect_success \
577 ! git-tag -v file-annotated-tag
578 '
579
580 +test_expect_success \
581 + 'trying to verify two annotated non-signed tags should fail' '
582 + tag_exists annotated-tag file-annotated-tag &&
583 + ! git-tag -v annotated-tag file-annotated-tag
584 +'
585 +
586 # creating and verifying signed tags:
587
588 gpg --version >/dev/null
@@ -589,9 +621,47 @@ test_expect_success 'creating a signed tag with -m message should succeed' '
621 git diff expect actual
622 '
623
624 +cat >sigmsgfile <<EOF
625 +Another signed tag
626 +message in a file.
627 +EOF
628 +get_tag_header file-signed-tag $commit commit $time >expect
629 +cat sigmsgfile >>expect
630 +echo '-----BEGIN PGP SIGNATURE-----' >>expect
631 +test_expect_success \
632 + 'creating a signed tag with -F messagefile should succeed' '
633 + git-tag -s -F sigmsgfile file-signed-tag &&
634 + get_tag_msg file-signed-tag >actual &&
635 + git diff expect actual
636 +'
637 +
638 +cat >siginputmsg <<EOF
639 +A signed tag message from
640 +the standard input
641 +EOF
642 +get_tag_header stdin-signed-tag $commit commit $time >expect
643 +cat siginputmsg >>expect
644 +echo '-----BEGIN PGP SIGNATURE-----' >>expect
645 +test_expect_success 'creating a signed tag with -F - should succeed' '
646 + git-tag -s -F - stdin-signed-tag <siginputmsg &&
647 + get_tag_msg stdin-signed-tag >actual &&
648 + git diff expect actual
649 +'
650 +
651 test_expect_success 'verifying a signed tag should succeed' \
652 'git-tag -v signed-tag'
653
654 +test_expect_success 'verifying two signed tags in one command should succeed' \
655 + 'git-tag -v signed-tag file-signed-tag'
656 +
657 +test_expect_success \
658 + 'verifying many signed and non-signed tags should fail' '
659 + ! git-tag -v signed-tag annotated-tag &&
660 + ! git-tag -v file-annotated-tag file-signed-tag &&
661 + ! git-tag -v annotated-tag file-signed-tag file-annotated-tag &&
662 + ! git-tag -v signed-tag annotated-tag file-signed-tag
663 +'
664 +
665 test_expect_success 'verifying a forged tag should fail' '
666 forged=$(git cat-file tag signed-tag |
667 sed -e "s/signed-tag/forged-tag/" |