]> git.proxmox.com Git - libgit2.git/blame - include/git2/blob.h
New upstream version 1.4.3+dfsg.1
[libgit2.git] / include / git2 / blob.h
CommitLineData
f5918330 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
f5918330 3 *
bb742ede
VM
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
f5918330 6 */
237da401
VM
7#ifndef INCLUDE_git_blob_h__
8#define INCLUDE_git_blob_h__
9
10#include "common.h"
d12299fe 11#include "types.h"
237da401 12#include "oid.h"
5de079b8 13#include "object.h"
0cf77103 14#include "buffer.h"
237da401
VM
15
16/**
f5918330 17 * @file git2/blob.h
237da401
VM
18 * @brief Git blob load and write routines
19 * @defgroup git_blob Git blob load and write routines
20 * @ingroup Git
21 * @{
22 */
23GIT_BEGIN_DECL
24
237da401
VM
25/**
26 * Lookup a blob object from a repository.
237da401
VM
27 *
28 * @param blob pointer to the looked up blob
29 * @param repo the repo to use when locating the blob.
30 * @param id identity of the blob to locate.
e172cf08 31 * @return 0 or an error code
237da401 32 */
d7761102 33GIT_EXTERN(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id);
237da401 34
790c6c95
MP
35/**
36 * Lookup a blob object from a repository,
37 * given a prefix of its identifier (short id).
38 *
39 * @see git_object_lookup_prefix
40 *
41 * @param blob pointer to the looked up blob
42 * @param repo the repo to use when locating the blob.
43 * @param id identity of the blob to locate.
44 * @param len the length of the short identifier
e172cf08 45 * @return 0 or an error code
790c6c95 46 */
d7761102 47GIT_EXTERN(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len);
790c6c95 48
b0b83135
CMN
49/**
50 * Close an open blob
51 *
45e79e37 52 * This is a wrapper around git_object_free()
b0b83135
CMN
53 *
54 * IMPORTANT:
55 * It *is* necessary to call this method when you stop
56 * using a blob. Failure to do so will cause a memory leak.
57 *
58 * @param blob the blob to close
59 */
d7761102 60GIT_EXTERN(void) git_blob_free(git_blob *blob);
b0b83135 61
cfbe4be3
VM
62/**
63 * Get the id of a blob.
64 *
65 * @param blob a previously loaded blob.
66 * @return SHA1 hash for this blob.
67 */
d7761102 68GIT_EXTERN(const git_oid *) git_blob_id(const git_blob *blob);
cfbe4be3 69
d7761102
RB
70/**
71 * Get the repository that contains the blob.
72 *
73 * @param blob A previously loaded blob.
74 * @return Repository that contains this blob.
75 */
76GIT_EXTERN(git_repository *) git_blob_owner(const git_blob *blob);
b0b83135 77
237da401 78/**
30b171a1 79 * Get a read-only buffer with the raw content of a blob.
237da401 80 *
30b171a1
VM
81 * A pointer to the raw content of a blob is returned;
82 * this pointer is owned internally by the object and shall
83 * not be free'd. The pointer may be invalidated at a later
72a3fe42 84 * time.
237da401
VM
85 *
86 * @param blob pointer to the blob
c25aa7cd 87 * @return the pointer, or NULL on error
237da401 88 */
f2b7f7a6 89GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
237da401
VM
90
91/**
92 * Get the size in bytes of the contents of a blob
93 *
94 * @param blob pointer to the blob
95 * @return size on bytes
96 */
22a2d3d5
UG
97GIT_EXTERN(git_object_size_t) git_blob_rawsize(const git_blob *blob);
98
99/**
100 * Flags to control the functionality of `git_blob_filter`.
101 */
102typedef enum {
103 /** When set, filters will not be applied to binary files. */
104 GIT_BLOB_FILTER_CHECK_FOR_BINARY = (1 << 0),
105
106 /**
107 * When set, filters will not load configuration from the
108 * system-wide `gitattributes` in `/etc` (or system equivalent).
109 */
110 GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES = (1 << 1),
111
112 /**
113 * When set, filters will be loaded from a `.gitattributes` file
114 * in the HEAD commit.
115 */
c25aa7cd
PP
116 GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD = (1 << 2),
117
118 /**
119 * When set, filters will be loaded from a `.gitattributes` file
120 * in the specified commit.
121 */
e579e0f7 122 GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT = (1 << 3)
22a2d3d5
UG
123} git_blob_filter_flag_t;
124
125/**
126 * The options used when applying filter options to a file.
c25aa7cd
PP
127 *
128 * Initialize with `GIT_BLOB_FILTER_OPTIONS_INIT`. Alternatively, you can
129 * use `git_blob_filter_options_init`.
130 *
22a2d3d5
UG
131 */
132typedef struct {
133 int version;
134
135 /** Flags to control the filtering process, see `git_blob_filter_flag_t` above */
136 uint32_t flags;
c25aa7cd
PP
137
138#ifdef GIT_DEPRECATE_HARD
139 void *reserved;
140#else
141 git_oid *commit_id;
142#endif
143
144 /**
145 * The commit to load attributes from, when
146 * `GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT` is specified.
147 */
148 git_oid attr_commit_id;
22a2d3d5
UG
149} git_blob_filter_options;
150
151#define GIT_BLOB_FILTER_OPTIONS_VERSION 1
152#define GIT_BLOB_FILTER_OPTIONS_INIT {GIT_BLOB_FILTER_OPTIONS_VERSION, GIT_BLOB_FILTER_CHECK_FOR_BINARY}
237da401 153
c25aa7cd
PP
154/**
155 * Initialize git_blob_filter_options structure
156 *
157 * Initializes a `git_blob_filter_options` with default values. Equivalent
158 * to creating an instance with `GIT_BLOB_FILTER_OPTIONS_INIT`.
159 *
160 * @param opts The `git_blob_filter_options` struct to initialize.
161 * @param version The struct version; pass `GIT_BLOB_FILTER_OPTIONS_VERSION`.
162 * @return Zero on success; -1 on failure.
163 */
164GIT_EXTERN(int) git_blob_filter_options_init(git_blob_filter_options *opts, unsigned int version);
165
0cf77103
RB
166/**
167 * Get a buffer with the filtered content of a blob.
168 *
169 * This applies filters as if the blob was being checked out to the
170 * working directory under the specified filename. This may apply
171 * CRLF filtering or other types of changes depending on the file
172 * attributes set for the blob and the content detected in it.
173 *
a9f51e43 174 * The output is written into a `git_buf` which the caller must free
ac3d33df 175 * when done (via `git_buf_dispose`).
0cf77103 176 *
bd470d00
CMN
177 * If no filters need to be applied, then the `out` buffer will just
178 * be populated with a pointer to the raw content of the blob. In
179 * that case, be careful to *not* free the blob until done with the
180 * buffer or copy it into memory you own.
0cf77103 181 *
a9f51e43 182 * @param out The git_buf to be filled in
0cf77103
RB
183 * @param blob Pointer to the blob
184 * @param as_path Path used for file attribute lookups, etc.
22a2d3d5 185 * @param opts Options to use for filtering the blob
0cf77103
RB
186 * @return 0 on success or an error code
187 */
22a2d3d5 188GIT_EXTERN(int) git_blob_filter(
a9f51e43 189 git_buf *out,
0cf77103
RB
190 git_blob *blob,
191 const char *as_path,
22a2d3d5 192 git_blob_filter_options *opts);
0cf77103 193
237da401
VM
194/**
195 * Read a file from the working folder of a repository
72a3fe42 196 * and write it to the Object Database as a loose blob
237da401 197 *
cfbe4be3 198 * @param id return the id of the written blob
72a3fe42
VM
199 * @param repo repository where the blob will be written.
200 * this repository cannot be bare
cfbe4be3 201 * @param relative_path file from which the blob will be created,
72a3fe42 202 * relative to the repository's working dir
e172cf08 203 * @return 0 or an error code
72a3fe42 204 */
22a2d3d5 205GIT_EXTERN(int) git_blob_create_from_workdir(git_oid *id, git_repository *repo, const char *relative_path);
72a3fe42 206
6ca9643c 207/**
208 * Read a file from the filesystem and write its content
209 * to the Object Database as a loose blob
210 *
cfbe4be3 211 * @param id return the id of the written blob
6ca9643c 212 * @param repo repository where the blob will be written.
213 * this repository can be bare or not
214 * @param path file from which the blob will be created
e172cf08 215 * @return 0 or an error code
6ca9643c 216 */
22a2d3d5 217GIT_EXTERN(int) git_blob_create_from_disk(git_oid *id, git_repository *repo, const char *path);
cfbe4be3 218
0a5c6028
CMN
219/**
220 * Create a stream to write a new blob into the object db
221 *
222 * This function may need to buffer the data on disk and will in
223 * general not be the right choice if you know the size of the data
224 * to write. If you have data in memory, use
22a2d3d5 225 * `git_blob_create_from_buffer()`. If you do not, but know the size of
0a5c6028
CMN
226 * the contents (and don't want/need to perform filtering), use
227 * `git_odb_open_wstream()`.
228 *
229 * Don't close this stream yourself but pass it to
22a2d3d5 230 * `git_blob_create_from_stream_commit()` to commit the write to the
0a5c6028
CMN
231 * object db and get the object id.
232 *
233 * If the `hintpath` parameter is filled, it will be used to determine
234 * what git filters should be applied to the object before it is written
235 * to the object database.
236 *
237 * @param out the stream into which to write
238 * @param repo Repository where the blob will be written.
239 * This repository can be bare or not.
240 * @param hintpath If not NULL, will be used to select data filters
241 * to apply onto the content of the blob to be created.
242 * @return 0 or error code
243 */
22a2d3d5 244GIT_EXTERN(int) git_blob_create_from_stream(
0a5c6028
CMN
245 git_writestream **out,
246 git_repository *repo,
247 const char *hintpath);
248
249/**
250 * Close the stream and write the blob to the object db
251 *
252 * The stream will be closed and freed.
253 *
254 * @param out the id of the new blob
255 * @param stream the stream to close
256 * @return 0 or an error code
257 */
22a2d3d5 258GIT_EXTERN(int) git_blob_create_from_stream_commit(
0a5c6028
CMN
259 git_oid *out,
260 git_writestream *stream);
261
72a3fe42
VM
262/**
263 * Write an in-memory buffer to the ODB as a blob
264 *
5572d2b8 265 * @param id return the id of the written blob
c25aa7cd 266 * @param repo repository where the blob will be written
72a3fe42
VM
267 * @param buffer data to be written into the blob
268 * @param len length of the data
e172cf08 269 * @return 0 or an error code
237da401 270 */
22a2d3d5 271GIT_EXTERN(int) git_blob_create_from_buffer(
5572d2b8 272 git_oid *id, git_repository *repo, const void *buffer, size_t len);
237da401 273
a3337f10 274/**
275 * Determine if the blob content is most certainly binary or not.
276 *
277 * The heuristic used to guess if a file is binary is taken from core git:
278 * Searching for NUL bytes and looking for a reasonable ratio of printable
fda73bc5 279 * to non-printable characters among the first 8000 bytes.
a3337f10 280 *
281 * @param blob The blob which content should be analyzed
282 * @return 1 if the content of the blob is detected
283 * as binary; 0 otherwise.
284 */
3b4ba278 285GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob);
a3337f10 286
e579e0f7
MB
287/**
288 * Determine if the given content is most certainly binary or not;
289 * this is the same mechanism used by `git_blob_is_binary` but only
290 * looking at raw data.
291 *
292 * @param data The blob data which content should be analyzed
293 * @param len The length of the data
294 * @return 1 if the content of the blob is detected
295 * as binary; 0 otherwise.
296 */
297GIT_EXTERN(int) git_blob_data_is_binary(const char *data, size_t len);
298
f0224772
ET
299/**
300 * Create an in-memory copy of a blob. The copy must be explicitly
301 * free'd or it will leak.
302 *
303 * @param out Pointer to store the copy of the object
304 * @param source Original object to copy
e579e0f7 305 * @return 0.
f0224772
ET
306 */
307GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source);
308
237da401
VM
309/** @} */
310GIT_END_DECL
311#endif