8000 SHA256 improvements by ethomson · Pull Request #6965 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

SHA256 improvements #6965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
index: provide a index_options structure when opening
Instead of simply taking the oid type, future-proof our index opening
and creation functionality by taking an options structure.
  • Loading branch information
ethomson committed Dec 18, 2024
commit 708d64f1e8eaa1ded7ba0915e154fe15defe5d7a
2 changes: 1 addition & 1 deletion examples/show-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int lg2_show_index(git_repository *repo, int argc, char **argv)
dirlen = strlen(dir);
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
#ifdef GIT_EXPERIMENTAL_ 8000 SHA256
check_lg2(git_index_open(&index, dir, GIT_OID_SHA1), "could not open index", dir);
check_lg2(git_index_open(&index, dir, NULL), "could not open index", dir);
#else
check_lg2(git_index_open(&index, dir), "could not open index", dir);
#endif
Expand Down
52 changes: 48 additions & 4 deletions include/git2/index.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,69 @@ typedef enum {

#ifdef GIT_EXPERIMENTAL_SHA256

/**
* The options for opening or creating an index.
*
* Initialize with `GIT_INDEX_OPTIONS_INIT`. Alternatively, you can
* use `git_index_options_init`.
*
* @options[version] GIT_INDEX_OPTIONS_VERSION
* @options[init_macro] GIT_INDEX_OPTIONS_INIT
* @options[init_function] git_index_options_init
*/
typedef struct git_index_options {
unsigned int version; /**< The version */

/**
* The object ID type for the object IDs that exist in the index.
*
* If this is not specified, this defaults to `GIT_OID_SHA1`.
*/
git_oid_t oid_type;
} git_index_options;

/** Current version for the `git_index_options` structure */
#define GIT_INDEX_OPTIONS_VERSION 1

/** Static constructor for `git_index_options` */
#define GIT_INDEX_OPTIONS_INIT { GIT_INDEX_OPTIONS_VERSION }

/**
* Initialize git_index_options structure
*
* Initializes a `git_index_options` with default values. Equivalent to creating
* an instance with GIT_INDEX_OPTIONS_INIT.
*
* @param opts The `git_index_options` struct to initialize.
* @param version The struct version; pass `GIT_INDEX_OPTIONS_VERSION`.
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_index_options_init(
git_index_options *opts,
unsigned int version);

/**
* Creates a new bare Git index object, without a repository to back
* it. This index object is capable of storing SHA256 objects.
*
* @param index_out the pointer for the new index
* @param index_path the path to the index file in disk
* @param oid_type the object ID type to use for the repository
* @param opts the options for opening the index, or NULL
* @return 0 or an error code
*/
GIT_EXTERN(int) git_index_open(git_index **index_out, const char *index_path, git_oid_t oid_type);
GIT_EXTERN(int) git_index_open(
git_index **index_out,
const char *index_path,
const git_index_options *opts);

/**
* Create an in-memory index object.
*
* @param index_out the pointer for the new index
* @param oid_type the object ID type to use for the repository
* @param opts the options for opening the index, or NULL
* @return 0 or an error code
*/
GIT_EXTERN(int) git_index_new(git_index **index_out, git_oid_t oid_type);
GIT_EXTERN(int) git_index_new(git_index **index_out, const git_index_options *opts);

#else

Expand Down
14 changes: 10 additions & 4 deletions src/libgit2/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ int git_index__open(
index = git__calloc(1, sizeof(git_index));
GIT_ERROR_CHECK_ALLOC(index);

GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type));
index->oid_type = oid_type;

if (git_pool_init(&index->tree_pool, 1) < 0)
Expand Down Expand Up @@ -441,9 +442,13 @@ int git_index__open(
}

#ifdef GIT_EXPERIMENTAL_SHA256
int git_index_open(git_index **index_out, const char *index_path, git_oid_t oid_type)
int git_index_open(
git_index **index_out,
const char *index_path,
const git_index_options *opts)
{
return git_index__open(index_out, index_path, oid_type);
return git_index__open(index_out, index_path,
opts && opts->oid_type ? opts->oid_type : GIT_OID_DEFAULT);
}
#else
int git_index_open(git_index **index_out, const char *index_path)
Expand All @@ -458,9 +463,10 @@ int git_index__new(git_index **out, git_oid_t oid_type)
}

#ifdef GIT_EXPERIMENTAL_SHA256
int git_index_new(git_index **out, git_oid_t oid_type)
int git_index_new(git_index **out, const git_index_options *opts)
{
return git_index__new(out, oid_type);
return git_index__new(out,
opts && opts->oid_type ? opts->oid_type : GIT_OID_DEFAULT);
}
#else
int git_index_new(git_index **out)
Expand Down
0