tree.c API: move read_tree() into builtin/ls-files.c

Since the read_tree() API was added around the same time as read_tree_recursive() in 94537c78a82 (Move "read_tree()" to "tree.c"[...], 2005-04-22) and b12ec373b8e ([PATCH] Teach read-tree about commit objects, 2005-04-20) things have gradually migrated over to the read_tree_recursive() version. Now builtin/ls-files.c is the last user of this code, let's move all the relevant code there. This allows for subsequent simplification of it, and an eventual move to read_tree_recursive(). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 20, 2021 at 23:37 UTC eefadd18e10fc0c4c808faa8ee077f375f28050c
4 files changed +92 -95
builtin/ls-files.c
+91
@@ -12,6 +12,7 @@
12 #include "dir.h"
13 #include "builtin.h"
14 #include "tree.h"
15 +#include "cache-tree.h"
16 #include "parse-options.h"
17 #include "resolve-undo.h"
18 #include "string-list.h"
@@ -420,6 +421,96 @@ static int get_common_prefix_len(const char *common_prefix)
421 return common_prefix_len;
422 }
423
424 +static int read_one_entry_opt(struct index_state *istate,
425 + const struct object_id *oid,
426 + const char *base, int baselen,
427 + const char *pathname,
428 + unsigned mode, int stage, int opt)
429 +{
430 + int len;
431 + struct cache_entry *ce;
432 +
433 + if (S_ISDIR(mode))
434 + return READ_TREE_RECURSIVE;
435 +
436 + len = strlen(pathname);
437 + ce = make_empty_cache_entry(istate, baselen + len);
438 +
439 + ce->ce_mode = create_ce_mode(mode);
440 + ce->ce_flags = create_ce_flags(stage);
441 + ce->ce_namelen = baselen + len;
442 + memcpy(ce->name, base, baselen);
443 + memcpy(ce->name + baselen, pathname, len+1);
444 + oidcpy(&ce->oid, oid);
445 + return add_index_entry(istate, ce, opt);
446 +}
447 +
448 +static int read_one_entry(const struct object_id *oid, struct strbuf *base,
449 + const char *pathname, unsigned mode, int stage,
450 + void *context)
451 +{
452 + struct index_state *istate = context;
453 + return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
454 + mode, stage,
455 + ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
456 +}
457 +
458 +/*
459 + * This is used when the caller knows there is no existing entries at
460 + * the stage that will conflict with the entry being added.
461 + */
462 +static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
463 + const char *pathname, unsigned mode, int stage,
464 + void *context)
465 +{
466 + struct index_state *istate = context;
467 + return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
468 + mode, stage,
469 + ADD_CACHE_JUST_APPEND);
470 +}
471 +
472 +
473 +static int read_tree(struct repository *r, struct tree *tree, int stage,
474 + struct pathspec *match, struct index_state *istate)
475 +{
476 + read_tree_fn_t fn = NULL;
477 + int i, err;
478 +
479 + /*
480 + * Currently the only existing callers of this function all
481 + * call it with stage=1 and after making sure there is nothing
482 + * at that stage; we could always use read_one_entry_quick().
483 + *
484 + * But when we decide to straighten out git-read-tree not to
485 + * use unpack_trees() in some cases, this will probably start
486 + * to matter.
487 + */
488 +
489 + /*
490 + * See if we have cache entry at the stage. If so,
491 + * do it the original slow way, otherwise, append and then
492 + * sort at the end.
493 + */
494 + for (i = 0; !fn && i < istate->cache_nr; i++) {
495 + const struct cache_entry *ce = istate->cache[i];
496 + if (ce_stage(ce) == stage)
497 + fn = read_one_entry;
498 + }
499 +
500 + if (!fn)
501 + fn = read_one_entry_quick;
502 + err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
503 + if (fn == read_one_entry || err)
504 + return err;
505 +
506 + /*
507 + * Sort the cache entry -- we need to nuke the cache tree, though.
508 + */
509 + cache_tree_free(&istate->cache_tree);
510 + QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
511 + return 0;
512 +}
513 +
514 /*
515 * Read the tree specified with --with-tree option
516 * (typically, HEAD) into stage #1 and then
cache.h
+1 -1
@@ -803,7 +803,7 @@ static inline int index_pos_to_insert_pos(uintmax_t pos)
803 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
804 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
805 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
806 -#define ADD_CACHE_JUST_APPEND 8 /* Append only; tree.c::read_tree() */
806 +#define ADD_CACHE_JUST_APPEND 8 /* Append only */
807 #define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
808 #define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
809 #define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
tree.c
-89
@@ -11,54 +11,6 @@
11
12 const char *tree_type = "tree";
13
14 -static int read_one_entry_opt(struct index_state *istate,
15 - const struct object_id *oid,
16 - const char *base, int baselen,
17 - const char *pathname,
18 - unsigned mode, int stage, int opt)
19 -{
20 - int len;
21 - struct cache_entry *ce;
22 -
23 - if (S_ISDIR(mode))
24 - return READ_TREE_RECURSIVE;
25 -
26 - len = strlen(pathname);
27 - ce = make_empty_cache_entry(istate, baselen + len);
28 -
29 - ce->ce_mode = create_ce_mode(mode);
30 - ce->ce_flags = create_ce_flags(stage);
31 - ce->ce_namelen = baselen + len;
32 - memcpy(ce->name, base, baselen);
33 - memcpy(ce->name + baselen, pathname, len+1);
34 - oidcpy(&ce->oid, oid);
35 - return add_index_entry(istate, ce, opt);
36 -}
37 -
38 -static int read_one_entry(const struct object_id *oid, struct strbuf *base,
39 - const char *pathname, unsigned mode, int stage,
40 - void *context)
41 -{
42 - struct index_state *istate = context;
43 - return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
44 - mode, stage,
45 - ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
46 -}
47 -
48 -/*
49 - * This is used when the caller knows there is no existing entries at
50 - * the stage that will conflict with the entry being added.
51 - */
52 -static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
53 - const char *pathname, unsigned mode, int stage,
54 - void *context)
55 -{
56 - struct index_state *istate = context;
57 - return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
58 - mode, stage,
59 - ADD_CACHE_JUST_APPEND);
60 -}
61 -
14 static int read_tree_1(struct repository *r,
15 struct tree *tree, struct strbuf *base,
16 int stage, const struct pathspec *pathspec,
@@ -154,47 +106,6 @@ int cmp_cache_name_compare(const void *a_, const void *b_)
106 ce2->name, ce2->ce_namelen, ce_stage(ce2));
107 }
108
157 -int read_tree(struct repository *r, struct tree *tree, int stage,
158 - struct pathspec *match, struct index_state *istate)
159 -{
160 - read_tree_fn_t fn = NULL;
161 - int i, err;
162 -
163 - /*
164 - * Currently the only existing callers of this function all
165 - * call it with stage=1 and after making sure there is nothing
166 - * at that stage; we could always use read_one_entry_quick().
167 - *
168 - * But when we decide to straighten out git-read-tree not to
169 - * use unpack_trees() in some cases, this will probably start
170 - * to matter.
171 - */
172 -
173 - /*
174 - * See if we have cache entry at the stage. If so,
175 - * do it the original slow way, otherwise, append and then
176 - * sort at the end.
177 - */
178 - for (i = 0; !fn && i < istate->cache_nr; i++) {
179 - const struct cache_entry *ce = istate->cache[i];
180 - if (ce_stage(ce) == stage)
181 - fn = read_one_entry;
182 - }
183 -
184 - if (!fn)
185 - fn = read_one_entry_quick;
186 - err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
187 - if (fn == read_one_entry || err)
188 - return err;
189 -
190 - /*
191 - * Sort the cache entry -- we need to nuke the cache tree, though.
192 - */
193 - cache_tree_free(&istate->cache_tree);
194 - QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
195 - return 0;
196 -}
197 -
109 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
110 {
111 struct object *obj = lookup_object(r, oid);
tree.h
-5
@@ -38,9 +38,4 @@ int read_tree_recursive(struct repository *r,
38 const char *base, int baselen,
39 int stage, const struct pathspec *pathspec,
40 read_tree_fn_t fn, void *context);
41 -
42 -int read_tree(struct repository *r, struct tree *tree,
43 - int stage, struct pathspec *pathspec,
44 - struct index_state *istate);
45 -
41 #endif /* TREE_H */