]> git.ipfire.org Git - thirdparty/git.git/commitdiff
tree.c API: move read_tree() into builtin/ls-files.c
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Sat, 20 Mar 2021 22:37:46 +0000 (23:37 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 20 Mar 2021 23:09:25 +0000 (16:09 -0700)
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>
builtin/ls-files.c
cache.h
tree.c
tree.h

index f6f9e483b27e183e29e9c409669c6b23d5590cfc..a44586228137e14ac2c878376bf853c63492810f 100644 (file)
@@ -12,6 +12,7 @@
 #include "dir.h"
 #include "builtin.h"
 #include "tree.h"
+#include "cache-tree.h"
 #include "parse-options.h"
 #include "resolve-undo.h"
 #include "string-list.h"
@@ -420,6 +421,96 @@ static int get_common_prefix_len(const char *common_prefix)
        return common_prefix_len;
 }
 
+static int read_one_entry_opt(struct index_state *istate,
+                             const struct object_id *oid,
+                             const char *base, int baselen,
+                             const char *pathname,
+                             unsigned mode, int stage, int opt)
+{
+       int len;
+       struct cache_entry *ce;
+
+       if (S_ISDIR(mode))
+               return READ_TREE_RECURSIVE;
+
+       len = strlen(pathname);
+       ce = make_empty_cache_entry(istate, baselen + len);
+
+       ce->ce_mode = create_ce_mode(mode);
+       ce->ce_flags = create_ce_flags(stage);
+       ce->ce_namelen = baselen + len;
+       memcpy(ce->name, base, baselen);
+       memcpy(ce->name + baselen, pathname, len+1);
+       oidcpy(&ce->oid, oid);
+       return add_index_entry(istate, ce, opt);
+}
+
+static int read_one_entry(const struct object_id *oid, struct strbuf *base,
+                         const char *pathname, unsigned mode, int stage,
+                         void *context)
+{
+       struct index_state *istate = context;
+       return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
+                                 mode, stage,
+                                 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
+}
+
+/*
+ * This is used when the caller knows there is no existing entries at
+ * the stage that will conflict with the entry being added.
+ */
+static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
+                               const char *pathname, unsigned mode, int stage,
+                               void *context)
+{
+       struct index_state *istate = context;
+       return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
+                                 mode, stage,
+                                 ADD_CACHE_JUST_APPEND);
+}
+
+
+static int read_tree(struct repository *r, struct tree *tree, int stage,
+                    struct pathspec *match, struct index_state *istate)
+{
+       read_tree_fn_t fn = NULL;
+       int i, err;
+
+       /*
+        * Currently the only existing callers of this function all
+        * call it with stage=1 and after making sure there is nothing
+        * at that stage; we could always use read_one_entry_quick().
+        *
+        * But when we decide to straighten out git-read-tree not to
+        * use unpack_trees() in some cases, this will probably start
+        * to matter.
+        */
+
+       /*
+        * See if we have cache entry at the stage.  If so,
+        * do it the original slow way, otherwise, append and then
+        * sort at the end.
+        */
+       for (i = 0; !fn && i < istate->cache_nr; i++) {
+               const struct cache_entry *ce = istate->cache[i];
+               if (ce_stage(ce) == stage)
+                       fn = read_one_entry;
+       }
+
+       if (!fn)
+               fn = read_one_entry_quick;
+       err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
+       if (fn == read_one_entry || err)
+               return err;
+
+       /*
+        * Sort the cache entry -- we need to nuke the cache tree, though.
+        */
+       cache_tree_free(&istate->cache_tree);
+       QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
+       return 0;
+}
+
 /*
  * Read the tree specified with --with-tree option
  * (typically, HEAD) into stage #1 and then
diff --git a/cache.h b/cache.h
index d92814961405dc3d87d2d886ba0394552ca7bd0d..bb317abc91fbc1d236047fcb69323b60b35c0f5c 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -803,7 +803,7 @@ static inline int index_pos_to_insert_pos(uintmax_t pos)
 #define ADD_CACHE_OK_TO_ADD 1          /* Ok to add */
 #define ADD_CACHE_OK_TO_REPLACE 2      /* Ok to replace file/directory */
 #define ADD_CACHE_SKIP_DFCHECK 4       /* Ok to skip DF conflict checks */
-#define ADD_CACHE_JUST_APPEND 8                /* Append only; tree.c::read_tree() */
+#define ADD_CACHE_JUST_APPEND 8                /* Append only */
 #define ADD_CACHE_NEW_ONLY 16          /* Do not replace existing ones */
 #define ADD_CACHE_KEEP_CACHE_TREE 32   /* Do not invalidate cache-tree */
 #define ADD_CACHE_RENORMALIZE 64        /* Pass along HASH_RENORMALIZE */
diff --git a/tree.c b/tree.c
index a52479812ce0c46ce972a56f0fa1d8bbb7c0b03b..a6c12f2745a60e68d66da16702aa4a15128497d2 100644 (file)
--- a/tree.c
+++ b/tree.c
 
 const char *tree_type = "tree";
 
-static int read_one_entry_opt(struct index_state *istate,
-                             const struct object_id *oid,
-                             const char *base, int baselen,
-                             const char *pathname,
-                             unsigned mode, int stage, int opt)
-{
-       int len;
-       struct cache_entry *ce;
-
-       if (S_ISDIR(mode))
-               return READ_TREE_RECURSIVE;
-
-       len = strlen(pathname);
-       ce = make_empty_cache_entry(istate, baselen + len);
-
-       ce->ce_mode = create_ce_mode(mode);
-       ce->ce_flags = create_ce_flags(stage);
-       ce->ce_namelen = baselen + len;
-       memcpy(ce->name, base, baselen);
-       memcpy(ce->name + baselen, pathname, len+1);
-       oidcpy(&ce->oid, oid);
-       return add_index_entry(istate, ce, opt);
-}
-
-static int read_one_entry(const struct object_id *oid, struct strbuf *base,
-                         const char *pathname, unsigned mode, int stage,
-                         void *context)
-{
-       struct index_state *istate = context;
-       return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
-                                 mode, stage,
-                                 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
-}
-
-/*
- * This is used when the caller knows there is no existing entries at
- * the stage that will conflict with the entry being added.
- */
-static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
-                               const char *pathname, unsigned mode, int stage,
-                               void *context)
-{
-       struct index_state *istate = context;
-       return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
-                                 mode, stage,
-                                 ADD_CACHE_JUST_APPEND);
-}
-
 static int read_tree_1(struct repository *r,
                       struct tree *tree, struct strbuf *base,
                       int stage, const struct pathspec *pathspec,
@@ -154,47 +106,6 @@ int cmp_cache_name_compare(const void *a_, const void *b_)
                                  ce2->name, ce2->ce_namelen, ce_stage(ce2));
 }
 
-int read_tree(struct repository *r, struct tree *tree, int stage,
-             struct pathspec *match, struct index_state *istate)
-{
-       read_tree_fn_t fn = NULL;
-       int i, err;
-
-       /*
-        * Currently the only existing callers of this function all
-        * call it with stage=1 and after making sure there is nothing
-        * at that stage; we could always use read_one_entry_quick().
-        *
-        * But when we decide to straighten out git-read-tree not to
-        * use unpack_trees() in some cases, this will probably start
-        * to matter.
-        */
-
-       /*
-        * See if we have cache entry at the stage.  If so,
-        * do it the original slow way, otherwise, append and then
-        * sort at the end.
-        */
-       for (i = 0; !fn && i < istate->cache_nr; i++) {
-               const struct cache_entry *ce = istate->cache[i];
-               if (ce_stage(ce) == stage)
-                       fn = read_one_entry;
-       }
-
-       if (!fn)
-               fn = read_one_entry_quick;
-       err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
-       if (fn == read_one_entry || err)
-               return err;
-
-       /*
-        * Sort the cache entry -- we need to nuke the cache tree, though.
-        */
-       cache_tree_free(&istate->cache_tree);
-       QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
-       return 0;
-}
-
 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
 {
        struct object *obj = lookup_object(r, oid);
diff --git a/tree.h b/tree.h
index 3eb0484cbf2a7523d15c2102d138f1622aaecd1a..6b0b1dc211acf5ff5c73a9c3541b35e1062e5693 100644 (file)
--- a/tree.h
+++ b/tree.h
@@ -38,9 +38,4 @@ int read_tree_recursive(struct repository *r,
                        const char *base, int baselen,
                        int stage, const struct pathspec *pathspec,
                        read_tree_fn_t fn, void *context);
-
-int read_tree(struct repository *r, struct tree *tree,
-             int stage, struct pathspec *pathspec,
-             struct index_state *istate);
-
 #endif /* TREE_H */