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 /**
143  * The options used when applying filter options to a file.
144  */
145 struct git_blob_filter_options
146 {
147 	int version_;
148 
149 	/**
150 	 * Flags to control the filtering process, see `git_blob_filter_flag_t` above
151 	 */
152 	uint flags;
153 }
154 
155 enum GIT_BLOB_FILTER_OPTIONS_VERSION = 1;
156 
157 pragma(inline, true)
158 pure nothrow @safe @nogc
159 .git_blob_filter_options GIT_BLOB_FILTER_OPTIONS_INIT()
160 
161 	do
162 	{
163 		.git_blob_filter_options OUTPUT =
164 		{
165 			version_: .GIT_BLOB_FILTER_OPTIONS_VERSION,
166 			flags: .git_blob_filter_flag_t.GIT_BLOB_FILTER_CHECK_FOR_BINARY,
167 		};
168 
169 		return OUTPUT;
170 	}
171 
172 /**
173  * Get a buffer with the filtered content of a blob.
174  *
175  * This applies filters as if the blob was being checked out to the
176  * working directory under the specified filename.  This may apply
177  * CRLF filtering or other types of changes depending on the file
178  * attributes set for the blob and the content detected in it.
179  *
180  * The output is written into a `git_buf` which the caller must free
181  * when done (via `git_buf_dispose`).
182  *
183  * If no filters need to be applied, then the `out` buffer will just
184  * be populated with a pointer to the raw content of the blob.  In
185  * that case, be careful to *not* free the blob until done with the
186  * buffer or copy it into memory you own.
187  *
188  * Params:
189  *      out_ = The git_buf to be filled in
190  *      blob = Pointer to the blob
191  *      as_path = Path used for file attribute lookups, etc.
192  *      opts = Options to use for filtering the blob
193  *
194  * Returns: 0 on success or an error code
195  */
196 //GIT_EXTERN
197 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);
198 
199 /**
200  * Read a file from the working folder of a repository
201  * and write it to the Object Database as a loose blob
202  *
203  * Params:
204  *      id = return the id of the written blob
205  *      repo = repository where the blob will be written. this repository cannot be bare
206  *      relative_path = file from which the blob will be created, relative to the repository's working dir
207  *
208  * Returns: 0 or an error code
209  */
210 //GIT_EXTERN
211 int git_blob_create_from_workdir(libgit2_d.oid.git_oid* id, libgit2_d.types.git_repository* repo, const (char)* relative_path);
212 
213 /**
214  * Read a file from the filesystem and write its content
215  * to the Object Database as a loose blob
216  *
217  * Params:
218  *      id = return the id of the written blob
219  *      repo = repository where the blob will be written. this repository can be bare or not
220  *      path = file from which the blob will be created
221  *
222  * Returns: 0 or an error code
223  */
224 //GIT_EXTERN
225 int git_blob_create_from_disk(libgit2_d.oid.git_oid* id, libgit2_d.types.git_repository* repo, const (char)* path);
226 
227 /**
228  * Create a stream to write a new blob into the object db
229  *
230  * This function may need to buffer the data on disk and will in
231  * general not be the right choice if you know the size of the data
232  * to write. If you have data in memory, use
233  * `git_blob_create_from_buffer()`. If you do not, but know the size of
234  * the contents (and don't want/need to perform filtering), use
235  * `git_odb_open_wstream()`.
236  *
237  * Don't close this stream yourself but pass it to
238  * `git_blob_create_from_stream_commit()` to commit the write to the
239  * object db and get the object id.
240  *
241  * If the `hintpath` parameter is filled, it will be used to determine
242  * what git filters should be applied to the object before it is written
243  * to the object database.
244  *
245  * Params:
246  *      out_ = the stream into which to write
247  *      repo = Repository where the blob will be written. This repository can be bare or not.
248  *      hintpath = If not NULL, will be used to select data filters to apply onto the content of the blob to be created.
249  *
250  * Returns: 0 or error code
251  */
252 //GIT_EXTERN
253 int git_blob_create_from_stream(libgit2_d.types.git_writestream** out_, libgit2_d.types.git_repository* repo, const (char)* hintpath);
254 
255 /**
256  * Close the stream and write the blob to the object db
257  *
258  * The stream will be closed and freed.
259  *
260  * Params:
261  *      out_ = the id of the new blob
262  *      stream = the stream to close
263  *
264  * Returns: 0 or an error code
265  */
266 //GIT_EXTERN
267 int git_blob_create_from_stream_commit(libgit2_d.oid.git_oid* out_, libgit2_d.types.git_writestream* stream);
268 
269 /**
270  * Write an in-memory buffer to the ODB as a blob
271  *
272  * Params:
273  *      id = return the id of the written blob
274  *      repo = repository where to blob will be written
275  *      buffer = data to be written into the blob
276  *      len = length of the data
277  *
278  * Returns: 0 or an error code
279  */
280 //GIT_EXTERN
281 int git_blob_create_from_buffer(libgit2_d.oid.git_oid* id, libgit2_d.types.git_repository* repo, const (void)* buffer, size_t len);
282 
283 /**
284  * Determine if the blob content is most certainly binary or not.
285  *
286  * The heuristic used to guess if a file is binary is taken from core git:
287  * Searching for NUL bytes and looking for a reasonable ratio of printable
288  * to non-printable characters among the first 8000 bytes.
289  *
290  * Params:
291  *      blob = The blob which content should be analyzed
292  *
293  * Returns: 1 if the content of the blob is detected as binary; 0 otherwise.
294  */
295 //GIT_EXTERN
296 int git_blob_is_binary(const (libgit2_d.types.git_blob)* blob);
297 
298 /**
299  * Create an in-memory copy of a blob. The copy must be explicitly
300  * free'd or it will leak.
301  *
302  * Params:
303  *      out_ = Pointer to store the copy of the object
304  *      source = Original object to copy
305  */
306 //GIT_EXTERN
307 int git_blob_dup(libgit2_d.types.git_blob** out_, libgit2_d.types.git_blob* source);
308 
309 /** @} */