aboutsummaryrefslogtreecommitdiffstats
path: root/read-cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'read-cache.c')
-rw-r--r--read-cache.c125
1 files changed, 94 insertions, 31 deletions
diff --git a/read-cache.c b/read-cache.c
index a6db25a16d..1f67bb755b 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -3,6 +3,9 @@
*
* Copyright (C) Linus Torvalds, 2005
*/
+
+#define USE_THE_REPOSITORY_VARIABLE
+
#include "git-compat-util.h"
#include "bulk-checkin.h"
#include "config.h"
@@ -271,7 +274,8 @@ static int ce_compare_gitlink(const struct cache_entry *ce)
*
* If so, we consider it always to match.
*/
- if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0)
+ if (repo_resolve_gitlink_ref(the_repository, ce->name,
+ "HEAD", &oid) < 0)
return 0;
return !oideq(&oid, &ce->oid);
}
@@ -336,7 +340,7 @@ static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
/* Racily smudged entry? */
if (!ce->ce_stat_data.sd_size) {
- if (!is_empty_blob_sha1(ce->oid.hash))
+ if (!is_empty_blob_oid(&ce->oid, the_repository->hash_algo))
changed |= DATA_CHANGED;
}
@@ -711,7 +715,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
namelen = strlen(path);
if (S_ISDIR(st_mode)) {
- if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
+ if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
return error(_("'%s' does not have a commit checked out"), path);
while (namelen && path[namelen-1] == '/')
namelen--;
@@ -1727,14 +1731,14 @@ static int verify_hdr(const struct cache_header *hdr, unsigned long size)
end = (unsigned char *)hdr + size;
start = end - the_hash_algo->rawsz;
- oidread(&oid, start);
+ oidread(&oid, start, the_repository->hash_algo);
if (oideq(&oid, null_oid()))
return 0;
the_hash_algo->init_fn(&c);
the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
the_hash_algo->final_fn(hash, &c);
- if (!hasheq(hash, start))
+ if (!hasheq(hash, start, the_repository->hash_algo))
return error(_("bad index file sha1 signature"));
return 0;
}
@@ -1875,7 +1879,8 @@ static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
ce->ce_flags = flags & ~CE_NAMEMASK;
ce->ce_namelen = len;
ce->index = 0;
- oidread(&ce->oid, (const unsigned char *)ondisk + offsetof(struct ondisk_cache_entry, data));
+ oidread(&ce->oid, (const unsigned char *)ondisk + offsetof(struct ondisk_cache_entry, data),
+ the_repository->hash_algo);
if (expand_name_field) {
if (copy_len)
@@ -2248,7 +2253,8 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
if (verify_hdr(hdr, mmap_size) < 0)
goto unmap;
- oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz);
+ oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz,
+ the_repository->hash_algo);
istate->version = ntohl(hdr->hdr_version);
istate->cache_nr = ntohl(hdr->hdr_entries);
istate->cache_alloc = alloc_nr(istate->cache_nr);
@@ -2640,7 +2646,7 @@ static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
ondisk->uid = htonl(ce->ce_stat_data.sd_uid);
ondisk->gid = htonl(ce->ce_stat_data.sd_gid);
ondisk->size = htonl(ce->ce_stat_data.sd_size);
- hashcpy(ondisk->data, ce->oid.hash);
+ hashcpy(ondisk->data, ce->oid.hash, the_repository->hash_algo);
flags = ce->ce_flags & ~CE_NAMEMASK;
flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
@@ -2729,7 +2735,7 @@ static int verify_index_from(const struct index_state *istate, const char *path)
if (n != the_hash_algo->rawsz)
goto out;
- if (!hasheq(istate->oid.hash, hash))
+ if (!hasheq(istate->oid.hash, hash, the_repository->hash_algo))
goto out;
close(fd);
@@ -2957,7 +2963,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
if (err) {
free(ieot);
- return err;
+ goto cleanup;
}
offset = hashfile_total(f);
@@ -2986,8 +2992,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
free(ieot);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
if (write_extensions & WRITE_SPLIT_INDEX_EXTENSION &&
@@ -3002,8 +3014,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
sb.len) < 0;
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_link_extension() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
if (write_extensions & WRITE_CACHE_TREE_EXTENSION &&
!drop_cache_tree && istate->cache_tree) {
@@ -3013,8 +3031,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
err = write_index_ext_header(f, eoie_c, CACHE_EXT_TREE, sb.len) < 0;
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
if (write_extensions & WRITE_RESOLVE_UNDO_EXTENSION &&
istate->resolve_undo) {
@@ -3025,8 +3049,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
sb.len) < 0;
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
if (write_extensions & WRITE_UNTRACKED_CACHE_EXTENSION &&
istate->untracked) {
@@ -3037,8 +3067,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
sb.len) < 0;
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
if (write_extensions & WRITE_FSMONITOR_EXTENSION &&
istate->fsmonitor_last_update) {
@@ -3048,12 +3084,25 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
err = write_index_ext_header(f, eoie_c, CACHE_EXT_FSMONITOR, sb.len) < 0;
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
if (istate->sparse_index) {
- if (write_index_ext_header(f, eoie_c, CACHE_EXT_SPARSE_DIRECTORIES, 0) < 0)
- return -1;
+ err = write_index_ext_header(f, eoie_c, CACHE_EXT_SPARSE_DIRECTORIES, 0);
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
/*
@@ -3069,8 +3118,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
err = write_index_ext_header(f, NULL, CACHE_EXT_ENDOFINDEXENTRIES, sb.len) < 0;
hashwrite(f, sb.buf, sb.len);
strbuf_release(&sb);
- if (err)
- return -1;
+ /*
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
+ * and this part may want to be simplified.
+ */
+ if (err) {
+ err = -1;
+ goto cleanup;
+ }
}
csum_fsync_flag = 0;
@@ -3079,13 +3134,16 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
finalize_hashfile(f, istate->oid.hash, FSYNC_COMPONENT_INDEX,
CSUM_HASH_IN_STREAM | csum_fsync_flag);
+ f = NULL;
if (close_tempfile_gently(tempfile)) {
- error(_("could not close '%s'"), get_tempfile_path(tempfile));
- return -1;
+ err = error(_("could not close '%s'"), get_tempfile_path(tempfile));
+ goto cleanup;
+ }
+ if (stat(get_tempfile_path(tempfile), &st)) {
+ err = error_errno(_("could not stat '%s'"), get_tempfile_path(tempfile));
+ goto cleanup;
}
- if (stat(get_tempfile_path(tempfile), &st))
- return -1;
istate->timestamp.sec = (unsigned int)st.st_mtime;
istate->timestamp.nsec = ST_MTIME_NSEC(st);
trace_performance_since(start, "write index, changed mask = %x", istate->cache_changed);
@@ -3100,6 +3158,11 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
istate->cache_nr);
return 0;
+
+cleanup:
+ if (f)
+ discard_hashfile(f);
+ return err;
}
void set_alternate_index_output(const char *name)
@@ -3602,7 +3665,7 @@ static size_t read_eoie_extension(const char *mmap, size_t mmap_size)
src_offset += extsize;
}
the_hash_algo->final_fn(hash, &c);
- if (!hasheq(hash, (const unsigned char *)index))
+ if (!hasheq(hash, (const unsigned char *)index, the_repository->hash_algo))
return 0;
/* Validate that the extension offsets returned us back to the eoie extension. */