Merge branch 'bg/xcalloc-nmemb-then-size'
authorJunio C Hamano <gitster@pobox.com>
Mon, 16 Jun 2014 19:17:50 +0000 (12:17 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 Jun 2014 19:17:50 +0000 (12:17 -0700)
Like calloc(3), xcalloc() takes nmemb and then size.

* bg/xcalloc-nmemb-then-size:
transport-helper.c: rearrange xcalloc arguments
remote.c: rearrange xcalloc arguments
reflog-walk.c: rearrange xcalloc arguments
pack-revindex.c: rearrange xcalloc arguments
notes.c: rearrange xcalloc arguments
imap-send.c: rearrange xcalloc arguments
http-push.c: rearrange xcalloc arguments
diff.c: rearrange xcalloc arguments
config.c: rearrange xcalloc arguments
commit.c: rearrange xcalloc arguments
builtin/remote.c: rearrange xcalloc arguments
builtin/ls-remote.c: rearrange xcalloc arguments

1  2 
config.c
diff.c
imap-send.c
transport-helper.c
diff --combined config.c
index 9319aa12f3e6a2cb5b82a5466382f083c1bcfc35,6bd3575c118dc8582ab2bf59a47998d239da7a7f..3e21fecb6a9424dc97a26939082769f562a0aecf
+++ b/config.c
@@@ -147,6 -147,12 +147,6 @@@ int git_config_include(const char *var
        return ret;
  }
  
 -static void lowercase(char *p)
 -{
 -      for (; *p; p++)
 -              *p = tolower(*p);
 -}
 -
  void git_config_push_parameter(const char *text)
  {
        struct strbuf env = STRBUF_INIT;
@@@ -174,7 -180,7 +174,7 @@@ int git_config_parse_parameter(const ch
                strbuf_list_free(pair);
                return error("bogus config parameter: %s", text);
        }
 -      lowercase(pair[0]->buf);
 +      strbuf_tolower(pair[0]);
        if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) {
                strbuf_list_free(pair);
                return -1;
@@@ -820,16 -826,9 +820,16 @@@ static int git_default_core_config(cons
        if (!strcmp(var, "core.commentchar")) {
                const char *comment;
                int ret = git_config_string(&comment, var, value);
 -              if (!ret)
 +              if (ret)
 +                      return ret;
 +              else if (!strcasecmp(comment, "auto"))
 +                      auto_comment_line_char = 1;
 +              else if (comment[0] && !comment[1]) {
                        comment_line_char = comment[0];
 -              return ret;
 +                      auto_comment_line_char = 0;
 +              } else
 +                      return error("core.commentChar should only be one character");
 +              return 0;
        }
  
        if (!strcmp(var, "core.askpass"))
@@@ -1539,7 -1538,7 +1539,7 @@@ int git_config_set_multivar_in_file(con
         * The lock serves a purpose in addition to locking: the new
         * contents of .git/config will be written into it.
         */
-       lock = xcalloc(sizeof(struct lock_file), 1);
+       lock = xcalloc(1, sizeof(struct lock_file));
        fd = hold_lock_file_for_update(lock, config_filename, 0);
        if (fd < 0) {
                error("could not lock config file %s: %s", config_filename, strerror(errno));
                        MAP_PRIVATE, in_fd, 0);
                close(in_fd);
  
 +              if (fchmod(fd, st.st_mode & 07777) < 0) {
 +                      error("fchmod on %s failed: %s",
 +                              lock->filename, strerror(errno));
 +                      ret = CONFIG_NO_WRITE;
 +                      goto out_free;
 +              }
 +
                if (store.seen == 0)
                        store.seen = 1;
  
@@@ -1792,7 -1784,6 +1792,7 @@@ int git_config_rename_section_in_file(c
        int out_fd;
        char buf[1024];
        FILE *config_file;
 +      struct stat st;
  
        if (new_name && !section_name_is_ok(new_name)) {
                ret = error("invalid section name: %s", new_name);
        if (!config_filename)
                config_filename = filename_buf = git_pathdup("config");
  
-       lock = xcalloc(sizeof(struct lock_file), 1);
+       lock = xcalloc(1, sizeof(struct lock_file));
        out_fd = hold_lock_file_for_update(lock, config_filename, 0);
        if (out_fd < 0) {
                ret = error("could not lock config file %s", config_filename);
                goto unlock_and_out;
        }
  
 +      fstat(fileno(config_file), &st);
 +
 +      if (fchmod(out_fd, st.st_mode & 07777) < 0) {
 +              ret = error("fchmod on %s failed: %s",
 +                              lock->filename, strerror(errno));
 +              goto out;
 +      }
 +
        while (fgets(buf, sizeof(buf), config_file)) {
                int i;
                int length;
diff --combined diff.c
index de25819e4edede0a24ee30b202dee478165b4fed,146521e1947e36d010524dece848bfb03edfac24..bba9a558a60a490b7edd6dec370181a0941bf985
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -1361,7 -1361,7 +1361,7 @@@ static struct diffstat_file *diffstat_a
                                          const char *name_b)
  {
        struct diffstat_file *x;
-       x = xcalloc(sizeof (*x), 1);
+       x = xcalloc(1, sizeof(*x));
        ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
        diffstat->files[diffstat->nr++] = x;
        if (name_b) {
@@@ -2880,16 -2880,6 +2880,16 @@@ static struct diff_tempfile *prepare_te
        return temp;
  }
  
 +static void add_external_diff_name(struct argv_array *argv,
 +                                 const char *name,
 +                                 struct diff_filespec *df)
 +{
 +      struct diff_tempfile *temp = prepare_temp_file(name, df);
 +      argv_array_push(argv, temp->name);
 +      argv_array_push(argv, temp->hex);
 +      argv_array_push(argv, temp->mode);
 +}
 +
  /* An external diff command takes:
   *
   * diff-cmd name infile1 infile1-sha1 infile1-mode \
@@@ -2906,32 -2896,48 +2906,32 @@@ static void run_external_diff(const cha
                              struct diff_options *o)
  {
        struct argv_array argv = ARGV_ARRAY_INIT;
 -      int retval;
 +      struct argv_array env = ARGV_ARRAY_INIT;
        struct diff_queue_struct *q = &diff_queued_diff;
 -      const char *env[3] = { NULL };
 -      char env_counter[50];
 -      char env_total[50];
 +
 +      argv_array_push(&argv, pgm);
 +      argv_array_push(&argv, name);
  
        if (one && two) {
 -              struct diff_tempfile *temp_one, *temp_two;
 -              const char *othername = (other ? other : name);
 -              temp_one = prepare_temp_file(name, one);
 -              temp_two = prepare_temp_file(othername, two);
 -              argv_array_push(&argv, pgm);
 -              argv_array_push(&argv, name);
 -              argv_array_push(&argv, temp_one->name);
 -              argv_array_push(&argv, temp_one->hex);
 -              argv_array_push(&argv, temp_one->mode);
 -              argv_array_push(&argv, temp_two->name);
 -              argv_array_push(&argv, temp_two->hex);
 -              argv_array_push(&argv, temp_two->mode);
 -              if (other) {
 +              add_external_diff_name(&argv, name, one);
 +              if (!other)
 +                      add_external_diff_name(&argv, name, two);
 +              else {
 +                      add_external_diff_name(&argv, other, two);
                        argv_array_push(&argv, other);
                        argv_array_push(&argv, xfrm_msg);
                }
 -      } else {
 -              argv_array_push(&argv, pgm);
 -              argv_array_push(&argv, name);
        }
 -      fflush(NULL);
  
 -      env[0] = env_counter;
 -      snprintf(env_counter, sizeof(env_counter), "GIT_DIFF_PATH_COUNTER=%d",
 -               ++o->diff_path_counter);
 -      env[1] = env_total;
 -      snprintf(env_total, sizeof(env_total), "GIT_DIFF_PATH_TOTAL=%d", q->nr);
 +      argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
 +      argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
 +
 +      if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv))
 +              die(_("external diff died, stopping at %s"), name);
  
 -      retval = run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env);
        remove_tempfile();
        argv_array_clear(&argv);
 -      if (retval) {
 -              fprintf(stderr, "external diff died, stopping at %s.\n", name);
 -              exit(1);
 -      }
 +      argv_array_clear(&env);
  }
  
  static int similarity_index(struct diff_filepair *p)
@@@ -3199,7 -3205,6 +3199,7 @@@ void diff_setup(struct diff_options *op
        options->context = diff_context_default;
        DIFF_OPT_SET(options, RENAME_EMPTY);
  
 +      /* pathchange left =NULL by default */
        options->change = diff_change;
        options->add_remove = diff_addremove;
        options->use_color = diff_use_color_default;
@@@ -3320,9 -3325,6 +3320,9 @@@ void diff_setup_done(struct diff_option
        }
  
        options->diff_path_counter = 0;
 +
 +      if (DIFF_OPT_TST(options, FOLLOW_RENAMES) && options->pathspec.nr != 1)
 +              die(_("--follow requires exactly one pathspec"));
  }
  
  static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
@@@ -4747,7 -4749,6 +4747,7 @@@ void diffcore_fix_diff_index(struct dif
  
  void diffcore_std(struct diff_options *options)
  {
 +      /* NOTE please keep the following in sync with diff_tree_combined() */
        if (options->skip_stat_unmatch)
                diffcore_skip_stat_unmatch(options);
        if (!options->found_follow) {
diff --combined imap-send.c
index 5c4f336330e416379a15cee75b593f0aea451025,45230e19cdd75e7424544a31a52c44bf275df55b..83a6ed2ac338d7fc82f3bed6fcb93ffe8dae5739
@@@ -23,9 -23,9 +23,9 @@@
   */
  
  #include "cache.h"
 +#include "credential.h"
  #include "exec_cmd.h"
  #include "run-command.h"
 -#include "prompt.h"
  #ifdef NO_OPENSSL
  typedef void *SSL;
  #endif
@@@ -946,13 -946,12 +946,13 @@@ static int auth_cram_md5(struct imap_st
  
  static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
  {
 +      struct credential cred = CREDENTIAL_INIT;
        struct imap_store *ctx;
        struct imap *imap;
        char *arg, *rsp;
        int s = -1, preauth;
  
-       ctx = xcalloc(sizeof(*ctx), 1);
+       ctx = xcalloc(1, sizeof(*ctx));
  
        ctx->imap = imap = xcalloc(sizeof(*imap), 1);
        imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
                }
  #endif
                imap_info("Logging in...\n");
 -              if (!srvc->user) {
 -                      fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
 -                      goto bail;
 -              }
 -              if (!srvc->pass) {
 -                      struct strbuf prompt = STRBUF_INIT;
 -                      strbuf_addf(&prompt, "Password (%s@%s): ", srvc->user, srvc->host);
 -                      arg = git_getpass(prompt.buf);
 -                      strbuf_release(&prompt);
 -                      if (!*arg) {
 -                              fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
 -                              goto bail;
 -                      }
 -                      /*
 -                       * getpass() returns a pointer to a static buffer.  make a copy
 -                       * for long term storage.
 -                       */
 -                      srvc->pass = xstrdup(arg);
 +              if (!srvc->user || !srvc->pass) {
 +                      cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
 +                      cred.host = xstrdup(srvc->host);
 +
 +                      if (srvc->user)
 +                              cred.username = xstrdup(srvc->user);
 +                      if (srvc->pass)
 +                              cred.password = xstrdup(srvc->pass);
 +
 +                      credential_fill(&cred);
 +
 +                      if (!srvc->user)
 +                              srvc->user = xstrdup(cred.username);
 +                      if (!srvc->pass)
 +                              srvc->pass = xstrdup(cred.password);
                }
 +
                if (CAP(NOLOGIN)) {
                        fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
                        goto bail;
                }
        } /* !preauth */
  
 +      if (cred.username)
 +              credential_approve(&cred);
 +      credential_clear(&cred);
 +
        ctx->prefix = "";
        return ctx;
  
  bail:
 +      if (cred.username)
 +              credential_reject(&cred);
 +      credential_clear(&cred);
 +
        imap_close_store(ctx);
        return NULL;
  }
diff --combined transport-helper.c
index d9063d714591bad2e44a4e58fdffc3bb1708157d,b9ba0416b24fb2d53b410b3b97424e9efd552b43..4cfb8f3a4acc36681774e165f92de7fbd948fde5
@@@ -101,6 -101,7 +101,6 @@@ static void do_take_over(struct transpo
  static struct child_process *get_helper(struct transport *transport)
  {
        struct helper_data *data = transport->data;
 -      struct argv_array argv = ARGV_ARRAY_INIT;
        struct strbuf buf = STRBUF_INIT;
        struct child_process *helper;
        const char **refspecs = NULL;
        helper->in = -1;
        helper->out = -1;
        helper->err = 0;
 -      argv_array_pushf(&argv, "git-remote-%s", data->name);
 -      argv_array_push(&argv, transport->remote->name);
 -      argv_array_push(&argv, remove_ext_force(transport->url));
 -      helper->argv = argv_array_detach(&argv, NULL);
 +      argv_array_pushf(&helper->args, "git-remote-%s", data->name);
 +      argv_array_push(&helper->args, transport->remote->name);
 +      argv_array_push(&helper->args, remove_ext_force(transport->url));
        helper->git_cmd = 0;
        helper->silent_exec_failure = 1;
  
@@@ -243,6 -245,7 +243,6 @@@ static int disconnect_helper(struct tra
                close(data->helper->out);
                fclose(data->out);
                res = finish_command(data->helper);
 -              argv_array_free_detached(data->helper->argv);
                free(data->helper);
                data->helper = NULL;
        }
@@@ -394,16 -397,18 +394,16 @@@ static int get_importer(struct transpor
  {
        struct child_process *helper = get_helper(transport);
        struct helper_data *data = transport->data;
 -      struct argv_array argv = ARGV_ARRAY_INIT;
        int cat_blob_fd, code;
        memset(fastimport, 0, sizeof(*fastimport));
        fastimport->in = helper->out;
 -      argv_array_push(&argv, "fast-import");
 -      argv_array_push(&argv, debug ? "--stats" : "--quiet");
 +      argv_array_push(&fastimport->args, "fast-import");
 +      argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
  
        if (data->bidi_import) {
                cat_blob_fd = xdup(helper->in);
 -              argv_array_pushf(&argv, "--cat-blob-fd=%d", cat_blob_fd);
 +              argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
        }
 -      fastimport->argv = argv.argv;
        fastimport->git_cmd = 1;
  
        code = start_command(fastimport);
@@@ -416,24 -421,30 +416,24 @@@ static int get_exporter(struct transpor
  {
        struct helper_data *data = transport->data;
        struct child_process *helper = get_helper(transport);
 -      int argc = 0, i;
 -      struct strbuf tmp = STRBUF_INIT;
 +      int i;
  
        memset(fastexport, 0, sizeof(*fastexport));
  
        /* we need to duplicate helper->in because we want to use it after
         * fastexport is done with it. */
        fastexport->out = dup(helper->in);
 -      fastexport->argv = xcalloc(6 + revlist_args->nr, sizeof(*fastexport->argv));
 -      fastexport->argv[argc++] = "fast-export";
 -      fastexport->argv[argc++] = "--use-done-feature";
 -      fastexport->argv[argc++] = data->signed_tags ?
 -              "--signed-tags=verbatim" : "--signed-tags=warn-strip";
 -      if (data->export_marks) {
 -              strbuf_addf(&tmp, "--export-marks=%s.tmp", data->export_marks);
 -              fastexport->argv[argc++] = strbuf_detach(&tmp, NULL);
 -      }
 -      if (data->import_marks) {
 -              strbuf_addf(&tmp, "--import-marks=%s", data->import_marks);
 -              fastexport->argv[argc++] = strbuf_detach(&tmp, NULL);
 -      }
 +      argv_array_push(&fastexport->args, "fast-export");
 +      argv_array_push(&fastexport->args, "--use-done-feature");
 +      argv_array_push(&fastexport->args, data->signed_tags ?
 +              "--signed-tags=verbatim" : "--signed-tags=warn-strip");
 +      if (data->export_marks)
 +              argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
 +      if (data->import_marks)
 +              argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
  
        for (i = 0; i < revlist_args->nr; i++)
 -              fastexport->argv[argc++] = revlist_args->items[i].string;
 +              argv_array_push(&fastexport->args, revlist_args->items[i].string);
  
        fastexport->git_cmd = 1;
        return start_command(fastexport);
@@@ -474,6 -485,7 +474,6 @@@ static int fetch_with_import(struct tra
  
        if (finish_command(&fastimport))
                die("Error while running fast-import");
 -      argv_array_free_detached(fastimport.argv);
  
        /*
         * The fast-import stream of a remote helper that advertises
@@@ -1014,7 -1026,7 +1014,7 @@@ static struct ref *get_refs_list(struc
  
  int transport_helper_init(struct transport *transport, const char *name)
  {
-       struct helper_data *data = xcalloc(sizeof(*data), 1);
+       struct helper_data *data = xcalloc(1, sizeof(*data));
        data->name = name;
  
        if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))