diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-05 10:39:01 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-05 10:39:01 +0100 |
commit | 751887211b9f9fde4eff15018d2b524da55274d2 (patch) | |
tree | c59f9815cd971168a9bc2c90ef9a2066ebdd8d43 /src | |
parent | f7fd9ce947fc2189ce80abc31fdc6077df94f73b (diff) | |
download | libgit2-751887211b9f9fde4eff15018d2b524da55274d2.tar.gz |
index: add sha256 supportethomson/sha256_index
Diffstat (limited to 'src')
-rw-r--r-- | src/libgit2/apply.c | 7 | ||||
-rw-r--r-- | src/libgit2/index.c | 433 | ||||
-rw-r--r-- | src/libgit2/index.h | 15 | ||||
-rw-r--r-- | src/libgit2/merge.c | 15 | ||||
-rw-r--r-- | src/libgit2/repository.c | 3 | ||||
-rw-r--r-- | src/libgit2/stash.c | 17 | ||||
-rw-r--r-- | src/libgit2/tree-cache.c | 42 | ||||
-rw-r--r-- | src/libgit2/tree-cache.h | 8 | ||||
-rw-r--r-- | src/libgit2/tree.c | 2 |
9 files changed, 386 insertions, 156 deletions
diff --git a/src/libgit2/apply.c b/src/libgit2/apply.c index 18304da4d..6b55b812f 100644 --- a/src/libgit2/apply.c +++ b/src/libgit2/apply.c @@ -19,6 +19,7 @@ #include "zstream.h" #include "reader.h" #include "index.h" +#include "repository.h" #include "apply.h" typedef struct { @@ -644,7 +645,7 @@ int git_apply_to_tree( * put the current tree into the postimage as-is - the diff will * replace any entries contained therein */ - if ((error = git_index_new(&postimage)) < 0 || + if ((error = git_index__new(&postimage, repo->oid_type)) < 0 || (error = git_index_read_tree(postimage, preimage)) < 0 || (error = git_reader_for_index(&post_reader, repo, postimage)) < 0) goto done; @@ -851,8 +852,8 @@ int git_apply( * having the full repo index, so we will limit our checkout * to only write these files that were affected by the diff. */ - if ((error = git_index_new(&preimage)) < 0 || - (error = git_index_new(&postimage)) < 0 || + if ((error = git_index__new(&preimage, repo->oid_type)) < 0 || + (error = git_index__new(&postimage, repo->oid_type)) < 0 || (error = git_reader_for_index(&post_reader, repo, postimage)) < 0) goto done; diff --git a/src/libgit2/index.c b/src/libgit2/index.c index 6777f0358..9d919093b 100644 --- a/src/libgit2/index.c +++ b/src/libgit2/index.c @@ -32,8 +32,6 @@ static int index_apply_to_wd_diff(git_index *index, int action, const git_strarr unsigned int flags, git_index_matched_path_cb cb, void *payload); -#define minimal_entry_size (offsetof(struct entry_short, path)) - static const size_t INDEX_HEADER_SIZE = 12; static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2; @@ -65,7 +63,7 @@ struct entry_time { uint32_t nanoseconds; }; -struct entry_short { +struct entry_common { struct entry_time ctime; struct entry_time mtime; uint32_t dev; @@ -74,25 +72,35 @@ struct entry_short { uint32_t uid; uint32_t gid; uint32_t file_size; - unsigned char oid[GIT_OID_SHA1_SIZE]; - uint16_t flags; - char path[1]; /* arbitrary length */ }; -struct entry_long { - struct entry_time ctime; - struct entry_time mtime; - uint32_t dev; - uint32_t ino; - uint32_t mode; - uint32_t uid; - uint32_t gid; - uint32_t file_size; - unsigned char oid[GIT_OID_SHA1_SIZE]; - uint16_t flags; - uint16_t flags_extended; - char path[1]; /* arbitrary length */ -}; +#define entry_short(oid_size) \ + struct { \ + struct entry_common common; \ + unsigned char oid[oid_size]; \ + uint16_t flags; \ + char path[1]; /* arbitrary length */ \ + } + +#define entry_long(oid_size) \ + struct { \ + struct entry_common common; \ + unsigned char oid[oid_size]; \ + uint16_t flags; \ + uint16_t flags_extended; \ + char path[1]; /* arbitrary length */ \ + } + +typedef entry_short(GIT_OID_SHA1_SIZE) index_entry_short_sha1; +typedef entry_long(GIT_OID_SHA1_SIZE) index_entry_long_sha1; + +#ifdef GIT_EXPERIMENTAL_SHA256 +typedef entry_short(GIT_OID_SHA256_SIZE) index_entry_short_sha256; +typedef entry_long(GIT_OID_SHA256_SIZE) index_entry_long_sha256; +#endif + +#undef entry_short +#undef entry_long struct entry_srch_key { const char *path; @@ -115,12 +123,12 @@ struct reuc_entry_internal { bool git_index__enforce_unsaved_safety = false; /* local declarations */ -static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size); +static int read_extension(size_t *read_len, git_index *index, size_t checksum_size, const char *buffer, size_t buffer_size); static int read_header(struct index_header *dest, const void *buffer); static int parse_index(git_index *index, const char *buffer, size_t buffer_size); static bool is_index_extended(git_index *index); -static int write_index(unsigned char checksum[GIT_HASH_SHA1_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file); +static int write_index(unsigned char checksum[GIT_HASH_MAX_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file); static void index_entry_free(git_index_entry *entry); static void index_entry_reuc_free(git_index_reuc_entry *reuc); @@ -401,7 +409,10 @@ void git_index__set_ignore_case(git_index *index, bool ignore_case) git_vector_sort(&index->reuc); } -int git_index_open(git_index **index_out, const char *index_path) +int git_index__open( + git_index **index_out, + const char *index_path, + git_oid_t oid_type) { git_index *index; int error = -1; @@ -411,6 +422,8 @@ int git_index_open(git_index **index_out, const char *index_path) index = git__calloc(1, sizeof(git_index)); GIT_ERROR_CHECK_ALLOC(index); + index->oid_type = oid_type; + if (git_pool_init(&index->tree_pool, 1) < 0) goto fail; @@ -451,10 +464,34 @@ fail: return error; } +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_index_open(git_index **index_out, const char *index_path, git_oid_t oid_type) +{ + return git_index__open(index_out, index_path, oid_type); +} +#else +int git_index_open(git_index **index_out, const char *index_path) +{ + return git_index__open(index_out, index_path, GIT_OID_SHA1); +} +#endif + +int git_index__new(git_index **out, git_oid_t oid_type) +{ + return git_index__open(out, NULL, oid_type); +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_index_new(git_index **out, git_oid_t oid_type) +{ + return git_index__new(out, oid_type); +} +#else int git_index_new(git_index **out) { - return git_index_open(out, NULL); + return git_index__new(out, GIT_OID_SHA1); } +#endif static void index_free(git_index *index) { @@ -620,8 +657,8 @@ static int compare_checksum(git_index *index) { int fd; ssize_t bytes_read; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_oid_size(index->oid_type); if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0) return fd; @@ -2306,6 +2343,7 @@ static int index_error_invalid(const char *message) static int read_reuc(git_index *index, const char *buffer, size_t size) { const char *endptr; + size_t oid_size = git_oid_size(index->oid_type); size_t len; int i; @@ -2354,16 +2392,16 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) for (i = 0; i < 3; i++) { if (!lost->mode[i]) continue; - if (size < GIT_OID_SHA1_SIZE) { + if (size < oid_size) { index_entry_reuc_free(lost); return index_error_invalid("reading reuc entry oid"); } - if (git_oid__fromraw(&lost->oid[i], (const unsigned char *) buffer, GIT_OID_SHA1) < 0) + if (git_oid__fromraw(&lost->oid[i], (const unsigned char *) buffer, index->oid_type) < 0) return -1; - size -= GIT_OID_SHA1_SIZE; - buffer += GIT_OID_SHA1_SIZE; + size -= oid_size; + buffer += oid_size; } /* entry was read successfully - insert into reuc vector */ @@ -2433,73 +2471,157 @@ out_err: return 0; } -static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags) +GIT_INLINE(size_t) index_entry_path_offset( + git_oid_t oid_type, + uint32_t flags) { + if (oid_type == GIT_OID_SHA1) + return (flags & GIT_INDEX_ENTRY_EXTENDED) ? + offsetof(index_entry_long_sha1, path) : + offsetof(index_entry_short_sha1, path); + +#ifdef GIT_EXPERIMENTAL_SHA256 + else if (oid_type == GIT_OID_SHA256) + return (flags & GIT_INDEX_ENTRY_EXTENDED) ? + offsetof(index_entry_long_sha256, path) : + offsetof(index_entry_short_sha256, path); +#endif + + git_error_set(GIT_ERROR_INTERNAL, "invalid oid type"); + return 0; +} + +GIT_INLINE(size_t) index_entry_flags_offset(git_oid_t oid_type) +{ + if (oid_type == GIT_OID_SHA1) + return offsetof(index_entry_long_sha1, flags_extended); + +#ifdef GIT_EXPERIMENTAL_SHA256 + else if (oid_type == GIT_OID_SHA256) + return offsetof(index_entry_long_sha256, flags_extended); +#endif + + git_error_set(GIT_ERROR_INTERNAL, "invalid oid type"); + return 0; +} + +static size_t index_entry_size( + size_t path_len, + size_t varint_len, + git_oid_t oid_type, + uint32_t flags) +{ + size_t offset, size; + + if (!(offset = index_entry_path_offset(oid_type, flags))) + return 0; + if (varint_len) { - if (flags & GIT_INDEX_ENTRY_EXTENDED) - return offsetof(struct entry_long, path) + path_len + 1 + varint_len; - else - return offsetof(struct entry_short, path) + path_len + 1 + varint_len; + if (GIT_ADD_SIZET_OVERFLOW(&size, offset, path_len) || + GIT_ADD_SIZET_OVERFLOW(&size, size, 1) || + GIT_ADD_SIZET_OVERFLOW(&size, size, varint_len)) + return 0; } else { -#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7) - if (flags & GIT_INDEX_ENTRY_EXTENDED) - return entry_size(struct entry_long, path_len); - else - return entry_size(struct entry_short, path_len); -#undef entry_size + if (GIT_ADD_SIZET_OVERFLOW(&size, offset, path_len) || + GIT_ADD_SIZET_OVERFLOW(&size, size, 8)) + return 0; + + size &= ~7; } + + return size; } static int read_entry( git_index_entry **out, size_t *out_size, git_index *index, + size_t checksum_size, const void *buffer, size_t buffer_size, const char *last) { - size_t path_length, entry_size; + size_t path_length, path_offset, entry_size; const char *path_ptr; - struct entry_short source; + struct entry_common *source_common; + index_entry_short_sha1 source_sha1; +#ifdef GIT_EXPERIMENTAL_SHA256 + index_entry_short_sha256 source_sha256; +#endif git_index_entry entry = {{0}}; bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP; char *tmp_path = NULL; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + + size_t minimal_entry_size = index_entry_path_offset(index->oid_type, 0); if (checksum_size + minimal_entry_size > buffer_size) return -1; /* buffer is not guaranteed to be aligned */ - memcpy(&source, buffer, sizeof(struct entry_short)); - - entry.ctime.seconds = (git_time_t)ntohl(source.ctime.seconds); - entry.ctime.nanoseconds = ntohl(source.ctime.nanoseconds); - entry.mtime.seconds = (git_time_t)ntohl(source.mtime.seconds); - entry.mtime.nanoseconds = ntohl(source.mtime.nanoseconds); - entry.dev = ntohl(source.dev); - entry.ino = ntohl(source.ino); - entry.mode = ntohl(source.mode); - entry.uid = ntohl(source.uid); - entry.gid = ntohl(source.gid); - entry.file_size = ntohl(source.file_size); - entry.flags = ntohs(source.flags); - - if (git_oid__fromraw(&entry.id, source.oid, GIT_OID_SHA1) < 0) + switch (index->oid_type) { + case GIT_OID_SHA1: + source_common = &source_sha1.common; + memcpy(&source_sha1, buffer, sizeof(source_sha1)); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + source_common = &source_sha256.common; + memcpy(&source_sha256, buffer, sizeof(source_sha256)); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + entry.ctime.seconds = (git_time_t)ntohl(source_common->ctime.seconds); + entry.ctime.nanoseconds = ntohl(source_common->ctime.nanoseconds); + entry.mtime.seconds = (git_time_t)ntohl(source_common->mtime.seconds); + entry.mtime.nanoseconds = ntohl(source_common->mtime.nanoseconds); + entry.dev = ntohl(source_common->dev); + entry.ino = ntohl(source_common->ino); + entry.mode = ntohl(source_common->mode); + entry.uid = ntohl(source_common->uid); + entry.gid = ntohl(source_common->gid); + entry.file_size = ntohl(source_common->file_size); + + switch (index->oid_type) { + case GIT_OID_SHA1: + if (git_oid__fromraw(&entry.id, source_sha1.oid, + GIT_OID_SHA1) < 0) + return -1; + entry.flags = ntohs(source_sha1.flags); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + if (git_oid__fromraw(&entry.id, source_sha256.oid, + GIT_OID_SHA256) < 0) + return -1; + entry.flags = ntohs(source_sha256.flags); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + if (!(path_offset = index_entry_path_offset(index->oid_type, entry.flags))) return -1; + if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) { uint16_t flags_raw; size_t flags_offset; - flags_offset = offsetof(struct entry_long, flags_extended); - memcpy(&flags_raw, (const char *) buffer + flags_offset, - sizeof(flags_raw)); + if (!(flags_offset = index_entry_flags_offset(index->oid_type))) + return -1; + + memcpy(&flags_raw, (const char *)buffer + flags_offset, sizeof(flags_raw)); flags_raw = ntohs(flags_raw); memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw)); - path_ptr = (const char *) buffer + offsetof(struct entry_long, path); - } else - path_ptr = (const char *) buffer + offsetof(struct entry_short, path); + path_ptr = (const char *)buffer + path_offset; + } else { + path_ptr = (const char *)buffer + path_offset; + } if (!compressed) { path_length = entry.flags & GIT_INDEX_ENTRY_NAMEMASK; @@ -2511,12 +2633,12 @@ static int read_entry( path_end = memchr(path_ptr, '\0', buffer_size); if (path_end == NULL) - return -1; + return index_error_invalid("invalid path name"); path_length = path_end - path_ptr; } - entry_size = index_entry_size(path_length, 0, entry.flags); + entry_size = index_entry_size(path_length, 0, index->oid_type, entry.flags); entry.path = (char *)path_ptr; } else { size_t varint_len, last_len, prefix_len, suffix_len, path_len; @@ -2542,15 +2664,18 @@ static int read_entry( memcpy(tmp_path, last, prefix_len); memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1); - entry_size = index_entry_size(suffix_len, varint_len, entry.flags); + + entry_size = index_entry_size(suffix_len, varint_len, index->oid_type, entry.flags); entry.path = tmp_path; } if (entry_size == 0) return -1; - if (checksum_size + entry_size > buffer_size) + if (checksum_size + entry_size > buffer_size) { + git_error_set(GIT_ERROR_INTERNAL, "invalid index checksum"); return -1; + } if (index_entry_dup(out, index, &entry) < 0) { git__free(tmp_path); @@ -2579,11 +2704,10 @@ static int read_header(struct index_header *dest, const void *buffer) return 0; } -static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size) +static int read_extension(size_t *read_len, git_index *index, size_t checksum_size, const char *buffer, size_t buffer_size) { struct index_extension dest; size_t total_size; - size_t checksum_size = GIT_HASH_SHA1_SIZE; /* buffer is not guaranteed to be aligned */ memcpy(&dest, buffer, sizeof(struct index_extension)); @@ -2602,7 +2726,7 @@ static int read_extension(size_t *read_len, git_index *index, const char *buffer if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { /* tree cache */ if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) { - if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0) + if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, index->oid_type, &index->tree_pool) < 0) return -1; } else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) { if (read_reuc(index, buffer + 8, dest.extension_size) < 0) @@ -2630,8 +2754,8 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) int error = 0; unsigned int i; struct index_header header = { 0 }; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_hash_size(git_oid_algorithm(index->oid_type)); const char *last = NULL; const char *empty = ""; @@ -2646,9 +2770,12 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) if (buffer_size < INDEX_HEADER_SIZE + checksum_size) return index_error_invalid("insufficient buffer space"); - /* Precalculate the SHA1 of the files's contents -- we'll match it to - * the provided SHA1 in the footer */ - git_hash_buf(checksum, buffer, buffer_size - checksum_size, GIT_HASH_ALGORITHM_SHA1); + /* + * Precalculate the hash of the files's contents -- we'll match + * it to the provided checksum in the footer. + */ + git_hash_buf(checksum, buffer, buffer_size - checksum_size, + git_oid_algorithm(index->oid_type)); /* Parse header */ if ((error = read_header(&header, buffer)) < 0) @@ -2670,7 +2797,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) git_index_entry *entry = NULL; size_t entry_size; - if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) { + if ((error = read_entry(&entry, &entry_size, index, checksum_size, buffer, buffer_size, last)) < 0) { error = index_error_invalid("invalid entry"); goto done; } @@ -2701,7 +2828,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) while (buffer_size > checksum_size) { size_t extension_size; - if ((error = read_extension(&extension_size, index, buffer, buffer_size)) < 0) { + if ((error = read_extension(&extension_size, index, checksum_size, buffer, buffer_size)) < 0) { goto done; } @@ -2714,7 +2841,10 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) goto done; } - /* 160-bit SHA-1 over the content of the index file before this checksum. */ + /* + * SHA-1 or SHA-256 (depending on the repository's object format) + * over the content of the index file before this checksum. + */ if (memcmp(checksum, buffer, checksum_size) != 0) { error = index_error_invalid( "calculated checksum does not match expected"); @@ -2754,16 +2884,40 @@ static bool is_index_extended(git_index *index) return (extended > 0); } -static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last) +static int write_disk_entry( + git_index *index, + git_filebuf *file, + git_index_entry *entry, + const char *last) { void *mem = NULL; - struct entry_short ondisk; - size_t path_len, disk_size; + struct entry_common *ondisk_common; + size_t path_len, path_offset, disk_size; int varint_len = 0; char *path; const char *path_start = entry->path; size_t same_len = 0; + index_entry_short_sha1 ondisk_sha1; + index_entry_long_sha1 ondisk_ext_sha1; +#ifdef GIT_EXPERIMENTAL_SHA256 + index_entry_short_sha256 ondisk_sha256; + index_entry_long_sha256 ondisk_ext_sha256; +#endif + + switch (index->oid_type) { + case GIT_OID_SHA1: + ondisk_common = &ondisk_sha1.common; + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + ondisk_common = &ondisk_sha256.common; + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + path_len = ((struct entry_internal *)entry)->pathlen; if (last) { @@ -2780,9 +2934,9 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha varint_len = git_encode_varint(NULL, 0, strlen(last) - same_len); } - disk_size = index_entry_size(path_len, varint_len, entry->flags); + disk_size = index_entry_size(path_len, varint_len, index->oid_type, entry->flags); - if (git_filebuf_reserve(file, &mem, disk_size) < 0) + if (!disk_size || git_filebuf_reserve(file, &mem, disk_size) < 0) return -1; memset(mem, 0x0, disk_size); @@ -2797,35 +2951,77 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha * * In 2038 I will be either too dead or too rich to care about this */ - ondisk.ctime.seconds = htonl((uint32_t)entry->ctime.seconds); - ondisk.mtime.seconds = htonl((uint32_t)entry->mtime.seconds); - ondisk.ctime.nanoseconds = htonl(entry->ctime.nanoseconds); - ondisk.mtime.nanoseconds = htonl(entry->mtime.nanoseconds); - ondisk.dev = htonl(entry->dev); - ondisk.ino = htonl(entry->ino); - ondisk.mode = htonl(entry->mode); - ondisk.uid = htonl(entry->uid); - ondisk.gid = htonl(entry->gid); - ondisk.file_size = htonl((uint32_t)entry->file_size); - git_oid_raw_cpy(ondisk.oid, entry->id.id, GIT_OID_SHA1_SIZE); - ondisk.flags = htons(entry->flags); + ondisk_common->ctime.seconds = htonl((uint32_t)entry->ctime.seconds); + ondisk_common->mtime.seconds = htonl((uint32_t)entry->mtime.seconds); + ondisk_common->ctime.nanoseconds = htonl(entry->ctime.nanoseconds); + ondisk_common->mtime.nanoseconds = htonl(entry->mtime.nanoseconds); + ondisk_common->dev = htonl(entry->dev); + ondisk_common->ino = htonl(entry->ino); + ondisk_common->mode = htonl(entry->mode); + ondisk_common->uid = htonl(entry->uid); + ondisk_common->gid = htonl(entry->gid); + ondisk_common->file_size = htonl((uint32_t)entry->file_size); + + switch (index->oid_type) { + case GIT_OID_SHA1: + git_oid_raw_cpy(ondisk_sha1.oid, entry->id.id, GIT_OID_SHA1_SIZE); + ondisk_sha1.flags = htons(entry->flags); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + git_oid_raw_cpy(ondisk_sha256.oid, entry->id.id, GIT_OID_SHA256_SIZE); + ondisk_sha256.flags = htons(entry->flags); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + path_offset = index_entry_path_offset(index->oid_type, entry->flags); if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) { - const size_t path_offset = offsetof(struct entry_long, path); - struct entry_long ondisk_ext; - memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short)); - ondisk_ext.flags_extended = htons(entry->flags_extended & + struct entry_common *ondisk_ext; + uint16_t flags_extended = htons(entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS); - memcpy(mem, &ondisk_ext, path_offset); - path = (char *)mem + path_offset; - disk_size -= path_offset; + + switch (index->oid_type) { + case GIT_OID_SHA1: + memcpy(&ondisk_ext_sha1, &ondisk_sha1, + sizeof(index_entry_short_sha1)); + ondisk_ext_sha1.flags_extended = flags_extended; + ondisk_ext = &ondisk_ext_sha1.common; + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + memcpy(&ondisk_ext_sha256, &ondisk_sha256, + sizeof(index_entry_short_sha256)); + ondisk_ext_sha256.flags_extended = flags_extended; + ondisk_ext = &ondisk_ext_sha256.common; + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + memcpy(mem, ondisk_ext, path_offset); } else { - const size_t path_offset = offsetof(struct entry_short, path); - memcpy(mem, &ondisk, path_offset); - path = (char *)mem + path_offset; - disk_size -= path_offset; + switch (index->oid_type) { + case GIT_OID_SHA1: + memcpy(mem, &ondisk_sha1, path_offset); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + memcpy(mem, &ondisk_sha256, path_offset); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } } + path = (char *)mem + path_offset; + disk_size -= path_offset; + if (last) { varint_len = git_encode_varint((unsigned char *) path, disk_size, strlen(last) - same_len); @@ -2877,7 +3073,7 @@ static int write_entries(git_index *index, git_filebuf *file) last = ""; git_vector_foreach(entries, i, entry) { - if ((error = write_disk_entry(file, entry, last)) < 0) + if ((error = write_disk_entry(index, file, entry, last)) < 0) break; if (index->version >= INDEX_VERSION_NUMBER_COMP) last = entry->path; @@ -2955,8 +3151,9 @@ done: return error; } -static int create_reuc_extension_data(git_str *reuc_buf, git_index_reuc_entry *reuc) +static int create_reuc_extension_data(git_str *reuc_buf, git_index *index, git_index_reuc_entry *reuc) { + size_t oid_size = git_oid_size(index->oid_type); int i; int error = 0; @@ -2970,7 +3167,7 @@ static int create_reuc_extension_data(git_str *reuc_buf, git_index_reuc_entry *r } for (i = 0; i < 3; i++) { - if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_SHA1_SIZE)) < 0) + if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, oid_size)) < 0) return error; } @@ -2987,7 +3184,7 @@ static int write_reuc_extension(git_index *index, git_filebuf *file) int error = 0; git_vector_foreach(out, i, reuc) { - if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0) + if ((error = create_reuc_extension_data(&reuc_buf, index, reuc)) < 0) goto done; } @@ -3036,7 +3233,7 @@ static void clear_uptodate(git_index *index) } static int write_index( - unsigned char checksum[GIT_HASH_SHA1_SIZE], + unsigned char checksum[GIT_HASH_MAX_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file) @@ -3048,7 +3245,9 @@ static int write_index( GIT_ASSERT_ARG(index); GIT_ASSERT_ARG(file); - *checksum_size = GIT_HASH_SHA1_SIZE; + GIT_ASSERT(index->oid_type); + + *checksum_size = git_hash_size(git_oid_algorithm(index->oid_type)); if (index->version <= INDEX_VERSION_NUMBER_EXT) { is_extended = is_index_extended(index); @@ -3209,7 +3408,7 @@ cleanup: if (error < 0) return error; - error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); + error = git_tree_cache_read_tree(&index->tree, tree, index->oid_type, &index->tree_pool); return error; } @@ -3674,7 +3873,7 @@ int git_indexwriter_init( writer->index = index; - filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1)); + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(index->oid_type)); GIT_ASSERT(filebuf_hash); if (!index->index_file_path) @@ -3716,7 +3915,7 @@ int git_indexwriter_init_for_operation( int git_indexwriter_commit(git_indexwriter *writer) { - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_MAX_SIZE]; size_t checksum_size; int error; diff --git a/src/libgit2/index.h b/src/libgit2/index.h index 71bb096f7..53c29977d 100644 --- a/src/libgit2/index.h +++ b/src/libgit2/index.h @@ -27,7 +27,7 @@ struct git_index { char *index_file_path; git_futils_filestamp stamp; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_MAX_SIZE]; git_vector entries; git_idxmap *entries_map; @@ -35,6 +35,8 @@ struct git_index { git_vector deleted; /* deleted entries if readers > 0 */ git_atomic32 readers; /* number of active iterators */ + git_oid_t oid_type; + unsigned int on_disk:1; unsigned int ignore_case:1; unsigned int distrust_filemode:1; @@ -141,6 +143,17 @@ GIT_INLINE(unsigned char *) git_index__checksum(git_index *index) return index->checksum; } +/* SHA256-aware internal functions */ + +extern int git_index__new( + git_index **index_out, + git_oid_t oid_type); + +extern int git_index__open( + git_index **index_out, + const char *index_path, + git_oid_t oid_type); + /* Copy the current entries vector *and* increment the index refcount. * Call `git_index__release_snapshot` when done. */ diff --git a/src/libgit2/merge.c b/src/libgit2/merge.c index df2cefb29..9a52b9f78 100644 --- a/src/libgit2/merge.c +++ b/src/libgit2/merge.c @@ -1997,8 +1997,11 @@ static int index_update_reuc(git_index *index, git_merge_diff_list *diff_list) return 0; } -static int index_from_diff_list(git_index **out, - git_merge_diff_list *diff_list, bool skip_reuc) +static int index_from_diff_list( + git_index **out, + git_merge_diff_list *diff_list, + git_oid_t oid_type, + bool skip_reuc) { git_index *index; size_t i; @@ -2007,7 +2010,7 @@ static int index_from_diff_list(git_index **out, *out = NULL; - if ((error = git_index_new(&index)) < 0) + if ((error = git_index__new(&index, oid_type)) < 0) return error; if ((error = git_index__fill(index, &diff_list->staged)) < 0) @@ -2157,7 +2160,7 @@ int git_merge__iterators( } } - error = index_from_diff_list(out, diff_list, + error = index_from_diff_list(out, diff_list, repo->oid_type, (opts.flags & GIT_MERGE_SKIP_REUC)); done: @@ -2200,8 +2203,8 @@ int git_merge_trees( result = our_tree; if (result) { - if ((error = git_index_new(out)) == 0) - error = git_index_read_tree(*out, result); + if ((error = git_index__new(out, repo->oid_type)) == 0) + error = git_index_read_tree(*out, result); return error; } diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 3f57fb23c..4bf92dcdc 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -1547,7 +1547,8 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo) if ((error = repository_index_path(&index_path, repo)) < 0) return error; - error = git_index_open(&index, index_path.ptr); + error = git_index__open(&index, index_path.ptr, repo->oid_type); + if (!error) { GIT_REFCOUNT_OWN(index, repo); diff --git a/src/libgit2/stash.c b/src/libgit2/stash.c index 319ae3a3f..b49e95cdb 100644 --- a/src/libgit2/stash.c +++ b/src/libgit2/stash.c @@ -284,7 +284,7 @@ static int build_untracked_tree( struct stash_update_rules data = {0}; int error; - if ((error = git_index_new(&i_index)) < 0) + if ((error = git_index__new(&i_index, repo->oid_type)) < 0) goto cleanup; if (flags & GIT_STASH_INCLUDE_UNTRACKED) { @@ -487,7 +487,7 @@ static int commit_worktree( int error = 0, ignorecase; if ((error = git_repository_index(&r_index, repo) < 0) || - (error = git_index_new(&i_index)) < 0 || + (error = git_index__new(&i_index, repo->oid_type)) < 0 || (error = git_index__fill(i_index, &r_index->entries) < 0) || (error = git_repository__configmap_lookup(&ignorecase, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) goto cleanup; @@ -732,7 +732,7 @@ int git_stash_save_with_opts( i_commit, b_commit, u_commit)) < 0) goto cleanup; } else { - if ((error = git_index_new(&paths_index)) < 0 || + if ((error = git_index__new(&paths_index, repo->oid_type)) < 0 || (error = retrieve_head(&head, repo)) < 0 || (error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0 || (error = git_index_read_tree(paths_index, tree)) < 0 || @@ -1003,6 +1003,7 @@ static int stage_new_file(const git_index_entry **entries, void *data) static int stage_new_files( git_index **out, + git_repository *repo, git_tree *parent_tree, git_tree *tree) { @@ -1011,7 +1012,7 @@ static int stage_new_files( git_index *index = NULL; int error; - if ((error = git_index_new(&index)) < 0 || + if ((error = git_index__new(&index, repo->oid_type)) < 0 || (error = git_iterator_for_tree( &iterators[0], parent_tree, &iterator_options)) < 0 || (error = git_iterator_for_tree( @@ -1095,10 +1096,10 @@ int git_stash_apply( * previously unstaged contents are staged, not the previously staged.) */ } else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) { - if ((error = stage_new_files( - &stash_adds, stash_parent_tree, stash_tree)) < 0 || - (error = merge_indexes( - &unstashed_index, repo, stash_parent_tree, repo_index, stash_adds)) < 0) + if ((error = stage_new_files(&stash_adds, repo, + stash_parent_tree, stash_tree)) < 0 || + (error = merge_indexes(&unstashed_index, repo, + stash_parent_tree, repo_index, stash_adds)) < 0) goto cleanup; } diff --git a/src/libgit2/tree-cache.c b/src/libgit2/tree-cache.c index 19fad85ae..95d879860 100644 --- a/src/libgit2/tree-cache.c +++ b/src/libgit2/tree-cache.c @@ -71,12 +71,16 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char } } -static int read_tree_internal(git_tree_cache **out, - const char **buffer_in, const char *buffer_end, - git_pool *pool) +static int read_tree_internal( + git_tree_cache **out, + const char **buffer_in, + const char *buffer_end, + git_oid_t oid_type, + git_pool *pool) { git_tree_cache *tree = NULL; const char *name_start, *buffer; + size_t oid_size = git_oid_size(oid_type); int count; buffer = name_start = *buffer_in; @@ -87,7 +91,7 @@ static int read_tree_internal(git_tree_cache **out, if (++buffer >= buffer_end) goto corrupted; - if (git_tree_cache_new(&tree, name_start, pool) < 0) + if (git_tree_cache_new(&tree, name_start, oid_type, pool) < 0) return -1; /* Blank-terminated ASCII decimal number of entries in this tree */ @@ -108,14 +112,14 @@ static int read_tree_internal(git_tree_cache **out, if (*buffer != '\n' || ++buffer > buffer_end) goto corrupted; - /* The SHA1 is only there if it's not invalidated */ + /* The OID is only there if it's not invalidated */ if (tree->entry_count >= 0) { /* 160-bit SHA-1 for this tree and it's children */ - if (buffer + GIT_OID_SHA1_SIZE > buffer_end) + if (buffer + oid_size > buffer_end) goto corrupted; - git_oid__fromraw(&tree->oid, (const unsigned char *)buffer, GIT_OID_SHA1); - buffer += GIT_OID_SHA1_SIZE; + git_oid__fromraw(&tree->oid, (const unsigned char *)buffer, oid_type); + buffer += oid_size; } /* Parse children: */ @@ -130,7 +134,7 @@ static int read_tree_internal(git_tree_cache **out, memset(tree->children, 0x0, bufsize); for (i = 0; i < tree->children_count; ++i) { - if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0) + if (read_tree_internal(&tree->children[i], &buffer, buffer_end, oid_type, pool) < 0) goto corrupted; } } @@ -144,11 +148,16 @@ static int read_tree_internal(git_tree_cache **out, return -1; } -int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool) +int git_tree_cache_read( + git_tree_cache **tree, + const char *buffer, + size_t buffer_size, + git_oid_t oid_type, + git_pool *pool) { const char *buffer_end = buffer + buffer_size; - if (read_tree_internal(tree, &buffer, buffer_end, pool) < 0) + if (read_tree_internal(tree, &buffer, buffer_end, oid_type, pool) < 0) return -1; if (buffer < buffer_end) { @@ -201,7 +210,7 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_ continue; } - if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), pool)) < 0) + if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), cache->oid_type, pool)) < 0) return error; if ((error = git_tree_lookup(&subtree, repo, git_tree_entry_id(entry))) < 0) @@ -219,12 +228,12 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_ return 0; } -int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_pool *pool) +int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_oid_t oid_type, git_pool *pool) { int error; git_tree_cache *cache; - if ((error = git_tree_cache_new(&cache, "", pool)) < 0) + if ((error = git_tree_cache_new(&cache, "", oid_type, pool)) < 0) return error; if ((error = read_tree_recursive(cache, tree, pool)) < 0) @@ -234,7 +243,7 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo return 0; } -int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool) +int git_tree_cache_new(git_tree_cache **out, const char *name, git_oid_t oid_type, git_pool *pool) { size_t name_len, alloc_size; git_tree_cache *tree; @@ -248,6 +257,7 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool) memset(tree, 0x0, sizeof(git_tree_cache)); /* NUL-terminated tree name */ + tree->oid_type = oid_type; tree->namelen = name_len; memcpy(tree->name, name, name_len); tree->name[name_len] = '\0'; @@ -263,7 +273,7 @@ static void write_tree(git_str *out, git_tree_cache *tree) git_str_printf(out, "%s%c%"PRIdZ" %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count); if (tree->entry_count != -1) - git_str_put(out, (char *)&tree->oid.id, GIT_OID_SHA1_SIZE); + git_str_put(out, (char *)&tree->oid.id, git_oid_size(tree->oid_type)); for (i = 0; i < tree->children_count; i++) write_tree(out, tree->children[i]); diff --git a/src/libgit2/tree-cache.h b/src/libgit2/tree-cache.h index a27e30466..e4a73f277 100644 --- a/src/libgit2/tree-cache.h +++ b/src/libgit2/tree-cache.h @@ -18,6 +18,8 @@ typedef struct git_tree_cache { struct git_tree_cache **children; size_t children_count; + git_oid_t oid_type; + ssize_t entry_count; git_oid oid; size_t namelen; @@ -25,14 +27,14 @@ typedef struct git_tree_cache { } git_tree_cache; int git_tree_cache_write(git_str *out, git_tree_cache *tree); -int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool); +int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_oid_t oid_type, git_pool *pool); void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path); const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path); -int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool); +int git_tree_cache_new(git_tree_cache **out, const char *name, git_oid_t oid_type, git_pool *pool); /** * Read a tree as the root of the tree cache (like for `git read-tree`) */ -int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_pool *pool); +int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_oid_t oid_type, git_pool *pool); void git_tree_cache_free(git_tree_cache *tree); #endif diff --git a/src/libgit2/tree.c b/src/libgit2/tree.c index 9293d9422..236a87f7e 100644 --- a/src/libgit2/tree.c +++ b/src/libgit2/tree.c @@ -731,7 +731,7 @@ int git_tree__write_index( return ret; /* Read the tree cache into the index */ - ret = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); + ret = git_tree_cache_read_tree(&index->tree, tree, index->oid_type, &index->tree_pool); git_tree_free(tree); return ret; |