]> git.proxmox.com Git - libgit2.git/blame - include/git2/blob.h
Merge pull request #1208 from ethomson/ppc_sha1_asm_deadness
[libgit2.git] / include / git2 / blob.h
CommitLineData
f5918330 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
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"
237da401
VM
14
15/**
f5918330 16 * @file git2/blob.h
237da401
VM
17 * @brief Git blob load and write routines
18 * @defgroup git_blob Git blob load and write routines
19 * @ingroup Git
20 * @{
21 */
22GIT_BEGIN_DECL
23
237da401
VM
24/**
25 * Lookup a blob object from a repository.
237da401
VM
26 *
27 * @param blob pointer to the looked up blob
28 * @param repo the repo to use when locating the blob.
29 * @param id identity of the blob to locate.
e172cf08 30 * @return 0 or an error code
237da401 31 */
e52ed7a5
VM
32GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id)
33{
5de079b8 34 return git_object_lookup((git_object **)blob, repo, id, GIT_OBJ_BLOB);
e52ed7a5 35}
237da401 36
790c6c95
MP
37/**
38 * Lookup a blob object from a repository,
39 * given a prefix of its identifier (short id).
40 *
41 * @see git_object_lookup_prefix
42 *
43 * @param blob pointer to the looked up blob
44 * @param repo the repo to use when locating the blob.
45 * @param id identity of the blob to locate.
46 * @param len the length of the short identifier
e172cf08 47 * @return 0 or an error code
790c6c95 48 */
b8457baa 49GIT_INLINE(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len)
790c6c95
MP
50{
51 return git_object_lookup_prefix((git_object **)blob, repo, id, len, GIT_OBJ_BLOB);
52}
53
b0b83135
CMN
54/**
55 * Close an open blob
56 *
45e79e37 57 * This is a wrapper around git_object_free()
b0b83135
CMN
58 *
59 * IMPORTANT:
60 * It *is* necessary to call this method when you stop
61 * using a blob. Failure to do so will cause a memory leak.
62 *
63 * @param blob the blob to close
64 */
65
45e79e37 66GIT_INLINE(void) git_blob_free(git_blob *blob)
b0b83135 67{
45e79e37 68 git_object_free((git_object *) blob);
b0b83135
CMN
69}
70
cfbe4be3
VM
71/**
72 * Get the id of a blob.
73 *
74 * @param blob a previously loaded blob.
75 * @return SHA1 hash for this blob.
76 */
77GIT_INLINE(const git_oid *) git_blob_id(const git_blob *blob)
78{
79 return git_object_id((const git_object *)blob);
80}
81
b0b83135 82
237da401 83/**
30b171a1 84 * Get a read-only buffer with the raw content of a blob.
237da401 85 *
30b171a1
VM
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
72a3fe42 89 * time.
237da401
VM
90 *
91 * @param blob pointer to the blob
30b171a1 92 * @return the pointer; NULL if the blob has no contents
237da401 93 */
f2b7f7a6 94GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
237da401
VM
95
96/**
97 * Get the size in bytes of the contents of a blob
98 *
99 * @param blob pointer to the blob
100 * @return size on bytes
101 */
f2b7f7a6 102GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
237da401
VM
103
104/**
105 * Read a file from the working folder of a repository
72a3fe42 106 * and write it to the Object Database as a loose blob
237da401 107 *
cfbe4be3 108 * @param id return the id of the written blob
72a3fe42
VM
109 * @param repo repository where the blob will be written.
110 * this repository cannot be bare
cfbe4be3 111 * @param relative_path file from which the blob will be created,
72a3fe42 112 * relative to the repository's working dir
e172cf08 113 * @return 0 or an error code
72a3fe42 114 */
cfbe4be3 115GIT_EXTERN(int) git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path);
72a3fe42 116
6ca9643c 117/**
118 * Read a file from the filesystem and write its content
119 * to the Object Database as a loose blob
120 *
cfbe4be3 121 * @param id return the id of the written blob
6ca9643c 122 * @param repo repository where the blob will be written.
123 * this repository can be bare or not
124 * @param path file from which the blob will be created
e172cf08 125 * @return 0 or an error code
6ca9643c 126 */
cfbe4be3
VM
127GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path);
128
129
130typedef int (*git_blob_chunk_cb)(char *content, size_t max_length, void *payload);
6ca9643c 131
cd445767 132/**
133 * Write a loose blob to the Object Database from a
134 * provider of chunks of data.
135 *
136 * Provided the `hintpath` parameter is filled, its value
137 * will help to determine what git filters should be applied
138 * to the object before it can be placed to the object database.
139 *
140 *
141 * The implementation of the callback has to respect the
142 * following rules:
143 *
144 * - `content` will have to be filled by the consumer. The maximum number
145 * of bytes that the buffer can accept per call is defined by the
146 * `max_length` parameter. Allocation and freeing of the buffer will be taken
147 * care of by the function.
148 *
149 * - The callback is expected to return the number of bytes
150 * that `content` have been filled with.
151 *
152 * - When there is no more data to stream, the callback should
153 * return 0. This will prevent it from being invoked anymore.
154 *
155 * - When an error occurs, the callback should return -1.
156 *
157 *
cfbe4be3 158 * @param id Return the id of the written blob
cd445767 159 *
160 * @param repo repository where the blob will be written.
161 * This repository can be bare or not.
162 *
163 * @param hintpath if not NULL, will help selecting the filters
164 * to apply onto the content of the blob to be created.
165 *
166 * @return GIT_SUCCESS or an error code
167 */
168GIT_EXTERN(int) git_blob_create_fromchunks(
cfbe4be3 169 git_oid *id,
cd445767 170 git_repository *repo,
171 const char *hintpath,
cfbe4be3 172 git_blob_chunk_cb callback,
cd445767 173 void *payload);
72a3fe42
VM
174
175/**
176 * Write an in-memory buffer to the ODB as a blob
177 *
178 * @param oid return the oid of the written blob
179 * @param repo repository where to blob will be written
180 * @param buffer data to be written into the blob
181 * @param len length of the data
e172cf08 182 * @return 0 or an error code
237da401 183 */
72a3fe42 184GIT_EXTERN(int) git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len);
237da401 185
a3337f10 186/**
187 * Determine if the blob content is most certainly binary or not.
188 *
189 * The heuristic used to guess if a file is binary is taken from core git:
190 * Searching for NUL bytes and looking for a reasonable ratio of printable
191 * to non-printable characters among the first 4000 bytes.
192 *
193 * @param blob The blob which content should be analyzed
194 * @return 1 if the content of the blob is detected
195 * as binary; 0 otherwise.
196 */
197GIT_EXTERN(int) git_blob_is_binary(git_blob *blob);
198
237da401
VM
199/** @} */
200GIT_END_DECL
201#endif