2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_git_oid_h__
8 #define INCLUDE_git_oid_h__
15 * @brief Git object id routines
16 * @defgroup git_oid Git object id routines
22 /** Size (in bytes) of a raw/binary oid */
23 #define GIT_OID_RAWSZ 20
25 /** Size (in bytes) of a hex formatted oid */
26 #define GIT_OID_HEXSZ (GIT_OID_RAWSZ * 2)
28 /** Minimum length (in number of hex characters,
29 * i.e. packets of 4 bits) of an oid prefix */
30 #define GIT_OID_MINPREFIXLEN 4
32 /** Unique identity of any object (commit, tree, blob, tag). */
33 typedef struct git_oid
{
34 /** raw binary formatted id */
35 unsigned char id
[GIT_OID_RAWSZ
];
39 * Parse a hex formatted object id into a git_oid.
41 * @param out oid structure the result is written into.
42 * @param str input hex string; must be pointing at the start of
43 * the hex sequence and have at least the number of bytes
44 * needed for an oid encoded in hex (40 bytes).
45 * @return 0 or an error code
47 GIT_EXTERN(int) git_oid_fromstr(git_oid
*out
, const char *str
);
50 * Parse a hex formatted null-terminated string into a git_oid.
52 * @param out oid structure the result is written into.
53 * @param str input hex string; must be null-terminated.
54 * @return 0 or an error code
56 GIT_EXTERN(int) git_oid_fromstrp(git_oid
*out
, const char *str
);
59 * Parse N characters of a hex formatted object id into a git_oid.
61 * If N is odd, the last byte's high nibble will be read in and the
62 * low nibble set to zero.
64 * @param out oid structure the result is written into.
65 * @param str input hex string of at least size `length`
66 * @param length length of the input string
67 * @return 0 or an error code
69 GIT_EXTERN(int) git_oid_fromstrn(git_oid
*out
, const char *str
, size_t length
);
72 * Copy an already raw oid into a git_oid structure.
74 * @param out oid structure the result is written into.
75 * @param raw the raw input bytes to be copied.
76 * @return 0 on success or error code
78 GIT_EXTERN(int) git_oid_fromraw(git_oid
*out
, const unsigned char *raw
);
81 * Format a git_oid into a hex string.
83 * @param out output hex string; must be pointing at the start of
84 * the hex sequence and have at least the number of bytes
85 * needed for an oid encoded in hex (40 bytes). Only the
86 * oid digits are written; a '\\0' terminator must be added
87 * by the caller if it is required.
88 * @param id oid structure to format.
89 * @return 0 on success or error code
91 GIT_EXTERN(int) git_oid_fmt(char *out
, const git_oid
*id
);
94 * Format a git_oid into a partial hex string.
96 * @param out output hex string; you say how many bytes to write.
97 * If the number of bytes is > GIT_OID_HEXSZ, extra bytes
98 * will be zeroed; if not, a '\0' terminator is NOT added.
99 * @param n number of characters to write into out string
100 * @param id oid structure to format.
101 * @return 0 on success or error code
103 GIT_EXTERN(int) git_oid_nfmt(char *out
, size_t n
, const git_oid
*id
);
106 * Format a git_oid into a loose-object path string.
108 * The resulting string is "aa/...", where "aa" is the first two
109 * hex digits of the oid and "..." is the remaining 38 digits.
111 * @param out output hex string; must be pointing at the start of
112 * the hex sequence and have at least the number of bytes
113 * needed for an oid encoded in hex (41 bytes). Only the
114 * oid digits are written; a '\\0' terminator must be added
115 * by the caller if it is required.
116 * @param id oid structure to format.
117 * @return 0 on success, non-zero callback return value, or error code
119 GIT_EXTERN(int) git_oid_pathfmt(char *out
, const git_oid
*id
);
122 * Format a git_oid into a statically allocated c-string.
124 * The c-string is owned by the library and should not be freed
125 * by the user. If libgit2 is built with thread support, the string
126 * will be stored in TLS (i.e. one buffer per thread) to allow for
127 * concurrent calls of the function.
129 * @param oid The oid structure to format
130 * @return the c-string
132 GIT_EXTERN(char *) git_oid_tostr_s(const git_oid
*oid
);
135 * Format a git_oid into a buffer as a hex format c-string.
137 * If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting
138 * oid c-string will be truncated to n-1 characters (but will still be
139 * NUL-byte terminated).
141 * If there are any input parameter errors (out == NULL, n == 0, oid ==
142 * NULL), then a pointer to an empty string is returned, so that the
143 * return value can always be printed.
145 * @param out the buffer into which the oid string is output.
146 * @param n the size of the out buffer.
147 * @param id the oid structure to format.
148 * @return the out buffer pointer, assuming no input parameter
149 * errors, otherwise a pointer to an empty string.
151 GIT_EXTERN(char *) git_oid_tostr(char *out
, size_t n
, const git_oid
*id
);
154 * Copy an oid from one structure to another.
156 * @param out oid structure the result is written into.
157 * @param src oid structure to copy from.
158 * @return 0 on success or error code
160 GIT_EXTERN(int) git_oid_cpy(git_oid
*out
, const git_oid
*src
);
163 * Compare two oid structures.
165 * @param a first oid structure.
166 * @param b second oid structure.
167 * @return <0, 0, >0 if a < b, a == b, a > b.
169 GIT_EXTERN(int) git_oid_cmp(const git_oid
*a
, const git_oid
*b
);
172 * Compare two oid structures for equality
174 * @param a first oid structure.
175 * @param b second oid structure.
176 * @return true if equal, false otherwise
178 GIT_EXTERN(int) git_oid_equal(const git_oid
*a
, const git_oid
*b
);
181 * Compare the first 'len' hexadecimal characters (packets of 4 bits)
182 * of two oid structures.
184 * @param a first oid structure.
185 * @param b second oid structure.
186 * @param len the number of hex chars to compare
187 * @return 0 in case of a match
189 GIT_EXTERN(int) git_oid_ncmp(const git_oid
*a
, const git_oid
*b
, size_t len
);
192 * Check if an oid equals an hex formatted object id.
194 * @param id oid structure.
195 * @param str input hex string of an object id.
196 * @return 0 in case of a match, -1 otherwise.
198 GIT_EXTERN(int) git_oid_streq(const git_oid
*id
, const char *str
);
201 * Compare an oid to an hex formatted object id.
203 * @param id oid structure.
204 * @param str input hex string of an object id.
205 * @return -1 if str is not valid, <0 if id sorts before str,
206 * 0 if id matches str, >0 if id sorts after str.
208 GIT_EXTERN(int) git_oid_strcmp(const git_oid
*id
, const char *str
);
211 * Check is an oid is all zeros.
213 * @return 1 if all zeros, 0 otherwise.
215 GIT_EXTERN(int) git_oid_is_zero(const git_oid
*id
);
218 * OID Shortener object
220 typedef struct git_oid_shorten git_oid_shorten
;
223 * Create a new OID shortener.
225 * The OID shortener is used to process a list of OIDs
226 * in text form and return the shortest length that would
227 * uniquely identify all of them.
229 * E.g. look at the result of `git log --abbrev`.
231 * @param min_length The minimal length for all identifiers,
232 * which will be used even if shorter OIDs would still
234 * @return a `git_oid_shorten` instance, NULL if OOM
236 GIT_EXTERN(git_oid_shorten
*) git_oid_shorten_new(size_t min_length
);
239 * Add a new OID to set of shortened OIDs and calculate
240 * the minimal length to uniquely identify all the OIDs in
243 * The OID is expected to be a 40-char hexadecimal string.
244 * The OID is owned by the user and will not be modified
247 * For performance reasons, there is a hard-limit of how many
248 * OIDs can be added to a single set (around ~32000, assuming
249 * a mostly randomized distribution), which should be enough
250 * for any kind of program, and keeps the algorithm fast and
253 * Attempting to add more than those OIDs will result in a
254 * GIT_ERROR_INVALID error
256 * @param os a `git_oid_shorten` instance
257 * @param text_id an OID in text form
258 * @return the minimal length to uniquely identify all OIDs
259 * added so far to the set; or an error code (<0) if an
262 GIT_EXTERN(int) git_oid_shorten_add(git_oid_shorten
*os
, const char *text_id
);
265 * Free an OID shortener instance
267 * @param os a `git_oid_shorten` instance
269 GIT_EXTERN(void) git_oid_shorten_free(git_oid_shorten
*os
);