|
||||
File indexing completed on 2025-01-18 09:59:38
0001 /* 0002 * Copyright (C) the libgit2 contributors. All rights reserved. 0003 * 0004 * This file is part of libgit2, distributed under the GNU GPL v2 with 0005 * a Linking Exception. For full terms see the included COPYING file. 0006 */ 0007 #ifndef INCLUDE_git_oid_h__ 0008 #define INCLUDE_git_oid_h__ 0009 0010 #include "common.h" 0011 #include "types.h" 0012 #include "experimental.h" 0013 0014 /** 0015 * @file git2/oid.h 0016 * @brief Git object id routines 0017 * @defgroup git_oid Git object id routines 0018 * @ingroup Git 0019 * @{ 0020 */ 0021 GIT_BEGIN_DECL 0022 0023 /** The type of object id. */ 0024 typedef enum { 0025 0026 #ifdef GIT_EXPERIMENTAL_SHA256 0027 GIT_OID_SHA1 = 1, /**< SHA1 */ 0028 GIT_OID_SHA256 = 2 /**< SHA256 */ 0029 #else 0030 GIT_OID_SHA1 = 1 /**< SHA1 */ 0031 #endif 0032 0033 } git_oid_t; 0034 0035 /* 0036 * SHA1 is currently the only supported object ID type. 0037 */ 0038 0039 /** SHA1 is currently libgit2's default oid type. */ 0040 #define GIT_OID_DEFAULT GIT_OID_SHA1 0041 0042 /** Size (in bytes) of a raw/binary sha1 oid */ 0043 #define GIT_OID_SHA1_SIZE 20 0044 /** Size (in bytes) of a hex formatted sha1 oid */ 0045 #define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) 0046 0047 /** 0048 * The binary representation of the null sha1 object ID. 0049 */ 0050 #ifndef GIT_EXPERIMENTAL_SHA256 0051 # define GIT_OID_SHA1_ZERO { { 0 } } 0052 #else 0053 # define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } } 0054 #endif 0055 0056 /** 0057 * The string representation of the null sha1 object ID. 0058 */ 0059 #define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" 0060 0061 /* 0062 * Experimental SHA256 support is a breaking change to the API. 0063 * This exists for application compatibility testing. 0064 */ 0065 0066 #ifdef GIT_EXPERIMENTAL_SHA256 0067 0068 /** Size (in bytes) of a raw/binary sha256 oid */ 0069 # define GIT_OID_SHA256_SIZE 32 0070 /** Size (in bytes) of a hex formatted sha256 oid */ 0071 # define GIT_OID_SHA256_HEXSIZE (GIT_OID_SHA256_SIZE * 2) 0072 0073 /** 0074 * The binary representation of the null sha256 object ID. 0075 */ 0076 # define GIT_OID_SHA256_ZERO { GIT_OID_SHA256, { 0 } } 0077 0078 /** 0079 * The string representation of the null sha256 object ID. 0080 */ 0081 # define GIT_OID_SHA256_HEXZERO "0000000000000000000000000000000000000000000000000000000000000000" 0082 0083 #endif 0084 0085 /* Maximum possible object ID size in raw / hex string format. */ 0086 #ifndef GIT_EXPERIMENTAL_SHA256 0087 # define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE 0088 # define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE 0089 #else 0090 # define GIT_OID_MAX_SIZE GIT_OID_SHA256_SIZE 0091 # define GIT_OID_MAX_HEXSIZE GIT_OID_SHA256_HEXSIZE 0092 #endif 0093 0094 /** Minimum length (in number of hex characters, 0095 * i.e. packets of 4 bits) of an oid prefix */ 0096 #define GIT_OID_MINPREFIXLEN 4 0097 0098 /** Unique identity of any object (commit, tree, blob, tag). */ 0099 typedef struct git_oid { 0100 0101 #ifdef GIT_EXPERIMENTAL_SHA256 0102 /** type of object id */ 0103 unsigned char type; 0104 #endif 0105 0106 /** raw binary formatted id */ 0107 unsigned char id[GIT_OID_MAX_SIZE]; 0108 } git_oid; 0109 0110 /** 0111 * Parse a hex formatted object id into a git_oid. 0112 * 0113 * The appropriate number of bytes for the given object ID type will 0114 * be read from the string - 40 bytes for SHA1, 64 bytes for SHA256. 0115 * The given string need not be NUL terminated. 0116 * 0117 * @param out oid structure the result is written into. 0118 * @param str input hex string; must be pointing at the start of 0119 * the hex sequence and have at least the number of bytes 0120 * needed for an oid encoded in hex (40 bytes for sha1, 0121 * 256 bytes for sha256). 0122 * @param type the type of object id 0123 * @return 0 or an error code 0124 */ 0125 #ifdef GIT_EXPERIMENTAL_SHA256 0126 GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type); 0127 #else 0128 GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); 0129 #endif 0130 0131 /** 0132 * Parse a hex formatted NUL-terminated string into a git_oid. 0133 * 0134 * @param out oid structure the result is written into. 0135 * @param str input hex string; must be null-terminated. 0136 * @param type the type of object id 0137 * @return 0 or an error code 0138 */ 0139 #ifdef GIT_EXPERIMENTAL_SHA256 0140 GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type); 0141 #else 0142 GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str); 0143 #endif 0144 0145 /** 0146 * Parse N characters of a hex formatted object id into a git_oid. 0147 * 0148 * If N is odd, the last byte's high nibble will be read in and the 0149 * low nibble set to zero. 0150 * 0151 * @param out oid structure the result is written into. 0152 * @param str input hex string of at least size `length` 0153 * @param length length of the input string 0154 * @param type the type of object id 0155 * @return 0 or an error code 0156 */ 0157 #ifdef GIT_EXPERIMENTAL_SHA256 0158 GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type); 0159 #else 0160 GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length); 0161 #endif 0162 0163 /** 0164 * Copy an already raw oid into a git_oid structure. 0165 * 0166 * @param out oid structure the result is written into. 0167 * @param raw the raw input bytes to be copied. 0168 * @return 0 on success or error code 0169 */ 0170 #ifdef GIT_EXPERIMENTAL_SHA256 0171 GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw, git_oid_t type); 0172 #else 0173 GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw); 0174 #endif 0175 0176 /** 0177 * Format a git_oid into a hex string. 0178 * 0179 * @param out output hex string; must be pointing at the start of 0180 * the hex sequence and have at least the number of bytes 0181 * needed for an oid encoded in hex (40 bytes for SHA1, 0182 * 64 bytes for SHA256). Only the oid digits are written; 0183 * a '\\0' terminator must be added by the caller if it is 0184 * required. 0185 * @param id oid structure to format. 0186 * @return 0 on success or error code 0187 */ 0188 GIT_EXTERN(int) git_oid_fmt(char *out, const git_oid *id); 0189 0190 /** 0191 * Format a git_oid into a partial hex string. 0192 * 0193 * @param out output hex string; you say how many bytes to write. 0194 * If the number of bytes is > GIT_OID_SHA1_HEXSIZE, extra bytes 0195 * will be zeroed; if not, a '\0' terminator is NOT added. 0196 * @param n number of characters to write into out string 0197 * @param id oid structure to format. 0198 * @return 0 on success or error code 0199 */ 0200 GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id); 0201 0202 /** 0203 * Format a git_oid into a loose-object path string. 0204 * 0205 * The resulting string is "aa/...", where "aa" is the first two 0206 * hex digits of the oid and "..." is the remaining 38 digits. 0207 * 0208 * @param out output hex string; must be pointing at the start of 0209 * the hex sequence and have at least the number of bytes 0210 * needed for an oid encoded in hex (41 bytes for SHA1, 0211 * 65 bytes for SHA256). Only the oid digits are written; 0212 * a '\\0' terminator must be added by the caller if it 0213 * is required. 0214 * @param id oid structure to format. 0215 * @return 0 on success, non-zero callback return value, or error code 0216 */ 0217 GIT_EXTERN(int) git_oid_pathfmt(char *out, const git_oid *id); 0218 0219 /** 0220 * Format a git_oid into a statically allocated c-string. 0221 * 0222 * The c-string is owned by the library and should not be freed 0223 * by the user. If libgit2 is built with thread support, the string 0224 * will be stored in TLS (i.e. one buffer per thread) to allow for 0225 * concurrent calls of the function. 0226 * 0227 * @param oid The oid structure to format 0228 * @return the c-string or NULL on failure 0229 */ 0230 GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid); 0231 0232 /** 0233 * Format a git_oid into a buffer as a hex format c-string. 0234 * 0235 * If the buffer is smaller than the size of a hex-formatted oid string 0236 * plus an additional byte (GIT_OID_SHA_HEXSIZE + 1 for SHA1 or 0237 * GIT_OID_SHA256_HEXSIZE + 1 for SHA256), then the resulting 0238 * oid c-string will be truncated to n-1 characters (but will still be 0239 * NUL-byte terminated). 0240 * 0241 * If there are any input parameter errors (out == NULL, n == 0, oid == 0242 * NULL), then a pointer to an empty string is returned, so that the 0243 * return value can always be printed. 0244 * 0245 * @param out the buffer into which the oid string is output. 0246 * @param n the size of the out buffer. 0247 * @param id the oid structure to format. 0248 * @return the out buffer pointer, assuming no input parameter 0249 * errors, otherwise a pointer to an empty string. 0250 */ 0251 GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *id); 0252 0253 /** 0254 * Copy an oid from one structure to another. 0255 * 0256 * @param out oid structure the result is written into. 0257 * @param src oid structure to copy from. 0258 * @return 0 on success or error code 0259 */ 0260 GIT_EXTERN(int) git_oid_cpy(git_oid *out, const git_oid *src); 0261 0262 /** 0263 * Compare two oid structures. 0264 * 0265 * @param a first oid structure. 0266 * @param b second oid structure. 0267 * @return <0, 0, >0 if a < b, a == b, a > b. 0268 */ 0269 GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b); 0270 0271 /** 0272 * Compare two oid structures for equality 0273 * 0274 * @param a first oid structure. 0275 * @param b second oid structure. 0276 * @return true if equal, false otherwise 0277 */ 0278 GIT_EXTERN(int) git_oid_equal(const git_oid *a, const git_oid *b); 0279 0280 /** 0281 * Compare the first 'len' hexadecimal characters (packets of 4 bits) 0282 * of two oid structures. 0283 * 0284 * @param a first oid structure. 0285 * @param b second oid structure. 0286 * @param len the number of hex chars to compare 0287 * @return 0 in case of a match 0288 */ 0289 GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, size_t len); 0290 0291 /** 0292 * Check if an oid equals an hex formatted object id. 0293 * 0294 * @param id oid structure. 0295 * @param str input hex string of an object id. 0296 * @return 0 in case of a match, -1 otherwise. 0297 */ 0298 GIT_EXTERN(int) git_oid_streq(const git_oid *id, const char *str); 0299 0300 /** 0301 * Compare an oid to an hex formatted object id. 0302 * 0303 * @param id oid structure. 0304 * @param str input hex string of an object id. 0305 * @return -1 if str is not valid, <0 if id sorts before str, 0306 * 0 if id matches str, >0 if id sorts after str. 0307 */ 0308 GIT_EXTERN(int) git_oid_strcmp(const git_oid *id, const char *str); 0309 0310 /** 0311 * Check is an oid is all zeros. 0312 * 0313 * @return 1 if all zeros, 0 otherwise. 0314 */ 0315 GIT_EXTERN(int) git_oid_is_zero(const git_oid *id); 0316 0317 /** 0318 * OID Shortener object 0319 */ 0320 typedef struct git_oid_shorten git_oid_shorten; 0321 0322 /** 0323 * Create a new OID shortener. 0324 * 0325 * The OID shortener is used to process a list of OIDs 0326 * in text form and return the shortest length that would 0327 * uniquely identify all of them. 0328 * 0329 * E.g. look at the result of `git log --abbrev`. 0330 * 0331 * @param min_length The minimal length for all identifiers, 0332 * which will be used even if shorter OIDs would still 0333 * be unique. 0334 * @return a `git_oid_shorten` instance, NULL if OOM 0335 */ 0336 GIT_EXTERN(git_oid_shorten *) git_oid_shorten_new(size_t min_length); 0337 0338 /** 0339 * Add a new OID to set of shortened OIDs and calculate 0340 * the minimal length to uniquely identify all the OIDs in 0341 * the set. 0342 * 0343 * The OID is expected to be a 40-char hexadecimal string. 0344 * The OID is owned by the user and will not be modified 0345 * or freed. 0346 * 0347 * For performance reasons, there is a hard-limit of how many 0348 * OIDs can be added to a single set (around ~32000, assuming 0349 * a mostly randomized distribution), which should be enough 0350 * for any kind of program, and keeps the algorithm fast and 0351 * memory-efficient. 0352 * 0353 * Attempting to add more than those OIDs will result in a 0354 * GIT_ERROR_INVALID error 0355 * 0356 * @param os a `git_oid_shorten` instance 0357 * @param text_id an OID in text form 0358 * @return the minimal length to uniquely identify all OIDs 0359 * added so far to the set; or an error code (<0) if an 0360 * error occurs. 0361 */ 0362 GIT_EXTERN(int) git_oid_shorten_add(git_oid_shorten *os, const char *text_id); 0363 0364 /** 0365 * Free an OID shortener instance 0366 * 0367 * @param os a `git_oid_shorten` instance 0368 */ 0369 GIT_EXTERN(void) git_oid_shorten_free(git_oid_shorten *os); 0370 0371 /** @} */ 0372 GIT_END_DECL 0373 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |