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