Blame include/git2/oid.h

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