Merge branch 'jk/tag-contains-ab' (early part) into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 12 Sep 2011 04:54:32 +0000 (21:54 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Sep 2011 04:54:32 +0000 (21:54 -0700)
* 'jk/tag-contains-ab' (early part):
tag: speed up --contains calculation

1  2 
builtin/tag.c
diff --combined builtin/tag.c
index cef27263bc79e519398ae5eb0a49d78f3b389090,f7a7943c9ddf783270addc3a50b23496f44fe175..667515e5278d22548fb83507347ab652533a67b2
  #include "tag.h"
  #include "run-command.h"
  #include "parse-options.h"
+ #include "diff.h"
+ #include "revision.h"
  
  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
  };
  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)
+ {
+       for (; want; want = want->next)
+               if (!hashcmp(want->item->object.sha1, c->object.sha1))
+                       return 1;
+       return 0;
+ }
+ static int contains_recurse(struct commit *candidate,
+                           const struct commit_list *want)
+ {
+       struct commit_list *p;
+       /* was it previously marked as containing a want commit? */
+       if (candidate->object.flags & TMP_MARK)
+               return 1;
+       /* or marked as not possibly containing a want commit? */
+       if (candidate->object.flags & UNINTERESTING)
+               return 0;
+       /* or are we it? */
+       if (in_commit_list(want, candidate))
+               return 1;
+       if (parse_commit(candidate) < 0)
+               return 0;
+       /* Otherwise recurse and mark ourselves for future traversals. */
+       for (p = candidate->parents; p; p = p->next) {
+               if (contains_recurse(p->item, want)) {
+                       candidate->object.flags |= TMP_MARK;
+                       return 1;
+               }
+       }
+       candidate->object.flags |= UNINTERESTING;
+       return 0;
+ }
+ static int contains(struct commit *candidate, const struct commit_list *want)
+ {
+       return contains_recurse(candidate, want);
+ }
  static int show_reference(const char *refname, const unsigned char *sha1,
                          int flag, void *cb_data)
  {
        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;
                        commit = lookup_commit_reference_gently(sha1, 1);
                        if (!commit)
                                return 0;
-                       if (!is_descendant_of(commit, filter->with_commit))
+                       if (!contains(commit, filter->with_commit))
                                return 0;
                }
  
                        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    ");
        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;
  
@@@ -126,12 -164,12 +170,12 @@@ static int for_each_tag_name(const cha
        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;
                }
@@@ -146,7 -184,7 +190,7 @@@ static int delete_tag(const char *name
  {
        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;
  }
  
@@@ -158,7 -196,7 +202,7 @@@ static int verify_tag(const char *name
        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;
  }
  
@@@ -173,7 -211,7 +217,7 @@@ static int do_sign(struct strbuf *buffe
        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';
        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++)
  }
  
  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)
@@@ -248,7 -286,8 +292,7 @@@ static void write_tag_body(int fd, cons
  {
        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)
                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);
  }
  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;
  }
  
@@@ -286,7 -330,7 +330,7 @@@ static void create_tag(const unsigned c
  
        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"
                          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;
                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);
                }
        }
        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);
        }
@@@ -360,22 -404,11 +404,22 @@@ static int parse_msg_arg(const struct o
        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;
  
                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"),
                {
        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)
  
        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);
                        }
                }
  
        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;
  }