@@ -68,17 +68,21 @@ struct snapshot {
6868 int mmapped ;
6969
7070 /*
71- * The contents of the `packed-refs` file. If the file was
72- * already sorted, this points at the mmapped contents of the
73- * file. If not, this points at heap-allocated memory
74- * containing the contents, sorted. If there were no contents
75- * (e.g., because the file didn't exist), `buf` and `eof` are
76- * both NULL.
71+ * The contents of the `packed-refs` file:
72+ *
73+ * - buf -- a pointer to the start of the memory
74+ * - start -- a pointer to the first byte of actual references
75+ * (i.e., after the header line, if one is present)
76+ * - eof -- a pointer just past the end of the reference
77+ * contents
78+ *
79+ * If the `packed-refs` file was already sorted, `buf` points
80+ * at the mmapped contents of the file. If not, it points at
81+ * heap-allocated memory containing the contents, sorted. If
82+ * there were no contents (e.g., because the file didn't
83+ * exist), `buf`, `start`, and `eof` are all NULL.
7784 */
78- char * buf , * eof ;
79-
80- /* The size of the header line, if any; otherwise, 0: */
81- size_t header_len ;
85+ char * buf , * start , * eof ;
8286
8387 /*
8488 * What is the peeled state of the `packed-refs` file that
@@ -169,8 +173,7 @@ static void clear_snapshot_buffer(struct snapshot *snapshot)
169173 } else {
170174 free (snapshot -> buf );
171175 }
172- snapshot -> buf = snapshot -> eof = NULL ;
173- snapshot -> header_len = 0 ;
176+ snapshot -> buf = snapshot -> start = snapshot -> eof = NULL ;
174177}
175178
176179/*
@@ -319,13 +322,14 @@ static void sort_snapshot(struct snapshot *snapshot)
319322 size_t len , i ;
320323 char * new_buffer , * dst ;
321324
322- pos = snapshot -> buf + snapshot -> header_len ;
325+ pos = snapshot -> start ;
323326 eof = snapshot -> eof ;
324- len = eof - pos ;
325327
326- if (! len )
328+ if (pos == eof )
327329 return ;
328330
331+ len = eof - pos ;
332+
329333 /*
330334 * Initialize records based on a crude estimate of the number
331335 * of references in the file (we'll grow it below if needed):
@@ -391,9 +395,8 @@ static void sort_snapshot(struct snapshot *snapshot)
391395 * place:
392396 */
393397 clear_snapshot_buffer (snapshot );
394- snapshot -> buf = new_buffer ;
398+ snapshot -> buf = snapshot -> start = new_buffer ;
395399 snapshot -> eof = new_buffer + len ;
396- snapshot -> header_len = 0 ;
397400
398401cleanup :
399402 free (records );
@@ -442,14 +445,14 @@ static const char *find_end_of_record(const char *p, const char *end)
442445 */
443446static void verify_buffer_safe (struct snapshot * snapshot )
444447{
445- const char * buf = snapshot -> buf + snapshot -> header_len ;
448+ const char * start = snapshot -> start ;
446449 const char * eof = snapshot -> eof ;
447450 const char * last_line ;
448451
449- if (buf == eof )
452+ if (start == eof )
450453 return ;
451454
452- last_line = find_start_of_record (buf , eof - 1 );
455+ last_line = find_start_of_record (start , eof - 1 );
453456 if (* (eof - 1 ) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2 )
454457 die_invalid_line (snapshot -> refs -> path ,
455458 last_line , eof - last_line );
@@ -495,18 +498,19 @@ static int load_contents(struct snapshot *snapshot)
495498 bytes_read = read_in_full (fd , snapshot -> buf , size );
496499 if (bytes_read < 0 || bytes_read != size )
497500 die_errno ("couldn't read %s" , snapshot -> refs -> path );
498- snapshot -> eof = snapshot -> buf + size ;
499501 snapshot -> mmapped = 0 ;
500502 break ;
501503 case MMAP_TEMPORARY :
502504 case MMAP_OK :
503505 snapshot -> buf = xmmap (NULL , size , PROT_READ , MAP_PRIVATE , fd , 0 );
504- snapshot -> eof = snapshot -> buf + size ;
505506 snapshot -> mmapped = 1 ;
506507 break ;
507508 }
508509 close (fd );
509510
511+ snapshot -> start = snapshot -> buf ;
512+ snapshot -> eof = snapshot -> buf + size ;
513+
510514 return 1 ;
511515}
512516
@@ -539,7 +543,7 @@ static const char *find_reference_location(struct snapshot *snapshot,
539543 * preceding records all have reference names that come
540544 * *before* `refname`.
541545 */
542- const char * lo = snapshot -> buf + snapshot -> header_len ;
546+ const char * lo = snapshot -> start ;
543547
544548 /*
545549 * A pointer to a the first character of a record whose
@@ -617,8 +621,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
617621 /* If the file has a header line, process it: */
618622 if (snapshot -> buf < snapshot -> eof && * snapshot -> buf == '#' ) {
619623 struct strbuf tmp = STRBUF_INIT ;
620- char * p ;
621- const char * eol ;
624+ char * p , * eol ;
622625 struct string_list traits = STRING_LIST_INIT_NODUP ;
623626
624627 eol = memchr (snapshot -> buf , '\n' ,
@@ -647,7 +650,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
647650 /* perhaps other traits later as well */
648651
649652 /* The "+ 1" is for the LF character. */
650- snapshot -> header_len = eol + 1 - snapshot -> buf ;
653+ snapshot -> start = eol + 1 ;
651654
652655 string_list_clear (& traits , 0 );
653656 strbuf_release (& tmp );
@@ -671,13 +674,12 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
671674 * We don't want to leave the file mmapped, so we are
672675 * forced to make a copy now:
673676 */
674- size_t size = snapshot -> eof -
675- (snapshot -> buf + snapshot -> header_len );
677+ size_t size = snapshot -> eof - snapshot -> start ;
676678 char * buf_copy = xmalloc (size );
677679
678- memcpy (buf_copy , snapshot -> buf + snapshot -> header_len , size );
680+ memcpy (buf_copy , snapshot -> start , size );
679681 clear_snapshot_buffer (snapshot );
680- snapshot -> buf = buf_copy ;
682+ snapshot -> buf = snapshot -> start = buf_copy ;
681683 snapshot -> eof = buf_copy + size ;
682684 }
683685
@@ -937,7 +939,7 @@ static struct ref_iterator *packed_ref_iterator_begin(
937939 if (prefix && * prefix )
938940 start = find_reference_location (snapshot , prefix , 0 );
939941 else
940- start = snapshot -> buf + snapshot -> header_len ;
942+ start = snapshot -> start ;
941943
942944 iter -> pos = start ;
943945 iter -> eof = snapshot -> eof ;
0 commit comments