sha1_file.c: "legacy" is really the current format

Every time I look at the read-loose-object codepath, legacy_loose_object() function makes my brain go through mental contortion. When we were playing with the experimental loose object format, it may have made sense to call the traditional format "legacy", in the hope that the experimental one will some day replace it to become official, but it never happened. This renames the function (and negates its return value) to detect if we are looking at the experimental format, and move the code around in its caller which used to do "if we are looing at legacy, do this special case, otherwise the normal case is this". The codepath to read from the loose objects in experimental format is the "unlikely" case. Someday after Git 2.0, we should drop the support of this format. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 8, 2011 at 11:29 UTC cc5c54e78b18c2edc42228c06dab22002fa304fa
1 file changed +33 -29
sha1_file.c
+33 -29
@@ -1077,20 +1077,29 @@ static void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
1077 return map;
1078 }
1079
1080 -static int legacy_loose_object(unsigned char *map)
1080 +/*
1081 + * There used to be a second loose object header format which
1082 + * was meant to mimic the in-pack format, allowing for direct
1083 + * copy of the object data. This format turned up not to be
1084 + * really worth it and we no longer write loose objects in that
1085 + * format.
1086 + */
1087 +static int experimental_loose_object(unsigned char *map)
1088 {
1089 unsigned int word;
1090
1091 /*
1092 * Is it a zlib-compressed buffer? If so, the first byte
1093 * must be 0x78 (15-bit window size, deflated), and the
1087 - * first 16-bit word is evenly divisible by 31
1094 + * first 16-bit word is evenly divisible by 31. If so,
1095 + * we are looking at the official format, not the experimental
1096 + * one.
1097 */
1098 word = (map[0] << 8) + map[1];
1099 if (map[0] == 0x78 && !(word % 31))
1091 - return 1;
1092 - else
1100 return 0;
1101 + else
1102 + return 1;
1103 }
1104
1105 unsigned long unpack_object_header_buffer(const unsigned char *buf,
@@ -1134,34 +1143,29 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
1143 stream->next_out = buffer;
1144 stream->avail_out = bufsiz;
1145
1137 - if (legacy_loose_object(map)) {
1138 - git_inflate_init(stream);
1139 - return git_inflate(stream, 0);
1140 - }
1141 -
1146 + if (experimental_loose_object(map)) {
1147 + /*
1148 + * The old experimental format we no longer produce;
1149 + * we can still read it.
1150 + */
1151 + used = unpack_object_header_buffer(map, mapsize, &type, &size);
1152 + if (!used || !valid_loose_object_type[type])
1153 + return -1;
1154 + map += used;
1155 + mapsize -= used;
1156
1143 - /*
1144 - * There used to be a second loose object header format which
1145 - * was meant to mimic the in-pack format, allowing for direct
1146 - * copy of the object data. This format turned up not to be
1147 - * really worth it and we don't write it any longer. But we
1148 - * can still read it.
1149 - */
1150 - used = unpack_object_header_buffer(map, mapsize, &type, &size);
1151 - if (!used || !valid_loose_object_type[type])
1152 - return -1;
1153 - map += used;
1154 - mapsize -= used;
1157 + /* Set up the stream for the rest.. */
1158 + stream->next_in = map;
1159 + stream->avail_in = mapsize;
1160 + git_inflate_init(stream);
1161
1156 - /* Set up the stream for the rest.. */
1157 - stream->next_in = map;
1158 - stream->avail_in = mapsize;
1162 + /* And generate the fake traditional header */
1163 + stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
1164 + typename(type), size);
1165 + return 0;
1166 + }
1167 git_inflate_init(stream);
1160 -
1161 - /* And generate the fake traditional header */
1162 - stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
1163 - typename(type), size);
1164 - return 0;
1168 + return git_inflate(stream, 0);
1169 }
1170
1171 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)