Merge branch 'nd/show-gitcomp-compilation-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Sat, 15 Dec 2018 03:24:33 +0000 (12:24 +0900)
committerJunio C Hamano <gitster@pobox.com>
Sat, 15 Dec 2018 03:24:33 +0000 (12:24 +0900)
Portability fix for a recent update to parse-options API.

* nd/show-gitcomp-compilation-fix:
parse-options: fix SunCC compiler warning

1  2 
builtin/blame.c
builtin/shortlog.c
builtin/update-index.c
parse-options.c
parse-options.h
diff --combined builtin/blame.c
index 06a7163ffe269cd43dc783c81ca6ac71b91b3614,7e880392a63262078d0dc7291d04e4fa5cf2e4e1..6d798f99392e54b4392713846652111d457787bb
@@@ -9,7 -9,6 +9,7 @@@
  #include "config.h"
  #include "color.h"
  #include "builtin.h"
 +#include "repository.h"
  #include "commit.h"
  #include "diff.h"
  #include "revision.h"
@@@ -24,7 -23,6 +24,7 @@@
  #include "line-log.h"
  #include "dir.h"
  #include "progress.h"
 +#include "object-store.h"
  #include "blame.h"
  #include "string-list.h"
  
@@@ -410,7 -408,7 +410,7 @@@ static void parse_color_fields(const ch
        }
  
        if (next == EXPECT_COLOR)
 -              die (_("must end with a color"));
 +              die(_("must end with a color"));
  
        colorfield[colorfield_nr].hop = TIME_MAX;
        string_list_clear(&l, 0);
@@@ -578,7 -576,7 +578,7 @@@ static int read_ancestry(const char *gr
                /* The format is just "Commit Parent1 Parent2 ...\n" */
                struct commit_graft *graft = read_graft_line(&buf);
                if (graft)
 -                      register_commit_graft(graft, 0);
 +                      register_commit_graft(the_repository, graft, 0);
        }
        fclose(fp);
        strbuf_release(&buf);
@@@ -732,8 -730,6 +732,8 @@@ static int blame_copy_callback(const st
  {
        int *opt = option->value;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        /*
         * -C enables copy from removed files;
         * -C -C enables copy from existing files, but only
@@@ -756,8 -752,6 +756,8 @@@ static int blame_move_callback(const st
  {
        int *opt = option->value;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        *opt |= PICKAXE_BLAME_MOVE;
  
        if (arg)
@@@ -834,7 -828,7 +834,7 @@@ int cmd_blame(int argc, const char **ar
  
        setup_default_color_by_age();
        git_config(git_blame_config, &output_option);
 -      init_revisions(&revs, NULL);
 +      repo_init_revisions(the_repository, &revs, NULL);
        revs.date_mode = blame_date_mode;
        revs.diffopt.flags.allow_textconv = 1;
        revs.diffopt.flags.follow_renames = 1;
                case PARSE_OPT_HELP:
                case PARSE_OPT_ERROR:
                        exit(129);
+               case PARSE_OPT_COMPLETE:
+                       exit(0);
                case PARSE_OPT_DONE:
                        if (ctx.argv[0])
                                dashdash_pos = ctx.cpidx;
@@@ -992,7 -988,6 +994,7 @@@ parse_done
        sb.revs = &revs;
        sb.contents_from = contents_from;
        sb.reverse = reverse;
 +      sb.repo = the_repository;
        setup_scoreboard(&sb, path, &o);
        lno = sb.num_lines;
  
                long bottom, top;
                if (parse_range_arg(range_list.items[range_i].string,
                                    nth_line_cb, &sb, lno, anchor,
 -                                  &bottom, &top, sb.path))
 +                                  &bottom, &top, sb.path, &the_index))
                        usage(blame_usage);
 -              if (lno < top || ((lno || bottom) && lno < bottom))
 +              if ((!lno && (top || bottom)) || lno < bottom)
                        die(Q_("file %s has only %lu line",
                               "file %s has only %lu lines",
                               lno), path, lno);
                if (bottom < 1)
                        bottom = 1;
 -              if (top < 1)
 +              if (top < 1 || lno < top)
                        top = lno;
                bottom--;
                range_set_append_unsafe(&ranges, bottom, top);
                find_alignment(&sb, &output_option);
                if (!*repeated_meta_color &&
                    (output_option & OUTPUT_COLOR_LINE))
 -                      strcpy(repeated_meta_color, GIT_COLOR_CYAN);
 +                      xsnprintf(repeated_meta_color,
 +                                sizeof(repeated_meta_color),
 +                                "%s", GIT_COLOR_CYAN);
        }
        if (output_option & OUTPUT_ANNOTATE_COMPAT)
                output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);
diff --combined builtin/shortlog.c
index 88f88e97b28c24f74aad6ec6cf1c637003f41a85,e9c12bd392db811669ed7d7a0c1664155eee2c6c..65cd41392c1c5e256313b10812a39bf4b848ce1b
@@@ -268,9 -268,8 +268,9 @@@ int cmd_shortlog(int argc, const char *
                         N_("Suppress commit descriptions, only provides commit count")),
                OPT_BOOL('e', "email", &log.email,
                         N_("Show the email address of each author")),
 -              { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
 -                      N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
 +              { OPTION_CALLBACK, 'w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
 +                      N_("Linewrap output"), PARSE_OPT_OPTARG,
 +                      &parse_wrap_args },
                OPT_END(),
        };
  
  
        git_config(git_default_config, NULL);
        shortlog_init(&log);
 -      init_revisions(&rev, prefix);
 +      repo_init_revisions(the_repository, &rev, prefix);
        parse_options_start(&ctx, argc, argv, prefix, options,
                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
  
                case PARSE_OPT_HELP:
                case PARSE_OPT_ERROR:
                        exit(129);
+               case PARSE_OPT_COMPLETE:
+                       exit(0);
                case PARSE_OPT_DONE:
                        goto parse_done;
                }
diff --combined builtin/update-index.c
index 31e7cce3013a3b3f358834f24848fc73b67f8e8b,9d41ba0ad4649fe2a87df2cff84dec3b7e03bdde..e19da77edcaa024cc2bffc6fa8818c2b1cf0d4b6
@@@ -268,29 -268,30 +268,29 @@@ static int process_lstat_error(const ch
  
  static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
  {
 -      int option, size;
 +      int option;
        struct cache_entry *ce;
  
        /* Was the old index entry already up-to-date? */
        if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
                return 0;
  
 -      size = cache_entry_size(len);
 -      ce = xcalloc(1, size);
 +      ce = make_empty_cache_entry(&the_index, len);
        memcpy(ce->name, path, len);
        ce->ce_flags = create_ce_flags(0);
        ce->ce_namelen = len;
        fill_stat_cache_info(ce, st);
        ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
  
 -      if (index_path(&ce->oid, path, st,
 +      if (index_path(&the_index, &ce->oid, path, st,
                       info_only ? 0 : HASH_WRITE_OBJECT)) {
 -              free(ce);
 +              discard_cache_entry(ce);
                return -1;
        }
        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
        if (add_cache_entry(ce, option)) {
 -              free(ce);
 +              discard_cache_entry(ce);
                return error("%s: cannot add to the index - missing --add option?", path);
        }
        return 0;
@@@ -401,14 -402,15 +401,14 @@@ static int process_path(const char *pat
  static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
                         const char *path, int stage)
  {
 -      int size, len, option;
 +      int len, option;
        struct cache_entry *ce;
  
        if (!verify_path(path, mode))
                return error("Invalid path '%s'", path);
  
        len = strlen(path);
 -      size = cache_entry_size(len);
 -      ce = xcalloc(1, size);
 +      ce = make_empty_cache_entry(&the_index, len);
  
        oidcpy(&ce->oid, oid);
        memcpy(ce->name, path, len);
@@@ -490,7 -492,6 +490,7 @@@ static void update_one(const char *path
  
  static void read_index_info(int nul_term_line)
  {
 +      const int hexsz = the_hash_algo->hexsz;
        struct strbuf buf = STRBUF_INIT;
        struct strbuf uq = STRBUF_INIT;
        strbuf_getline_fn getline_fn;
                mode = ul;
  
                tab = strchr(ptr, '\t');
 -              if (!tab || tab - ptr < GIT_SHA1_HEXSZ + 1)
 +              if (!tab || tab - ptr < hexsz + 1)
                        goto bad_line;
  
                if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
                        ptr = tab + 1; /* point at the head of path */
                }
  
 -              if (get_oid_hex(tab - GIT_SHA1_HEXSZ, &oid) ||
 -                      tab[-(GIT_SHA1_HEXSZ + 1)] != ' ')
 +              if (get_oid_hex(tab - hexsz, &oid) ||
 +                      tab[-(hexsz + 1)] != ' ')
                        goto bad_line;
  
                path_name = ptr;
                         * ptr[-1] points at tab,
                         * ptr[-41] is at the beginning of sha1
                         */
 -                      ptr[-(GIT_SHA1_HEXSZ + 2)] = ptr[-1] = 0;
 +                      ptr[-(hexsz + 2)] = ptr[-1] = 0;
                        if (add_cacheinfo(mode, &oid, path_name, stage))
                                die("git update-index: unable to update %s",
                                    path_name);
@@@ -598,6 -599,7 +598,6 @@@ static struct cache_entry *read_one_ent
  {
        unsigned mode;
        struct object_id oid;
 -      int size;
        struct cache_entry *ce;
  
        if (get_tree_entry(ent, path, &oid, &mode)) {
                        error("%s: not a blob in %s branch.", path, which);
                return NULL;
        }
 -      size = cache_entry_size(namelen);
 -      ce = xcalloc(1, size);
 +      ce = make_empty_cache_entry(&the_index, namelen);
  
        oidcpy(&ce->oid, &oid);
        memcpy(ce->name, path, namelen);
@@@ -669,7 -672,7 +669,7 @@@ static int unresolve_one(const char *pa
                ret = -1;
                goto free_return;
        }
 -      if (!oidcmp(&ce_2->oid, &ce_3->oid) &&
 +      if (oideq(&ce_2->oid, &ce_3->oid) &&
            ce_2->ce_mode == ce_3->ce_mode) {
                fprintf(stderr, "%s: identical in both, skipping.\n",
                        path);
        error("%s: cannot add their version to the index.", path);
        ret = -1;
   free_return:
 -      free(ce_2);
 -      free(ce_3);
 +      discard_cache_entry(ce_2);
 +      discard_cache_entry(ce_3);
        return ret;
  }
  
@@@ -748,14 -751,14 +748,14 @@@ static int do_reupdate(int ac, const ch
                int save_nr;
                char *path;
  
 -              if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
 +              if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
                        continue;
                if (has_head)
                        old = read_one_ent(NULL, &head_oid,
                                           ce->name, ce_namelen(ce), 0);
                if (old && ce->ce_mode == old->ce_mode &&
 -                  !oidcmp(&ce->oid, &old->oid)) {
 -                      free(old);
 +                  oideq(&ce->oid, &old->oid)) {
 +                      discard_cache_entry(old);
                        continue; /* unchanged */
                }
                /* Be careful.  The working tree may not have the
                path = xstrdup(ce->name);
                update_one(path);
                free(path);
 -              free(old);
 +              discard_cache_entry(old);
                if (save_nr != active_nr)
                        goto redo;
        }
@@@ -782,7 -785,7 +782,7 @@@ struct refresh_params 
  static int refresh(struct refresh_params *o, unsigned int flag)
  {
        setup_work_tree();
 -      read_cache_preload(NULL);
 +      read_cache();
        *o->has_errors |= refresh_cache(o->flags | flag);
        return 0;
  }
  static int refresh_callback(const struct option *opt,
                                const char *arg, int unset)
  {
 +      BUG_ON_OPT_NEG(unset);
 +      BUG_ON_OPT_ARG(arg);
        return refresh(opt->value, 0);
  }
  
  static int really_refresh_callback(const struct option *opt,
                                const char *arg, int unset)
  {
 +      BUG_ON_OPT_NEG(unset);
 +      BUG_ON_OPT_ARG(arg);
        return refresh(opt->value, REFRESH_REALLY);
  }
  
@@@ -807,7 -806,6 +807,7 @@@ static int chmod_callback(const struct 
                                const char *arg, int unset)
  {
        char *flip = opt->value;
 +      BUG_ON_OPT_NEG(unset);
        if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
                return error("option 'chmod' expects \"+x\" or \"-x\"");
        *flip = arg[0];
  static int resolve_undo_clear_callback(const struct option *opt,
                                const char *arg, int unset)
  {
 +      BUG_ON_OPT_NEG(unset);
 +      BUG_ON_OPT_ARG(arg);
        resolve_undo_clear();
        return 0;
  }
@@@ -830,7 -826,6 +830,7 @@@ static int parse_new_style_cacheinfo(co
  {
        unsigned long ul;
        char *endp;
 +      const char *p;
  
        if (!arg)
                return -1;
                return -1; /* not a new-style cacheinfo */
        *mode = ul;
        endp++;
 -      if (get_oid_hex(endp, oid) || endp[GIT_SHA1_HEXSZ] != ',')
 +      if (parse_oid_hex(endp, oid, &p) || *p != ',')
                return -1;
 -      *path = endp + GIT_SHA1_HEXSZ + 1;
 +      *path = p + 1;
        return 0;
  }
  
@@@ -854,8 -849,6 +854,8 @@@ static int cacheinfo_callback(struct pa
        unsigned int mode;
        const char *path;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
                if (add_cacheinfo(mode, &oid, path, 0))
                        die("git update-index: --cacheinfo cannot add %s", path);
@@@ -878,8 -871,6 +878,8 @@@ static int stdin_cacheinfo_callback(str
  {
        int *nul_term_line = opt->value;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        if (ctx->argc != 1)
                return error("option '%s' must be the last argument", opt->long_name);
        allow_add = allow_replace = allow_remove = 1;
@@@ -892,8 -883,6 +892,8 @@@ static int stdin_callback(struct parse_
  {
        int *read_from_stdin = opt->value;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        if (ctx->argc != 1)
                return error("option '%s' must be the last argument", opt->long_name);
        *read_from_stdin = 1;
  }
  
  static int unresolve_callback(struct parse_opt_ctx_t *ctx,
 -                              const struct option *opt, int flags)
 +                              const struct option *opt, int unset)
  {
        int *has_errors = opt->value;
        const char *prefix = startup_info->prefix;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        /* consume remaining arguments. */
        *has_errors = do_unresolve(ctx->argc, ctx->argv,
                                prefix, prefix ? strlen(prefix) : 0);
  }
  
  static int reupdate_callback(struct parse_opt_ctx_t *ctx,
 -                              const struct option *opt, int flags)
 +                              const struct option *opt, int unset)
  {
        int *has_errors = opt->value;
        const char *prefix = startup_info->prefix;
  
 +      BUG_ON_OPT_NEG(unset);
 +
        /* consume remaining arguments. */
        setup_work_tree();
        *has_errors = do_reupdate(ctx->argc, ctx->argv,
@@@ -986,9 -971,9 +986,9 @@@ int cmd_update_index(int argc, const ch
                        PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
                        (parse_opt_cb *) cacheinfo_callback},
 -              {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
 +              {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+|-)x",
                        N_("override the executable bit of the listed files"),
 -                      PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
 +                      PARSE_OPT_NONEG,
                        chmod_callback},
                {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
                        N_("mark files as \"not changing\""),
                case PARSE_OPT_HELP:
                case PARSE_OPT_ERROR:
                        exit(129);
+               case PARSE_OPT_COMPLETE:
+                       exit(0);
                case PARSE_OPT_NON_OPTION:
                case PARSE_OPT_DONE:
                {
diff --combined parse-options.c
index 3b874a83a0c897845deeec37c1a3b85e0b625050,92e40994492c3158a5a2e23f2b135354305a27f7..6932eaff6144bdf9fc8f430f149a73ade00f0d8b
@@@ -516,7 -516,7 +516,7 @@@ static int show_gitcomp(struct parse_op
        show_negated_gitcomp(original_opts, -1);
        show_negated_gitcomp(original_opts, nr_noopts);
        fputc('\n', stdout);
-       exit(0);
+       return PARSE_OPT_COMPLETE;
  }
  
  static int usage_with_options_internal(struct parse_opt_ctx_t *,
@@@ -638,6 -638,8 +638,8 @@@ int parse_options(int argc, const char 
        case PARSE_OPT_HELP:
        case PARSE_OPT_ERROR:
                exit(129);
+       case PARSE_OPT_COMPLETE:
+               exit(0);
        case PARSE_OPT_NON_OPTION:
        case PARSE_OPT_DONE:
                break;
  static int usage_argh(const struct option *opts, FILE *outfile)
  {
        const char *s;
 -      int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
 +      int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
 +              !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
        if (opts->flags & PARSE_OPT_OPTARG)
                if (opts->long_name)
                        s = literal ? "[=%s]" : "[=<%s>]";
diff --combined parse-options.h
index 6c4fe2016d65f1093400f8f5080085eb244f0026,c433c428283d844ac549d51a124fc8834511381e..a650a7d220bd972b10b7e4a30119e9de3b8ba23a
@@@ -150,6 -150,9 +150,6 @@@ struct option 
                                      (h), 0, &parse_opt_string_list }
  #define OPT_UYN(s, l, v, h)         { OPTION_CALLBACK, (s), (l), (v), NULL, \
                                      (h), PARSE_OPT_NOARG, &parse_opt_tertiary }
 -#define OPT_DATE(s, l, v, h) \
 -      { OPTION_CALLBACK, (s), (l), (v), N_("time"),(h), 0,    \
 -        parse_opt_approxidate_cb }
  #define OPT_EXPIRY_DATE(s, l, v, h) \
        { OPTION_CALLBACK, (s), (l), (v), N_("expiry-date"),(h), 0,     \
          parse_opt_expiry_date_cb }
@@@ -191,23 -194,10 +191,24 @@@ extern int opterror(const struct optio
  #define opterror(o,r,f) (opterror((o),(r),(f)), const_error())
  #endif
  
 +/*
 + * Use these assertions for callbacks that expect to be called with NONEG and
 + * NOARG respectively, and do not otherwise handle the "unset" and "arg"
 + * parameters.
 + */
 +#define BUG_ON_OPT_NEG(unset) do { \
 +      if ((unset)) \
 +              BUG("option callback does not expect negation"); \
 +} while (0)
 +#define BUG_ON_OPT_ARG(arg) do { \
 +      if ((arg)) \
 +              BUG("option callback does not expect an argument"); \
 +} while (0)
 +
  /*----- incremental advanced APIs -----*/
  
  enum {
+       PARSE_OPT_COMPLETE = -2,
        PARSE_OPT_HELP = -1,
        PARSE_OPT_DONE,
        PARSE_OPT_NON_OPTION,
@@@ -243,6 -233,7 +244,6 @@@ extern struct option *parse_options_con
  
  /*----- some often used options -----*/
  extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
 -extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
  extern int parse_opt_expiry_date_cb(const struct option *, const char *, int);
  extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
  extern int parse_opt_verbosity_cb(const struct option *, const char *, int);