apply: refactor read_file_or_gitlink()

Reading a blob out of the object store does not have to require that the caller has a cache entry for it. Create a read_blob_object() helper function that takes the object name and mode, and use it to reimplement the original function as a thin wrapper to it. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 8, 2012 at 15:11 UTC e42a96e7726365557b5865957927dc1ca272cad2
1 file changed +11 -7
builtin/apply.c
+11 -7
@@ -2930,20 +2930,17 @@ static int apply_fragments(struct image *img, struct patch *patch)
2930 return 0;
2931 }
2932
2933 -static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
2933 +static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
2934 {
2935 - if (!ce)
2936 - return 0;
2937 -
2938 - if (S_ISGITLINK(ce->ce_mode)) {
2935 + if (S_ISGITLINK(mode)) {
2936 strbuf_grow(buf, 100);
2940 - strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
2937 + strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
2938 } else {
2939 enum object_type type;
2940 unsigned long sz;
2941 char *result;
2942
2946 - result = read_sha1_file(ce->sha1, &type, &sz);
2943 + result = read_sha1_file(sha1, &type, &sz);
2944 if (!result)
2945 return -1;
2946 /* XXX read_sha1_file NUL-terminates */
@@ -2952,6 +2949,13 @@ static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
2949 return 0;
2950 }
2951
2952 +static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
2953 +{
2954 + if (!ce)
2955 + return 0;
2956 + return read_blob_object(buf, ce->sha1, ce->ce_mode);
2957 +}
2958 +
2959 static struct patch *in_fn_table(const char *name)
2960 {
2961 struct string_list_item *item;