Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:39

0001 /*
0002  * Copyright (C) the libgit2 contributors. All rights reserved.
0003  *
0004  * This file is part of libgit2, distributed under the GNU GPL v2 with
0005  * a Linking Exception. For full terms see the included COPYING file.
0006  */
0007 #ifndef INCLUDE_git_tree_h__
0008 #define INCLUDE_git_tree_h__
0009 
0010 #include "common.h"
0011 #include "types.h"
0012 #include "oid.h"
0013 #include "object.h"
0014 
0015 /**
0016  * @file git2/tree.h
0017  * @brief Git tree parsing, loading routines
0018  * @defgroup git_tree Git tree parsing, loading routines
0019  * @ingroup Git
0020  * @{
0021  */
0022 GIT_BEGIN_DECL
0023 
0024 /**
0025  * Lookup a tree object from the repository.
0026  *
0027  * @param out Pointer to the looked up tree
0028  * @param repo The repo to use when locating the tree.
0029  * @param id Identity of the tree to locate.
0030  * @return 0 or an error code
0031  */
0032 GIT_EXTERN(int) git_tree_lookup(
0033     git_tree **out, git_repository *repo, const git_oid *id);
0034 
0035 /**
0036  * Lookup a tree object from the repository,
0037  * given a prefix of its identifier (short id).
0038  *
0039  * @see git_object_lookup_prefix
0040  *
0041  * @param out pointer to the looked up tree
0042  * @param repo the repo to use when locating the tree.
0043  * @param id identity of the tree to locate.
0044  * @param len the length of the short identifier
0045  * @return 0 or an error code
0046  */
0047 GIT_EXTERN(int) git_tree_lookup_prefix(
0048     git_tree **out,
0049     git_repository *repo,
0050     const git_oid *id,
0051     size_t len);
0052 
0053 /**
0054  * Close an open tree
0055  *
0056  * You can no longer use the git_tree pointer after this call.
0057  *
0058  * IMPORTANT: You MUST call this method when you stop using a tree to
0059  * release memory. Failure to do so will cause a memory leak.
0060  *
0061  * @param tree The tree to close
0062  */
0063 GIT_EXTERN(void) git_tree_free(git_tree *tree);
0064 
0065 /**
0066  * Get the id of a tree.
0067  *
0068  * @param tree a previously loaded tree.
0069  * @return object identity for the tree.
0070  */
0071 GIT_EXTERN(const git_oid *) git_tree_id(const git_tree *tree);
0072 
0073 /**
0074  * Get the repository that contains the tree.
0075  *
0076  * @param tree A previously loaded tree.
0077  * @return Repository that contains this tree.
0078  */
0079 GIT_EXTERN(git_repository *) git_tree_owner(const git_tree *tree);
0080 
0081 /**
0082  * Get the number of entries listed in a tree
0083  *
0084  * @param tree a previously loaded tree.
0085  * @return the number of entries in the tree
0086  */
0087 GIT_EXTERN(size_t) git_tree_entrycount(const git_tree *tree);
0088 
0089 /**
0090  * Lookup a tree entry by its filename
0091  *
0092  * This returns a git_tree_entry that is owned by the git_tree.  You don't
0093  * have to free it, but you must not use it after the git_tree is released.
0094  *
0095  * @param tree a previously loaded tree.
0096  * @param filename the filename of the desired entry
0097  * @return the tree entry; NULL if not found
0098  */
0099 GIT_EXTERN(const git_tree_entry *) git_tree_entry_byname(
0100     const git_tree *tree, const char *filename);
0101 
0102 /**
0103  * Lookup a tree entry by its position in the tree
0104  *
0105  * This returns a git_tree_entry that is owned by the git_tree.  You don't
0106  * have to free it, but you must not use it after the git_tree is released.
0107  *
0108  * @param tree a previously loaded tree.
0109  * @param idx the position in the entry list
0110  * @return the tree entry; NULL if not found
0111  */
0112 GIT_EXTERN(const git_tree_entry *) git_tree_entry_byindex(
0113     const git_tree *tree, size_t idx);
0114 
0115 /**
0116  * Lookup a tree entry by SHA value.
0117  *
0118  * This returns a git_tree_entry that is owned by the git_tree.  You don't
0119  * have to free it, but you must not use it after the git_tree is released.
0120  *
0121  * Warning: this must examine every entry in the tree, so it is not fast.
0122  *
0123  * @param tree a previously loaded tree.
0124  * @param id the sha being looked for
0125  * @return the tree entry; NULL if not found
0126  */
0127 GIT_EXTERN(const git_tree_entry *) git_tree_entry_byid(
0128     const git_tree *tree, const git_oid *id);
0129 
0130 /**
0131  * Retrieve a tree entry contained in a tree or in any of its subtrees,
0132  * given its relative path.
0133  *
0134  * Unlike the other lookup functions, the returned tree entry is owned by
0135  * the user and must be freed explicitly with `git_tree_entry_free()`.
0136  *
0137  * @param out Pointer where to store the tree entry
0138  * @param root Previously loaded tree which is the root of the relative path
0139  * @param path Path to the contained entry
0140  * @return 0 on success; GIT_ENOTFOUND if the path does not exist
0141  */
0142 GIT_EXTERN(int) git_tree_entry_bypath(
0143     git_tree_entry **out,
0144     const git_tree *root,
0145     const char *path);
0146 
0147 /**
0148  * Duplicate a tree entry
0149  *
0150  * Create a copy of a tree entry. The returned copy is owned by the user,
0151  * and must be freed explicitly with `git_tree_entry_free()`.
0152  *
0153  * @param dest pointer where to store the copy
0154  * @param source tree entry to duplicate
0155  * @return 0 or an error code
0156  */
0157 GIT_EXTERN(int) git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source);
0158 
0159 /**
0160  * Free a user-owned tree entry
0161  *
0162  * IMPORTANT: This function is only needed for tree entries owned by the
0163  * user, such as the ones returned by `git_tree_entry_dup()` or
0164  * `git_tree_entry_bypath()`.
0165  *
0166  * @param entry The entry to free
0167  */
0168 GIT_EXTERN(void) git_tree_entry_free(git_tree_entry *entry);
0169 
0170 /**
0171  * Get the filename of a tree entry
0172  *
0173  * @param entry a tree entry
0174  * @return the name of the file
0175  */
0176 GIT_EXTERN(const char *) git_tree_entry_name(const git_tree_entry *entry);
0177 
0178 /**
0179  * Get the id of the object pointed by the entry
0180  *
0181  * @param entry a tree entry
0182  * @return the oid of the object
0183  */
0184 GIT_EXTERN(const git_oid *) git_tree_entry_id(const git_tree_entry *entry);
0185 
0186 /**
0187  * Get the type of the object pointed by the entry
0188  *
0189  * @param entry a tree entry
0190  * @return the type of the pointed object
0191  */
0192 GIT_EXTERN(git_object_t) git_tree_entry_type(const git_tree_entry *entry);
0193 
0194 /**
0195  * Get the UNIX file attributes of a tree entry
0196  *
0197  * @param entry a tree entry
0198  * @return filemode as an integer
0199  */
0200 GIT_EXTERN(git_filemode_t) git_tree_entry_filemode(const git_tree_entry *entry);
0201 
0202 /**
0203  * Get the raw UNIX file attributes of a tree entry
0204  *
0205  * This function does not perform any normalization and is only useful
0206  * if you need to be able to recreate the original tree object.
0207  *
0208  * @param entry a tree entry
0209  * @return filemode as an integer
0210  */
0211 
0212 GIT_EXTERN(git_filemode_t) git_tree_entry_filemode_raw(const git_tree_entry *entry);
0213 /**
0214  * Compare two tree entries
0215  *
0216  * @param e1 first tree entry
0217  * @param e2 second tree entry
0218  * @return <0 if e1 is before e2, 0 if e1 == e2, >0 if e1 is after e2
0219  */
0220 GIT_EXTERN(int) git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2);
0221 
0222 /**
0223  * Convert a tree entry to the git_object it points to.
0224  *
0225  * You must call `git_object_free()` on the object when you are done with it.
0226  *
0227  * @param object_out pointer to the converted object
0228  * @param repo repository where to lookup the pointed object
0229  * @param entry a tree entry
0230  * @return 0 or an error code
0231  */
0232 GIT_EXTERN(int) git_tree_entry_to_object(
0233     git_object **object_out,
0234     git_repository *repo,
0235     const git_tree_entry *entry);
0236 
0237 /**
0238  * Create a new tree builder.
0239  *
0240  * The tree builder can be used to create or modify trees in memory and
0241  * write them as tree objects to the database.
0242  *
0243  * If the `source` parameter is not NULL, the tree builder will be
0244  * initialized with the entries of the given tree.
0245  *
0246  * If the `source` parameter is NULL, the tree builder will start with no
0247  * entries and will have to be filled manually.
0248  *
0249  * @param out Pointer where to store the tree builder
0250  * @param repo Repository in which to store the object
0251  * @param source Source tree to initialize the builder (optional)
0252  * @return 0 on success; error code otherwise
0253  */
0254 GIT_EXTERN(int) git_treebuilder_new(
0255     git_treebuilder **out, git_repository *repo, const git_tree *source);
0256 
0257 /**
0258  * Clear all the entries in the builder
0259  *
0260  * @param bld Builder to clear
0261  * @return 0 on success; error code otherwise
0262  */
0263 GIT_EXTERN(int) git_treebuilder_clear(git_treebuilder *bld);
0264 
0265 /**
0266  * Get the number of entries listed in a treebuilder
0267  *
0268  * @param bld a previously loaded treebuilder.
0269  * @return the number of entries in the treebuilder
0270  */
0271 GIT_EXTERN(size_t) git_treebuilder_entrycount(git_treebuilder *bld);
0272 
0273 /**
0274  * Free a tree builder
0275  *
0276  * This will clear all the entries and free to builder.
0277  * Failing to free the builder after you're done using it
0278  * will result in a memory leak
0279  *
0280  * @param bld Builder to free
0281  */
0282 GIT_EXTERN(void) git_treebuilder_free(git_treebuilder *bld);
0283 
0284 /**
0285  * Get an entry from the builder from its filename
0286  *
0287  * The returned entry is owned by the builder and should
0288  * not be freed manually.
0289  *
0290  * @param bld Tree builder
0291  * @param filename Name of the entry
0292  * @return pointer to the entry; NULL if not found
0293  */
0294 GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(
0295     git_treebuilder *bld, const char *filename);
0296 
0297 /**
0298  * Add or update an entry to the builder
0299  *
0300  * Insert a new entry for `filename` in the builder with the
0301  * given attributes.
0302  *
0303  * If an entry named `filename` already exists, its attributes
0304  * will be updated with the given ones.
0305  *
0306  * The optional pointer `out` can be used to retrieve a pointer to the
0307  * newly created/updated entry.  Pass NULL if you do not need it. The
0308  * pointer may not be valid past the next operation in this
0309  * builder. Duplicate the entry if you want to keep it.
0310  *
0311  * By default the entry that you are inserting will be checked for
0312  * validity; that it exists in the object database and is of the
0313  * correct type.  If you do not want this behavior, set the
0314  * `GIT_OPT_ENABLE_STRICT_OBJECT_CREATION` library option to false.
0315  *
0316  * @param out Pointer to store the entry (optional)
0317  * @param bld Tree builder
0318  * @param filename Filename of the entry
0319  * @param id SHA1 oid of the entry
0320  * @param filemode Folder attributes of the entry. This parameter must
0321  *          be valued with one of the following entries: 0040000, 0100644,
0322  *          0100755, 0120000 or 0160000.
0323  * @return 0 or an error code
0324  */
0325 GIT_EXTERN(int) git_treebuilder_insert(
0326     const git_tree_entry **out,
0327     git_treebuilder *bld,
0328     const char *filename,
0329     const git_oid *id,
0330     git_filemode_t filemode);
0331 
0332 /**
0333  * Remove an entry from the builder by its filename
0334  *
0335  * @param bld Tree builder
0336  * @param filename Filename of the entry to remove
0337  * @return 0 or an error code
0338  */
0339 GIT_EXTERN(int) git_treebuilder_remove(
0340     git_treebuilder *bld, const char *filename);
0341 
0342 /**
0343  * Callback for git_treebuilder_filter
0344  *
0345  * The return value is treated as a boolean, with zero indicating that the
0346  * entry should be left alone and any non-zero value meaning that the
0347  * entry should be removed from the treebuilder list (i.e. filtered out).
0348  */
0349 typedef int GIT_CALLBACK(git_treebuilder_filter_cb)(
0350     const git_tree_entry *entry, void *payload);
0351 
0352 /**
0353  * Selectively remove entries in the tree
0354  *
0355  * The `filter` callback will be called for each entry in the tree with a
0356  * pointer to the entry and the provided `payload`; if the callback returns
0357  * non-zero, the entry will be filtered (removed from the builder).
0358  *
0359  * @param bld Tree builder
0360  * @param filter Callback to filter entries
0361  * @param payload Extra data to pass to filter callback
0362  * @return 0 on success, non-zero callback return value, or error code
0363  */
0364 GIT_EXTERN(int) git_treebuilder_filter(
0365     git_treebuilder *bld,
0366     git_treebuilder_filter_cb filter,
0367     void *payload);
0368 
0369 /**
0370  * Write the contents of the tree builder as a tree object
0371  *
0372  * The tree builder will be written to the given `repo`, and its
0373  * identifying SHA1 hash will be stored in the `id` pointer.
0374  *
0375  * @param id Pointer to store the OID of the newly written tree
0376  * @param bld Tree builder to write
0377  * @return 0 or an error code
0378  */
0379 GIT_EXTERN(int) git_treebuilder_write(
0380     git_oid *id, git_treebuilder *bld);
0381 
0382 /** Callback for the tree traversal method */
0383 typedef int GIT_CALLBACK(git_treewalk_cb)(
0384     const char *root, const git_tree_entry *entry, void *payload);
0385 
0386 /** Tree traversal modes */
0387 typedef enum {
0388     GIT_TREEWALK_PRE = 0, /* Pre-order */
0389     GIT_TREEWALK_POST = 1 /* Post-order */
0390 } git_treewalk_mode;
0391 
0392 /**
0393  * Traverse the entries in a tree and its subtrees in post or pre order.
0394  *
0395  * The entries will be traversed in the specified order, children subtrees
0396  * will be automatically loaded as required, and the `callback` will be
0397  * called once per entry with the current (relative) root for the entry and
0398  * the entry data itself.
0399  *
0400  * If the callback returns a positive value, the passed entry will be
0401  * skipped on the traversal (in pre mode). A negative value stops the walk.
0402  *
0403  * @param tree The tree to walk
0404  * @param mode Traversal mode (pre or post-order)
0405  * @param callback Function to call on each tree entry
0406  * @param payload Opaque pointer to be passed on each callback
0407  * @return 0 or an error code
0408  */
0409 GIT_EXTERN(int) git_tree_walk(
0410     const git_tree *tree,
0411     git_treewalk_mode mode,
0412     git_treewalk_cb callback,
0413     void *payload);
0414 
0415 /**
0416  * Create an in-memory copy of a tree. The copy must be explicitly
0417  * free'd or it will leak.
0418  *
0419  * @param out Pointer to store the copy of the tree
0420  * @param source Original tree to copy
0421  * @return 0
0422  */
0423 GIT_EXTERN(int) git_tree_dup(git_tree **out, git_tree *source);
0424 
0425 /**
0426  * The kind of update to perform
0427  */
0428 typedef enum {
0429     /** Update or insert an entry at the specified path */
0430     GIT_TREE_UPDATE_UPSERT,
0431     /** Remove an entry from the specified path */
0432     GIT_TREE_UPDATE_REMOVE
0433 } git_tree_update_t;
0434 
0435 /**
0436  * An action to perform during the update of a tree
0437  */
0438 typedef struct {
0439     /** Update action. If it's an removal, only the path is looked at */
0440     git_tree_update_t action;
0441     /** The entry's id */
0442     git_oid id;
0443     /** The filemode/kind of object */
0444     git_filemode_t filemode;
0445     /** The full path from the root tree */
0446     const char *path;
0447 } git_tree_update;
0448 
0449 /**
0450  * Create a tree based on another one with the specified modifications
0451  *
0452  * Given the `baseline` perform the changes described in the list of
0453  * `updates` and create a new tree.
0454  *
0455  * This function is optimized for common file/directory addition, removal and
0456  * replacement in trees. It is much more efficient than reading the tree into a
0457  * `git_index` and modifying that, but in exchange it is not as flexible.
0458  *
0459  * Deleting and adding the same entry is undefined behaviour, changing
0460  * a tree to a blob or viceversa is not supported.
0461  *
0462  * @param out id of the new tree
0463  * @param repo the repository in which to create the tree, must be the
0464  * same as for `baseline`
0465  * @param baseline the tree to base these changes on
0466  * @param nupdates the number of elements in the update list
0467  * @param updates the list of updates to perform
0468  * @return 0 or an error code
0469  */
0470 GIT_EXTERN(int) git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseline, size_t nupdates, const git_tree_update *updates);
0471 
0472 /** @} */
0473 
0474 GIT_END_DECL
0475 #endif