optimize diffcore-delta by sorting hash entries.
[gitweb.git] / diff-lib.c
index c6d127346ab38b3b73285f61d2dc6874ab1dc7ac..da5571302df6ed418874fd4d7423853a7de5b52c 100644 (file)
@@ -24,7 +24,7 @@ static int read_directory(const char *path, struct path_list *list)
 
        while ((e = readdir(dir)))
                if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
-                       path_list_insert(xstrdup(e->d_name), list);
+                       path_list_insert(e->d_name, list);
 
        closedir(dir);
        return 0;
@@ -142,18 +142,34 @@ static int queue_diff(struct diff_options *o,
        }
 }
 
+/*
+ * Does the path name a blob in the working tree, or a directory
+ * in the working tree?
+ */
 static int is_in_index(const char *path)
 {
-       int len = strlen(path);
-       int pos = cache_name_pos(path, len);
-       char c;
-
-       if (pos < 0)
-               return 0;
-       if (strncmp(active_cache[pos]->name, path, len))
-               return 0;
-       c = active_cache[pos]->name[len];
-       return c == '\0' || c == '/';
+       int len, pos;
+       struct cache_entry *ce;
+
+       len = strlen(path);
+       while (path[len-1] == '/')
+               len--;
+       if (!len)
+               return 1; /* "." */
+       pos = cache_name_pos(path, len);
+       if (0 <= pos)
+               return 1;
+       pos = -1 - pos;
+       while (pos < active_nr) {
+               ce = active_cache[pos++];
+               if (ce_namelen(ce) <= len ||
+                   strncmp(ce->name, path, len) ||
+                   (ce->name[len] > '/'))
+                       break; /* path cannot be a prefix */
+               if (ce->name[len] == '/')
+                       return 1;
+       }
+       return 0;
 }
 
 static int handle_diff_files_args(struct rev_info *revs,
@@ -173,6 +189,7 @@ static int handle_diff_files_args(struct rev_info *revs,
                                !strcmp(argv[1], "--no-index")) {
                        revs->max_count = -2;
                        revs->diffopt.exit_with_status = 1;
+                       revs->diffopt.no_index = 1;
                }
                else if (!strcmp(argv[1], "-q"))
                        *silent = 1;
@@ -188,8 +205,10 @@ static int handle_diff_files_args(struct rev_info *revs,
                 */
                read_cache();
                if (!is_in_index(revs->diffopt.paths[0]) ||
-                                       !is_in_index(revs->diffopt.paths[1]))
+                                       !is_in_index(revs->diffopt.paths[1])) {
                        revs->max_count = -2;
+                       revs->diffopt.no_index = 1;
+               }
        }
 
        /*
@@ -277,7 +296,10 @@ int setup_diff_no_index(struct rev_info *revs,
        else
                revs->diffopt.paths = argv + argc - 2;
        revs->diffopt.nr_paths = 2;
+       revs->diffopt.no_index = 1;
        revs->max_count = -2;
+       if (diff_setup_done(&revs->diffopt) < 0)
+               die("diff_setup_done failed");
        return 0;
 }
 
@@ -288,7 +310,7 @@ int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
        if (handle_diff_files_args(revs, argc, argv, &silent_on_removed))
                return -1;
 
-       if (revs->max_count == -2) {
+       if (revs->diffopt.no_index) {
                if (revs->diffopt.nr_paths != 2)
                        return error("need two files/directories with --no-index");
                if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
@@ -648,7 +670,7 @@ int run_diff_index(struct rev_info *revs, int cached)
        const char *tree_name;
        int match_missing = 0;
 
-       /* 
+       /*
         * Backward compatibility wart - "diff-index -m" does
         * not mean "do not ignore merges", but totally different.
         */