Code

Merge branch 'jk/tag-contains-ab' (early part) into maint
[git.git] / builtin / tag.c
index f7a7943c9ddf783270addc3a50b23496f44fe175..667515e5278d22548fb83507347ab652533a67b2 100644 (file)
@@ -18,7 +18,7 @@
 static const char * const git_tag_usage[] = {
        "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
        "git tag -d <tagname>...",
-       "git tag -l [-n[<num>]] [<pattern>]",
+       "git tag -l [-n[<num>]] [<pattern>...]",
        "git tag -v <tagname>...",
        NULL
 };
@@ -26,12 +26,21 @@ static const char * const git_tag_usage[] = {
 static char signingkey[1000];
 
 struct tag_filter {
-       const char *pattern;
+       const char **patterns;
        int lines;
        struct commit_list *with_commit;
 };
 
-#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
+static int match_pattern(const char **patterns, const char *ref)
+{
+       /* no pattern means match everything */
+       if (!*patterns)
+               return 1;
+       for (; *patterns; patterns++)
+               if (!fnmatch(*patterns, ref, 0))
+                       return 1;
+       return 0;
+}
 
 static int in_commit_list(const struct commit_list *want, struct commit *c)
 {
@@ -80,7 +89,7 @@ static int show_reference(const char *refname, const unsigned char *sha1,
 {
        struct tag_filter *filter = cb_data;
 
-       if (!fnmatch(filter->pattern, refname, 0)) {
+       if (match_pattern(filter->patterns, refname)) {
                int i;
                unsigned long size;
                enum object_type type;
@@ -114,9 +123,9 @@ static int show_reference(const char *refname, const unsigned char *sha1,
                        return 0;
                }
                /* only take up to "lines" lines, and strip the signature */
+               size = parse_signature(buf, size);
                for (i = 0, sp += 2;
-                               i < filter->lines && sp < buf + size &&
-                               prefixcmp(sp, PGP_SIGNATURE "\n");
+                               i < filter->lines && sp < buf + size;
                                i++) {
                        if (i)
                                printf("\n    ");
@@ -134,15 +143,12 @@ static int show_reference(const char *refname, const unsigned char *sha1,
        return 0;
 }
 
-static int list_tags(const char *pattern, int lines,
+static int list_tags(const char **patterns, int lines,
                        struct commit_list *with_commit)
 {
        struct tag_filter filter;
 
-       if (pattern == NULL)
-               pattern = "*";
-
-       filter.pattern = pattern;
+       filter.patterns = patterns;
        filter.lines = lines;
        filter.with_commit = with_commit;
 
@@ -164,12 +170,12 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
        for (p = argv; *p; p++) {
                if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
                                        >= sizeof(ref)) {
-                       error("tag name too long: %.*s...", 50, *p);
+                       error(_("tag name too long: %.*s..."), 50, *p);
                        had_error = 1;
                        continue;
                }
                if (!resolve_ref(ref, sha1, 1, NULL)) {
-                       error("tag '%s' not found.", *p);
+                       error(_("tag '%s' not found."), *p);
                        had_error = 1;
                        continue;
                }
@@ -184,7 +190,7 @@ static int delete_tag(const char *name, const char *ref,
 {
        if (delete_ref(ref, sha1, 0))
                return 1;
-       printf("Deleted tag '%s' (was %s)\n", name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
+       printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
        return 0;
 }
 
@@ -196,7 +202,7 @@ static int verify_tag(const char *name, const char *ref,
        argv_verify_tag[2] = sha1_to_hex(sha1);
 
        if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
-               return error("could not verify the tag '%s'", name);
+               return error(_("could not verify the tag '%s'"), name);
        return 0;
 }
 
@@ -211,7 +217,7 @@ static int do_sign(struct strbuf *buffer)
        if (!*signingkey) {
                if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
                                sizeof(signingkey)) > sizeof(signingkey) - 1)
-                       return error("committer info too long.");
+                       return error(_("committer info too long."));
                bracket = strchr(signingkey, '>');
                if (bracket)
                        bracket[1] = '\0';
@@ -231,20 +237,20 @@ static int do_sign(struct strbuf *buffer)
        args[3] = NULL;
 
        if (start_command(&gpg))
-               return error("could not run gpg.");
+               return error(_("could not run gpg."));
 
        if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
                close(gpg.in);
                close(gpg.out);
                finish_command(&gpg);
-               return error("gpg did not accept the tag data");
+               return error(_("gpg did not accept the tag data"));
        }
        close(gpg.in);
        len = strbuf_read(buffer, gpg.out, 1024);
        close(gpg.out);
 
        if (finish_command(&gpg) || !len || len < 0)
-               return error("gpg failed to sign the tag");
+               return error(_("gpg failed to sign the tag"));
 
        /* Strip CR from the line endings, in case we are on Windows. */
        for (i = j = 0; i < buffer->len; i++)
@@ -259,15 +265,15 @@ static int do_sign(struct strbuf *buffer)
 }
 
 static const char tag_template[] =
-       "\n"
+       N_("\n"
        "#\n"
        "# Write a tag message\n"
-       "#\n";
+       "#\n");
 
 static void set_signingkey(const char *value)
 {
        if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
-               die("signing key value too long (%.10s...)", value);
+               die(_("signing key value too long (%.10s...)"), value);
 }
 
 static int git_tag_config(const char *var, const char *value, void *cb)
@@ -286,8 +292,7 @@ static void write_tag_body(int fd, const unsigned char *sha1)
 {
        unsigned long size;
        enum object_type type;
-       char *buf, *sp, *eob;
-       size_t len;
+       char *buf, *sp;
 
        buf = read_sha1_file(sha1, &type, &size);
        if (!buf)
@@ -300,12 +305,7 @@ static void write_tag_body(int fd, const unsigned char *sha1)
                return;
        }
        sp += 2; /* skip the 2 LFs */
-       eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
-       if (eob)
-               len = eob - sp;
-       else
-               len = buf + size - sp;
-       write_or_die(fd, sp, len);
+       write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
 
        free(buf);
 }
@@ -313,9 +313,9 @@ static void write_tag_body(int fd, const unsigned char *sha1)
 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
 {
        if (sign && do_sign(buf) < 0)
-               return error("unable to sign the tag");
+               return error(_("unable to sign the tag"));
        if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
-               return error("unable to write tag file");
+               return error(_("unable to write tag file"));
        return 0;
 }
 
@@ -330,7 +330,7 @@ static void create_tag(const unsigned char *object, const char *tag,
 
        type = sha1_object_info(object, NULL);
        if (type <= OBJ_NONE)
-           die("bad object type.");
+           die(_("bad object type."));
 
        header_len = snprintf(header_buf, sizeof(header_buf),
                          "object %s\n"
@@ -343,7 +343,7 @@ static void create_tag(const unsigned char *object, const char *tag,
                          git_committer_info(IDENT_ERROR_ON_NO_NAME));
 
        if (header_len > sizeof(header_buf) - 1)
-               die("tag header too big.");
+               die(_("tag header too big."));
 
        if (!message) {
                int fd;
@@ -352,17 +352,17 @@ static void create_tag(const unsigned char *object, const char *tag,
                path = git_pathdup("TAG_EDITMSG");
                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
                if (fd < 0)
-                       die_errno("could not create file '%s'", path);
+                       die_errno(_("could not create file '%s'"), path);
 
                if (!is_null_sha1(prev))
                        write_tag_body(fd, prev);
                else
-                       write_or_die(fd, tag_template, strlen(tag_template));
+                       write_or_die(fd, _(tag_template), strlen(_(tag_template)));
                close(fd);
 
                if (launch_editor(path, buf, NULL)) {
                        fprintf(stderr,
-                       "Please supply the message using either -m or -F option.\n");
+                       _("Please supply the message using either -m or -F option.\n"));
                        exit(1);
                }
        }
@@ -370,13 +370,13 @@ static void create_tag(const unsigned char *object, const char *tag,
        stripspace(buf, 1);
 
        if (!message && !buf->len)
-               die("no tag message?");
+               die(_("no tag message?"));
 
        strbuf_insert(buf, 0, header_buf, header_len);
 
        if (build_tag_object(buf, sign, result) < 0) {
                if (path)
-                       fprintf(stderr, "The tag message has been left in %s\n",
+                       fprintf(stderr, _("The tag message has been left in %s\n"),
                                path);
                exit(128);
        }
@@ -404,11 +404,22 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
        return 0;
 }
 
+static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
+{
+       if (name[0] == '-')
+               return CHECK_REF_FORMAT_ERROR;
+
+       strbuf_reset(sb);
+       strbuf_addf(sb, "refs/tags/%s", name);
+
+       return check_ref_format(sb->buf);
+}
+
 int cmd_tag(int argc, const char **argv, const char *prefix)
 {
        struct strbuf buf = STRBUF_INIT;
+       struct strbuf ref = STRBUF_INIT;
        unsigned char object[20], prev[20];
-       char ref[PATH_MAX];
        const char *object_ref, *tag;
        struct ref_lock *lock;
 
@@ -428,13 +439,13 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
                OPT_GROUP("Tag creation options"),
                OPT_BOOLEAN('a', NULL, &annotate,
                                        "annotated tag, needs a message"),
-               OPT_CALLBACK('m', NULL, &msg, "msg",
-                            "message for the tag", parse_msg_arg),
-               OPT_FILENAME('F', NULL, &msgfile, "message in a file"),
+               OPT_CALLBACK('m', NULL, &msg, "message",
+                            "tag message", parse_msg_arg),
+               OPT_FILENAME('F', NULL, &msgfile, "read message from file"),
                OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
                OPT_STRING('u', NULL, &keyid, "key-id",
                                        "use another key to sign the tag"),
-               OPT_BOOLEAN('f', "force", &force, "replace the tag if exists"),
+               OPT__FORCE(&force, "replace the tag if exists"),
 
                OPT_GROUP("Tag listing options"),
                {
@@ -466,12 +477,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
        if (list + delete + verify > 1)
                usage_with_options(git_tag_usage, options);
        if (list)
-               return list_tags(argv[0], lines == -1 ? 0 : lines,
+               return list_tags(argv, lines == -1 ? 0 : lines,
                                 with_commit);
        if (lines != -1)
-               die("-n option is only allowed with -l.");
+               die(_("-n option is only allowed with -l."));
        if (with_commit)
-               die("--contains option is only allowed with -l.");
+               die(_("--contains option is only allowed with -l."));
        if (delete)
                return for_each_tag_name(argv, delete_tag);
        if (verify)
@@ -479,17 +490,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
        if (msg.given || msgfile) {
                if (msg.given && msgfile)
-                       die("only one -F or -m option is allowed.");
+                       die(_("only one -F or -m option is allowed."));
                annotate = 1;
                if (msg.given)
                        strbuf_addbuf(&buf, &(msg.buf));
                else {
                        if (!strcmp(msgfile, "-")) {
                                if (strbuf_read(&buf, 0, 1024) < 0)
-                                       die_errno("cannot read '%s'", msgfile);
+                                       die_errno(_("cannot read '%s'"), msgfile);
                        } else {
                                if (strbuf_read_file(&buf, msgfile, 1024) < 0)
-                                       die_errno("could not open or read '%s'",
+                                       die_errno(_("could not open or read '%s'"),
                                                msgfile);
                        }
                }
@@ -499,33 +510,32 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
        object_ref = argc == 2 ? argv[1] : "HEAD";
        if (argc > 2)
-               die("too many params");
+               die(_("too many params"));
 
        if (get_sha1(object_ref, object))
-               die("Failed to resolve '%s' as a valid ref.", object_ref);
+               die(_("Failed to resolve '%s' as a valid ref."), object_ref);
 
-       if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
-               die("tag name too long: %.*s...", 50, tag);
-       if (check_ref_format(ref))
-               die("'%s' is not a valid tag name.", tag);
+       if (strbuf_check_tag_ref(&ref, tag))
+               die(_("'%s' is not a valid tag name."), tag);
 
-       if (!resolve_ref(ref, prev, 1, NULL))
+       if (!resolve_ref(ref.buf, prev, 1, NULL))
                hashclr(prev);
        else if (!force)
-               die("tag '%s' already exists", tag);
+               die(_("tag '%s' already exists"), tag);
 
        if (annotate)
                create_tag(object, tag, &buf, msg.given || msgfile,
                           sign, prev, object);
 
-       lock = lock_any_ref_for_update(ref, prev, 0);
+       lock = lock_any_ref_for_update(ref.buf, prev, 0);
        if (!lock)
-               die("%s: cannot lock the ref", ref);
+               die(_("%s: cannot lock the ref"), ref.buf);
        if (write_ref_sha1(lock, object, NULL) < 0)
-               die("%s: cannot update the ref", ref);
+               die(_("%s: cannot update the ref"), ref.buf);
        if (force && hashcmp(prev, object))
-               printf("Updated tag '%s' (was %s)\n", tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
+               printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
 
        strbuf_release(&buf);
+       strbuf_release(&ref);
        return 0;
 }