diff: properly error out when combining multiple pickaxe options
[gitweb.git] / diff.c
diff --git a/diff.c b/diff.c
index 5714382d3fa6d16b05a9cff862edf6d24eb4c356..42858d4c7d058b876b0279bdefdb23850e8532ad 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -707,83 +707,14 @@ struct moved_entry {
        struct moved_entry *next_line;
 };
 
-static int next_byte(const char **cp, const char **endp,
-                    const struct diff_options *diffopt)
-{
-       int retval;
-
-       if (*cp > *endp)
-               return -1;
-
-       if (isspace(**cp)) {
-               if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
-                       while (*cp < *endp && isspace(**cp))
-                               (*cp)++;
-                       /*
-                        * After skipping a couple of whitespaces,
-                        * we still have to account for one space.
-                        */
-                       return (int)' ';
-               }
-
-               if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
-                       while (*cp < *endp && isspace(**cp))
-                               (*cp)++;
-                       /* return the first non-ws character via the usual below */
-               }
-       }
-
-       retval = (unsigned char)(**cp);
-       (*cp)++;
-       return retval;
-}
-
 static int moved_entry_cmp(const struct diff_options *diffopt,
                           const struct moved_entry *a,
                           const struct moved_entry *b,
                           const void *keydata)
 {
-       const char *ap = a->es->line, *ae = a->es->line + a->es->len;
-       const char *bp = b->es->line, *be = b->es->line + b->es->len;
-
-       if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
-               return a->es->len != b->es->len  || memcmp(ap, bp, a->es->len);
-
-       if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
-               while (ae > ap && isspace(*ae))
-                       ae--;
-               while (be > bp && isspace(*be))
-                       be--;
-       }
-
-       while (1) {
-               int ca, cb;
-               ca = next_byte(&ap, &ae, diffopt);
-               cb = next_byte(&bp, &be, diffopt);
-               if (ca != cb)
-                       return 1;
-               if (ca < 0)
-                       return 0;
-       }
-}
-
-static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
-{
-       if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
-               static struct strbuf sb = STRBUF_INIT;
-               const char *ap = es->line, *ae = es->line + es->len;
-               int c;
-
-               strbuf_reset(&sb);
-               while (ae > ap && isspace(*ae))
-                       ae--;
-               while ((c = next_byte(&ap, &ae, o)) > 0)
-                       strbuf_addch(&sb, c);
-
-               return memhash(sb.buf, sb.len);
-       } else {
-               return memhash(es->line, es->len);
-       }
+       return !xdiff_compare_lines(a->es->line, a->es->len,
+                                   b->es->line, b->es->len,
+                                   diffopt->xdl_opts);
 }
 
 static struct moved_entry *prepare_entry(struct diff_options *o,
@@ -792,7 +723,7 @@ static struct moved_entry *prepare_entry(struct diff_options *o,
        struct moved_entry *ret = xmalloc(sizeof(*ret));
        struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
 
-       ret->ent.hash = get_string_hash(l, o);
+       ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts);
        ret->es = l;
        ret->next_line = NULL;
 
@@ -3614,14 +3545,12 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
                int fd;
 
                if (lstat(s->path, &st) < 0) {
-                       if (errno == ENOENT) {
-                       err_empty:
-                               err = -1;
-                       empty:
-                               s->data = (char *)"";
-                               s->size = 0;
-                               return err;
-                       }
+               err_empty:
+                       err = -1;
+               empty:
+                       s->data = (char *)"";
+                       s->size = 0;
+                       return err;
                }
                s->size = xsize_t(st.st_size);
                if (!s->size)
@@ -4153,6 +4082,7 @@ void diff_setup(struct diff_options *options)
        options->interhunkcontext = diff_interhunk_context_default;
        options->ws_error_highlight = ws_error_highlight_default;
        options->flags.rename_empty = 1;
+       options->objfind = NULL;
 
        /* pathchange left =NULL by default */
        options->change = diff_change;
@@ -4193,6 +4123,9 @@ void diff_setup_done(struct diff_options *options)
        if (count > 1)
                die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
 
+       if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
+               die(_("-G, -S and --find-object are mutually exclusive"));
+
        /*
         * Most of the time we can say "there are changes"
         * only by checking if there are changed paths, but
@@ -4244,7 +4177,7 @@ void diff_setup_done(struct diff_options *options)
        /*
         * Also pickaxe would not work very well if you do not say recursive
         */
-       if (options->pickaxe)
+       if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
                options->flags.recursive = 1;
        /*
         * When patches are generated, submodules diffed against the work tree
@@ -4558,6 +4491,23 @@ static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *ar
        return 1;
 }
 
+static int parse_objfind_opt(struct diff_options *opt, const char *arg)
+{
+       struct object_id oid;
+
+       if (get_oid(arg, &oid))
+               return error("unable to resolve '%s'", arg);
+
+       if (!opt->objfind)
+               opt->objfind = xcalloc(1, sizeof(*opt->objfind));
+
+       opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
+       opt->flags.recursive = 1;
+       opt->flags.tree_in_recursive = 1;
+       oidset_insert(opt->objfind, &oid);
+       return 1;
+}
+
 int diff_opt_parse(struct diff_options *options,
                   const char **av, int ac, const char *prefix)
 {
@@ -4807,7 +4757,8 @@ int diff_opt_parse(struct diff_options *options,
        else if ((argcount = short_opt('O', av, &optarg))) {
                options->orderfile = prefix_filename(prefix, optarg);
                return argcount;
-       }
+       } else if (skip_prefix(arg, "--find-object=", &arg))
+               return parse_objfind_opt(options, arg);
        else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
                int offending = parse_diff_filter_opt(optarg, options);
                if (offending)
@@ -5848,7 +5799,7 @@ void diffcore_std(struct diff_options *options)
                if (options->break_opt != -1)
                        diffcore_merge_broken();
        }
-       if (options->pickaxe)
+       if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
                diffcore_pickaxe(options);
        if (options->orderfile)
                diffcore_order(options->orderfile);