name-rev: refactor logic to see if a new candidate is a better name
[gitweb.git] / builtin / ls-files.c
index b6144a56d3f44d83d111997ccdd7cacb5bcafac1..1c0f057d02c55c8449bc9da1f1f01d509bbca8ba 100644 (file)
@@ -195,6 +195,7 @@ static void show_gitlink(const struct cache_entry *ce)
 {
        struct child_process cp = CHILD_PROCESS_INIT;
        int status;
+       int i;
 
        argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
                         super_prefix ? super_prefix : "",
@@ -205,6 +206,15 @@ static void show_gitlink(const struct cache_entry *ce)
        /* add supported options */
        argv_array_pushv(&cp.args, submodules_options.argv);
 
+       /*
+        * Pass in the original pathspec args.  The submodule will be
+        * responsible for prepending the 'submodule_prefix' prior to comparing
+        * against the pathspec for matches.
+        */
+       argv_array_push(&cp.args, "--");
+       for (i = 0; i < pathspec.nr; i++)
+               argv_array_push(&cp.args, pathspec.items[i].original);
+
        cp.git_cmd = 1;
        cp.dir = ce->name;
        status = run_command(&cp);
@@ -223,7 +233,8 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
        if (len >= ce_namelen(ce))
                die("git ls-files: internal error - cache entry not superset of prefix");
 
-       if (recurse_submodules && S_ISGITLINK(ce->ce_mode)) {
+       if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
+           submodule_path_match(&pathspec, name.buf, ps_matched)) {
                show_gitlink(ce);
        } else if (match_pathspec(&pathspec, name.buf, name.len,
                                  len, ps_matched,
@@ -252,7 +263,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
                        printf("%s%06o %s %d\t",
                               tag,
                               ce->ce_mode,
-                              find_unique_abbrev(ce->sha1,abbrev),
+                              find_unique_abbrev(ce->oid.hash, abbrev),
                               ce_stage(ce));
                }
                write_eolinfo(ce, ce->name);
@@ -358,28 +369,30 @@ static void show_files(struct dir_struct *dir)
 /*
  * Prune the index to only contain stuff starting with "prefix"
  */
-static void prune_cache(const char *prefix)
+static void prune_cache(const char *prefix, size_t prefixlen)
 {
-       int pos = cache_name_pos(prefix, max_prefix_len);
+       int pos;
        unsigned int first, last;
 
+       if (!prefix)
+               return;
+       pos = cache_name_pos(prefix, prefixlen);
        if (pos < 0)
                pos = -pos-1;
-       memmove(active_cache, active_cache + pos,
-               (active_nr - pos) * sizeof(struct cache_entry *));
-       active_nr -= pos;
-       first = 0;
+       first = pos;
        last = active_nr;
        while (last > first) {
                int next = (last + first) >> 1;
                const struct cache_entry *ce = active_cache[next];
-               if (!strncmp(ce->name, prefix, max_prefix_len)) {
+               if (!strncmp(ce->name, prefix, prefixlen)) {
                        first = next+1;
                        continue;
                }
                last = next;
        }
-       active_nr = last;
+       memmove(active_cache, active_cache + pos,
+               (last - pos) * sizeof(struct cache_entry *));
+       active_nr = last - pos;
 }
 
 /*
@@ -602,16 +615,20 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
                die("ls-files --recurse-submodules does not support "
                    "--error-unmatch");
 
-       if (recurse_submodules && argc)
-               die("ls-files --recurse-submodules does not support pathspec");
-
        parse_pathspec(&pathspec, 0,
                       PATHSPEC_PREFER_CWD |
                       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
                       prefix, argv);
 
-       /* Find common prefix for all pathspec's */
-       max_prefix = common_prefix(&pathspec);
+       /*
+        * Find common prefix for all pathspec's
+        * This is used as a performance optimization which unfortunately cannot
+        * be done when recursing into submodules
+        */
+       if (recurse_submodules)
+               max_prefix = NULL;
+       else
+               max_prefix = common_prefix(&pathspec);
        max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
 
        /* Treat unmatching pathspec elements as errors */
@@ -626,8 +643,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
              show_killed || show_modified || show_resolve_undo))
                show_cached = 1;
 
-       if (max_prefix)
-               prune_cache(max_prefix);
+       prune_cache(max_prefix, max_prefix_len);
        if (with_tree) {
                /*
                 * Basic sanity check; show-stages and show-unmerged