Back to home page

EIC code displayed by LXR

 
 

    


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

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_blob_h__
0008 #define INCLUDE_git_blob_h__
0009 
0010 #include "common.h"
0011 #include "types.h"
0012 #include "oid.h"
0013 #include "object.h"
0014 #include "buffer.h"
0015 
0016 /**
0017  * @file git2/blob.h
0018  * @brief Git blob load and write routines
0019  * @defgroup git_blob Git blob load and write routines
0020  * @ingroup Git
0021  * @{
0022  */
0023 GIT_BEGIN_DECL
0024 
0025 /**
0026  * Lookup a blob object from a repository.
0027  *
0028  * @param blob pointer to the looked up blob
0029  * @param repo the repo to use when locating the blob.
0030  * @param id identity of the blob to locate.
0031  * @return 0 or an error code
0032  */
0033 GIT_EXTERN(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id);
0034 
0035 /**
0036  * Lookup a blob object from a repository,
0037  * given a prefix of its identifier (short id).
0038  *
0039  * @see git_object_lookup_prefix
0040  *
0041  * @param blob pointer to the looked up blob
0042  * @param repo the repo to use when locating the blob.
0043  * @param id identity of the blob to locate.
0044  * @param len the length of the short identifier
0045  * @return 0 or an error code
0046  */
0047 GIT_EXTERN(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len);
0048 
0049 /**
0050  * Close an open blob
0051  *
0052  * This is a wrapper around git_object_free()
0053  *
0054  * IMPORTANT:
0055  * It *is* necessary to call this method when you stop
0056  * using a blob. Failure to do so will cause a memory leak.
0057  *
0058  * @param blob the blob to close
0059  */
0060 GIT_EXTERN(void) git_blob_free(git_blob *blob);
0061 
0062 /**
0063  * Get the id of a blob.
0064  *
0065  * @param blob a previously loaded blob.
0066  * @return SHA1 hash for this blob.
0067  */
0068 GIT_EXTERN(const git_oid *) git_blob_id(const git_blob *blob);
0069 
0070 /**
0071  * Get the repository that contains the blob.
0072  *
0073  * @param blob A previously loaded blob.
0074  * @return Repository that contains this blob.
0075  */
0076 GIT_EXTERN(git_repository *) git_blob_owner(const git_blob *blob);
0077 
0078 /**
0079  * Get a read-only buffer with the raw content of a blob.
0080  *
0081  * A pointer to the raw content of a blob is returned;
0082  * this pointer is owned internally by the object and shall
0083  * not be free'd. The pointer may be invalidated at a later
0084  * time.
0085  *
0086  * @param blob pointer to the blob
0087  * @return the pointer, or NULL on error
0088  */
0089 GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
0090 
0091 /**
0092  * Get the size in bytes of the contents of a blob
0093  *
0094  * @param blob pointer to the blob
0095  * @return size on bytes
0096  */
0097 GIT_EXTERN(git_object_size_t) git_blob_rawsize(const git_blob *blob);
0098 
0099 /**
0100  * Flags to control the functionality of `git_blob_filter`.
0101  */
0102 typedef enum {
0103     /** When set, filters will not be applied to binary files. */
0104     GIT_BLOB_FILTER_CHECK_FOR_BINARY = (1 << 0),
0105 
0106     /**
0107      * When set, filters will not load configuration from the
0108      * system-wide `gitattributes` in `/etc` (or system equivalent).
0109      */
0110     GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES = (1 << 1),
0111 
0112     /**
0113      * When set, filters will be loaded from a `.gitattributes` file
0114      * in the HEAD commit.
0115      */
0116     GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD = (1 << 2),
0117 
0118     /**
0119      * When set, filters will be loaded from a `.gitattributes` file
0120      * in the specified commit.
0121      */
0122     GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT = (1 << 3)
0123 } git_blob_filter_flag_t;
0124 
0125 /**
0126  * The options used when applying filter options to a file.
0127  *
0128  * Initialize with `GIT_BLOB_FILTER_OPTIONS_INIT`. Alternatively, you can
0129  * use `git_blob_filter_options_init`.
0130  *
0131  */
0132 typedef struct {
0133     int version;
0134 
0135     /** Flags to control the filtering process, see `git_blob_filter_flag_t` above */
0136     uint32_t flags;
0137 
0138 #ifdef GIT_DEPRECATE_HARD
0139     void *reserved;
0140 #else
0141     git_oid *commit_id;
0142 #endif
0143 
0144     /**
0145      * The commit to load attributes from, when
0146      * `GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT` is specified.
0147      */
0148     git_oid attr_commit_id;
0149 } git_blob_filter_options;
0150 
0151 #define GIT_BLOB_FILTER_OPTIONS_VERSION 1
0152 #define GIT_BLOB_FILTER_OPTIONS_INIT {GIT_BLOB_FILTER_OPTIONS_VERSION, GIT_BLOB_FILTER_CHECK_FOR_BINARY}
0153 
0154 /**
0155  * Initialize git_blob_filter_options structure
0156  *
0157  * Initializes a `git_blob_filter_options` with default values. Equivalent
0158  * to creating an instance with `GIT_BLOB_FILTER_OPTIONS_INIT`.
0159  *
0160  * @param opts The `git_blob_filter_options` struct to initialize.
0161  * @param version The struct version; pass `GIT_BLOB_FILTER_OPTIONS_VERSION`.
0162  * @return Zero on success; -1 on failure.
0163  */
0164 GIT_EXTERN(int) git_blob_filter_options_init(git_blob_filter_options *opts, unsigned int version);
0165 
0166 /**
0167  * Get a buffer with the filtered content of a blob.
0168  *
0169  * This applies filters as if the blob was being checked out to the
0170  * working directory under the specified filename.  This may apply
0171  * CRLF filtering or other types of changes depending on the file
0172  * attributes set for the blob and the content detected in it.
0173  *
0174  * The output is written into a `git_buf` which the caller must free
0175  * when done (via `git_buf_dispose`).
0176  *
0177  * If no filters need to be applied, then the `out` buffer will just
0178  * be populated with a pointer to the raw content of the blob.  In
0179  * that case, be careful to *not* free the blob until done with the
0180  * buffer or copy it into memory you own.
0181  *
0182  * @param out The git_buf to be filled in
0183  * @param blob Pointer to the blob
0184  * @param as_path Path used for file attribute lookups, etc.
0185  * @param opts Options to use for filtering the blob
0186  * @return 0 on success or an error code
0187  */
0188 GIT_EXTERN(int) git_blob_filter(
0189     git_buf *out,
0190     git_blob *blob,
0191     const char *as_path,
0192     git_blob_filter_options *opts);
0193 
0194 /**
0195  * Read a file from the working folder of a repository
0196  * and write it to the Object Database as a loose blob
0197  *
0198  * @param id return the id of the written blob
0199  * @param repo repository where the blob will be written.
0200  *  this repository cannot be bare
0201  * @param relative_path file from which the blob will be created,
0202  *  relative to the repository's working dir
0203  * @return 0 or an error code
0204  */
0205 GIT_EXTERN(int) git_blob_create_from_workdir(git_oid *id, git_repository *repo, const char *relative_path);
0206 
0207 /**
0208  * Read a file from the filesystem and write its content
0209  * to the Object Database as a loose blob
0210  *
0211  * @param id return the id of the written blob
0212  * @param repo repository where the blob will be written.
0213  *  this repository can be bare or not
0214  * @param path file from which the blob will be created
0215  * @return 0 or an error code
0216  */
0217 GIT_EXTERN(int) git_blob_create_from_disk(git_oid *id, git_repository *repo, const char *path);
0218 
0219 /**
0220  * Create a stream to write a new blob into the object db
0221  *
0222  * This function may need to buffer the data on disk and will in
0223  * general not be the right choice if you know the size of the data
0224  * to write. If you have data in memory, use
0225  * `git_blob_create_from_buffer()`. If you do not, but know the size of
0226  * the contents (and don't want/need to perform filtering), use
0227  * `git_odb_open_wstream()`.
0228  *
0229  * Don't close this stream yourself but pass it to
0230  * `git_blob_create_from_stream_commit()` to commit the write to the
0231  * object db and get the object id.
0232  *
0233  * If the `hintpath` parameter is filled, it will be used to determine
0234  * what git filters should be applied to the object before it is written
0235  * to the object database.
0236  *
0237  * @param out the stream into which to write
0238  * @param repo Repository where the blob will be written.
0239  *        This repository can be bare or not.
0240  * @param hintpath If not NULL, will be used to select data filters
0241  *        to apply onto the content of the blob to be created.
0242  * @return 0 or error code
0243  */
0244 GIT_EXTERN(int) git_blob_create_from_stream(
0245     git_writestream **out,
0246     git_repository *repo,
0247     const char *hintpath);
0248 
0249 /**
0250  * Close the stream and write the blob to the object db
0251  *
0252  * The stream will be closed and freed.
0253  *
0254  * @param out the id of the new blob
0255  * @param stream the stream to close
0256  * @return 0 or an error code
0257  */
0258 GIT_EXTERN(int) git_blob_create_from_stream_commit(
0259     git_oid *out,
0260     git_writestream *stream);
0261 
0262 /**
0263  * Write an in-memory buffer to the ODB as a blob
0264  *
0265  * @param id return the id of the written blob
0266  * @param repo repository where the blob will be written
0267  * @param buffer data to be written into the blob
0268  * @param len length of the data
0269  * @return 0 or an error code
0270  */
0271 GIT_EXTERN(int) git_blob_create_from_buffer(
0272     git_oid *id, git_repository *repo, const void *buffer, size_t len);
0273 
0274 /**
0275  * Determine if the blob content is most certainly binary or not.
0276  *
0277  * The heuristic used to guess if a file is binary is taken from core git:
0278  * Searching for NUL bytes and looking for a reasonable ratio of printable
0279  * to non-printable characters among the first 8000 bytes.
0280  *
0281  * @param blob The blob which content should be analyzed
0282  * @return 1 if the content of the blob is detected
0283  * as binary; 0 otherwise.
0284  */
0285 GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob);
0286 
0287 /**
0288  * Determine if the given content is most certainly binary or not;
0289  * this is the same mechanism used by `git_blob_is_binary` but only
0290  * looking at raw data.
0291  *
0292  * @param data The blob data which content should be analyzed
0293  * @param len The length of the data
0294  * @return 1 if the content of the blob is detected
0295  * as binary; 0 otherwise.
0296  */
0297 GIT_EXTERN(int) git_blob_data_is_binary(const char *data, size_t len);
0298 
0299 /**
0300  * Create an in-memory copy of a blob. The copy must be explicitly
0301  * free'd or it will leak.
0302  *
0303  * @param out Pointer to store the copy of the object
0304  * @param source Original object to copy
0305  * @return 0.
0306  */
0307 GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source);
0308 
0309 /** @} */
0310 GIT_END_DECL
0311 #endif