object: move internal use to git_object_id_from...

Using the `git_object_id_from` is an overall code simplification; switch
to it.
This commit is contained in:
Edward Thomson
2025-01-03 13:14:57 +00:00
parent 2dd70bc196
commit 8bb32e5780
16 changed files with 142 additions and 379 deletions

View File

@@ -99,17 +99,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (git_indexer_append(indexer, data, size, &stats) < 0) if (git_indexer_append(indexer, data, size, &stats) < 0)
goto cleanup; goto cleanup;
if (append_hash) { if (append_hash) {
#ifdef GIT_EXPERIMENTAL_SHA256 if (git_object_id_from_buffer(&oid, data, size, NULL) < 0) {
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB, GIT_OID_SHA1) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n"); fprintf(stderr, "Failed to compute the SHA1 hash\n");
abort(); abort();
} }
#else
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
abort();
}
#endif
if (git_indexer_append(indexer, &oid.id, GIT_OID_SHA1_SIZE, &stats) < 0) { if (git_indexer_append(indexer, &oid.id, GIT_OID_SHA1_SIZE, &stats) < 0) {
goto cleanup; goto cleanup;

View File

@@ -52,6 +52,7 @@ static int hash_buf(
git_object_t object_type, git_object_t object_type,
git_oid_t oid_type) git_oid_t oid_type)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_oid oid; git_oid oid;
if (!literally) { if (!literally) {
@@ -72,13 +73,10 @@ static int hash_buf(
if (git_odb_write(&oid, odb, buf->ptr, buf->size, object_type) < 0) if (git_odb_write(&oid, odb, buf->ptr, buf->size, object_type) < 0)
return cli_error_git(); return cli_error_git();
} else { } else {
#ifdef GIT_EXPERIMENTAL_SHA256 id_opts.object_type = object_type;
if (git_odb_hash(&oid, buf->ptr, buf->size, object_type, GIT_OID_SHA1) < 0)
if (git_object_id_from_buffer(&oid, buf->ptr, buf->size, &id_opts) < 0)
return cli_error_git(); return cli_error_git();
#else
if (git_odb_hash(&oid, buf->ptr, buf->size, object_type) < 0)
return cli_error_git();
#endif
} }
if (printf("%s\n", git_oid_tostr_s(&oid)) < 0) if (printf("%s\n", git_oid_tostr_s(&oid)) < 0)

View File

@@ -138,10 +138,15 @@ int git_diff_file_content__init_from_src(
const git_diff_file_content_src *src, const git_diff_file_content_src *src,
git_diff_file *as_file) git_diff_file *as_file)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
memset(fc, 0, sizeof(*fc)); memset(fc, 0, sizeof(*fc));
fc->repo = repo; fc->repo = repo;
fc->file = as_file; fc->file = as_file;
id_opts.object_type = GIT_OBJECT_BLOB;
id_opts.oid_type = opts->oid_type;
if (!src->blob && !src->buf) { if (!src->blob && !src->buf) {
fc->flags |= GIT_DIFF_FLAG__NO_DATA; fc->flags |= GIT_DIFF_FLAG__NO_DATA;
git_oid_clear(&fc->file->id, opts->oid_type); git_oid_clear(&fc->file->id, opts->oid_type);
@@ -162,7 +167,11 @@ int git_diff_file_content__init_from_src(
fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
} else { } else {
int error; int error;
if ((error = git_odb__hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, opts->oid_type)) < 0)
if ((error = git_object_id_from_buffer(
&fc->file->id,
src->buf, src->buflen,
&id_opts)) < 0)
return error; return error;
fc->file->size = src->buflen; fc->file->size = src->buflen;
fc->file->id_abbrev = (uint16_t)git_oid_hexsize(opts->oid_type); fc->file->id_abbrev = (uint16_t)git_oid_hexsize(opts->oid_type);
@@ -399,9 +408,13 @@ static int diff_file_content_load_workdir(
git_diff_file_content *fc, git_diff_file_content *fc,
git_diff_options *diff_opts) git_diff_options *diff_opts)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
int error = 0; int error = 0;
git_str path = GIT_STR_INIT; git_str path = GIT_STR_INIT;
id_opts.object_type = GIT_OBJECT_BLOB;
id_opts.oid_type = diff_opts->oid_type;
if (fc->file->mode == GIT_FILEMODE_COMMIT) if (fc->file->mode == GIT_FILEMODE_COMMIT)
return diff_file_content_commit_to_str(fc, true); return diff_file_content_commit_to_str(fc, true);
@@ -418,9 +431,8 @@ static int diff_file_content_load_workdir(
/* once data is loaded, update OID if we didn't have it previously */ /* once data is loaded, update OID if we didn't have it previously */
if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) { if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
error = git_odb__hash( error = git_object_id_from_buffer(&fc->file->id,
&fc->file->id, fc->map.data, fc->map.len, fc->map.data, fc->map.len, &id_opts);
GIT_OBJECT_BLOB, diff_opts->oid_type);
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
} }

View File

@@ -632,6 +632,7 @@ int git_diff__oid_for_entry(
uint16_t mode, uint16_t mode,
const git_oid *update_match) const git_oid *update_match)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_diff_generated *diff; git_diff_generated *diff;
git_str full_path = GIT_STR_INIT; git_str full_path = GIT_STR_INIT;
git_index_entry entry = *src; git_index_entry entry = *src;
@@ -643,6 +644,9 @@ int git_diff__oid_for_entry(
git_oid_clear(out, diff->base.opts.oid_type); git_oid_clear(out, diff->base.opts.oid_type);
id_opts.object_type = GIT_OBJECT_BLOB;
id_opts.oid_type = diff->base.opts.oid_type;
if (git_repository_workdir_path(&full_path, diff->base.repo, entry.path) < 0) if (git_repository_workdir_path(&full_path, diff->base.repo, entry.path) < 0)
return -1; return -1;
@@ -677,8 +681,8 @@ int git_diff__oid_for_entry(
git_error_clear(); git_error_clear();
} }
} else if (S_ISLNK(mode)) { } else if (S_ISLNK(mode)) {
error = git_odb__hashlink(out, full_path.ptr, error = git_object_id_from_symlink(out, full_path.ptr,
diff->base.opts.oid_type); &id_opts);
diff->base.perf.oid_calculations++; diff->base.perf.oid_calculations++;
} else if (!git__is_sizet(entry.file_size)) { } else if (!git__is_sizet(entry.file_size)) {
git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'",
@@ -689,13 +693,14 @@ int git_diff__oid_for_entry(
GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE))) GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
{ {
int fd = git_futils_open_ro(full_path.ptr); int fd = git_futils_open_ro(full_path.ptr);
if (fd < 0)
id_opts.filters = fl;
if (fd < 0) {
error = fd; error = fd;
else { } else {
error = git_odb__hashfd_filtered( error = git_object_id_from_fd(out,
out, fd, (size_t)entry.file_size, fd, (size_t)entry.file_size, &id_opts);
GIT_OBJECT_BLOB, diff->base.opts.oid_type,
fl);
p_close(fd); p_close(fd);
diff->base.perf.oid_calculations++; diff->base.perf.oid_calculations++;
} }

View File

@@ -597,6 +597,7 @@ static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_ent
static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start) static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_oid oid; git_oid oid;
size_t entry_size; size_t entry_size;
struct entry *entry; struct entry *entry;
@@ -605,7 +606,10 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start)
entry = git__calloc(1, sizeof(*entry)); entry = git__calloc(1, sizeof(*entry));
GIT_ERROR_CHECK_ALLOC(entry); GIT_ERROR_CHECK_ALLOC(entry);
if (git_odb__hashobj(&oid, obj, idx->oid_type) < 0) { id_opts.object_type = obj->type;
id_opts.oid_type = idx->oid_type;
if (git_object_id_from_buffer(&oid, obj->data, obj->len, &id_opts) < 0) {
git_error_set(GIT_ERROR_INDEXER, "failed to hash object"); git_error_set(GIT_ERROR_INDEXER, "failed to hash object");
goto on_error; goto on_error;
} }

View File

@@ -1275,9 +1275,13 @@ static int filesystem_iterator_entry_hash(
filesystem_iterator *iter, filesystem_iterator *iter,
filesystem_iterator_entry *entry) filesystem_iterator_entry *entry)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_str fullpath = GIT_STR_INIT; git_str fullpath = GIT_STR_INIT;
int error; int error;
id_opts.object_type = GIT_OBJECT_BLOB;
id_opts.oid_type = iter->oid_type;
if (S_ISDIR(entry->st.st_mode)) { if (S_ISDIR(entry->st.st_mode)) {
memset(&entry->id, 0, git_oid_size(iter->oid_type)); memset(&entry->id, 0, git_oid_size(iter->oid_type));
return 0; return 0;
@@ -1289,7 +1293,8 @@ static int filesystem_iterator_entry_hash(
if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) && if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) &&
!(error = git_path_validate_str_length(iter->base.repo, &fullpath))) !(error = git_path_validate_str_length(iter->base.repo, &fullpath)))
error = git_odb__hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, iter->oid_type); error = git_object_id_from_file(&entry->id,
fullpath.ptr, &id_opts);
git_str_dispose(&fullpath); git_str_dispose(&fullpath);
return error; return error;

View File

@@ -56,6 +56,7 @@ int git_object__from_raw(
git_object_t object_type, git_object_t object_type,
git_oid_t oid_type) git_oid_t oid_type)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_object_def *def; git_object_def *def;
git_object *object; git_object *object;
size_t object_size; size_t object_size;
@@ -64,6 +65,9 @@ int git_object__from_raw(
GIT_ASSERT_ARG(object_out); GIT_ASSERT_ARG(object_out);
*object_out = NULL; *object_out = NULL;
id_opts.object_type = object_type;
id_opts.oid_type = oid_type;
/* Validate type match */ /* Validate type match */
if (object_type != GIT_OBJECT_BLOB && if (object_type != GIT_OBJECT_BLOB &&
object_type != GIT_OBJECT_TREE && object_type != GIT_OBJECT_TREE &&
@@ -83,7 +87,9 @@ int git_object__from_raw(
GIT_ERROR_CHECK_ALLOC(object); GIT_ERROR_CHECK_ALLOC(object);
object->cached.flags = GIT_CACHE_STORE_PARSED; object->cached.flags = GIT_CACHE_STORE_PARSED;
object->cached.type = object_type; object->cached.type = object_type;
if ((error = git_odb__hash(&object->cached.oid, data, size, object_type, oid_type)) < 0)
if ((error = git_object_id_from_buffer(&object->cached.oid,
data, size, &id_opts)) < 0)
return error; return error;
/* Parse raw object data */ /* Parse raw object data */
@@ -627,7 +633,7 @@ GIT_INLINE(int) normalize_options(
normalized->oid_type = (given_opts && given_opts->oid_type) ? normalized->oid_type = (given_opts && given_opts->oid_type) ?
given_opts->oid_type : GIT_OID_DEFAULT; given_opts->oid_type : GIT_OID_DEFAULT;
if (!git_object_typeisloose(normalized->object_type)) { if (!git_object_type_is_valid(normalized->object_type)) {
git_error_set(GIT_ERROR_INVALID, "invalid object type"); git_error_set(GIT_ERROR_INVALID, "invalid object type");
return -1; return -1;
} }
@@ -637,7 +643,8 @@ GIT_INLINE(int) normalize_options(
return -1; return -1;
} }
normalized->filters = given_opts->filters; if (given_opts)
normalized->filters = given_opts->filters;
return 0; return 0;
} }

View File

@@ -83,12 +83,21 @@ GIT_INLINE(git_object_t) git_object__type_from_filemode(git_filemode_t mode)
} }
} }
/**
* Calculate the object ID for an already open file descriptor.
* This is a performance call when the contents of a fd need to be hashed,
* but the fd is already open and we have the size of the contents.
* This can reduce the number of `swtat` calls.
*/
int git_object_id_from_fd( int git_object_id_from_fd(
git_oid *id, git_oid *id,
git_file fd, git_file fd,
size_t size, size_t size,
git_object_id_options *opts); git_object_id_options *opts);
/**
* Calculate the object ID for a file that is a symbolic link.
-*/
int git_object_id_from_symlink( int git_object_id_from_symlink(
git_oid *id, git_oid *id,
const char *path, const char *path,

View File

@@ -105,49 +105,6 @@ int git_odb__format_object_header(
return 0; return 0;
} }
int git_odb__hashobj(git_oid *id, git_rawobj *obj, git_oid_t oid_type)
{
git_str_vec vec[2];
char header[64];
size_t hdrlen;
git_hash_algorithm_t algorithm;
int error;
GIT_ASSERT_ARG(id);
GIT_ASSERT_ARG(obj);
if (!git_object_type_is_valid(obj->type)) {
git_error_set(GIT_ERROR_INVALID, "invalid object type");
return -1;
}
if (!(algorithm = git_oid_algorithm(oid_type))) {
git_error_set(GIT_ERROR_INVALID, "unknown oid type");
return -1;
}
if (!obj->data && obj->len != 0) {
git_error_set(GIT_ERROR_INVALID, "invalid object");
return -1;
}
if ((error = git_odb__format_object_header(&hdrlen,
header, sizeof(header), obj->len, obj->type)) < 0)
return error;
vec[0].data = header;
vec[0].len = hdrlen;
vec[1].data = obj->data;
vec[1].len = obj->len;
#ifdef GIT_EXPERIMENTAL_SHA256
id->type = oid_type;
#endif
return git_hash_vec(id->id, vec, 2, algorithm);
}
static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source) static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source)
{ {
git_odb_object *object = git__calloc(1, sizeof(git_odb_object)); git_odb_object *object = git__calloc(1, sizeof(git_odb_object));
@@ -205,233 +162,35 @@ void git_odb_object_free(git_odb_object *object)
git_cached_obj_decref(object); git_cached_obj_decref(object);
} }
int git_odb__hashfd(
git_oid *out,
git_file fd,
size_t size,
git_object_t object_type,
git_oid_t oid_type)
{
size_t hdr_len;
char hdr[64], buffer[GIT_BUFSIZE_FILEIO];
git_hash_ctx ctx;
git_hash_algorithm_t algorithm;
ssize_t read_len = 0;
int error = 0;
if (!git_object_type_is_valid(object_type)) {
git_error_set(GIT_ERROR_INVALID, "invalid object type for hash");
return -1;
}
if (!(algorithm = git_oid_algorithm(oid_type))) {
git_error_set(GIT_ERROR_INVALID, "unknown oid type");
return -1;
}
if ((error = git_hash_ctx_init(&ctx, algorithm)) < 0)
return error;
if ((error = git_odb__format_object_header(&hdr_len, hdr,
sizeof(hdr), size, object_type)) < 0)
goto done;
if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0)
goto done;
while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) {
if ((error = git_hash_update(&ctx, buffer, read_len)) < 0)
goto done;
size -= read_len;
}
/* If p_read returned an error code, the read obviously failed.
* If size is not zero, the file was truncated after we originally
* stat'd it, so we consider this a read failure too */
if (read_len < 0 || size > 0) {
git_error_set(GIT_ERROR_OS, "error reading file for hashing");
error = -1;
goto done;
}
error = git_hash_final(out->id, &ctx);
#ifdef GIT_EXPERIMENTAL_SHA256
out->type = oid_type;
#endif
done:
git_hash_ctx_cleanup(&ctx);
return error;
}
int git_odb__hashfd_filtered(
git_oid *out,
git_file fd,
size_t size,
git_object_t object_type,
git_oid_t oid_type,
git_filter_list *fl)
{
int error;
git_str raw = GIT_STR_INIT;
if (!fl)
return git_odb__hashfd(out, fd, size, object_type, oid_type);
/* size of data is used in header, so we have to read the whole file
* into memory to apply filters before beginning to calculate the hash
*/
if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
git_str post = GIT_STR_INIT;
error = git_filter_list__convert_buf(&post, fl, &raw);
if (!error)
error = git_odb__hash(out, post.ptr, post.size, object_type, oid_type);
git_str_dispose(&post);
}
return error;
}
int git_odb__hashlink(git_oid *out, const char *path, git_oid_t oid_type)
{
struct stat st;
int size;
int result;
if (git_fs_path_lstat(path, &st) < 0)
return -1;
if (!git__is_int(st.st_size) || (int)st.st_size < 0) {
git_error_set(GIT_ERROR_FILESYSTEM, "file size overflow for 32-bit systems");
return -1;
}
size = (int)st.st_size;
if (S_ISLNK(st.st_mode)) {
char *link_data;
int read_len;
size_t alloc_size;
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, size, 1);
link_data = git__malloc(alloc_size);
GIT_ERROR_CHECK_ALLOC(link_data);
read_len = p_readlink(path, link_data, size);
if (read_len == -1) {
git_error_set(GIT_ERROR_OS, "failed to read symlink data for '%s'", path);
git__free(link_data);
return -1;
}
GIT_ASSERT(read_len <= size);
link_data[read_len] = '\0';
result = git_odb__hash(out, link_data, read_len, GIT_OBJECT_BLOB, oid_type);
git__free(link_data);
} else {
int fd = git_futils_open_ro(path);
if (fd < 0)
return -1;
result = git_odb__hashfd(out, fd, size, GIT_OBJECT_BLOB, oid_type);
p_close(fd);
}
return result;
}
int git_odb__hashfile(
git_oid *out,
const char *path,
git_object_t object_type,
git_oid_t oid_type)
{
uint64_t size;
int fd, error = 0;
if ((fd = git_futils_open_ro(path)) < 0)
return fd;
if ((error = git_futils_filesize(&size, fd)) < 0)
goto done;
if (!git__is_sizet(size)) {
git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
error = -1;
goto done;
}
error = git_odb__hashfd(out, fd, (size_t)size, object_type, oid_type);
done:
p_close(fd);
return error;
}
#ifdef GIT_EXPERIMENTAL_SHA256
int git_odb_hashfile(
git_oid *out,
const char *path,
git_object_t object_type,
git_oid_t oid_type)
{
return git_odb__hashfile(out, path, object_type, oid_type);
}
#else
int git_odb_hashfile( int git_odb_hashfile(
git_oid *out, git_oid *out,
const char *path, const char *path,
git_object_t object_type) git_object_t object_type)
{ {
return git_odb__hashfile(out, path, object_type, GIT_OID_SHA1); git_object_id_options opts = GIT_OBJECT_ID_OPTIONS_INIT;
}
#endif
int git_odb__hash( GIT_ASSERT_ARG(object_type);
opts.object_type = object_type;
return git_object_id_from_file(out, path, &opts);
}
int git_odb_hash(
git_oid *id, git_oid *id,
const void *data, const void *data,
size_t len, size_t len,
git_object_t object_type, git_object_t object_type)
git_oid_t oid_type)
{ {
git_rawobj raw; git_object_id_options opts = GIT_OBJECT_ID_OPTIONS_INIT;
GIT_ASSERT_ARG(id); GIT_ASSERT_ARG(object_type);
raw.data = (void *)data; opts.object_type = object_type;
raw.len = len;
raw.type = object_type;
return git_odb__hashobj(id, &raw, oid_type); return git_object_id_from_buffer(id, data, len, &opts);
} }
#ifdef GIT_EXPERIMENTAL_SHA256
int git_odb_hash(
git_oid *out,
const void *data,
size_t len,
git_object_t object_type,
git_oid_t oid_type)
{
return git_odb__hash(out, data, len, object_type, oid_type);
}
#else
int git_odb_hash(
git_oid *out,
const void *data,
size_t len,
git_object_t type)
{
return git_odb__hash(out, data, len, type, GIT_OID_SHA1);
}
#endif
/** /**
* FAKE WSTREAM * FAKE WSTREAM
*/ */
@@ -1358,7 +1117,13 @@ static int odb_read_1(
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
if (git_odb__strict_hash_verification) { if (git_odb__strict_hash_verification) {
if ((error = git_odb__hash(&hashed, raw.data, raw.len, raw.type, db->options.oid_type)) < 0) git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
id_opts.object_type = raw.type;
id_opts.oid_type = db->options.oid_type;
if ((error = git_object_id_from_buffer(&hashed,
raw.data, raw.len, &id_opts)) < 0)
goto out; goto out;
if (!git_oid_equal(id, &hashed)) { if (!git_oid_equal(id, &hashed)) {
@@ -1502,9 +1267,14 @@ static int read_prefix_1(git_odb_object **out, git_odb *db,
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
if (git_odb__strict_hash_verification) { if (git_odb__strict_hash_verification) {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_oid hash; git_oid hash;
if ((error = git_odb__hash(&hash, raw.data, raw.len, raw.type, db->options.oid_type)) < 0) id_opts.object_type = raw.type;
id_opts.oid_type = db->options.oid_type;
if ((error = git_object_id_from_buffer(&hash,
raw.data, raw.len, &id_opts)) < 0)
goto out; goto out;
if (!git_oid_equal(&found_full_oid, &hash)) { if (!git_oid_equal(&found_full_oid, &hash)) {
@@ -1598,14 +1368,18 @@ cleanup:
int git_odb_write( int git_odb_write(
git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type) git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
git_odb_stream *stream;
size_t i; size_t i;
int error; int error;
git_odb_stream *stream;
id_opts.object_type = type;
id_opts.oid_type = db->options.oid_type;
GIT_ASSERT_ARG(oid); GIT_ASSERT_ARG(oid);
GIT_ASSERT_ARG(db); GIT_ASSERT_ARG(db);
if ((error = git_odb__hash(oid, data, len, type, db->options.oid_type)) < 0) if ((error = git_object_id_from_buffer(oid, data, len, &id_opts)) < 0)
return error; return error;
if (git_oid_is_zero(oid)) if (git_oid_is_zero(oid))

View File

@@ -70,56 +70,11 @@ int git_odb__add_default_backends(
git_odb *db, const char *objects_dir, git_odb *db, const char *objects_dir,
bool as_alternates, int alternate_depth); bool as_alternates, int alternate_depth);
/*
* Hash a git_rawobj internally.
* The `git_rawobj` is supposed to be previously initialized
*/
int git_odb__hashobj(git_oid *id, git_rawobj *obj, git_oid_t oid_type);
/* /*
* Format the object header such as it would appear in the on-disk object * Format the object header such as it would appear in the on-disk object
*/ */
int git_odb__format_object_header(size_t *out_len, char *hdr, size_t hdr_size, git_object_size_t obj_len, git_object_t obj_type); int git_odb__format_object_header(size_t *out_len, char *hdr, size_t hdr_size, git_object_size_t obj_len, git_object_t obj_type);
/*
* Hash an open file descriptor.
* This is a performance call when the contents of a fd need to be hashed,
* but the fd is already open and we have the size of the contents.
*
* Saves us some `stat` calls.
*
* The fd is never closed, not even on error. It must be opened and closed
* by the caller
*/
int git_odb__hashfd(
git_oid *out,
git_file fd,
size_t size,
git_object_t object_type,
git_oid_t oid_type);
/*
* Hash an open file descriptor applying an array of filters
* Acts just like git_odb__hashfd with the addition of filters...
*/
int git_odb__hashfd_filtered(
git_oid *out,
git_file fd,
size_t len,
git_object_t object_type,
git_oid_t oid_type,
git_filter_list *fl);
/*
* Hash a `path`, assuming it could be a POSIX symlink: if the path is a
* symlink, then the raw contents of the symlink will be hashed. Otherwise,
* this will fallback to `git_odb__hashfd`.
*
* The hash type for this call is always `GIT_OBJECT_BLOB` because
* symlinks may only point to blobs.
*/
int git_odb__hashlink(git_oid *out, const char *path, git_oid_t oid_type);
/** /**
* Generate a GIT_EMISMATCH error for the ODB. * Generate a GIT_EMISMATCH error for the ODB.
*/ */
@@ -160,19 +115,6 @@ void git_odb_object__free(void *object);
/* SHA256 support */ /* SHA256 support */
int git_odb__hash(
git_oid *out,
const void *data,
size_t len,
git_object_t object_type,
git_oid_t oid_type);
int git_odb__hashfile(
git_oid *out,
const char *path,
git_object_t object_type,
git_oid_t oid_type);
int git_odb__backend_loose( int git_odb__backend_loose(
git_odb_backend **out, git_odb_backend **out,
const char *objects_dir, const char *objects_dir,

View File

@@ -125,7 +125,13 @@ static int workdir_reader_read(
goto done; goto done;
if (out_id || reader->index) { if (out_id || reader->index) {
if ((error = git_odb__hash(&id, out->ptr, out->size, GIT_OBJECT_BLOB, reader->repo->oid_type)) < 0) git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
id_opts.object_type = GIT_OBJECT_BLOB;
id_opts.oid_type = reader->repo->oid_type;
if ((error = git_object_id_from_buffer(&id,
out->ptr, out->size, &id_opts)) < 0)
goto done; goto done;
} }

View File

@@ -3558,6 +3558,7 @@ int git_repository_hashfile(
git_object_t type, git_object_t type,
const char *as_path) const char *as_path)
{ {
git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
int error; int error;
git_filter_list *fl = NULL; git_filter_list *fl = NULL;
git_file fd = -1; git_file fd = -1;
@@ -3612,7 +3613,11 @@ int git_repository_hashfile(
goto cleanup; goto cleanup;
} }
error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, repo->oid_type, fl); id_opts.object_type = type;
id_opts.oid_type = repo->oid_type;
id_opts.filters = fl;
error = git_object_id_from_fd(out, fd, (size_t)len, &id_opts);
cleanup: cleanup:
if (fd >= 0) if (fd >= 0)

View File

@@ -35,12 +35,12 @@ static void execute_test(void)
/* Verify that the lenna.jpg file was checked out correctly */ /* Verify that the lenna.jpg file was checked out correctly */
cl_git_pass(git_oid_from_string(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1", GIT_OID_SHA1)); cl_git_pass(git_oid_from_string(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1", GIT_OID_SHA1));
cl_git_pass(git_odb__hashfile(&oid, "binaryunicode/lenna.jpg", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&oid, "binaryunicode/lenna.jpg", NULL));
cl_assert_equal_oid(&oid, &check); cl_assert_equal_oid(&oid, &check);
/* Verify that the text file was checked out correctly */ /* Verify that the text file was checked out correctly */
cl_git_pass(git_oid_from_string(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a", GIT_OID_SHA1)); cl_git_pass(git_oid_from_string(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a", GIT_OID_SHA1));
cl_git_pass(git_odb__hashfile(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", NULL));
cl_assert_equal_oid(&oid, &check); cl_assert_equal_oid(&oid, &check);
} }

View File

@@ -8,11 +8,19 @@
static void hash_object_pass(git_oid *oid, git_rawobj *obj) static void hash_object_pass(git_oid *oid, git_rawobj *obj)
{ {
cl_git_pass(git_odb__hash(oid, obj->data, obj->len, obj->type, GIT_OID_SHA1)); git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
id_opts.object_type = obj->type;
cl_git_pass(git_object_id_from_buffer(oid, obj->data, obj->len, &id_opts));
} }
static void hash_object_fail(git_oid *oid, git_rawobj *obj) static void hash_object_fail(git_oid *oid, git_rawobj *obj)
{ {
cl_git_fail(git_odb__hash(oid, obj->data, obj->len, obj->type, GIT_OID_SHA1)); git_object_id_options id_opts = GIT_OBJECT_ID_OPTIONS_INIT;
id_opts.object_type = obj->type;
cl_git_fail(git_object_id_from_buffer(oid, obj->data, obj->len, &id_opts));
} }
static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511"; static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511";
@@ -87,9 +95,6 @@ void test_object_raw_hash__hash_junk_data(void)
junk_obj.data = some_data; junk_obj.data = some_data;
hash_object_fail(&id, &junk_obj); hash_object_fail(&id, &junk_obj);
junk_obj.type = 0; /* unused */
hash_object_fail(&id, &junk_obj);
junk_obj.type = 5; /* unused */ junk_obj.type = 5; /* unused */
hash_object_fail(&id, &junk_obj); hash_object_fail(&id, &junk_obj);

View File

@@ -21,19 +21,18 @@ void test_repo_hashfile__simple(void)
git_str full = GIT_STR_INIT; git_str full = GIT_STR_INIT;
/* hash with repo relative path */ /* hash with repo relative path */
cl_git_pass(git_odb__hashfile(&a, "status/current_file", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/current_file", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "current_file", GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, "current_file", GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
cl_git_pass(git_str_joinpath(&full, git_repository_workdir(_repo), "current_file")); cl_git_pass(git_str_joinpath(&full, git_repository_workdir(_repo), "current_file"));
/* hash with full path */ /* hash with full path */
cl_git_pass(git_odb__hashfile(&a, full.ptr, GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, full.ptr, NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
/* hash with invalid type */ /* hash with invalid type */
cl_git_fail(git_odb__hashfile(&a, full.ptr, GIT_OBJECT_ANY, GIT_OID_SHA1));
cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, 6, NULL)); cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, 6, NULL));
git_str_dispose(&full); git_str_dispose(&full);
@@ -60,64 +59,63 @@ void test_repo_hashfile__filtered_in_workdir(void)
cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n"); cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n");
/* not equal hashes because of filtering */ /* not equal hashes because of filtering */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, NULL));
cl_assert(git_oid_cmp(&a, &b)); cl_assert(git_oid_cmp(&a, &b));
/* not equal hashes because of filtering when specified by absolute path */ /* not equal hashes because of filtering when specified by absolute path */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, NULL));
cl_assert(git_oid_cmp(&a, &b)); cl_assert(git_oid_cmp(&a, &b));
/* equal hashes because filter is binary */ /* equal hashes because filter is binary */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
/* equal hashes because filter is binary when specified by absolute path */ /* equal hashes because filter is binary when specified by absolute path */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
/* equal hashes when 'as_file' points to binary filtering */ /* equal hashes when 'as_file' points to binary filtering */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, "foo.bin")); cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, "foo.bin"));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
/* equal hashes when 'as_file' points to binary filtering (absolute path) */ /* equal hashes when 'as_file' points to binary filtering (absolute path) */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "foo.bin")); cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "foo.bin"));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
/* not equal hashes when 'as_file' points to text filtering */ /* not equal hashes when 'as_file' points to text filtering */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, "foo.txt")); cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, "foo.txt"));
cl_assert(git_oid_cmp(&a, &b)); cl_assert(git_oid_cmp(&a, &b));
/* not equal hashes when 'as_file' points to text filtering */ /* not equal hashes when 'as_file' points to text filtering */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "foo.txt")); cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "foo.txt"));
cl_assert(git_oid_cmp(&a, &b)); cl_assert(git_oid_cmp(&a, &b));
/* equal hashes when 'as_file' is empty and turns off filtering */ /* equal hashes when 'as_file' is empty and turns off filtering */
cl_git_pass(git_odb__hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, "")); cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, ""));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
cl_git_pass(git_odb__hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, "")); cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, ""));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
cl_git_pass(git_odb__hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "")); cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, ""));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
cl_git_pass(git_odb__hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "status/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "")); cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, ""));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
/* some hash type failures */ /* some hash type failures */
cl_git_fail(git_odb__hashfile(&a, "status/testfile.txt", 0, GIT_OID_SHA1));
cl_git_fail(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_ANY, NULL)); cl_git_fail(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_ANY, NULL));
git_str_dispose(&txt); git_str_dispose(&txt);
@@ -145,12 +143,12 @@ void test_repo_hashfile__filtered_outside_workdir(void)
cl_git_mkfile("absolute/testfile.bin", "other\r\nstuff\r\n"); cl_git_mkfile("absolute/testfile.bin", "other\r\nstuff\r\n");
/* not equal hashes because of filtering */ /* not equal hashes because of filtering */
cl_git_pass(git_odb__hashfile(&a, "absolute/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "absolute/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "testfile.txt")); cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "testfile.txt"));
cl_assert(git_oid_cmp(&a, &b)); cl_assert(git_oid_cmp(&a, &b));
/* equal hashes because filter is binary */ /* equal hashes because filter is binary */
cl_git_pass(git_odb__hashfile(&a, "absolute/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "absolute/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "testfile.bin")); cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "testfile.bin"));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
@@ -158,11 +156,11 @@ void test_repo_hashfile__filtered_outside_workdir(void)
* equal hashes because no filtering occurs for absolute paths outside the working * equal hashes because no filtering occurs for absolute paths outside the working
* directory unless as_path is specified * directory unless as_path is specified
*/ */
cl_git_pass(git_odb__hashfile(&a, "absolute/testfile.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "absolute/testfile.txt", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);
cl_git_pass(git_odb__hashfile(&a, "absolute/testfile.bin", GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&a, "absolute/testfile.bin", NULL));
cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, NULL)); cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&a, &b); cl_assert_equal_oid(&a, &b);

View File

@@ -22,7 +22,7 @@ void test_status_single__hash_single_file(void)
cl_git_mkfile(file_name, file_contents); cl_git_mkfile(file_name, file_contents);
cl_set_cleanup(&cleanup__remove_file, (void *)file_name); cl_set_cleanup(&cleanup__remove_file, (void *)file_name);
cl_git_pass(git_odb__hashfile(&actual_id, file_name, GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&actual_id, file_name, NULL));
cl_assert_equal_oid(&expected_id, &actual_id); cl_assert_equal_oid(&expected_id, &actual_id);
} }
@@ -40,7 +40,7 @@ void test_status_single__hash_single_empty_file(void)
cl_git_mkfile(file_name, file_contents); cl_git_mkfile(file_name, file_contents);
cl_set_cleanup(&cleanup__remove_file, (void *)file_name); cl_set_cleanup(&cleanup__remove_file, (void *)file_name);
cl_git_pass(git_odb__hashfile(&actual_id, file_name, GIT_OBJECT_BLOB, GIT_OID_SHA1)); cl_git_pass(git_object_id_from_file(&actual_id, file_name, NULL));
cl_assert_equal_oid(&expected_id, &actual_id); cl_assert_equal_oid(&expected_id, &actual_id);
} }