Skip to content

Commit 8980690

Browse files
tboegigitster
authored andcommitted
read-cache: factor out get_sha1_from_index() helper
Factor out the retrieval of the sha1 for a given path in read_blob_data_from_index() into the function get_sha1_from_index(). This will be used in the next commit, when convert.c can do the analyze for "text=auto" without slurping the whole blob into memory at once. Add a wrapper definition get_sha1_from_cache(). Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a23ca1b commit 8980690

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ extern void free_name_hash(struct index_state *istate);
380380
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
381381
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
382382
#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
383+
#define get_sha1_from_cache(path) get_sha1_from_index (&the_index, (path))
383384
#endif
384385

385386
enum object_type {
@@ -1089,6 +1090,8 @@ static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *
10891090
return read_sha1_file_extended(sha1, type, size, LOOKUP_REPLACE_OBJECT);
10901091
}
10911092

1093+
const unsigned char *get_sha1_from_index(struct index_state *istate, const char *path);
1094+
10921095
/*
10931096
* This internal function is only declared here for the benefit of
10941097
* lookup_replace_object(). Please do not call it directly.

read-cache.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,13 +2290,27 @@ int index_name_is_other(const struct index_state *istate, const char *name,
22902290

22912291
void *read_blob_data_from_index(struct index_state *istate, const char *path, unsigned long *size)
22922292
{
2293-
int pos, len;
2293+
const unsigned char *sha1;
22942294
unsigned long sz;
22952295
enum object_type type;
22962296
void *data;
22972297

2298-
len = strlen(path);
2299-
pos = index_name_pos(istate, path, len);
2298+
sha1 = get_sha1_from_index(istate, path);
2299+
if (!sha1)
2300+
return NULL;
2301+
data = read_sha1_file(sha1, &type, &sz);
2302+
if (!data || type != OBJ_BLOB) {
2303+
free(data);
2304+
return NULL;
2305+
}
2306+
if (size)
2307+
*size = sz;
2308+
return data;
2309+
}
2310+
2311+
const unsigned char *get_sha1_from_index(struct index_state *istate, const char *path)
2312+
{
2313+
int pos = index_name_pos(istate, path, strlen(path));
23002314
if (pos < 0) {
23012315
/*
23022316
* We might be in the middle of a merge, in which
@@ -2312,14 +2326,7 @@ void *read_blob_data_from_index(struct index_state *istate, const char *path, un
23122326
}
23132327
if (pos < 0)
23142328
return NULL;
2315-
data = read_sha1_file(istate->cache[pos]->oid.hash, &type, &sz);
2316-
if (!data || type != OBJ_BLOB) {
2317-
free(data);
2318-
return NULL;
2319-
}
2320-
if (size)
2321-
*size = sz;
2322-
return data;
2329+
return istate->cache[pos]->oid.hash;
23232330
}
23242331

23252332
void stat_validity_clear(struct stat_validity *sv)

0 commit comments

Comments
 (0)