Skip to content

Commit cc5c54e

Browse files
committed
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 <[email protected]>
1 parent 6a7f71d commit cc5c54e

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

sha1_file.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,20 +1077,29 @@ static void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
10771077
return map;
10781078
}
10791079

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)
10811088
{
10821089
unsigned int word;
10831090

10841091
/*
10851092
* Is it a zlib-compressed buffer? If so, the first byte
10861093
* 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.
10881097
*/
10891098
word = (map[0] << 8) + map[1];
10901099
if (map[0] == 0x78 && !(word % 31))
1091-
return 1;
1092-
else
10931100
return 0;
1101+
else
1102+
return 1;
10941103
}
10951104

10961105
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
11341143
stream->next_out = buffer;
11351144
stream->avail_out = bufsiz;
11361145

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;
11421156

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);
11551161

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+
}
11591167
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);
11651169
}
11661170

11671171
static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)

0 commit comments

Comments
 (0)