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