From dbc4ac1c76827e954e0aa27afe8bb7e0b8993a93 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 22 Jan 2022 23:10:03 -0500 Subject: oid: `GIT_OID_*SZ` is now `GIT_OID_SHA1_*SIZE` In preparation for SHA256 support, `GIT_OID_RAWSZ` and `GIT_OID_HEXSZ` need to indicate that they're the size of _SHA1_ OIDs. --- include/git2/diff.h | 2 +- include/git2/odb.h | 4 ++-- include/git2/oid.h | 10 +++++----- include/git2/sys/odb_backend.h | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/git2/diff.h b/include/git2/diff.h index 3839f0033..850d215a6 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -274,7 +274,7 @@ typedef struct { /** * Represents the known length of the `id` field, when - * converted to a hex string. It is generally `GIT_OID_HEXSZ`, unless this + * converted to a hex string. It is generally `GIT_OID_SHA1_HEXSIZE`, unless this * delta was created from reading a patch file, in which case it may be * abbreviated to something reasonable, like 7 characters. */ diff --git a/include/git2/odb.h b/include/git2/odb.h index 0ffe3f328..54c38a477 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -117,7 +117,7 @@ GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *i * This method queries all available ODB backends * trying to match the 'len' first hexadecimal * characters of the 'short_id'. - * The remaining (GIT_OID_HEXSZ-len)*4 bits of + * The remaining (GIT_OID_SHA1_HEXSIZE-len)*4 bits of * 'short_id' must be 0s. * 'len' must be at least GIT_OID_MINPREFIXLEN, * and the prefix must be long enough to identify @@ -218,7 +218,7 @@ typedef struct git_odb_expand_id { * * The given array will be updated in place: for each abbreviated ID that is * unique in the database, and of the given type (if specified), - * the full object ID, object ID length (`GIT_OID_HEXSZ`) and type will be + * the full object ID, object ID length (`GIT_OID_SHA1_HEXSIZE`) and type will be * written back to the array. For IDs that are not found (or are ambiguous), * the array entry will be zeroed. * diff --git a/include/git2/oid.h b/include/git2/oid.h index 549df4eab..0d0f05489 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -20,10 +20,10 @@ GIT_BEGIN_DECL /** Size (in bytes) of a raw/binary oid */ -#define GIT_OID_RAWSZ 20 +#define GIT_OID_SHA1_SIZE 20 /** Size (in bytes) of a hex formatted oid */ -#define GIT_OID_HEXSZ (GIT_OID_RAWSZ * 2) +#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) /** Minimum length (in number of hex characters, * i.e. packets of 4 bits) of an oid prefix */ @@ -32,7 +32,7 @@ GIT_BEGIN_DECL /** Unique identity of any object (commit, tree, blob, tag). */ typedef struct git_oid { /** raw binary formatted id */ - unsigned char id[GIT_OID_RAWSZ]; + unsigned char id[GIT_OID_SHA1_SIZE]; } git_oid; /** @@ -94,7 +94,7 @@ GIT_EXTERN(int) git_oid_fmt(char *out, const git_oid *id); * Format a git_oid into a partial hex string. * * @param out output hex string; you say how many bytes to write. - * If the number of bytes is > GIT_OID_HEXSZ, extra bytes + * If the number of bytes is > GIT_OID_SHA1_HEXSIZE, extra bytes * will be zeroed; if not, a '\0' terminator is NOT added. * @param n number of characters to write into out string * @param id oid structure to format. @@ -134,7 +134,7 @@ GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid); /** * Format a git_oid into a buffer as a hex format c-string. * - * If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting + * If the buffer is smaller than GIT_OID_SHA1_HEXSIZE+1, then the resulting * oid c-string will be truncated to n-1 characters (but will still be * NUL-byte terminated). * diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h index 8598f94e5..c42abd370 100644 --- a/include/git2/sys/odb_backend.h +++ b/include/git2/sys/odb_backend.h @@ -36,7 +36,7 @@ struct git_odb_backend { void **, size_t *, git_object_t *, git_odb_backend *, const git_oid *); /* To find a unique object given a prefix of its oid. The oid given - * must be so that the remaining (GIT_OID_HEXSZ - len)*4 bits are 0s. + * must be so that the remaining (GIT_OID_SHA1_HEXSIZE - len)*4 bits are 0s. */ int GIT_CALLBACK(read_prefix)( git_oid *, void **, size_t *, git_object_t *, -- cgit v1.2.1 From 0acaf3a8eb7c3276b7864df8f5588c7007b38f35 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 17 Jan 2022 13:40:37 -0500 Subject: oid: define GIT_OID_SHA1_ZERO Callers should not assume the layout of the oid structure; provide them a macro that defines the null / zero sha1 object id. --- include/git2/common.h | 5 ----- include/git2/oid.h | 10 ++++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/git2/common.h b/include/git2/common.h index c3e3e7b4e..ccf66334a 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -105,11 +105,6 @@ GIT_BEGIN_DECL */ #define GIT_PATH_MAX 4096 -/** - * The string representation of the null object ID. - */ -#define GIT_OID_HEX_ZERO "0000000000000000000000000000000000000000" - /** * Return the version of the libgit2 library * being currently used. diff --git a/include/git2/oid.h b/include/git2/oid.h index 0d0f05489..3dedae85e 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -35,6 +35,16 @@ typedef struct git_oid { unsigned char id[GIT_OID_SHA1_SIZE]; } git_oid; +/** + * The binary representation of the null object ID. + */ +#define GIT_OID_SHA1_ZERO { { 0 } } + +/** + * The string representation of the null object ID. + */ +#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" + /** * Parse a hex formatted object id into a git_oid. * -- cgit v1.2.1 From 74ffce20780783e20668f7ac9d543cfc3e03fe0c Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 11 Dec 2021 12:00:26 -0500 Subject: oid: introduce `git_oid_t` We require an enumeration to help us identify the different types of object IDs. Currently, we only support SHA1 but we will support SHA256 in the future. --- include/git2/oid.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'include') diff --git a/include/git2/oid.h b/include/git2/oid.h index 3dedae85e..8f92e75c9 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -19,6 +19,11 @@ */ GIT_BEGIN_DECL +/** The type of object id, currently only SHA1. */ +typedef enum { + GIT_OID_SHA1 = 1 /**< SHA1 */ +} git_oid_t; + /** Size (in bytes) of a raw/binary oid */ #define GIT_OID_SHA1_SIZE 20 -- cgit v1.2.1 From c43601160b6ecc66db4a26c784bbe62624305474 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sun, 12 Dec 2021 09:56:28 -0500 Subject: oid: include maximum oid raw/hex sizes --- include/git2/oid.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/git2/oid.h b/include/git2/oid.h index 8f92e75c9..db433a0b4 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -26,9 +26,11 @@ typedef enum { /** Size (in bytes) of a raw/binary oid */ #define GIT_OID_SHA1_SIZE 20 +#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE /** Size (in bytes) of a hex formatted oid */ #define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) +#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE /** Minimum length (in number of hex characters, * i.e. packets of 4 bits) of an oid prefix */ -- cgit v1.2.1 From 3fbf580c91935ccdea25a135204419991f503b63 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sun, 23 Jan 2022 09:47:01 -0500 Subject: oid: give oids a type `git_oid`s now have a type, and we require the oid type when creating the object id from creation functions. --- include/git2/oid.h | 57 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/git2/oid.h b/include/git2/oid.h index db433a0b4..8feb0a358 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -21,16 +21,16 @@ GIT_BEGIN_DECL /** The type of object id, currently only SHA1. */ typedef enum { - GIT_OID_SHA1 = 1 /**< SHA1 */ + GIT_OID_SHA1 = 1 /**< SHA1 */ } git_oid_t; /** Size (in bytes) of a raw/binary oid */ -#define GIT_OID_SHA1_SIZE 20 -#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE +#define GIT_OID_SHA1_SIZE 20 +#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE /** Size (in bytes) of a hex formatted oid */ -#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) -#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE +#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) +#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE /** Minimum length (in number of hex characters, * i.e. packets of 4 bits) of an oid prefix */ @@ -38,39 +38,49 @@ typedef enum { /** Unique identity of any object (commit, tree, blob, tag). */ typedef struct git_oid { + /** type of object id */ + unsigned char type; + /** raw binary formatted id */ - unsigned char id[GIT_OID_SHA1_SIZE]; + unsigned char id[GIT_OID_MAX_SIZE]; } git_oid; /** * The binary representation of the null object ID. */ -#define GIT_OID_SHA1_ZERO { { 0 } } +#define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } } /** * The string representation of the null object ID. */ -#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" +#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" /** * Parse a hex formatted object id into a git_oid. * + * The appropriate number of bytes for the given object ID type will + * be read from the string - 40 bytes for SHA1, 64 bytes for SHA256. + * The given string need not be NUL terminated. + * * @param out oid structure the result is written into. * @param 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). + * needed for an oid encoded in hex (40 bytes for sha1, + * 256 bytes for sha256). + * @param type the type of object id * @return 0 or an error code */ -GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); +GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type); /** - * Parse a hex formatted null-terminated string into a git_oid. + * Parse a hex formatted NUL-terminated string into a git_oid. * * @param out oid structure the result is written into. * @param str input hex string; must be null-terminated. + * @param type the type of object id * @return 0 or an error code */ -GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str); +GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type); /** * Parse N characters of a hex formatted object id into a git_oid. @@ -81,9 +91,10 @@ GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str); * @param out oid structure the result is written into. * @param str input hex string of at least size `length` * @param length length of the input string + * @param type the type of object id * @return 0 or an error code */ -GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length); +GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type); /** * Copy an already raw oid into a git_oid structure. @@ -92,16 +103,17 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length); * @param raw the raw input bytes to be copied. * @return 0 on success or error code */ -GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw); +GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw, git_oid_t type); /** * Format a git_oid into a hex string. * * @param 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. + * needed for an oid encoded in hex (40 bytes for SHA1, + * 64 bytes for SHA256). Only the oid digits are written; + * a '\\0' terminator must be added by the caller if it is + * required. * @param id oid structure to format. * @return 0 on success or error code */ @@ -127,9 +139,10 @@ GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id); * * @param 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. + * needed for an oid encoded in hex (41 bytes for SHA1, + * 65 bytes for SHA256). Only the oid digits are written; + * a '\\0' terminator must be added by the caller if it + * is required. * @param id oid structure to format. * @return 0 on success, non-zero callback return value, or error code */ @@ -151,7 +164,9 @@ GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid); /** * Format a git_oid into a buffer as a hex format c-string. * - * If the buffer is smaller than GIT_OID_SHA1_HEXSIZE+1, then the resulting + * If the buffer is smaller than the size of a hex-formatted oid string + * plus an additional byte (GIT_OID_SHA_HEXSIZE + 1 for SHA1 or + * GIT_OID_SHA256_HEXSIZE + 1 for SHA256), then the resulting * oid c-string will be truncated to n-1 characters (but will still be * NUL-byte terminated). * -- cgit v1.2.1 From 0db1c57cf0cd555db456ebf854ac99aeb68b83cf Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 25 Jan 2022 10:32:47 -0500 Subject: oid: add sha256 typed oids --- include/git2/oid.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/git2/oid.h b/include/git2/oid.h index 8feb0a358..924c680cc 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -19,18 +19,21 @@ */ GIT_BEGIN_DECL -/** The type of object id, currently only SHA1. */ +/** The type of object id. */ typedef enum { - GIT_OID_SHA1 = 1 /**< SHA1 */ + GIT_OID_SHA1 = 1, /**< SHA1 */ + GIT_OID_SHA256 = 2 /**< SHA256 */ } git_oid_t; /** Size (in bytes) of a raw/binary oid */ #define GIT_OID_SHA1_SIZE 20 -#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE +#define GIT_OID_SHA256_SIZE 32 +#define GIT_OID_MAX_SIZE GIT_OID_SHA256_SIZE /** Size (in bytes) of a hex formatted oid */ #define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) -#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE +#define GIT_OID_SHA256_HEXSIZE (GIT_OID_SHA256_SIZE * 2) +#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA256_HEXSIZE /** Minimum length (in number of hex characters, * i.e. packets of 4 bits) of an oid prefix */ @@ -49,11 +52,13 @@ typedef struct git_oid { * The binary representation of the null object ID. */ #define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } } +#define GIT_OID_SHA256_ZERO { GIT_OID_SHA256, { 0 } } /** * The string representation of the null object ID. */ #define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" +#define GIT_OID_SHA256_HEXZERO "0000000000000000000000000000000000000000000000000000000000000000" /** * Parse a hex formatted object id into a git_oid. -- cgit v1.2.1 From 14798060a639924c7cd9cae3234abfe2d5327aff Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 26 Jan 2022 13:10:36 -0500 Subject: oid: specify a default implementation libgit2's current default oid type is SHA1, set a public macro for that. --- include/git2/oid.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include') diff --git a/include/git2/oid.h b/include/git2/oid.h index 924c680cc..6136e2858 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -25,6 +25,9 @@ typedef enum { GIT_OID_SHA256 = 2 /**< SHA256 */ } git_oid_t; +/** SHA1 is currently libgit2's default oid type. */ +#define GIT_OID_DEFAULT GIT_OID_SHA1 + /** Size (in bytes) of a raw/binary oid */ #define GIT_OID_SHA1_SIZE 20 #define GIT_OID_SHA256_SIZE 32 -- cgit v1.2.1 From 8444b6dce71d53a44f300d7c023cdd075faaa872 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 26 Jan 2022 13:07:28 -0500 Subject: odb_hash*: accept the oid type to hash into The git_odb_hash helper functions should not assume SHA1, and instead should be given the oid type that they're producing. --- include/git2/odb.h | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/git2/odb.h b/include/git2/odb.h index 54c38a477..61e04d190 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -435,18 +435,24 @@ GIT_EXTERN(int) git_odb_write_multi_pack_index( git_odb *db); /** - * Determine the object-ID (sha1 hash) of a data buffer + * Determine the object-ID (sha1 or sha256 hash) of a data buffer * - * The resulting SHA-1 OID will be the identifier for the data - * buffer as if the data buffer it were to written to the ODB. + * The resulting OID will be the identifier for the data buffer as if + * the data buffer it were to written to the ODB. * * @param out the resulting object-ID. * @param data data to hash * @param len size of the data - * @param type of the data to hash + * @param object_type of the data to hash + * @param oid_type the oid type to hash to * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type); +GIT_EXTERN(int) git_odb_hash( + git_oid *out, + const void *data, + size_t len, + git_object_t object_type, + git_oid_t oid_type); /** * Read a file from disk and fill a git_oid with the object id @@ -458,10 +464,15 @@ GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_obj * * @param out oid structure the result is written into. * @param path file to read and determine object id for - * @param type the type of the object that will be hashed + * @param object_type of the data to hash + * @param oid_type the oid type to hash to * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_object_t type); +GIT_EXTERN(int) git_odb_hashfile( + git_oid *out, + const char *path, + git_object_t object_type, + git_oid_t oid_type); /** * Create a copy of an odb_object -- cgit v1.2.1 From 3eba9181cf74693d5906c799e08aba15b5745355 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 26 Jan 2022 13:02:49 -0500 Subject: odb: add git_odb_options Users will need to be able to specify the object id type for the given object database; add a new `git_odb_options` with that option. --- include/git2/odb.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/git2/odb.h b/include/git2/odb.h index 61e04d190..12b5fc730 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -38,6 +38,19 @@ typedef enum { */ typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload); +/** Options for configuring a loose object backend. */ +typedef struct { + unsigned int version; /**< version for the struct */ +} git_odb_options; + +/* The current version of the diff options structure */ +#define GIT_ODB_OPTIONS_VERSION 1 + +/* Stack initializer for odb options. Alternatively use + * `git_odb_options_init` programmatic initialization. + */ +#define GIT_ODB_OPTIONS_INIT { GIT_ODB_OPTIONS_VERSION } + /** * Create a new object database with no backends. * @@ -46,9 +59,10 @@ typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload); * * @param out location to store the database pointer, if opened. * Set to NULL if the open failed. + * @param opts the options for this object database or NULL for defaults * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_new(git_odb **out); +GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts); /** * Create a new object database and automatically add @@ -64,9 +78,13 @@ GIT_EXTERN(int) git_odb_new(git_odb **out); * @param out location to store the database pointer, if opened. * Set to NULL if the open failed. * @param objects_dir path of the backends' "objects" directory. + * @param opts the options for this object database or NULL for defaults * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir); +GIT_EXTERN(int) git_odb_open( + git_odb **out, + const char *objects_dir, + const git_odb_options *opts); /** * Add an on-disk alternate to an existing Object DB. -- cgit v1.2.1 From dbccfc203e776196ea2901dbcb652bba6a848a30 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 26 Jan 2022 13:57:48 -0500 Subject: odb: accept an oid type in options Allow the object database to take an oid type that it supports. This oid type will be used to validate the objects that the backends provide. --- include/git2/odb.h | 6 ++++++ include/git2/odb_backend.h | 2 ++ 2 files changed, 8 insertions(+) (limited to 'include') diff --git a/include/git2/odb.h b/include/git2/odb.h index 12b5fc730..0952a1be0 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -41,6 +41,12 @@ typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload); /** Options for configuring a loose object backend. */ typedef struct { unsigned int version; /**< version for the struct */ + + /** + * Type of object IDs to use for this object database, or + * 0 for default (currently SHA1). + */ + git_oid_t oid_type; } git_odb_options; /* The current version of the diff options structure */ diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h index 5ad777b17..d93ac7348 100644 --- a/include/git2/odb_backend.h +++ b/include/git2/odb_backend.h @@ -87,6 +87,8 @@ struct git_odb_stream { unsigned int mode; void *hash_ctx; + git_oid_t oid_type; + git_object_size_t declared_size; git_object_size_t received_bytes; -- cgit v1.2.1 From 4d7ec76c42d9bcf2ed4f59e0efff2f4e6e810808 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sun, 12 Dec 2021 09:19:25 -0500 Subject: odb: add git_odb_loose_backend_options Move the arguments to `git_odb_loose` into an options structure. --- include/git2/odb_backend.h | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h index d93ac7348..d65aeec5a 100644 --- a/include/git2/odb_backend.h +++ b/include/git2/odb_backend.h @@ -34,25 +34,55 @@ GIT_BEGIN_DECL */ GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **out, const char *objects_dir); +typedef enum { + GIT_ODB_BACKEND_LOOSE_FSYNC = (1 << 0) +} git_odb_backend_loose_flag_t; + +/** Options for configuring a loose object backend. */ +typedef struct { + unsigned int version; /**< version for the struct */ + + /** A combination of the `git_odb_backend_loose_flag_t` types. */ + uint32_t flags; + + /** + * zlib compression level to use (0-9), where 1 is the fastest + * at the expense of larger files, and 9 produces the best + * compression at the expense of speed. 0 indicates that no + * compression should be performed. -1 is the default (currently + * optimizing for speed). + */ + int compression_level; + + /** Permissions to use creating a directory or 0 for defaults */ + unsigned int dir_mode; + + /** Permissions to use creating a file or 0 for defaults */ + unsigned int file_mode; +} git_odb_backend_loose_options; + +/* The current version of the diff options structure */ +#define GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION 1 + +/* Stack initializer for diff options. Alternatively use + * `git_diff_options_init` programmatic initialization. + */ +#define GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT \ + { GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION, 0, -1 } + /** * Create a backend for loose objects * * @param out location to store the odb backend pointer * @param objects_dir the Git repository's objects directory - * @param compression_level zlib compression level to use - * @param do_fsync whether to do an fsync() after writing - * @param dir_mode permissions to use creating a directory or 0 for defaults - * @param file_mode permissions to use creating a file or 0 for defaults + * @param opts options for the loose object backend or NULL * * @return 0 or an error code */ GIT_EXTERN(int) git_odb_backend_loose( git_odb_backend **out, const char *objects_dir, - int compression_level, - int do_fsync, - unsigned int dir_mode, - unsigned int file_mode); + git_odb_backend_loose_options *opts); /** * Create a backend out of a single packfile -- cgit v1.2.1 From 04f34688c43d28d1fa110df7d32e8665232199ff Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 26 Jan 2022 13:10:01 -0500 Subject: odb_loose: SHA256 support for loose object storage Teach the loose object database how to cope with SHA256 objects. --- include/git2/odb_backend.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h index d65aeec5a..092eb91ae 100644 --- a/include/git2/odb_backend.h +++ b/include/git2/odb_backend.h @@ -59,13 +59,19 @@ typedef struct { /** Permissions to use creating a file or 0 for defaults */ unsigned int file_mode; + + /** + * Type of object IDs to use for this object database, or + * 0 for default (currently SHA1). + */ + git_oid_t oid_type; } git_odb_backend_loose_options; /* The current version of the diff options structure */ #define GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION 1 -/* Stack initializer for diff options. Alternatively use - * `git_diff_options_init` programmatic initialization. +/* Stack initializer for odb loose backend options. Alternatively use + * `git_odb_backend_loose_options_init` programmatic initialization. */ #define GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT \ { GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION, 0, -1 } -- cgit v1.2.1 From 6c57bac6b18f6a30e1a8c8272a8dc367f202aa88 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 14 Jun 2022 22:29:10 -0400 Subject: sha256: make sha256 an experimental optional feature libgit2 can be built with optional, experimental sha256 support. This allows consumers to begin testing and providing feedback for our sha256 support while we continue to develop it, and allows us to make API breaking changes while we iterate on a final sha256 implementation. The results will be `git2-experimental.dll` and installed as `git2-experimental.h` to avoid confusion with a production libgit2. --- include/git2.h | 1 + include/git2/deprecated.h | 6 ++++ include/git2/oid.h | 82 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/git2.h b/include/git2.h index 2961cc3e5..3457e5f04 100644 --- a/include/git2.h +++ b/include/git2.h @@ -28,6 +28,7 @@ #include "git2/diff.h" #include "git2/email.h" #include "git2/errors.h" +#include "git2/experimental.h" #include "git2/filter.h" #include "git2/global.h" #include "git2/graph.h" diff --git a/include/git2/deprecated.h b/include/git2/deprecated.h index c32abeeb7..52864ebe1 100644 --- a/include/git2/deprecated.h +++ b/include/git2/deprecated.h @@ -777,6 +777,12 @@ typedef git_trace_cb git_trace_callback; */ /**@{*/ +#ifndef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_RAWSZ GIT_OID_SHA1_SIZE +# define GIT_OID_HEXSZ GIT_OID_SHA1_HEXSIZE +# define GIT_OID_HEX_ZERO GIT_OID_SHA1_HEXZERO +#endif + GIT_EXTERN(int) git_oid_iszero(const git_oid *id); /**@}*/ diff --git a/include/git2/oid.h b/include/git2/oid.h index 6136e2858..3e828ae70 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -9,6 +9,7 @@ #include "common.h" #include "types.h" +#include "experimental.h" /** * @file git2/oid.h @@ -21,22 +22,74 @@ GIT_BEGIN_DECL /** The type of object id. */ typedef enum { + +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_OID_SHA1 = 1, /**< SHA1 */ GIT_OID_SHA256 = 2 /**< SHA256 */ +#else + GIT_OID_SHA1 = 1 /**< SHA1 */ +#endif + } git_oid_t; +/* + * SHA1 is currently the only supported object ID type. + */ + /** SHA1 is currently libgit2's default oid type. */ #define GIT_OID_DEFAULT GIT_OID_SHA1 -/** Size (in bytes) of a raw/binary oid */ +/** Size (in bytes) of a raw/binary sha1 oid */ #define GIT_OID_SHA1_SIZE 20 -#define GIT_OID_SHA256_SIZE 32 -#define GIT_OID_MAX_SIZE GIT_OID_SHA256_SIZE - -/** Size (in bytes) of a hex formatted oid */ +/** Size (in bytes) of a hex formatted sha1 oid */ #define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) -#define GIT_OID_SHA256_HEXSIZE (GIT_OID_SHA256_SIZE * 2) -#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA256_HEXSIZE + +/** + * The binary representation of the null sha1 object ID. + */ +#ifndef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_SHA1_ZERO { { 0 } } +#else +# define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } } +#endif + +/** + * The string representation of the null sha1 object ID. + */ +#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" + +/* + * Experimental SHA256 support is a breaking change to the API. + * This exists for application compatibility testing. + */ + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** Size (in bytes) of a raw/binary sha256 oid */ +# define GIT_OID_SHA256_SIZE 32 +/** Size (in bytes) of a hex formatted sha256 oid */ +# define GIT_OID_SHA256_HEXSIZE (GIT_OID_SHA256_SIZE * 2) + +/** + * The binary representation of the null sha256 object ID. + */ +# define GIT_OID_SHA256_ZERO { GIT_OID_SHA256, { 0 } } + +/** + * The string representation of the null sha256 object ID. + */ +# define GIT_OID_SHA256_HEXZERO "0000000000000000000000000000000000000000000000000000000000000000" + +#endif + +/* Maximum possible object ID size in raw / hex string format. */ +#ifndef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE +# define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE +#else +# define GIT_OID_MAX_SIZE GIT_OID_SHA256_SIZE +# define GIT_OID_MAX_HEXSIZE GIT_OID_SHA256_HEXSIZE +#endif /** Minimum length (in number of hex characters, * i.e. packets of 4 bits) of an oid prefix */ @@ -44,25 +97,16 @@ typedef enum { /** Unique identity of any object (commit, tree, blob, tag). */ typedef struct git_oid { + +#ifdef GIT_EXPERIMENTAL_SHA256 /** type of object id */ unsigned char type; +#endif /** raw binary formatted id */ unsigned char id[GIT_OID_MAX_SIZE]; } git_oid; -/** - * The binary representation of the null object ID. - */ -#define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } } -#define GIT_OID_SHA256_ZERO { GIT_OID_SHA256, { 0 } } - -/** - * The string representation of the null object ID. - */ -#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" -#define GIT_OID_SHA256_HEXZERO "0000000000000000000000000000000000000000000000000000000000000000" - /** * Parse a hex formatted object id into a git_oid. * -- cgit v1.2.1 From 43dd3700a2940679d15ea351f8394928f74510e4 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 13 Jul 2022 20:17:27 -0400 Subject: meta: update version number to v1.6.0-alpha --- include/git2/version.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/git2/version.h b/include/git2/version.h index d591904fd..09f4ae3e4 100644 --- a/include/git2/version.h +++ b/include/git2/version.h @@ -11,13 +11,13 @@ * The version string for libgit2. This string follows semantic * versioning (v2) guidelines. */ -#define LIBGIT2_VERSION "1.5.0" +#define LIBGIT2_VERSION "1.6.0-alpha" /** The major version number for this version of libgit2. */ #define LIBGIT2_VER_MAJOR 1 /** The minor version number for this version of libgit2. */ -#define LIBGIT2_VER_MINOR 5 +#define LIBGIT2_VER_MINOR 6 /** The revision ("teeny") version number for this version of libgit2. */ #define LIBGIT2_VER_REVISION 0 @@ -31,9 +31,9 @@ * a prerelease name like "beta" or "rc1". For final releases, this will * be `NULL`. */ -#define LIBGIT2_VER_PRERELEASE NULL +#define LIBGIT2_VER_PRERELEASE "alpha" /** The library ABI soversion for this version of libgit2. */ -#define LIBGIT2_SOVERSION "1.5" +#define LIBGIT2_SOVERSION "1.6" #endif -- cgit v1.2.1 From b43567d655b6fbc562a165095a6980d19c4ae278 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 13 Jul 2022 22:25:11 -0400 Subject: sha256: indirection for experimental functions The experimental function signature is only available when `GIT_EXPERIMENTAL_SHA256` is enabled. --- include/git2/odb.h | 16 ++++++++++++++++ include/git2/odb_backend.h | 10 ++++++++++ include/git2/oid.h | 16 ++++++++++++++++ 3 files changed, 42 insertions(+) (limited to 'include') diff --git a/include/git2/odb.h b/include/git2/odb.h index 0952a1be0..c7d6a894c 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -68,7 +68,11 @@ typedef struct { * @param opts the options for this object database or NULL for defaults * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts); +#else +GIT_EXTERN(int) git_odb_new(git_odb **out); +#endif /** * Create a new object database and automatically add @@ -87,10 +91,14 @@ GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts); * @param opts the options for this object database or NULL for defaults * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_odb_open( git_odb **out, const char *objects_dir, const git_odb_options *opts); +#else +GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir); +#endif /** * Add an on-disk alternate to an existing Object DB. @@ -471,12 +479,16 @@ GIT_EXTERN(int) git_odb_write_multi_pack_index( * @param oid_type the oid type to hash to * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_odb_hash( git_oid *out, const void *data, size_t len, git_object_t object_type, git_oid_t oid_type); +#else +GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type); +#endif /** * Read a file from disk and fill a git_oid with the object id @@ -492,11 +504,15 @@ GIT_EXTERN(int) git_odb_hash( * @param oid_type the oid type to hash to * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_odb_hashfile( git_oid *out, const char *path, git_object_t object_type, git_oid_t oid_type); +#else +GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_object_t type); +#endif /** * Create a copy of an odb_object diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h index 092eb91ae..a31d1b782 100644 --- a/include/git2/odb_backend.h +++ b/include/git2/odb_backend.h @@ -85,10 +85,20 @@ typedef struct { * * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_odb_backend_loose( git_odb_backend **out, const char *objects_dir, git_odb_backend_loose_options *opts); +#else +GIT_EXTERN(int) git_odb_backend_loose( + git_odb_backend **out, + const char *objects_dir, + int compression_level, + int do_fsync, + unsigned int dir_mode, + unsigned int file_mode); +#endif /** * Create a backend out of a single packfile diff --git a/include/git2/oid.h b/include/git2/oid.h index 3e828ae70..399b7b907 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -122,7 +122,11 @@ typedef struct git_oid { * @param type the type of object id * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type); +#else +GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); +#endif /** * Parse a hex formatted NUL-terminated string into a git_oid. @@ -132,7 +136,11 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type); * @param type the type of object id * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type); +#else +GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str); +#endif /** * Parse N characters of a hex formatted object id into a git_oid. @@ -146,7 +154,11 @@ GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type); * @param type the type of object id * @return 0 or an error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type); +#else +GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length); +#endif /** * Copy an already raw oid into a git_oid structure. @@ -155,7 +167,11 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, g * @param raw the raw input bytes to be copied. * @return 0 on success or error code */ +#ifdef GIT_EXPERIMENTAL_SHA256 GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw, git_oid_t type); +#else +GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw); +#endif /** * Format a git_oid into a hex string. -- cgit v1.2.1