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()
This commit is contained in:
Junio C Hamano 2013-04-21 18:39:45 -07:00
Родитель d2949c7b3c 4982fd78f6
Коммит 4b35b007a6
4 изменённых файлов: 39 добавлений и 59 удалений

35
attr.c
Просмотреть файл

@ -381,46 +381,13 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
return res; return res;
} }
static void *read_index_data(const char *path)
{
int pos, len;
unsigned long sz;
enum object_type type;
void *data;
struct index_state *istate = use_index ? use_index : &the_index;
len = strlen(path);
pos = index_name_pos(istate, path, len);
if (pos < 0) {
/*
* We might be in the middle of a merge, in which
* case we would read stage #2 (ours).
*/
int i;
for (i = -pos - 1;
(pos < 0 && i < istate->cache_nr &&
!strcmp(istate->cache[i]->name, path));
i++)
if (ce_stage(istate->cache[i]) == 2)
pos = i;
}
if (pos < 0)
return NULL;
data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
if (!data || type != OBJ_BLOB) {
free(data);
return NULL;
}
return data;
}
static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
{ {
struct attr_stack *res; struct attr_stack *res;
char *buf, *sp; char *buf, *sp;
int lineno = 0; int lineno = 0;
buf = read_index_data(path); buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL);
if (!buf) if (!buf)
return NULL; return NULL;

Просмотреть файл

@ -311,6 +311,7 @@ extern void free_name_hash(struct index_state *istate);
#define resolve_undo_clear() resolve_undo_clear_index(&the_index) #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at) #define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec) #define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
#endif #endif
enum object_type { enum object_type {
@ -471,6 +472,7 @@ extern int add_file_to_index(struct index_state *, const char *path, int flags);
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh); extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b); extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
extern int index_name_is_other(const struct index_state *, const char *, int); extern int index_name_is_other(const struct index_state *, const char *, int);
extern void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
/* do stat comparison even if CE_VALID is true */ /* do stat comparison even if CE_VALID is true */
#define CE_MATCH_IGNORE_VALID 01 #define CE_MATCH_IGNORE_VALID 01

Просмотреть файл

@ -153,36 +153,13 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
static int has_cr_in_index(const char *path) static int has_cr_in_index(const char *path)
{ {
int pos, len;
unsigned long sz; unsigned long sz;
enum object_type type;
void *data; void *data;
int has_cr; int has_cr;
struct index_state *istate = &the_index;
len = strlen(path); data = read_blob_data_from_cache(path, &sz);
pos = index_name_pos(istate, path, len); if (!data)
if (pos < 0) {
/*
* We might be in the middle of a merge, in which
* case we would read stage #2 (ours).
*/
int i;
for (i = -pos - 1;
(pos < 0 && i < istate->cache_nr &&
!strcmp(istate->cache[i]->name, path));
i++)
if (ce_stage(istate->cache[i]) == 2)
pos = i;
}
if (pos < 0)
return 0; return 0;
data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
if (!data || type != OBJ_BLOB) {
free(data);
return 0;
}
has_cr = memchr(data, '\r', sz) != NULL; has_cr = memchr(data, '\r', sz) != NULL;
free(data); free(data);
return has_cr; return has_cr;

Просмотреть файл

@ -1899,3 +1899,37 @@ int index_name_is_other(const struct index_state *istate, const char *name,
} }
return 1; return 1;
} }
void *read_blob_data_from_index(struct index_state *istate, const char *path, unsigned long *size)
{
int pos, len;
unsigned long sz;
enum object_type type;
void *data;
len = strlen(path);
pos = index_name_pos(istate, path, len);
if (pos < 0) {
/*
* We might be in the middle of a merge, in which
* case we would read stage #2 (ours).
*/
int i;
for (i = -pos - 1;
(pos < 0 && i < istate->cache_nr &&
!strcmp(istate->cache[i]->name, path));
i++)
if (ce_stage(istate->cache[i]) == 2)
pos = i;
}
if (pos < 0)
return NULL;
data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
if (!data || type != OBJ_BLOB) {
free(data);
return NULL;
}
if (size)
*size = sz;
return data;
}