Skip to content

Commit 29fb37b

Browse files
Lukas Fleischergitster
authored andcommitted
attr.c: extract read_index_data() as read_blob_data_from_index()
Extract the read_index_data() function from attr.c and move it to read-cache.c; rename it to read_blob_data_from_index() and update the function signature of it to align better with index/cache API functions. This allows for reusing the function in convert.c later. Signed-off-by: Lukas Fleischer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 04a74b6 commit 29fb37b

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

attr.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -381,46 +381,13 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
381381
return res;
382382
}
383383

384-
static void *read_index_data(const char *path)
385-
{
386-
int pos, len;
387-
unsigned long sz;
388-
enum object_type type;
389-
void *data;
390-
struct index_state *istate = use_index ? use_index : &the_index;
391-
392-
len = strlen(path);
393-
pos = index_name_pos(istate, path, len);
394-
if (pos < 0) {
395-
/*
396-
* We might be in the middle of a merge, in which
397-
* case we would read stage #2 (ours).
398-
*/
399-
int i;
400-
for (i = -pos - 1;
401-
(pos < 0 && i < istate->cache_nr &&
402-
!strcmp(istate->cache[i]->name, path));
403-
i++)
404-
if (ce_stage(istate->cache[i]) == 2)
405-
pos = i;
406-
}
407-
if (pos < 0)
408-
return NULL;
409-
data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
410-
if (!data || type != OBJ_BLOB) {
411-
free(data);
412-
return NULL;
413-
}
414-
return data;
415-
}
416-
417384
static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
418385
{
419386
struct attr_stack *res;
420387
char *buf, *sp;
421388
int lineno = 0;
422389

423-
buf = read_index_data(path);
390+
buf = read_blob_data_from_index(use_index ? use_index : &the_index, path);
424391
if (!buf)
425392
return NULL;
426393

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ extern void free_name_hash(struct index_state *istate);
307307
#define resolve_undo_clear() resolve_undo_clear_index(&the_index)
308308
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
309309
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
310+
#define read_blob_data_from_cache(path) read_blob_data_from_index(&the_index, (path))
310311
#endif
311312

312313
enum object_type {
@@ -452,6 +453,7 @@ extern int add_file_to_index(struct index_state *, const char *path, int flags);
452453
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
453454
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
454455
extern int index_name_is_other(const struct index_state *, const char *, int);
456+
extern void *read_blob_data_from_index(struct index_state *, const char *);
455457

456458
/* do stat comparison even if CE_VALID is true */
457459
#define CE_MATCH_IGNORE_VALID 01

read-cache.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,3 +1895,35 @@ int index_name_is_other(const struct index_state *istate, const char *name,
18951895
}
18961896
return 1;
18971897
}
1898+
1899+
void *read_blob_data_from_index(struct index_state *istate, const char *path)
1900+
{
1901+
int pos, len;
1902+
unsigned long sz;
1903+
enum object_type type;
1904+
void *data;
1905+
1906+
len = strlen(path);
1907+
pos = index_name_pos(istate, path, len);
1908+
if (pos < 0) {
1909+
/*
1910+
* We might be in the middle of a merge, in which
1911+
* case we would read stage #2 (ours).
1912+
*/
1913+
int i;
1914+
for (i = -pos - 1;
1915+
(pos < 0 && i < istate->cache_nr &&
1916+
!strcmp(istate->cache[i]->name, path));
1917+
i++)
1918+
if (ce_stage(istate->cache[i]) == 2)
1919+
pos = i;
1920+
}
1921+
if (pos < 0)
1922+
return NULL;
1923+
data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
1924+
if (!data || type != OBJ_BLOB) {
1925+
free(data);
1926+
return NULL;
1927+
}
1928+
return data;
1929+
}

0 commit comments

Comments
 (0)