read_packed_refs(): only check for a header at the top of the file

This tightens up the parsing a bit; previously, stray header-looking lines would have been processed. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 13, 2017 at 19:16 UTC 36f23534aef58f747048ffea5addb0367406896d
1 file changed +24 -11
refs/packed-backend.c
+24 -11
@@ -255,11 +255,34 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
255 pos = buf;
256 eof = buf + size;
257
258 + /* If the file has a header line, process it: */
259 + if (pos < eof && *pos == '#') {
260 + const char *traits;
261 +
262 + eol = memchr(pos, '\n', eof - pos);
263 + if (!eol)
264 + die_unterminated_line(refs->path, pos, eof - pos);
265 +
266 + strbuf_add(&line, pos, eol + 1 - pos);
267 +
268 + if (!skip_prefix(line.buf, "# pack-refs with:", &traits))
269 + die_invalid_line(refs->path, pos, eof - pos);
270 +
271 + if (strstr(traits, " fully-peeled "))
272 + peeled = PEELED_FULLY;
273 + else if (strstr(traits, " peeled "))
274 + peeled = PEELED_TAGS;
275 + /* perhaps other traits later as well */
276 +
277 + /* The "+ 1" is for the LF character. */
278 + pos = eol + 1;
279 + strbuf_reset(&line);
280 + }
281 +
282 dir = get_ref_dir(packed_refs->cache->root);
283 while (pos < eof) {
284 struct object_id oid;
285 const char *refname;
262 - const char *traits;
286
287 eol = memchr(pos, '\n', eof - pos);
288 if (!eol)
@@ -267,15 +290,6 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
290
291 strbuf_add(&line, pos, eol + 1 - pos);
292
270 - if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
271 - if (strstr(traits, " fully-peeled "))
272 - peeled = PEELED_FULLY;
273 - else if (strstr(traits, " peeled "))
274 - peeled = PEELED_TAGS;
275 - /* perhaps other traits later as well */
276 - goto next_line;
277 - }
278 -
293 refname = parse_ref_line(&line, &oid);
294 if (refname) {
295 int flag = REF_ISPACKED;
@@ -307,7 +321,6 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
321 die_invalid_line(refs->path, line.buf, line.len);
322 }
323
310 - next_line:
324 /* The "+ 1" is for the LF character. */
325 pos = eol + 1;
326 strbuf_reset(&line);