|
Packit |
ae9e2a |
/*
|
|
Packit |
ae9e2a |
* Copyright (C) the libgit2 contributors. All rights reserved.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
|
Packit |
ae9e2a |
* a Linking Exception. For full terms see the included COPYING file.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
#ifndef INCLUDE_git_oid_h__
|
|
Packit |
ae9e2a |
#define INCLUDE_git_oid_h__
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#include "common.h"
|
|
Packit |
ae9e2a |
#include "types.h"
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* @file git2/oid.h
|
|
Packit |
ae9e2a |
* @brief Git object id routines
|
|
Packit |
ae9e2a |
* @defgroup git_oid Git object id routines
|
|
Packit |
ae9e2a |
* @ingroup Git
|
|
Packit |
ae9e2a |
* @{
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_BEGIN_DECL
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/** Size (in bytes) of a raw/binary oid */
|
|
Packit |
ae9e2a |
#define GIT_OID_RAWSZ 20
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/** Size (in bytes) of a hex formatted oid */
|
|
Packit |
ae9e2a |
#define GIT_OID_HEXSZ (GIT_OID_RAWSZ * 2)
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/** Minimum length (in number of hex characters,
|
|
Packit |
ae9e2a |
* i.e. packets of 4 bits) of an oid prefix */
|
|
Packit |
ae9e2a |
#define GIT_OID_MINPREFIXLEN 4
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/** Unique identity of any object (commit, tree, blob, tag). */
|
|
Packit |
ae9e2a |
typedef struct git_oid {
|
|
Packit |
ae9e2a |
/** raw binary formatted id */
|
|
Packit |
ae9e2a |
unsigned char id[GIT_OID_RAWSZ];
|
|
Packit |
ae9e2a |
} git_oid;
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Parse a hex formatted object id into a git_oid.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out oid structure the result is written into.
|
|
Packit |
ae9e2a |
* @param str input hex string; must be pointing at the start of
|
|
Packit |
ae9e2a |
* the hex sequence and have at least the number of bytes
|
|
Packit |
ae9e2a |
* needed for an oid encoded in hex (40 bytes).
|
|
Packit |
ae9e2a |
* @return 0 or an error code
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Parse a hex formatted null-terminated string into a git_oid.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out oid structure the result is written into.
|
|
Packit |
ae9e2a |
* @param str input hex string; must be null-terminated.
|
|
Packit |
ae9e2a |
* @return 0 or an error code
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Parse N characters of a hex formatted object id into a git_oid.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* If N is odd, the last byte's high nibble will be read in and the
|
|
Packit |
ae9e2a |
* low nibble set to zero.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out oid structure the result is written into.
|
|
Packit |
ae9e2a |
* @param str input hex string of at least size `length`
|
|
Packit |
ae9e2a |
* @param length length of the input string
|
|
Packit |
ae9e2a |
* @return 0 or an error code
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Copy an already raw oid into a git_oid structure.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out oid structure the result is written into.
|
|
Packit |
ae9e2a |
* @param raw the raw input bytes to be copied.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Format a git_oid into a hex string.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out output hex string; must be pointing at the start of
|
|
Packit |
ae9e2a |
* the hex sequence and have at least the number of bytes
|
|
Packit |
ae9e2a |
* needed for an oid encoded in hex (40 bytes). Only the
|
|
Packit |
ae9e2a |
* oid digits are written; a '\\0' terminator must be added
|
|
Packit |
ae9e2a |
* by the caller if it is required.
|
|
Packit |
ae9e2a |
* @param id oid structure to format.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Format a git_oid into a partial hex string.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out output hex string; you say how many bytes to write.
|
|
Packit |
ae9e2a |
* If the number of bytes is > GIT_OID_HEXSZ, extra bytes
|
|
Packit |
ae9e2a |
* will be zeroed; if not, a '\0' terminator is NOT added.
|
|
Packit |
ae9e2a |
* @param n number of characters to write into out string
|
|
Packit |
ae9e2a |
* @param id oid structure to format.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(void) git_oid_nfmt(char *out, size_t n, const git_oid *id);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Format a git_oid into a loose-object path string.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* The resulting string is "aa/...", where "aa" is the first two
|
|
Packit |
ae9e2a |
* hex digits of the oid and "..." is the remaining 38 digits.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out output hex string; must be pointing at the start of
|
|
Packit |
ae9e2a |
* the hex sequence and have at least the number of bytes
|
|
Packit |
ae9e2a |
* needed for an oid encoded in hex (41 bytes). Only the
|
|
Packit |
ae9e2a |
* oid digits are written; a '\\0' terminator must be added
|
|
Packit |
ae9e2a |
* by the caller if it is required.
|
|
Packit |
ae9e2a |
* @param id oid structure to format.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(void) git_oid_pathfmt(char *out, const git_oid *id);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Format a git_oid into a statically allocated c-string.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* The c-string is owned by the library and should not be freed
|
|
Packit |
ae9e2a |
* by the user. If libgit2 is built with thread support, the string
|
|
Packit |
ae9e2a |
* will be stored in TLS (i.e. one buffer per thread) to allow for
|
|
Packit |
ae9e2a |
* concurrent calls of the function.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param oid The oid structure to format
|
|
Packit |
ae9e2a |
* @return the c-string
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Format a git_oid into a buffer as a hex format c-string.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting
|
|
Packit |
ae9e2a |
* oid c-string will be truncated to n-1 characters (but will still be
|
|
Packit |
ae9e2a |
* NUL-byte terminated).
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* If there are any input parameter errors (out == NULL, n == 0, oid ==
|
|
Packit |
ae9e2a |
* NULL), then a pointer to an empty string is returned, so that the
|
|
Packit |
ae9e2a |
* return value can always be printed.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out the buffer into which the oid string is output.
|
|
Packit |
ae9e2a |
* @param n the size of the out buffer.
|
|
Packit |
ae9e2a |
* @param id the oid structure to format.
|
|
Packit |
ae9e2a |
* @return the out buffer pointer, assuming no input parameter
|
|
Packit |
ae9e2a |
* errors, otherwise a pointer to an empty string.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *id);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Copy an oid from one structure to another.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param out oid structure the result is written into.
|
|
Packit |
ae9e2a |
* @param src oid structure to copy from.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Compare two oid structures.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param a first oid structure.
|
|
Packit |
ae9e2a |
* @param b second oid structure.
|
|
Packit |
ae9e2a |
* @return <0, 0, >0 if a < b, a == b, a > b.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Compare two oid structures for equality
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param a first oid structure.
|
|
Packit |
ae9e2a |
* @param b second oid structure.
|
|
Packit |
ae9e2a |
* @return true if equal, false otherwise
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_equal(const git_oid *a, const git_oid *b);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Compare the first 'len' hexadecimal characters (packets of 4 bits)
|
|
Packit |
ae9e2a |
* of two oid structures.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param a first oid structure.
|
|
Packit |
ae9e2a |
* @param b second oid structure.
|
|
Packit |
ae9e2a |
* @param len the number of hex chars to compare
|
|
Packit |
ae9e2a |
* @return 0 in case of a match
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, size_t len);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Check if an oid equals an hex formatted object id.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param id oid structure.
|
|
Packit |
ae9e2a |
* @param str input hex string of an object id.
|
|
Packit |
ae9e2a |
* @return 0 in case of a match, -1 otherwise.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_streq(const git_oid *id, const char *str);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Compare an oid to an hex formatted object id.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param id oid structure.
|
|
Packit |
ae9e2a |
* @param str input hex string of an object id.
|
|
Packit |
ae9e2a |
* @return -1 if str is not valid, <0 if id sorts before str,
|
|
Packit |
ae9e2a |
* 0 if id matches str, >0 if id sorts after str.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_strcmp(const git_oid *id, const char *str);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Check is an oid is all zeros.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @return 1 if all zeros, 0 otherwise.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_iszero(const git_oid *id);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* OID Shortener object
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
typedef struct git_oid_shorten git_oid_shorten;
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Create a new OID shortener.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* The OID shortener is used to process a list of OIDs
|
|
Packit |
ae9e2a |
* in text form and return the shortest length that would
|
|
Packit |
ae9e2a |
* uniquely identify all of them.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* E.g. look at the result of `git log --abbrev`.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param min_length The minimal length for all identifiers,
|
|
Packit |
ae9e2a |
* which will be used even if shorter OIDs would still
|
|
Packit |
ae9e2a |
* be unique.
|
|
Packit |
ae9e2a |
* @return a `git_oid_shorten` instance, NULL if OOM
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(git_oid_shorten *) git_oid_shorten_new(size_t min_length);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Add a new OID to set of shortened OIDs and calculate
|
|
Packit |
ae9e2a |
* the minimal length to uniquely identify all the OIDs in
|
|
Packit |
ae9e2a |
* the set.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* The OID is expected to be a 40-char hexadecimal string.
|
|
Packit |
ae9e2a |
* The OID is owned by the user and will not be modified
|
|
Packit |
ae9e2a |
* or freed.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* For performance reasons, there is a hard-limit of how many
|
|
Packit |
ae9e2a |
* OIDs can be added to a single set (around ~32000, assuming
|
|
Packit |
ae9e2a |
* a mostly randomized distribution), which should be enough
|
|
Packit |
ae9e2a |
* for any kind of program, and keeps the algorithm fast and
|
|
Packit |
ae9e2a |
* memory-efficient.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* Attempting to add more than those OIDs will result in a
|
|
Packit |
ae9e2a |
* GITERR_INVALID error
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param os a `git_oid_shorten` instance
|
|
Packit |
ae9e2a |
* @param text_id an OID in text form
|
|
Packit |
ae9e2a |
* @return the minimal length to uniquely identify all OIDs
|
|
Packit |
ae9e2a |
* added so far to the set; or an error code (<0) if an
|
|
Packit |
ae9e2a |
* error occurs.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(int) git_oid_shorten_add(git_oid_shorten *os, const char *text_id);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/**
|
|
Packit |
ae9e2a |
* Free an OID shortener instance
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* @param os a `git_oid_shorten` instance
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
GIT_EXTERN(void) git_oid_shorten_free(git_oid_shorten *os);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/** @} */
|
|
Packit |
ae9e2a |
GIT_END_DECL
|
|
Packit |
ae9e2a |
#endif
|