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.oid;
8 
9 
10 /**
11  * @file git2/oid.h
12  * @brief Git object id routines
13  * @defgroup git_oid Git object id routines
14  * @ingroup Git
15  * @{
16  */
17 extern (C):
18 nothrow @nogc:
19 public:
20 
21 /**
22  * Size (in bytes) of a raw/binary oid
23  */
24 enum GIT_OID_RAWSZ = 20;
25 
26 /**
27  * Size (in bytes) of a hex formatted oid
28  */
29 enum GIT_OID_HEXSZ = .GIT_OID_RAWSZ * 2;
30 
31 /**
32  * Minimum length (in number of hex characters,
33  * i.e. packets of 4 bits) of an oid prefix
34  */
35 enum GIT_OID_MINPREFIXLEN = 4;
36 
37 /**
38  * Unique identity of any object (commit, tree, blob, tag).
39  */
40 struct git_oid
41 {
42 	/**
43 	 * raw binary formatted id
44 	 */
45 	ubyte[.GIT_OID_RAWSZ] id;
46 }
47 
48 /**
49  * Parse a hex formatted object id into a git_oid.
50  *
51  * @param out_ oid structure the result is written into.
52  * @param str input hex string; must be pointing at the start of
53  *		the hex sequence and have at least the number of bytes
54  *		needed for an oid encoded in hex (40 bytes).
55  * @return 0 or an error code
56  */
57 //GIT_EXTERN
58 int git_oid_fromstr(.git_oid* out_, const (char)* str);
59 
60 /**
61  * Parse a hex formatted null-terminated string into a git_oid.
62  *
63  * @param out_ oid structure the result is written into.
64  * @param str input hex string; must be null-terminated.
65  * @return 0 or an error code
66  */
67 //GIT_EXTERN
68 int git_oid_fromstrp(.git_oid* out_, const (char)* str);
69 
70 /**
71  * Parse N characters of a hex formatted object id into a git_oid.
72  *
73  * If N is odd, the last byte's high nibble will be read in and the
74  * low nibble set to zero.
75  *
76  * @param out_ oid structure the result is written into.
77  * @param str input hex string of at least size `length`
78  * @param length length of the input string
79  * @return 0 or an error code
80  */
81 //GIT_EXTERN
82 int git_oid_fromstrn(.git_oid* out_, const (char)* str, size_t length);
83 
84 /**
85  * Copy an already raw oid into a git_oid structure.
86  *
87  * @param out_ oid structure the result is written into.
88  * @param raw the raw input bytes to be copied.
89  * @return 0 on success or error code
90  */
91 //GIT_EXTERN
92 int git_oid_fromraw(.git_oid* out_, const (ubyte)* raw);
93 
94 /**
95  * Format a git_oid into a hex string.
96  *
97  * @param out_ output hex string; must be pointing at the start of
98  *		the hex sequence and have at least the number of bytes
99  *		needed for an oid encoded in hex (40 bytes). Only the
100  *		oid digits are written; a '\\0' terminator must be added
101  *		by the caller if it is required.
102  * @param id oid structure to format.
103  * @return 0 on success or error code
104  */
105 //GIT_EXTERN
106 int git_oid_fmt(char* out_, const (.git_oid)* id);
107 
108 /**
109  * Format a git_oid into a partial hex string.
110  *
111  * @param out_ output hex string; you say how many bytes to write.
112  *		If the number of bytes is > GIT_OID_HEXSZ, extra bytes
113  *		will be zeroed; if not, a '\0' terminator is NOT added.
114  * @param n number of characters to write into out string
115  * @param id oid structure to format.
116  * @return 0 on success or error code
117  */
118 //GIT_EXTERN
119 int git_oid_nfmt(char* out_, size_t n, const (.git_oid)* id);
120 
121 /**
122  * Format a git_oid into a loose-object path string.
123  *
124  * The resulting string is "aa/...", where "aa" is the first two
125  * hex digits of the oid and "..." is the remaining 38 digits.
126  *
127  * @param out_ output hex string; must be pointing at the start of
128  *		the hex sequence and have at least the number of bytes
129  *		needed for an oid encoded in hex (41 bytes). Only the
130  *		oid digits are written; a '\\0' terminator must be added
131  *		by the caller if it is required.
132  * @param id oid structure to format.
133  * @return 0 on success, non-zero callback return value, or error code
134  */
135 //GIT_EXTERN
136 int git_oid_pathfmt(char* out_, const (.git_oid)* id);
137 
138 /**
139  * Format a git_oid into a statically allocated c-string.
140  *
141  * The c-string is owned by the library and should not be freed
142  * by the user. If libgit2 is built with thread support, the string
143  * will be stored in TLS (i.e. one buffer per thread) to allow for
144  * concurrent calls of the function.
145  *
146  * @param oid The oid structure to format
147  * @return the c-string
148  */
149 //GIT_EXTERN
150 char* git_oid_tostr_s(const (.git_oid)* oid);
151 
152 /**
153  * Format a git_oid into a buffer as a hex format c-string.
154  *
155  * If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting
156  * oid c-string will be truncated to n-1 characters (but will still be
157  * NUL-byte terminated).
158  *
159  * If there are any input parameter errors (out == null, n == 0, oid ==
160  * null), then a pointer to an empty string is returned, so that the
161  * return value can always be printed.
162  *
163  * @param out_ the buffer into which the oid string is output.
164  * @param n the size of the out buffer.
165  * @param id the oid structure to format.
166  * @return the out buffer pointer, assuming no input parameter
167  *			errors, otherwise a pointer to an empty string.
168  */
169 //GIT_EXTERN
170 char* git_oid_tostr(char* out_, size_t n, const (.git_oid)* id);
171 
172 /**
173  * Copy an oid from one structure to another.
174  *
175  * @param out_ oid structure the result is written into.
176  * @param src oid structure to copy from.
177  * @return 0 on success or error code
178  */
179 //GIT_EXTERN
180 int git_oid_cpy(.git_oid* out_, const (.git_oid)* src);
181 
182 /**
183  * Compare two oid structures.
184  *
185  * @param a first oid structure.
186  * @param b second oid structure.
187  * @return <0, 0, >0 if a < b, a == b, a > b.
188  */
189 //GIT_EXTERN
190 int git_oid_cmp(const (.git_oid)* a, const (.git_oid)* b);
191 
192 /**
193  * Compare two oid structures for equality
194  *
195  * @param a first oid structure.
196  * @param b second oid structure.
197  * @return true if equal, false otherwise
198  */
199 //GIT_EXTERN
200 int git_oid_equal(const (.git_oid)* a, const (.git_oid)* b);
201 
202 /**
203  * Compare the first 'len' hexadecimal characters (packets of 4 bits)
204  * of two oid structures.
205  *
206  * @param a first oid structure.
207  * @param b second oid structure.
208  * @param len the number of hex chars to compare
209  * @return 0 in case of a match
210  */
211 //GIT_EXTERN
212 int git_oid_ncmp(const (.git_oid)* a, const (.git_oid)* b, size_t len);
213 
214 /**
215  * Check if an oid equals an hex formatted object id.
216  *
217  * @param id oid structure.
218  * @param str input hex string of an object id.
219  * @return 0 in case of a match, -1 otherwise.
220  */
221 //GIT_EXTERN
222 int git_oid_streq(const (.git_oid)* id, const (char)* str);
223 
224 /**
225  * Compare an oid to an hex formatted object id.
226  *
227  * @param id oid structure.
228  * @param str input hex string of an object id.
229  * @return -1 if str is not valid, <0 if id sorts before str,
230  *         0 if id matches str, >0 if id sorts after str.
231  */
232 //GIT_EXTERN
233 int git_oid_strcmp(const (.git_oid)* id, const (char)* str);
234 
235 /**
236  * Check is an oid is all zeros.
237  *
238  * @return 1 if all zeros, 0 otherwise.
239  */
240 //GIT_EXTERN
241 int git_oid_is_zero(const (.git_oid)* id);
242 
243 /**
244  * OID Shortener object
245  */
246 struct git_oid_shorten;
247 
248 /**
249  * Create a new OID shortener.
250  *
251  * The OID shortener is used to process a list of OIDs
252  * in text form and return the shortest length that would
253  * uniquely identify all of them.
254  *
255  * E.g. look at the result of `git log --abbrev`.
256  *
257  * @param min_length The minimal length for all identifiers,
258  *		which will be used even if shorter OIDs would still
259  *		be unique.
260  *	@return a `git_oid_shorten` instance, null if OOM
261  */
262 //GIT_EXTERN
263 .git_oid_shorten* git_oid_shorten_new(size_t min_length);
264 
265 /**
266  * Add a new OID to set of shortened OIDs and calculate
267  * the minimal length to uniquely identify all the OIDs in
268  * the set.
269  *
270  * The OID is expected to be a 40-char hexadecimal string.
271  * The OID is owned by the user and will not be modified
272  * or freed.
273  *
274  * For performance reasons, there is a hard-limit of how many
275  * OIDs can be added to a single set (around ~32000, assuming
276  * a mostly randomized distribution), which should be enough
277  * for any kind of program, and keeps the algorithm fast and
278  * memory-efficient.
279  *
280  * Attempting to add more than those OIDs will result in a
281  * git_error_t.GIT_ERROR_INVALID error
282  *
283  * @param os a `git_oid_shorten` instance
284  * @param text_id an OID in text form
285  * @return the minimal length to uniquely identify all OIDs
286  *		added so far to the set; or an error code (<0) if an
287  *		error occurs.
288  */
289 //GIT_EXTERN
290 int git_oid_shorten_add(.git_oid_shorten* os, const (char)* text_id);
291 
292 /**
293  * Free an OID shortener instance
294  *
295  * @param os a `git_oid_shorten` instance
296  */
297 //GIT_EXTERN
298 void git_oid_shorten_free(.git_oid_shorten* os);
299 
300 /** @} */