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.object;
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/object.h
20  * @brief Git revision object management routines
21  * @defgroup git_object Git revision object management routines
22  * @ingroup Git
23  * @{
24  */
25 extern (C):
26 nothrow @nogc:
27 public:
28 
29 enum GIT_OBJECT_SIZE_MAX = ulong.max;
30 
31 /**
32  * Lookup a reference to one of the objects in a repository.
33  *
34  * The generated reference is owned by the repository and
35  * should be closed with the `git_object_free` method
36  * instead of free'd manually.
37  *
38  * The 'type' parameter must match the type of the object
39  * in the odb; the method will fail otherwise.
40  * The special value 'git_object_t.GIT_OBJECT_ANY' may be passed to let
41  * the method guess the object's type.
42  *
43  * Params:
44  *      object = pointer to the looked-up object
45  *      repo = the repository to look up the object
46  *      id = the unique identifier for the object
47  *      type = the type of the object
48  *
49  * Returns: 0 or an error code
50  */
51 @GIT_EXTERN
52 int git_object_lookup(libgit2.types.git_object** object, libgit2.types.git_repository* repo, const (libgit2.oid.git_oid)* id, libgit2.types.git_object_t type);
53 
54 /**
55  * Lookup a reference to one of the objects in a repository,
56  * given a prefix of its identifier (short id).
57  *
58  * The object obtained will be so that its identifier
59  * matches the first 'len' hexadecimal characters
60  * (packets of 4 bits) of the given 'id'.
61  * 'len' must be at least GIT_OID_MINPREFIXLEN, and
62  * long enough to identify a unique object matching
63  * the prefix; otherwise the method will fail.
64  *
65  * The generated reference is owned by the repository and
66  * should be closed with the `git_object_free` method
67  * instead of free'd manually.
68  *
69  * The 'type' parameter must match the type of the object
70  * in the odb; the method will fail otherwise.
71  * The special value 'git_object_t.GIT_OBJECT_ANY' may be passed to let
72  * the method guess the object's type.
73  *
74  * Params:
75  *      object_out = pointer where to store the looked-up object
76  *      repo = the repository to look up the object
77  *      id = a short identifier for the object
78  *      len = the length of the short identifier
79  *      type = the type of the object
80  *
81  * Returns: 0 or an error code
82  */
83 @GIT_EXTERN
84 int git_object_lookup_prefix(libgit2.types.git_object** object_out, libgit2.types.git_repository* repo, const (libgit2.oid.git_oid)* id, size_t len, libgit2.types.git_object_t type);
85 
86 /**
87  * Lookup an object that represents a tree entry.
88  *
89  * Params:
90  *      out_ = buffer that receives a pointer to the object (which must be freed by the caller)
91  *      treeish = root object that can be peeled to a tree
92  *      path = relative path from the root object to the desired object
93  *      type = type of object desired
94  *
95  * Returns: 0 on success, or an error code
96  */
97 @GIT_EXTERN
98 int git_object_lookup_bypath(libgit2.types.git_object** out_, const (libgit2.types.git_object)* treeish, const (char)* path, libgit2.types.git_object_t type);
99 
100 /**
101  * Get the id (SHA1) of a repository object
102  *
103  * Params:
104  *      obj = the repository object
105  *
106  * Returns: the SHA1 id
107  */
108 @GIT_EXTERN
109 const (libgit2.oid.git_oid)* git_object_id(const (libgit2.types.git_object)* obj);
110 
111 /**
112  * Get a short abbreviated OID string for the object
113  *
114  * This starts at the "core.abbrev" length (default 7 characters) and
115  * iteratively extends to a longer string if that length is ambiguous.
116  * The result will be unambiguous (at least until new objects are added to
117  * the repository).
118  *
119  * Params:
120  *      out_ = Buffer to write string into
121  *      obj = The object to get an ID for
122  *
123  * Returns: 0 on success, <0 for error
124  */
125 @GIT_EXTERN
126 int git_object_short_id(libgit2.buffer.git_buf* out_, const (libgit2.types.git_object)* obj);
127 
128 /**
129  * Get the object type of an object
130  *
131  * Params:
132  *      obj = the repository object
133  *
134  * Returns: the object's type
135  */
136 @GIT_EXTERN
137 libgit2.types.git_object_t git_object_type(const (libgit2.types.git_object)* obj);
138 
139 /**
140  * Get the repository that owns this object
141  *
142  * Freeing or calling `git_repository_close` on the
143  * returned pointer will invalidate the actual object.
144  *
145  * Any other operation may be run on the repository without
146  * affecting the object.
147  *
148  * Params:
149  *      obj = the object
150  *
151  * Returns: the repository who owns this object
152  */
153 @GIT_EXTERN
154 libgit2.types.git_repository* git_object_owner(const (libgit2.types.git_object)* obj);
155 
156 /**
157  * Close an open object
158  *
159  * This method instructs the library to close an existing
160  * object; note that git_objects are owned and cached by the repository
161  * so the object may or may not be freed after this library call,
162  * depending on how aggressive is the caching mechanism used
163  * by the repository.
164  *
165  * IMPORTANT:
166  * It *is* necessary to call this method when you stop using
167  * an object. Failure to do so will cause a memory leak.
168  *
169  * Params:
170  *      object = the object to close
171  */
172 @GIT_EXTERN
173 void git_object_free(libgit2.types.git_object* object);
174 
175 /**
176  * Convert an object type to its string representation.
177  *
178  * The result is a pointer to a string in static memory and
179  * should not be free()'ed.
180  *
181  * Params:
182  *      type = object type to convert.
183  *
184  * Returns: the corresponding string representation.
185  */
186 @GIT_EXTERN
187 const (char)* git_object_type2string(libgit2.types.git_object_t type);
188 
189 /**
190  * Convert a string object type representation to it's libgit2.types.git_object_t.
191  *
192  * Params:
193  *      str = the string to convert.
194  *
195  * Returns: the corresponding libgit2.types.git_object_t.
196  */
197 @GIT_EXTERN
198 libgit2.types.git_object_t git_object_string2type(const (char)* str);
199 
200 /**
201  * Determine if the given libgit2.types.git_object_t is a valid loose object type.
202  *
203  * Params:
204  *      type = object type to test.
205  *
206  * Returns: true if the type represents a valid loose object type, false otherwise.
207  */
208 @GIT_EXTERN
209 int git_object_typeisloose(libgit2.types.git_object_t type);
210 
211 /**
212  * Recursively peel an object until an object of the specified type is met.
213  *
214  * If the query cannot be satisfied due to the object model,
215  * git_error_code.GIT_EINVALIDSPEC will be returned (e.g. trying to peel a blob to a
216  * tree).
217  *
218  * If you pass `git_object_t.GIT_OBJECT_ANY` as the target type, then the object will
219  * be peeled until the type changes. A tag will be peeled until the
220  * referenced object is no longer a tag, and a commit will be peeled
221  * to a tree. Any other object type will return git_error_code.GIT_EINVALIDSPEC.
222  *
223  * If peeling a tag we discover an object which cannot be peeled to
224  * the target type due to the object model, git_error_code.GIT_EPEEL will be
225  * returned.
226  *
227  * You must free the returned object.
228  *
229  * Params:
230  *      peeled = Pointer to the peeled git_object
231  *      object = The object to be processed
232  *      target_type = The type of the requested object (a GIT_OBJECT_ value)
233  *
234  * Returns: 0 on success, git_error_code.GIT_EINVALIDSPEC, git_error_code.GIT_EPEEL, or an error code
235  */
236 @GIT_EXTERN
237 int git_object_peel(libgit2.types.git_object** peeled, const (libgit2.types.git_object)* object, libgit2.types.git_object_t target_type);
238 
239 /**
240  * Create an in-memory copy of a Git object. The copy must be
241  * explicitly free'd or it will leak.
242  *
243  * Params:
244  *      dest = Pointer to store the copy of the object
245  *      source = Original object to copy
246  *
247  * Returns: 0 or an error code
248  */
249 @GIT_EXTERN
250 int git_object_dup(libgit2.types.git_object** dest, libgit2.types.git_object* source);
251 
252 version (GIT_EXPERIMENTAL_SHA256) {
253 	/**
254 	 * Analyzes a buffer of raw object content and determines its validity.
255 	 * Tree, commit, and tag objects will be parsed and ensured that they
256 	 * are valid, parseable content.  (Blobs are always valid by definition.)
257 	 * An error message will be set with an informative message if the object
258 	 * is not valid.
259 	 *
260 	 * @warning This function is experimental and its signature may change in the future.
261 	 *
262 	 * Params:
263 	 *      valid = Output pointer to set with validity of the object content
264 	 *      buf = The contents to validate
265 	 *      len = The length of the buffer
266 	 *      object_type = The type of the object in the buffer
267 	 *      oid_type = The object ID type for the OIDs in the given buffer
268 	 *
269 	 * Returns: 0 on success or an error code
270 	 */
271 	@GIT_EXTERN
272 	int git_object_rawcontent_is_valid(int* valid, const (char)* buf, size_t len, libgit2.types.git_object_t object_type, libgit2.oid.git_oid_t oid_type);
273 } else {
274 	/**
275 	 * Analyzes a buffer of raw object content and determines its validity.
276 	 * Tree, commit, and tag objects will be parsed and ensured that they
277 	 * are valid, parseable content.  (Blobs are always valid by definition.)
278 	 * An error message will be set with an informative message if the object
279 	 * is not valid.
280 	 *
281 	 * @warning This function is experimental and its signature may change in the future.
282 	 *
283 	 * Params:
284 	 *      valid = Output pointer to set with validity of the object content
285 	 *      buf = The contents to validate
286 	 *      len = The length of the buffer
287 	 *      type = The type of the object in the buffer
288 	 *
289 	 * Returns: 0 on success or an error code
290 	 */
291 	@GIT_EXTERN
292 	int git_object_rawcontent_is_valid(int* valid, const (char)* buf, size_t len, libgit2.types.git_object_t type);
293 }
294 
295 /* @} */