Skip to content

Commit 4b35b00

Browse files
committed
Merge branch 'lf/read-blob-data-from-index'
Reduce duplicated code between convert.c and attr.c. * lf/read-blob-data-from-index: convert.c: remove duplicate code read_blob_data_from_index(): optionally return the size of blob data attr.c: extract read_index_data() as read_blob_data_from_index()
2 parents d2949c7 + 4982fd7 commit 4b35b00

File tree

4 files changed

+39
-59
lines changed

4 files changed

+39
-59
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, NULL);
424391
if (!buf)
425392
return NULL;
426393

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ extern void free_name_hash(struct index_state *istate);
311311
#define resolve_undo_clear() resolve_undo_clear_index(&the_index)
312312
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
313313
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
314+
#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
314315
#endif
315316

316317
enum object_type {
@@ -471,6 +472,7 @@ extern int add_file_to_index(struct index_state *, const char *path, int flags);
471472
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
472473
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
473474
extern int index_name_is_other(const struct index_state *, const char *, int);
475+
extern void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
474476

475477
/* do stat comparison even if CE_VALID is true */
476478
#define CE_MATCH_IGNORE_VALID 01

convert.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,36 +153,13 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
153153

154154
static int has_cr_in_index(const char *path)
155155
{
156-
int pos, len;
157156
unsigned long sz;
158-
enum object_type type;
159157
void *data;
160158
int has_cr;
161-
struct index_state *istate = &the_index;
162159

163-
len = strlen(path);
164-
pos = index_name_pos(istate, path, len);
165-
if (pos < 0) {
166-
/*
167-
* We might be in the middle of a merge, in which
168-
* case we would read stage #2 (ours).
169-
*/
170-
int i;
171-
for (i = -pos - 1;
172-
(pos < 0 && i < istate->cache_nr &&
173-
!strcmp(istate->cache[i]->name, path));
174-
i++)
175-
if (ce_stage(istate->cache[i]) == 2)
176-
pos = i;
177-
}
178-
if (pos < 0)
160+
data = read_blob_data_from_cache(path, &sz);
161+
if (!data)
179162
return 0;
180-
data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
181-
if (!data || type != OBJ_BLOB) {
182-
free(data);
183-
return 0;
184-
}
185-
186163
has_cr = memchr(data, '\r', sz) != NULL;
187164
free(data);
188165
return has_cr;

read-cache.c

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

0 commit comments

Comments
 (0)