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 <git@cryptocrack.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lukas Fleischer committed Apr 13, 2013 at 15:28 UTC 29fb37b272debaf4f5f6eb7cf476de9274492930
3 files changed +35 -34
attr.c
+1 -34
@@ -381,46 +381,13 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
381 return res;
382 }
383
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 -
384 static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
385 {
386 struct attr_stack *res;
387 char *buf, *sp;
388 int lineno = 0;
389
423 - buf = read_index_data(path);
390 + buf = read_blob_data_from_index(use_index ? use_index : &the_index, path);
391 if (!buf)
392 return NULL;
393
cache.h
+2
@@ -307,6 +307,7 @@ extern void free_name_hash(struct index_state *istate);
307 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
308 #define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
309 #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))
311 #endif
312
313 enum object_type {
@@ -452,6 +453,7 @@ extern int add_file_to_index(struct index_state *, const char *path, int flags);
453 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
454 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
455 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 *);
457
458 /* do stat comparison even if CE_VALID is true */
459 #define CE_MATCH_IGNORE_VALID 01
read-cache.c
+32
@@ -1895,3 +1895,35 @@ int index_name_is_other(const struct index_state *istate, const char *name,
1895 }
1896 return 1;
1897 }
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 +}