Merge branch 'en/unicode-in-refnames'
authorJunio C Hamano <gitster@pobox.com>
Sun, 19 May 2019 07:45:30 +0000 (16:45 +0900)
committerJunio C Hamano <gitster@pobox.com>
Sun, 19 May 2019 07:45:30 +0000 (16:45 +0900)
On a filesystem like HFS+, the names of the refs stored as filesystem
entities may become different from what the end-user expects, just
like files in the working tree get "renamed". Work around the
mismatch by paying attention to the core.precomposeUnicode
configuration.

* en/unicode-in-refnames:
Honor core.precomposeUnicode in more places

1  2 
builtin/show-ref.c
upload-pack.c
diff --combined builtin/show-ref.c
index 6a706c02a6f9782ee4abb289cfafe468253aca1f,07ff311b46aee82f0cf5521d3b380a4d64d873e3..6456da70cc2c4d6a86c2466c483dd815b15bbb45
@@@ -1,5 -1,6 +1,6 @@@
  #include "builtin.h"
  #include "cache.h"
+ #include "config.h"
  #include "refs.h"
  #include "object-store.h"
  #include "object.h"
@@@ -23,7 -24,7 +24,7 @@@ static void show_one(const char *refnam
        const char *hex;
        struct object_id peeled;
  
 -      if (!has_sha1_file(oid->hash))
 +      if (!has_object_file(oid))
                die("git show-ref: bad ref %s (%s)", refname,
                    oid_to_hex(oid));
  
@@@ -182,6 -183,8 +183,8 @@@ static const struct option show_ref_opt
  
  int cmd_show_ref(int argc, const char **argv, const char *prefix)
  {
+       git_config(git_default_config, NULL);
        argc = parse_options(argc, argv, prefix, show_ref_options,
                             show_ref_usage, 0);
  
diff --combined upload-pack.c
index d2ea5eb20d90557de4dc979d98a9479e0c2e7d9b,c9b91763018eaee16aa85aec21c5f4204c815517..24298913c0d7932e20f4677d3b8a4ea62ed69f8d
@@@ -43,6 -43,7 +43,6 @@@
  
  static timestamp_t oldest_have;
  
 -static int deepen_relative;
  static int multi_ack;
  static int no_done;
  static int use_thin_pack, use_ofs_delta, use_include_tag;
@@@ -70,8 -71,6 +70,8 @@@ static int allow_filter
  static int allow_ref_in_want;
  static struct list_objects_filter_options filter_options;
  
 +static int allow_sideband_all;
 +
  static void reset_timeout(void)
  {
        alarm(timeout);
@@@ -141,17 -140,14 +141,17 @@@ static void create_pack_file(const stru
        if (use_include_tag)
                argv_array_push(&pack_objects.args, "--include-tag");
        if (filter_options.filter_spec) {
 +              struct strbuf expanded_filter_spec = STRBUF_INIT;
 +              expand_list_objects_filter_spec(&filter_options,
 +                                              &expanded_filter_spec);
                if (pack_objects.use_shell) {
                        struct strbuf buf = STRBUF_INIT;
 -                      sq_quote_buf(&buf, filter_options.filter_spec);
 +                      sq_quote_buf(&buf, expanded_filter_spec.buf);
                        argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
                        strbuf_release(&buf);
                } else {
                        argv_array_pushf(&pack_objects.args, "--filter=%s",
 -                                       filter_options.filter_spec);
 +                                       expanded_filter_spec.buf);
                }
        }
  
@@@ -358,8 -354,7 +358,8 @@@ static int ok_to_give_up(const struct o
                                            min_generation);
  }
  
 -static int get_common_commits(struct object_array *have_obj,
 +static int get_common_commits(struct packet_reader *reader,
 +                            struct object_array *have_obj,
                              struct object_array *want_obj)
  {
        struct object_id oid;
        save_commit_buffer = 0;
  
        for (;;) {
 -              char *line = packet_read_line(0, NULL);
                const char *arg;
  
                reset_timeout();
  
 -              if (!line) {
 +              if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
                        if (multi_ack == 2 && got_common
                            && !got_other && ok_to_give_up(have_obj, want_obj)) {
                                sent_ready = 1;
                        got_other = 0;
                        continue;
                }
 -              if (skip_prefix(line, "have ", &arg)) {
 +              if (skip_prefix(reader->line, "have ", &arg)) {
                        switch (got_oid(arg, &oid, have_obj)) {
                        case -1: /* they have what we do not */
                                got_other = 1;
                        }
                        continue;
                }
 -              if (!strcmp(line, "done")) {
 +              if (!strcmp(reader->line, "done")) {
                        if (have_obj->nr > 0) {
                                if (multi_ack)
                                        packet_write_fmt(1, "ACK %s\n", last_hex);
                        packet_write_fmt(1, "NAK\n");
                        return -1;
                }
 -              die("git upload-pack: expected SHA1 list, got '%s'", line);
 +              die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
        }
  }
  
@@@ -592,8 -588,7 +592,8 @@@ error
        return 1;
  }
  
 -static void check_non_tip(struct object_array *want_obj)
 +static void check_non_tip(struct object_array *want_obj,
 +                        struct packet_writer *writer)
  {
        int i;
  
@@@ -612,24 -607,19 +612,24 @@@ error
        /* Pick one of them (we know there at least is one) */
        for (i = 0; i < want_obj->nr; i++) {
                struct object *o = want_obj->objects[i].item;
 -              if (!is_our_ref(o))
 +              if (!is_our_ref(o)) {
 +                      packet_writer_error(writer,
 +                                          "upload-pack: not our ref %s",
 +                                          oid_to_hex(&o->oid));
                        die("git upload-pack: not our ref %s",
                            oid_to_hex(&o->oid));
 +              }
        }
  }
  
 -static void send_shallow(struct commit_list *result)
 +static void send_shallow(struct packet_writer *writer,
 +                       struct commit_list *result)
  {
        while (result) {
                struct object *object = &result->item->object;
                if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
 -                      packet_write_fmt(1, "shallow %s",
 -                                       oid_to_hex(&object->oid));
 +                      packet_writer_write(writer, "shallow %s",
 +                                          oid_to_hex(&object->oid));
                        register_shallow(the_repository, &object->oid);
                        shallow_nr++;
                }
        }
  }
  
 -static void send_unshallow(const struct object_array *shallows,
 +static void send_unshallow(struct packet_writer *writer,
 +                         const struct object_array *shallows,
                           struct object_array *want_obj)
  {
        int i;
                struct object *object = shallows->objects[i].item;
                if (object->flags & NOT_SHALLOW) {
                        struct commit_list *parents;
 -                      packet_write_fmt(1, "unshallow %s",
 -                                       oid_to_hex(&object->oid));
 +                      packet_writer_write(writer, "unshallow %s",
 +                                          oid_to_hex(&object->oid));
                        object->flags &= ~CLIENT_SHALLOW;
                        /*
                         * We want to _register_ "object" as shallow, but we
        }
  }
  
 -static void deepen(int depth, int deepen_relative,
 +static int check_ref(const char *refname_full, const struct object_id *oid,
 +                   int flag, void *cb_data);
 +static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
                   struct object_array *shallows, struct object_array *want_obj)
  {
        if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
                struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
                struct commit_list *result;
  
 +              /*
 +               * Checking for reachable shallows requires that our refs be
 +               * marked with OUR_REF.
 +               */
 +              head_ref_namespaced(check_ref, NULL);
 +              for_each_namespaced_ref(check_ref, NULL);
 +
                get_reachable_list(shallows, &reachable_shallows);
                result = get_shallow_commits(&reachable_shallows,
                                             depth + 1,
                                             SHALLOW, NOT_SHALLOW);
 -              send_shallow(result);
 +              send_shallow(writer, result);
                free_commit_list(result);
                object_array_clear(&reachable_shallows);
        } else {
  
                result = get_shallow_commits(want_obj, depth,
                                             SHALLOW, NOT_SHALLOW);
 -              send_shallow(result);
 +              send_shallow(writer, result);
                free_commit_list(result);
        }
  
 -      send_unshallow(shallows, want_obj);
 +      send_unshallow(writer, shallows, want_obj);
  }
  
 -static void deepen_by_rev_list(int ac, const char **av,
 +static void deepen_by_rev_list(struct packet_writer *writer, int ac,
 +                             const char **av,
                               struct object_array *shallows,
                               struct object_array *want_obj)
  {
  
        close_commit_graph(the_repository);
        result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
 -      send_shallow(result);
 +      send_shallow(writer, result);
        free_commit_list(result);
 -      send_unshallow(shallows, want_obj);
 +      send_unshallow(writer, shallows, want_obj);
  }
  
  /* Returns 1 if a shallow list is sent or 0 otherwise */
 -static int send_shallow_list(int depth, int deepen_rev_list,
 +static int send_shallow_list(struct packet_writer *writer,
 +                           int depth, int deepen_rev_list,
                             timestamp_t deepen_since,
                             struct string_list *deepen_not,
 +                           int deepen_relative,
                             struct object_array *shallows,
                             struct object_array *want_obj)
  {
        if (depth > 0 && deepen_rev_list)
                die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
        if (depth > 0) {
 -              deepen(depth, deepen_relative, shallows, want_obj);
 +              deepen(writer, depth, deepen_relative, shallows, want_obj);
                ret = 1;
        } else if (deepen_rev_list) {
                struct argv_array av = ARGV_ARRAY_INIT;
                        struct object *o = want_obj->objects[i].item;
                        argv_array_push(&av, oid_to_hex(&o->oid));
                }
 -              deepen_by_rev_list(av.argc, av.argv, shallows, want_obj);
 +              deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
                argv_array_clear(&av);
                ret = 1;
        } else {
@@@ -839,7 -816,7 +839,7 @@@ static int process_deepen_not(const cha
        if (skip_prefix(line, "deepen-not ", &arg)) {
                char *ref = NULL;
                struct object_id oid;
 -              if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
 +              if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
                        die("git upload-pack: ambiguous deepen-not: %s", line);
                string_list_append(deepen_not, ref);
                free(ref);
        return 0;
  }
  
 -static void receive_needs(struct object_array *want_obj)
 +static void receive_needs(struct packet_reader *reader, struct object_array *want_obj)
  {
        struct object_array shallows = OBJECT_ARRAY_INIT;
        struct string_list deepen_not = STRING_LIST_INIT_DUP;
        int has_non_tip = 0;
        timestamp_t deepen_since = 0;
        int deepen_rev_list = 0;
 +      int deepen_relative = 0;
 +      struct packet_writer writer;
  
        shallow_nr = 0;
 +      packet_writer_init(&writer, 1);
        for (;;) {
                struct object *o;
                const char *features;
                struct object_id oid_buf;
 -              char *line = packet_read_line(0, NULL);
                const char *arg;
  
                reset_timeout();
 -              if (!line)
 +              if (packet_reader_read(reader) != PACKET_READ_NORMAL)
                        break;
  
 -              if (process_shallow(line, &shallows))
 +              if (process_shallow(reader->line, &shallows))
                        continue;
 -              if (process_deepen(line, &depth))
 +              if (process_deepen(reader->line, &depth))
                        continue;
 -              if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
 +              if (process_deepen_since(reader->line, &deepen_since, &deepen_rev_list))
                        continue;
 -              if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
 +              if (process_deepen_not(reader->line, &deepen_not, &deepen_rev_list))
                        continue;
  
 -              if (skip_prefix(line, "filter ", &arg)) {
 +              if (skip_prefix(reader->line, "filter ", &arg)) {
                        if (!filter_capability_requested)
                                die("git upload-pack: filtering capability not negotiated");
                        parse_list_objects_filter(&filter_options, arg);
                        continue;
                }
  
 -              if (!skip_prefix(line, "want ", &arg) ||
 +              if (!skip_prefix(reader->line, "want ", &arg) ||
                    parse_oid_hex(arg, &oid_buf, &features))
                        die("git upload-pack: protocol error, "
 -                          "expected to get object ID, not '%s'", line);
 +                          "expected to get object ID, not '%s'", reader->line);
  
                if (parse_feature_request(features, "deepen-relative"))
                        deepen_relative = 1;
  
                o = parse_object(the_repository, &oid_buf);
                if (!o) {
 -                      packet_write_fmt(1,
 -                                       "ERR upload-pack: not our ref %s",
 -                                       oid_to_hex(&oid_buf));
 +                      packet_writer_error(&writer,
 +                                          "upload-pack: not our ref %s",
 +                                          oid_to_hex(&oid_buf));
                        die("git upload-pack: not our ref %s",
                            oid_to_hex(&oid_buf));
                }
         * by another process that handled the initial request.
         */
        if (has_non_tip)
 -              check_non_tip(want_obj);
 +              check_non_tip(want_obj, &writer);
  
        if (!use_sideband && daemon_mode)
                no_progress = 1;
        if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
                return;
  
 -      if (send_shallow_list(depth, deepen_rev_list, deepen_since,
 -                            &deepen_not, &shallows, want_obj))
 +      if (send_shallow_list(&writer, depth, deepen_rev_list, deepen_since,
 +                            &deepen_not, deepen_relative, &shallows,
 +                            want_obj))
                packet_flush(1);
        object_array_clear(&shallows);
  }
@@@ -1067,8 -1041,8 +1067,10 @@@ static int upload_pack_config(const cha
                allow_filter = git_config_bool(var, value);
        } else if (!strcmp("uploadpack.allowrefinwant", var)) {
                allow_ref_in_want = git_config_bool(var, value);
 +      } else if (!strcmp("uploadpack.allowsidebandall", var)) {
 +              allow_sideband_all = git_config_bool(var, value);
+       } else if (!strcmp("core.precomposeunicode", var)) {
+               precomposed_unicode = git_config_bool(var, value);
        }
  
        if (current_config_scope() != CONFIG_SCOPE_REPO) {
@@@ -1083,7 -1057,6 +1085,7 @@@ void upload_pack(struct upload_pack_opt
  {
        struct string_list symref = STRING_LIST_INIT_DUP;
        struct object_array want_obj = OBJECT_ARRAY_INIT;
 +      struct packet_reader reader;
  
        stateless_rpc = options->stateless_rpc;
        timeout = options->timeout;
        if (options->advertise_refs)
                return;
  
 -      receive_needs(&want_obj);
 +      packet_reader_init(&reader, 0, NULL, 0,
 +                         PACKET_READ_CHOMP_NEWLINE |
 +                         PACKET_READ_DIE_ON_ERR_PACKET);
 +
 +      receive_needs(&reader, &want_obj);
        if (want_obj.nr) {
                struct object_array have_obj = OBJECT_ARRAY_INIT;
 -              get_common_commits(&have_obj, &want_obj);
 +              get_common_commits(&reader, &have_obj, &want_obj);
                create_pack_file(&have_obj, &want_obj);
        }
  }
@@@ -1131,8 -1100,6 +1133,8 @@@ struct upload_pack_data 
        int deepen_rev_list;
        int deepen_relative;
  
 +      struct packet_writer writer;
 +
        unsigned stateless_rpc : 1;
  
        unsigned use_thin_pack : 1;
@@@ -1156,7 -1123,6 +1158,7 @@@ static void upload_pack_data_init(struc
        data->haves = haves;
        data->shallows = shallows;
        data->deepen_not = deepen_not;
 +      packet_writer_init(&data->writer, 1);
  }
  
  static void upload_pack_data_clear(struct upload_pack_data *data)
        string_list_clear(&data->deepen_not, 0);
  }
  
 -static int parse_want(const char *line, struct object_array *want_obj)
 +static int parse_want(struct packet_writer *writer, const char *line,
 +                    struct object_array *want_obj)
  {
        const char *arg;
        if (skip_prefix(line, "want ", &arg)) {
  
                o = parse_object(the_repository, &oid);
                if (!o) {
 -                      packet_write_fmt(1,
 -                                       "ERR upload-pack: not our ref %s",
 -                                       oid_to_hex(&oid));
 +                      packet_writer_error(writer,
 +                                          "upload-pack: not our ref %s",
 +                                          oid_to_hex(&oid));
                        die("git upload-pack: not our ref %s",
                            oid_to_hex(&oid));
                }
        return 0;
  }
  
 -static int parse_want_ref(const char *line, struct string_list *wanted_refs,
 +static int parse_want_ref(struct packet_writer *writer, const char *line,
 +                        struct string_list *wanted_refs,
                          struct object_array *want_obj)
  {
        const char *arg;
                struct object *o;
  
                if (read_ref(arg, &oid)) {
 -                      packet_write_fmt(1, "ERR unknown ref %s", arg);
 +                      packet_writer_error(writer, "unknown ref %s", arg);
                        die("unknown ref %s", arg);
                }
  
@@@ -1254,11 -1218,10 +1256,11 @@@ static void process_args(struct packet_
                const char *p;
  
                /* process want */
 -              if (parse_want(arg, want_obj))
 +              if (parse_want(&data->writer, arg, want_obj))
                        continue;
                if (allow_ref_in_want &&
 -                  parse_want_ref(arg, &data->wanted_refs, want_obj))
 +                  parse_want_ref(&data->writer, arg, &data->wanted_refs,
 +                                 want_obj))
                        continue;
                /* process have line */
                if (parse_have(arg, &data->haves))
                        continue;
                }
  
 +              if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
 +                   allow_sideband_all) &&
 +                  !strcmp(arg, "sideband-all")) {
 +                      data->writer.use_sideband = 1;
 +                      continue;
 +              }
 +
                /* ignore unknown lines maybe? */
                die("unexpected line: '%s'", arg);
        }
@@@ -1359,26 -1315,26 +1361,26 @@@ static int process_haves(struct oid_arr
        return 0;
  }
  
 -static int send_acks(struct oid_array *acks, struct strbuf *response,
 +static int send_acks(struct packet_writer *writer, struct oid_array *acks,
                     const struct object_array *have_obj,
                     struct object_array *want_obj)
  {
        int i;
  
 -      packet_buf_write(response, "acknowledgments\n");
 +      packet_writer_write(writer, "acknowledgments\n");
  
        /* Send Acks */
        if (!acks->nr)
 -              packet_buf_write(response, "NAK\n");
 +              packet_writer_write(writer, "NAK\n");
  
        for (i = 0; i < acks->nr; i++) {
 -              packet_buf_write(response, "ACK %s\n",
 -                               oid_to_hex(&acks->oid[i]));
 +              packet_writer_write(writer, "ACK %s\n",
 +                                  oid_to_hex(&acks->oid[i]));
        }
  
        if (ok_to_give_up(have_obj, want_obj)) {
                /* Send Ready */
 -              packet_buf_write(response, "ready\n");
 +              packet_writer_write(writer, "ready\n");
                return 1;
        }
  
@@@ -1390,20 -1346,25 +1392,20 @@@ static int process_haves_and_send_acks(
                                       struct object_array *want_obj)
  {
        struct oid_array common = OID_ARRAY_INIT;
 -      struct strbuf response = STRBUF_INIT;
        int ret = 0;
  
        process_haves(&data->haves, &common, have_obj);
        if (data->done) {
                ret = 1;
 -      } else if (send_acks(&common, &response, have_obj, want_obj)) {
 -              packet_buf_delim(&response);
 +      } else if (send_acks(&data->writer, &common, have_obj, want_obj)) {
 +              packet_writer_delim(&data->writer);
                ret = 1;
        } else {
                /* Add Flush */
 -              packet_buf_flush(&response);
 +              packet_writer_flush(&data->writer);
                ret = 0;
        }
  
 -      /* Send response */
 -      write_or_die(1, response.buf, response.len);
 -      strbuf_release(&response);
 -
        oid_array_clear(&data->haves);
        oid_array_clear(&common);
        return ret;
@@@ -1416,15 -1377,15 +1418,15 @@@ static void send_wanted_ref_info(struc
        if (!data->wanted_refs.nr)
                return;
  
 -      packet_write_fmt(1, "wanted-refs\n");
 +      packet_writer_write(&data->writer, "wanted-refs\n");
  
        for_each_string_list_item(item, &data->wanted_refs) {
 -              packet_write_fmt(1, "%s %s\n",
 -                               oid_to_hex(item->util),
 -                               item->string);
 +              packet_writer_write(&data->writer, "%s %s\n",
 +                                  oid_to_hex(item->util),
 +                                  item->string);
        }
  
 -      packet_delim(1);
 +      packet_writer_delim(&data->writer);
  }
  
  static void send_shallow_info(struct upload_pack_data *data,
            !is_repository_shallow(the_repository))
                return;
  
 -      packet_write_fmt(1, "shallow-info\n");
 +      packet_writer_write(&data->writer, "shallow-info\n");
  
 -      if (!send_shallow_list(data->depth, data->deepen_rev_list,
 +      if (!send_shallow_list(&data->writer, data->depth,
 +                             data->deepen_rev_list,
                               data->deepen_since, &data->deepen_not,
 +                             data->deepen_relative,
                               &data->shallows, want_obj) &&
            is_repository_shallow(the_repository))
 -              deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows,
 -                     want_obj);
 +              deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
 +                     &data->shallows, want_obj);
  
        packet_delim(1);
  }
@@@ -1506,7 -1465,7 +1508,7 @@@ int upload_pack_v2(struct repository *r
                        send_wanted_ref_info(&data);
                        send_shallow_info(&data, &want_obj);
  
 -                      packet_write_fmt(1, "packfile\n");
 +                      packet_writer_write(&data.writer, "packfile\n");
                        create_pack_file(&have_obj, &want_obj);
                        state = FETCH_DONE;
                        break;
@@@ -1527,7 -1486,6 +1529,7 @@@ int upload_pack_advertise(struct reposi
        if (value) {
                int allow_filter_value;
                int allow_ref_in_want;
 +              int allow_sideband_all_value;
  
                strbuf_addstr(value, "shallow");
  
                                         &allow_ref_in_want) &&
                    allow_ref_in_want)
                        strbuf_addstr(value, " ref-in-want");
 +
 +              if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
 +                  (!repo_config_get_bool(the_repository,
 +                                         "uploadpack.allowsidebandall",
 +                                         &allow_sideband_all_value) &&
 +                   allow_sideband_all_value))
 +                      strbuf_addstr(value, " sideband-all");
        }
  
        return 1;