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