read_sha1_file(): allow selective bypassing of replacement mechanism

The way "object replacement" mechanism was tucked to the read_sha1_file() interface was suboptimal in a couple of ways: - Callers that want it to die with useful diagnosis upon seeing a corrupt object does not have a way to say that they do not want any object replacement. - Callers who do not want it to die but want to handle the errors themselves are told to arrange to call read_object(), but the function does not use the replacement mechanism, and also it is a file scope static function that not many callers can call to begin with. This adds a read_sha1_file_extended() that takes a set of flags; the callers of read_sha1_file() passes a flag READ_SHA1_FILE_REPLACE to ask for object replacement mechanism to kick in. Later, we could add another flag bit to tell the function to return an error instead of dying and then remove the misguided "call read_object() yourself". Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 15, 2011 at 12:54 UTC 5bf29b950063c8fa2f3666cb6cf2ca20be61f3d1
2 files changed +12 -5
cache.h
+6 -1
@@ -757,7 +757,12 @@ int daemon_avoid_alias(const char *path);
757 int offset_1st_component(const char *path);
758
759 /* object replacement */
760 -extern void *read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
760 +#define READ_SHA1_FILE_REPLACE 1
761 +extern void *read_sha1_file_extended(const unsigned char *sha1, enum object_type *type, unsigned long *size, unsigned flag);
762 +static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
763 +{
764 + return read_sha1_file_extended(sha1, type, size, READ_SHA1_FILE_REPLACE);
765 +}
766 extern const unsigned char *do_lookup_replace_object(const unsigned char *sha1);
767 static inline const unsigned char *lookup_replace_object(const unsigned char *sha1)
768 {
sha1_file.c
+6 -4
@@ -2206,14 +2206,16 @@ static void *read_object(const unsigned char *sha1, enum object_type *type,
2206 * deal with them should arrange to call read_object() and give error
2207 * messages themselves.
2208 */
2209 -void *read_sha1_file(const unsigned char *sha1,
2210 - enum object_type *type,
2211 - unsigned long *size)
2209 +void *read_sha1_file_extended(const unsigned char *sha1,
2210 + enum object_type *type,
2211 + unsigned long *size,
2212 + unsigned flag)
2213 {
2213 - const unsigned char *repl = lookup_replace_object(sha1);
2214 void *data;
2215 char *path;
2216 const struct packed_git *p;
2217 + const unsigned char *repl = (flag & READ_SHA1_FILE_REPLACE)
2218 + ? lookup_replace_object(sha1) : sha1;
2219
2220 errno = 0;
2221 data = read_object(repl, type, size);