Merge branch 'rs/use-isxdigit' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 23 Mar 2015 18:23:41 +0000 (11:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Mar 2015 18:23:41 +0000 (11:23 -0700)
Code cleanup.

* rs/use-isxdigit:
use isxdigit() for checking if a character is a hexadecimal digit

1  2 
sha1_name.c
transport.c
diff --combined sha1_name.c
index 95f9f8fa1a72693757bbafe0f2a7773d8ae2de05,dc85aafeca28ad7c1948d2017e06f69a4105065e..6d10f052b5050c64de5589733c2a8600e2ddb3dc
@@@ -372,10 -372,10 +372,10 @@@ const char *find_unique_abbrev(const un
        int status, exists;
        static char hex[41];
  
 -      exists = has_sha1_file(sha1);
        memcpy(hex, sha1_to_hex(sha1), 40);
        if (len == 40 || !len)
                return hex;
 +      exists = has_sha1_file(sha1);
        while (len < 40) {
                unsigned char sha1_ret[20];
                status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
@@@ -432,8 -432,7 +432,8 @@@ static inline int upstream_mark(const c
  static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
  static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
  
 -static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 +static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
 +                        unsigned int flags)
  {
        static const char *warn_msg = "refname '%.*s' is ambiguous.";
        static const char *object_name_msg = N_(
        if (!refs_found)
                return -1;
  
 -      if (warn_ambiguous_refs &&
 +      if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
            (refs_found > 1 ||
             !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
                warning(warn_msg, len, str);
                        char *tmp = xstrndup(str + at + 2, reflog_len);
                        at_time = approxidate_careful(tmp, &errors);
                        free(tmp);
 -                      if (errors)
 +                      if (errors) {
 +                              free(real_ref);
                                return -1;
 +                      }
                }
 -              if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
 +              if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
                                &co_time, &co_tz, &co_cnt)) {
                        if (!len) {
                                if (starts_with(real_ref, "refs/heads/")) {
                                        len = 4;
                                }
                        }
 -                      if (at_time)
 -                              warning("Log for '%.*s' only goes "
 -                                      "back to %s.", len, str,
 -                                      show_date(co_time, co_tz, DATE_RFC2822));
 -                      else {
 +                      if (at_time) {
 +                              if (!(flags & GET_SHA1_QUIETLY)) {
 +                                      warning("Log for '%.*s' only goes "
 +                                              "back to %s.", len, str,
 +                                              show_date(co_time, co_tz, DATE_RFC2822));
 +                              }
 +                      } else {
 +                              if (flags & GET_SHA1_QUIETLY) {
 +                                      exit(128);
 +                              }
                                die("Log for '%.*s' only has %d entries.",
                                    len, str, co_cnt);
                        }
@@@ -757,7 -749,7 +757,7 @@@ static int get_describe_name(const cha
  
        for (cp = name + len - 1; name + 2 <= cp; cp--) {
                char ch = *cp;
-               if (hexval(ch) & ~0377) {
+               if (!isxdigit(ch)) {
                        /* We must be looking at g in "SOMETHING-g"
                         * for it to be describe output.
                         */
@@@ -807,7 -799,7 +807,7 @@@ static int get_sha1_1(const char *name
        if (!ret)
                return 0;
  
 -      ret = get_sha1_basic(name, len, sha1);
 +      ret = get_sha1_basic(name, len, sha1, lookup_flags);
        if (!ret)
                return 0;
  
@@@ -845,7 -837,7 +845,7 @@@ static int handle_one_ref(const char *p
        }
        if (object->type != OBJ_COMMIT)
                return 0;
 -      commit_list_insert_by_date((struct commit *)object, list);
 +      commit_list_insert((struct commit *)object, list);
        return 0;
  }
  
@@@ -909,8 -901,10 +909,8 @@@ static int grab_nth_branch_switch(unsig
        const char *match = NULL, *target = NULL;
        size_t len;
  
 -      if (starts_with(message, "checkout: moving from ")) {
 -              match = message + strlen("checkout: moving from ");
 +      if (skip_prefix(message, "checkout: moving from ", &match))
                target = strstr(match, " to ");
 -      }
  
        if (!match || !target)
                return 0;
@@@ -954,7 -948,7 +954,7 @@@ static int interpret_nth_prior_checkout
        retval = 0;
        if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) {
                strbuf_reset(buf);
 -              strbuf_add(buf, cb.buf.buf, cb.buf.len);
 +              strbuf_addbuf(buf, &cb.buf);
                retval = brace - name + 1;
        }
  
@@@ -993,7 -987,7 +993,7 @@@ int get_sha1_mb(const char *name, unsig
        two = lookup_commit_reference_gently(sha1_tmp, 0);
        if (!two)
                return -1;
 -      mbs = get_merge_bases(one, two, 1);
 +      mbs = get_merge_bases(one, two);
        if (!mbs || mbs->next)
                st = -1;
        else {
@@@ -1248,7 -1242,10 +1248,7 @@@ static void diagnose_invalid_sha1_path(
                die("Path '%s' exists on disk, but not in '%.*s'.",
                    filename, object_name_len, object_name);
        if (errno == ENOENT || errno == ENOTDIR) {
 -              char *fullname = xmalloc(strlen(filename)
 -                                           + strlen(prefix) + 1);
 -              strcpy(fullname, prefix);
 -              strcat(fullname, filename);
 +              char *fullname = xstrfmt("%s%s", prefix, filename);
  
                if (!get_tree_entry(tree_sha1, fullname,
                                    sha1, &mode)) {
@@@ -1372,7 -1369,6 +1372,7 @@@ static int get_sha1_with_context_1(cons
                if (!only_to_die && namelen > 2 && name[1] == '/') {
                        struct commit_list *list = NULL;
                        for_each_ref(handle_one_ref, &list);
 +                      commit_list_sort_by_date(&list);
                        return get_sha1_oneline(name + 2, sha1, list);
                }
                if (namelen < 3 ||
                        namelen = strlen(cp);
                }
  
 -              strncpy(oc->path, cp,
 -                      sizeof(oc->path));
 -              oc->path[sizeof(oc->path)-1] = '\0';
 +              strlcpy(oc->path, cp, sizeof(oc->path));
  
                if (!active_cache)
                        read_cache();
                                                           name, len);
                        }
                        hashcpy(oc->tree, tree_sha1);
 -                      strncpy(oc->path, filename,
 -                              sizeof(oc->path));
 -                      oc->path[sizeof(oc->path)-1] = '\0';
 +                      strlcpy(oc->path, filename, sizeof(oc->path));
  
                        free(new_filename);
                        return ret;
diff --combined transport.c
index 08bcd3a4eba42d2e72b9d84ec89e190c763300fa,7ac0df21ead112b1e2b98117c13a5516d16a4bbd..862f63f4e1a5250d3d701699e4c5bb4e71a6b2e9
@@@ -117,7 -117,7 +117,7 @@@ static void insert_packed_refs(const ch
                        return;
                }
  
-               if (hexval(buffer[0]) > 0xf)
+               if (!isxdigit(buffer[0]))
                        continue;
                len = strlen(buffer);
                if (len && buffer[len - 1] == '\n')
@@@ -168,8 -168,7 +168,8 @@@ static void set_upstreams(struct transp
                /* Follow symbolic refs (mainly for HEAD). */
                localname = ref->peer_ref->name;
                remotename = ref->name;
 -              tmp = resolve_ref_unsafe(localname, sha, 1, &flag);
 +              tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
 +                                       sha, &flag);
                if (tmp && flag & REF_ISSYMREF &&
                        starts_with(tmp, "refs/heads/"))
                        localname = tmp;
  
  static const char *rsync_url(const char *url)
  {
 -      return !starts_with(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
 +      if (!starts_with(url, "rsync://"))
 +              skip_prefix(url, "rsync:", &url);
 +      return url;
  }
  
  static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
  {
        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
        struct ref dummy = {NULL}, *tail = &dummy;
 -      struct child_process rsync;
 +      struct child_process rsync = CHILD_PROCESS_INIT;
        const char *args[5];
        int temp_dir_len;
  
        strbuf_addstr(&buf, rsync_url(transport->url));
        strbuf_addstr(&buf, "/refs");
  
 -      memset(&rsync, 0, sizeof(rsync));
        rsync.argv = args;
        rsync.stdout_to_stderr = 1;
        args[0] = "rsync";
  static int fetch_objs_via_rsync(struct transport *transport,
                                int nr_objs, struct ref **to_fetch)
  {
 -      struct strbuf buf = STRBUF_INIT;
 -      struct child_process rsync;
 -      const char *args[8];
 -      int result;
 +      struct child_process rsync = CHILD_PROCESS_INIT;
  
 -      strbuf_addstr(&buf, rsync_url(transport->url));
 -      strbuf_addstr(&buf, "/objects/");
 -
 -      memset(&rsync, 0, sizeof(rsync));
 -      rsync.argv = args;
        rsync.stdout_to_stderr = 1;
 -      args[0] = "rsync";
 -      args[1] = (transport->verbose > 1) ? "-rv" : "-r";
 -      args[2] = "--ignore-existing";
 -      args[3] = "--exclude";
 -      args[4] = "info";
 -      args[5] = buf.buf;
 -      args[6] = get_object_directory();
 -      args[7] = NULL;
 +      argv_array_push(&rsync.args, "rsync");
 +      argv_array_push(&rsync.args, (transport->verbose > 1) ? "-rv" : "-r");
 +      argv_array_push(&rsync.args, "--ignore-existing");
 +      argv_array_push(&rsync.args, "--exclude");
 +      argv_array_push(&rsync.args, "info");
 +      argv_array_pushf(&rsync.args, "%s/objects/", rsync_url(transport->url));
 +      argv_array_push(&rsync.args, get_object_directory());
  
        /* NEEDSWORK: handle one level of alternates */
 -      result = run_command(&rsync);
 -
 -      strbuf_release(&buf);
 -
 -      return result;
 +      return run_command(&rsync);
  }
  
  static int write_one_ref(const char *name, const unsigned char *sha1,
@@@ -326,7 -337,7 +326,7 @@@ static int rsync_transport_push(struct 
  {
        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
        int result = 0, i;
 -      struct child_process rsync;
 +      struct child_process rsync = CHILD_PROCESS_INIT;
        const char *args[10];
  
        if (flags & TRANSPORT_PUSH_MIRROR)
        strbuf_addstr(&buf, rsync_url(transport->url));
        strbuf_addch(&buf, '/');
  
 -      memset(&rsync, 0, sizeof(rsync));
        rsync.argv = args;
        rsync.stdout_to_stderr = 1;
        i = 0;
@@@ -478,9 -490,6 +478,9 @@@ static int set_git_option(struct git_tr
                                die("transport: invalid depth option '%s'", value);
                }
                return 0;
 +      } else if (!strcmp(name, TRANS_OPT_PUSH_CERT)) {
 +              opts->push_cert = !!value;
 +              return 0;
        }
        return 1;
  }
@@@ -744,7 -753,7 +744,7 @@@ void transport_print_push_status(const 
        unsigned char head_sha1[20];
        char *head;
  
 -      head = resolve_refdup("HEAD", head_sha1, 1, NULL);
 +      head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
  
        if (verbose) {
                for (ref = refs; ref; ref = ref->next)
                        *reject_reasons |= REJECT_NEEDS_FORCE;
                }
        }
 +      free(head);
  }
  
  void transport_verify_remote_names(int nr_heads, const char **heads)
@@@ -825,8 -833,6 +825,8 @@@ static int git_transport_push(struct tr
        args.progress = transport->progress;
        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
        args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
 +      args.push_cert = !!(flags & TRANSPORT_PUSH_CERT);
 +      args.url = transport->url;
  
        ret = send_pack(&args, data->fd, data->conn, remote_refs,
                        &data->extra_have);
@@@ -971,7 -977,9 +971,7 @@@ struct transport *transport_get(struct 
        } else {
                /* Unknown protocol in URL. Pass to external handler. */
                int len = external_specification_len(url);
 -              char *handler = xmalloc(len + 1);
 -              handler[len] = 0;
 -              strncpy(handler, url, len);
 +              char *handler = xmemdupz(url, len);
                transport_helper_init(ret, handler);
        }
  
@@@ -1058,7 -1066,7 +1058,7 @@@ static int run_pre_push_hook(struct tra
  {
        int ret = 0, x;
        struct ref *r;
 -      struct child_process proc;
 +      struct child_process proc = CHILD_PROCESS_INIT;
        struct strbuf buf;
        const char *argv[4];
  
        argv[2] = transport->url;
        argv[3] = NULL;
  
 -      memset(&proc, 0, sizeof(proc));
        proc.argv = argv;
        proc.in = -1;
  
@@@ -1177,8 -1186,10 +1177,8 @@@ int transport_push(struct transport *tr
                if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
                              TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
                        struct ref *ref = remote_refs;
 -                      struct string_list needs_pushing;
 +                      struct string_list needs_pushing = STRING_LIST_INIT_DUP;
  
 -                      memset(&needs_pushing, 0, sizeof(struct string_list));
 -                      needs_pushing.strdup_strings = 1;
                        for (; ref; ref = ref->next)
                                if (!is_null_sha1(ref->new_sha1) &&
                                    find_unpushed_submodules(ref->new_sha1,
@@@ -1358,11 -1369,11 +1358,11 @@@ static int refs_from_alternate_cb(struc
        while (other[len-1] == '/')
                other[--len] = '\0';
        if (len < 8 || memcmp(other + len - 8, "/objects", 8))
 -              return 0;
 +              goto out;
        /* Is this a git repository with refs? */
        memcpy(other + len - 8, "/refs", 6);
        if (!is_directory(other))
 -              return 0;
 +              goto out;
        other[len - 8] = '\0';
        remote = remote_get(other);
        transport = transport_get(remote, other);
             extra = extra->next)
                cb->fn(extra, cb->data);
        transport_disconnect(transport);
 +out:
        free(other);
        return 0;
  }