tree_entry_interesting(): remove dependency on struct diff_options
[gitweb.git] / tree-diff.c
index 12c9a88884ec3bb70a1744e44235c578a44e08e6..281b48c7c078cae0c92838ba015a4bfc46d1064b 100644 (file)
@@ -93,25 +93,21 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const
  *  - zero for no
  *  - negative for "no, and no subsequent entries will be either"
  */
-static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
+static int tree_entry_interesting(const struct name_entry *entry, const char *base, int baselen, const struct pathspec *ps)
 {
-       const char *path;
-       const unsigned char *sha1;
-       unsigned mode;
        int i;
        int pathlen;
        int never_interesting = -1;
 
-       if (!opt->nr_paths)
+       if (!ps || !ps->nr)
                return 2;
 
-       sha1 = tree_entry_extract(desc, &path, &mode);
+       pathlen = tree_entry_len(entry->path, entry->sha1);
 
-       pathlen = tree_entry_len(path, sha1);
-
-       for (i = 0; i < opt->nr_paths; i++) {
-               const char *match = opt->paths[i];
-               int matchlen = opt->pathlens[i];
+       for (i = 0; i < ps->nr; i++) {
+               const struct pathspec_item *item = ps->items+i;
+               const char *match = item->match;
+               int matchlen = item->len;
                int m = -1; /* signals that we haven't called strncmp() */
 
                if (baselen >= matchlen) {
@@ -149,7 +145,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
                         * Does match sort strictly earlier than path
                         * with their common parts?
                         */
-                       m = strncmp(match, path,
+                       m = strncmp(match, entry->path,
                                    (matchlen < pathlen) ? matchlen : pathlen);
                        if (m < 0)
                                continue;
@@ -176,7 +172,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
                if (matchlen > pathlen) {
                        if (match[pathlen] != '/')
                                continue;
-                       if (!S_ISDIR(mode))
+                       if (!S_ISDIR(entry->mode))
                                continue;
                }
 
@@ -185,7 +181,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
                         * we cheated and did not do strncmp(), so we do
                         * that here.
                         */
-                       m = strncmp(match, path, pathlen);
+                       m = strncmp(match, entry->path, pathlen);
 
                /*
                 * If common part matched earlier then it is a hit,
@@ -208,8 +204,7 @@ static void show_tree(struct diff_options *opt, const char *prefix, struct tree_
                if (all_interesting)
                        show = 1;
                else {
-                       show = tree_entry_interesting(desc, base, baselen,
-                                                     opt);
+                       show = tree_entry_interesting(&desc->entry, base, baselen, &opt->pathspec);
                        if (show == 2)
                                all_interesting = 1;
                }
@@ -262,7 +257,7 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
 static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt, int *all_interesting)
 {
        while (t->size) {
-               int show = tree_entry_interesting(t, base, baselen, opt);
+               int show = tree_entry_interesting(&t->entry, base, baselen, &opt->pathspec);
                if (show == 2)
                        *all_interesting = 1;
                if (!show) {
@@ -286,7 +281,7 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, stru
                if (DIFF_OPT_TST(opt, QUICK) &&
                    DIFF_OPT_TST(opt, HAS_CHANGES))
                        break;
-               if (opt->nr_paths) {
+               if (opt->pathspec.nr) {
                        if (!all_t1_interesting)
                                skip_uninteresting(t1, base, baselen, opt,
                                                   &all_t1_interesting);
@@ -349,7 +344,7 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co
        DIFF_OPT_SET(&diff_opts, RECURSIVE);
        DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
-       diff_opts.single_follow = opt->paths[0];
+       diff_opts.single_follow = opt->pathspec.raw[0];
        diff_opts.break_opt = opt->break_opt;
        paths[0] = NULL;
        diff_tree_setup_paths(paths, &diff_opts);
@@ -369,15 +364,16 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co
                 * diff_queued_diff, we will also use that as the path in
                 * the future!
                 */
-               if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
+               if ((p->status == 'R' || p->status == 'C') &&
+                   !strcmp(p->two->path, opt->pathspec.raw[0])) {
                        /* Switch the file-pairs around */
                        q->queue[i] = choice;
                        choice = p;
 
                        /* Update the path we use from now on.. */
                        diff_tree_release_paths(opt);
-                       opt->paths[0] = xstrdup(p->one->path);
-                       diff_tree_setup_paths(opt->paths, opt);
+                       opt->pathspec.raw[0] = xstrdup(p->one->path);
+                       diff_tree_setup_paths(opt->pathspec.raw, opt);
 
                        /*
                         * The caller expects us to return a set of vanilla
@@ -452,36 +448,12 @@ int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_
        return retval;
 }
 
-static int count_paths(const char **paths)
-{
-       int i = 0;
-       while (*paths++)
-               i++;
-       return i;
-}
-
 void diff_tree_release_paths(struct diff_options *opt)
 {
-       free(opt->pathlens);
+       free_pathspec(&opt->pathspec);
 }
 
 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
 {
-       opt->nr_paths = 0;
-       opt->pathlens = NULL;
-       opt->paths = NULL;
-
-       if (p) {
-               int i;
-
-               opt->paths = p;
-               opt->nr_paths = count_paths(p);
-               if (opt->nr_paths == 0) {
-                       opt->pathlens = NULL;
-                       return;
-               }
-               opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
-               for (i=0; i < opt->nr_paths; i++)
-                       opt->pathlens[i] = strlen(p[i]);
-       }
+       init_pathspec(&opt->pathspec, p);
 }