]> git.proxmox.com Git - libgit2.git/blame - include/git2/blob.h
New upstream version 0.28.1+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
6adcaab7 87 * @return the pointer
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 */
f2b7f7a6 97GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
237da401 98
0cf77103
RB
99/**
100 * Get a buffer with the filtered content of a blob.
101 *
102 * This applies filters as if the blob was being checked out to the
103 * working directory under the specified filename. This may apply
104 * CRLF filtering or other types of changes depending on the file
105 * attributes set for the blob and the content detected in it.
106 *
a9f51e43 107 * The output is written into a `git_buf` which the caller must free
ac3d33df 108 * when done (via `git_buf_dispose`).
0cf77103 109 *
bd470d00
CMN
110 * If no filters need to be applied, then the `out` buffer will just
111 * be populated with a pointer to the raw content of the blob. In
112 * that case, be careful to *not* free the blob until done with the
113 * buffer or copy it into memory you own.
0cf77103 114 *
a9f51e43 115 * @param out The git_buf to be filled in
0cf77103
RB
116 * @param blob Pointer to the blob
117 * @param as_path Path used for file attribute lookups, etc.
118 * @param check_for_binary_data Should this test if blob content contains
119 * NUL bytes / looks like binary data before applying filters?
120 * @return 0 on success or an error code
121 */
122GIT_EXTERN(int) git_blob_filtered_content(
a9f51e43 123 git_buf *out,
0cf77103
RB
124 git_blob *blob,
125 const char *as_path,
126 int check_for_binary_data);
127
237da401
VM
128/**
129 * Read a file from the working folder of a repository
72a3fe42 130 * and write it to the Object Database as a loose blob
237da401 131 *
cfbe4be3 132 * @param id return the id of the written blob
72a3fe42
VM
133 * @param repo repository where the blob will be written.
134 * this repository cannot be bare
cfbe4be3 135 * @param relative_path file from which the blob will be created,
72a3fe42 136 * relative to the repository's working dir
e172cf08 137 * @return 0 or an error code
72a3fe42 138 */
cfbe4be3 139GIT_EXTERN(int) git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path);
72a3fe42 140
6ca9643c 141/**
142 * Read a file from the filesystem and write its content
143 * to the Object Database as a loose blob
144 *
cfbe4be3 145 * @param id return the id of the written blob
6ca9643c 146 * @param repo repository where the blob will be written.
147 * this repository can be bare or not
148 * @param path file from which the blob will be created
e172cf08 149 * @return 0 or an error code
6ca9643c 150 */
cfbe4be3
VM
151GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path);
152
0a5c6028
CMN
153/**
154 * Create a stream to write a new blob into the object db
155 *
156 * This function may need to buffer the data on disk and will in
157 * general not be the right choice if you know the size of the data
158 * to write. If you have data in memory, use
159 * `git_blob_create_frombuffer()`. If you do not, but know the size of
160 * the contents (and don't want/need to perform filtering), use
161 * `git_odb_open_wstream()`.
162 *
163 * Don't close this stream yourself but pass it to
164 * `git_blob_create_fromstream_commit()` to commit the write to the
165 * object db and get the object id.
166 *
167 * If the `hintpath` parameter is filled, it will be used to determine
168 * what git filters should be applied to the object before it is written
169 * to the object database.
170 *
171 * @param out the stream into which to write
172 * @param repo Repository where the blob will be written.
173 * This repository can be bare or not.
174 * @param hintpath If not NULL, will be used to select data filters
175 * to apply onto the content of the blob to be created.
176 * @return 0 or error code
177 */
178GIT_EXTERN(int) git_blob_create_fromstream(
179 git_writestream **out,
180 git_repository *repo,
181 const char *hintpath);
182
183/**
184 * Close the stream and write the blob to the object db
185 *
186 * The stream will be closed and freed.
187 *
188 * @param out the id of the new blob
189 * @param stream the stream to close
190 * @return 0 or an error code
191 */
192GIT_EXTERN(int) git_blob_create_fromstream_commit(
193 git_oid *out,
194 git_writestream *stream);
195
72a3fe42
VM
196/**
197 * Write an in-memory buffer to the ODB as a blob
198 *
5572d2b8 199 * @param id return the id of the written blob
72a3fe42
VM
200 * @param repo repository where to blob will be written
201 * @param buffer data to be written into the blob
202 * @param len length of the data
e172cf08 203 * @return 0 or an error code
237da401 204 */
5572d2b8
RB
205GIT_EXTERN(int) git_blob_create_frombuffer(
206 git_oid *id, git_repository *repo, const void *buffer, size_t len);
237da401 207
a3337f10 208/**
209 * Determine if the blob content is most certainly binary or not.
210 *
211 * The heuristic used to guess if a file is binary is taken from core git:
212 * Searching for NUL bytes and looking for a reasonable ratio of printable
fda73bc5 213 * to non-printable characters among the first 8000 bytes.
a3337f10 214 *
215 * @param blob The blob which content should be analyzed
216 * @return 1 if the content of the blob is detected
217 * as binary; 0 otherwise.
218 */
3b4ba278 219GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob);
a3337f10 220
f0224772
ET
221/**
222 * Create an in-memory copy of a blob. The copy must be explicitly
223 * free'd or it will leak.
224 *
225 * @param out Pointer to store the copy of the object
226 * @param source Original object to copy
227 */
228GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source);
229
237da401
VM
230/** @} */
231GIT_END_DECL
232#endif