diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-05 10:33:11 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-05 10:35:33 +0100 |
commit | f7fd9ce947fc2189ce80abc31fdc6077df94f73b (patch) | |
tree | 4a3317440d9ebb11e506f0944bf7fc24e13a6f1b /src/libgit2 | |
parent | 389f9b10e6e71339fe28145098a37975c49a472d (diff) | |
download | libgit2-f7fd9ce947fc2189ce80abc31fdc6077df94f73b.tar.gz |
filebuf: configurable hash type
`git_filebuf` hashes contents as its written; allow for SHA1 or SHA256
as that hash. Currently, most callers simply use SHA1 as they do not yet
know about SHA256 themselves.
Diffstat (limited to 'src/libgit2')
-rw-r--r-- | src/libgit2/config_file.c | 16 | ||||
-rw-r--r-- | src/libgit2/index.c | 12 | ||||
-rw-r--r-- | src/libgit2/indexer.c | 5 |
3 files changed, 23 insertions, 10 deletions
diff --git a/src/libgit2/config_file.c b/src/libgit2/config_file.c index 932ca7601..f3b87578d 100644 --- a/src/libgit2/config_file.c +++ b/src/libgit2/config_file.c @@ -1116,7 +1116,12 @@ static int write_on_eof( /* * This is pretty much the parsing, except we write out anything we don't have */ -static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value) +static int config_file_write( + config_file_backend *cfg, + const char *orig_key, + const char *key, + const git_regexp *preg, + const char *value) { char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot; @@ -1124,15 +1129,18 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con git_config_parser parser = GIT_CONFIG_PARSER_INIT; git_filebuf file = GIT_FILEBUF_INIT; struct write_data write_data; - int error; + int filebuf_hash, error; + + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1)); + GIT_ASSERT(filebuf_hash); memset(&write_data, 0, sizeof(write_data)); if (cfg->locked) { error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content)); } else { - if ((error = git_filebuf_open(&file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS, - GIT_CONFIG_FILE_MODE)) < 0) + if ((error = git_filebuf_open(&file, cfg->file.path, + filebuf_hash, GIT_CONFIG_FILE_MODE)) < 0) goto done; /* We need to read in our own config file */ diff --git a/src/libgit2/index.c b/src/libgit2/index.c index d4532c005..6777f0358 100644 --- a/src/libgit2/index.c +++ b/src/libgit2/index.c @@ -3668,19 +3668,23 @@ int git_indexwriter_init( git_indexwriter *writer, git_index *index) { - int error; + int filebuf_hash, error; GIT_REFCOUNT_INC(index); writer->index = index; + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1)); + GIT_ASSERT(filebuf_hash); + if (!index->index_file_path) return create_index_error(-1, "failed to write index: The index is in-memory only"); - if ((error = git_filebuf_open( - &writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) { - + if ((error = git_filebuf_open(&writer->file, + index->index_file_path, + git_filebuf_hash_flags(filebuf_hash), + GIT_INDEX_FILE_MODE)) < 0) { if (error == GIT_ELOCKED) git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process"); diff --git a/src/libgit2/indexer.c b/src/libgit2/indexer.c index fa55fb5ea..7357a4aa5 100644 --- a/src/libgit2/indexer.c +++ b/src/libgit2/indexer.c @@ -1232,6 +1232,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) git_filebuf index_file = {0}; void *packfile_trailer; size_t checksum_size; + int filebuf_hash; bool mismatch; if (!idx->parsed_header) { @@ -1240,6 +1241,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) } checksum_size = git_hash_size(indexer_hash_algorithm(idx)); + filebuf_hash = git_filebuf_hash_flags(indexer_hash_algorithm(idx)); GIT_ASSERT(checksum_size); /* Test for this before resolve_deltas(), as it plays with idx->off */ @@ -1314,8 +1316,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) return -1; if (git_filebuf_open(&index_file, filename.ptr, - GIT_FILEBUF_HASH_CONTENTS | - (idx->do_fsync ? GIT_FILEBUF_FSYNC : 0), + filebuf_hash | (idx->do_fsync ? GIT_FILEBUF_FSYNC : 0), idx->mode) < 0) goto on_error; |