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