summaryrefslogtreecommitdiff
path: root/src/libgit2
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2022-09-19 10:39:58 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2023-02-12 21:26:10 +0000
commitacb00e4eae66bfed84046bccb60bf59707fe9626 (patch)
treecb7136b9359e1ba600cc7f24571fcb8a69f6ce0e /src/libgit2
parent4083e46f475cd3026a3c65a1e46c4b73d5c22513 (diff)
downloadlibgit2-acb00e4eae66bfed84046bccb60bf59707fe9626.tar.gz
repo: understand the `objectformat` extension
Teach the repository about the `objectformat` extension, supporting `sha1` always and `sha256` when the experimental sha256 support is active.
Diffstat (limited to 'src/libgit2')
-rw-r--r--src/libgit2/repository.c50
-rw-r--r--src/libgit2/repository.h1
2 files changed, 46 insertions, 5 deletions
diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c
index df41f581c..5d51be6b7 100644
--- a/src/libgit2/repository.c
+++ b/src/libgit2/repository.c
@@ -67,6 +67,7 @@ static const struct {
static int check_repositoryformatversion(int *version, git_config *config);
static int check_extensions(git_config *config, int version);
static int load_global_config(git_config **config);
+static int load_objectformat(git_repository *repo, git_config *config);
#define GIT_COMMONDIR_FILE "commondir"
#define GIT_GITDIR_FILE "gitdir"
@@ -240,7 +241,7 @@ GIT_INLINE(int) validate_repo_path(git_str *path)
*/
static size_t suffix_len =
CONST_STRLEN("objects/pack/pack-.pack.lock") +
- GIT_OID_SHA1_HEXSIZE;
+ GIT_OID_MAX_HEXSIZE;
return git_fs_path_validate_str_length_with_suffix(
path, suffix_len);
@@ -1030,6 +1031,13 @@ int git_repository_open_ext(
goto cleanup;
}
+ if (version > 0) {
+ if ((error = load_objectformat(repo, config)) < 0)
+ goto cleanup;
+ } else {
+ repo->oid_type = GIT_OID_SHA1;
+ }
+
/*
* Ensure that the git directory and worktree are
* owned by the current user.
@@ -1549,7 +1557,8 @@ static int check_repositoryformatversion(int *version, git_config *config)
}
static const char *builtin_extensions[] = {
- "noop"
+ "noop",
+ "objectformat"
};
static git_vector user_extensions = GIT_VECTOR_INIT;
@@ -1613,6 +1622,32 @@ static int check_extensions(git_config *config, int version)
return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL);
}
+static int load_objectformat(git_repository *repo, git_config *config)
+{
+ git_config_entry *entry = NULL;
+ int error;
+
+ if ((error = git_config_get_entry(&entry, config, "extensions.objectformat")) < 0) {
+ if (error == GIT_ENOTFOUND) {
+ repo->oid_type = GIT_OID_SHA1;
+ git_error_clear();
+ error = 0;
+ }
+
+ goto done;
+ }
+
+ if ((repo->oid_type = git_oid_type_fromstr(entry->value)) == 0) {
+ git_error_set(GIT_ERROR_REPOSITORY,
+ "unknown object format '%s'", entry->value);
+ error = GIT_EINVALID;
+ }
+
+done:
+ git_config_entry_free(entry);
+ return error;
+}
+
int git_repository__extensions(char ***out, size_t *out_len)
{
git_vector extensions;
@@ -2922,14 +2957,14 @@ int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head
{
git_filebuf file = GIT_FILEBUF_INIT;
git_str file_path = GIT_STR_INIT;
- char orig_head_str[GIT_OID_SHA1_HEXSIZE];
+ char orig_head_str[GIT_OID_MAX_HEXSIZE];
int error = 0;
git_oid_fmt(orig_head_str, orig_head);
if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) == 0 &&
- (error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_SHA1_HEXSIZE, orig_head_str)) == 0)
+ (error = git_filebuf_printf(&file, "%.*s\n", (int)git_oid_hexsize(repo->oid_type), orig_head_str)) == 0)
error = git_filebuf_commit(&file);
if (error < 0)
@@ -3042,7 +3077,7 @@ int git_repository_hashfile(
goto cleanup;
}
- error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, GIT_OID_SHA1, fl);
+ error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, repo->oid_type, fl);
cleanup:
if (fd >= 0)
@@ -3389,3 +3424,8 @@ int git_repository_submodule_cache_clear(git_repository *repo)
repo->submodule_cache = NULL;
return error;
}
+
+git_oid_t git_repository_oid_type(git_repository *repo)
+{
+ return repo ? repo->oid_type : 0;
+}
diff --git a/src/libgit2/repository.h b/src/libgit2/repository.h
index a488f2bf2..b1e212a32 100644
--- a/src/libgit2/repository.h
+++ b/src/libgit2/repository.h
@@ -153,6 +153,7 @@ struct git_repository {
unsigned is_bare:1;
unsigned is_worktree:1;
+ git_oid_t oid_type;
unsigned int lru_counter;