struct snapshot: store start
rather than header_len
Store a pointer to the start of the actual references within the `packed-refs` contents rather than storing the length of the header. This is more convenient for most users of this field. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
3013dff866
commit
4a2854f77c
@ -68,17 +68,21 @@ struct snapshot {
|
|||||||
int mmapped;
|
int mmapped;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The contents of the `packed-refs` file. If the file was
|
* The contents of the `packed-refs` file:
|
||||||
* already sorted, this points at the mmapped contents of the
|
*
|
||||||
* file. If not, this points at heap-allocated memory
|
* - buf -- a pointer to the start of the memory
|
||||||
* containing the contents, sorted. If there were no contents
|
* - start -- a pointer to the first byte of actual references
|
||||||
* (e.g., because the file didn't exist), `buf` and `eof` are
|
* (i.e., after the header line, if one is present)
|
||||||
* both NULL.
|
* - eof -- a pointer just past the end of the reference
|
||||||
|
* contents
|
||||||
|
*
|
||||||
|
* If the `packed-refs` file was already sorted, `buf` points
|
||||||
|
* at the mmapped contents of the file. If not, it points at
|
||||||
|
* heap-allocated memory containing the contents, sorted. If
|
||||||
|
* there were no contents (e.g., because the file didn't
|
||||||
|
* exist), `buf`, `start`, and `eof` are all NULL.
|
||||||
*/
|
*/
|
||||||
char *buf, *eof;
|
char *buf, *start, *eof;
|
||||||
|
|
||||||
/* The size of the header line, if any; otherwise, 0: */
|
|
||||||
size_t header_len;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* What is the peeled state of the `packed-refs` file that
|
* What is the peeled state of the `packed-refs` file that
|
||||||
@ -169,8 +173,7 @@ static void clear_snapshot_buffer(struct snapshot *snapshot)
|
|||||||
} else {
|
} else {
|
||||||
free(snapshot->buf);
|
free(snapshot->buf);
|
||||||
}
|
}
|
||||||
snapshot->buf = snapshot->eof = NULL;
|
snapshot->buf = snapshot->start = snapshot->eof = NULL;
|
||||||
snapshot->header_len = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -319,13 +322,14 @@ static void sort_snapshot(struct snapshot *snapshot)
|
|||||||
size_t len, i;
|
size_t len, i;
|
||||||
char *new_buffer, *dst;
|
char *new_buffer, *dst;
|
||||||
|
|
||||||
pos = snapshot->buf + snapshot->header_len;
|
pos = snapshot->start;
|
||||||
eof = snapshot->eof;
|
eof = snapshot->eof;
|
||||||
len = eof - pos;
|
|
||||||
|
|
||||||
if (!len)
|
if (pos == eof)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
len = eof - pos;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize records based on a crude estimate of the number
|
* Initialize records based on a crude estimate of the number
|
||||||
* of references in the file (we'll grow it below if needed):
|
* of references in the file (we'll grow it below if needed):
|
||||||
@ -391,9 +395,8 @@ static void sort_snapshot(struct snapshot *snapshot)
|
|||||||
* place:
|
* place:
|
||||||
*/
|
*/
|
||||||
clear_snapshot_buffer(snapshot);
|
clear_snapshot_buffer(snapshot);
|
||||||
snapshot->buf = new_buffer;
|
snapshot->buf = snapshot->start = new_buffer;
|
||||||
snapshot->eof = new_buffer + len;
|
snapshot->eof = new_buffer + len;
|
||||||
snapshot->header_len = 0;
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
free(records);
|
free(records);
|
||||||
@ -442,14 +445,14 @@ static const char *find_end_of_record(const char *p, const char *end)
|
|||||||
*/
|
*/
|
||||||
static void verify_buffer_safe(struct snapshot *snapshot)
|
static void verify_buffer_safe(struct snapshot *snapshot)
|
||||||
{
|
{
|
||||||
const char *buf = snapshot->buf + snapshot->header_len;
|
const char *start = snapshot->start;
|
||||||
const char *eof = snapshot->eof;
|
const char *eof = snapshot->eof;
|
||||||
const char *last_line;
|
const char *last_line;
|
||||||
|
|
||||||
if (buf == eof)
|
if (start == eof)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
last_line = find_start_of_record(buf, eof - 1);
|
last_line = find_start_of_record(start, eof - 1);
|
||||||
if (*(eof - 1) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2)
|
if (*(eof - 1) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2)
|
||||||
die_invalid_line(snapshot->refs->path,
|
die_invalid_line(snapshot->refs->path,
|
||||||
last_line, eof - last_line);
|
last_line, eof - last_line);
|
||||||
@ -495,18 +498,19 @@ static int load_contents(struct snapshot *snapshot)
|
|||||||
bytes_read = read_in_full(fd, snapshot->buf, size);
|
bytes_read = read_in_full(fd, snapshot->buf, size);
|
||||||
if (bytes_read < 0 || bytes_read != size)
|
if (bytes_read < 0 || bytes_read != size)
|
||||||
die_errno("couldn't read %s", snapshot->refs->path);
|
die_errno("couldn't read %s", snapshot->refs->path);
|
||||||
snapshot->eof = snapshot->buf + size;
|
|
||||||
snapshot->mmapped = 0;
|
snapshot->mmapped = 0;
|
||||||
break;
|
break;
|
||||||
case MMAP_TEMPORARY:
|
case MMAP_TEMPORARY:
|
||||||
case MMAP_OK:
|
case MMAP_OK:
|
||||||
snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
snapshot->eof = snapshot->buf + size;
|
|
||||||
snapshot->mmapped = 1;
|
snapshot->mmapped = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
snapshot->start = snapshot->buf;
|
||||||
|
snapshot->eof = snapshot->buf + size;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -539,7 +543,7 @@ static const char *find_reference_location(struct snapshot *snapshot,
|
|||||||
* preceding records all have reference names that come
|
* preceding records all have reference names that come
|
||||||
* *before* `refname`.
|
* *before* `refname`.
|
||||||
*/
|
*/
|
||||||
const char *lo = snapshot->buf + snapshot->header_len;
|
const char *lo = snapshot->start;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A pointer to a the first character of a record whose
|
* 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)
|
|||||||
/* If the file has a header line, process it: */
|
/* If the file has a header line, process it: */
|
||||||
if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
|
if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
|
||||||
struct strbuf tmp = STRBUF_INIT;
|
struct strbuf tmp = STRBUF_INIT;
|
||||||
char *p;
|
char *p, *eol;
|
||||||
const char *eol;
|
|
||||||
struct string_list traits = STRING_LIST_INIT_NODUP;
|
struct string_list traits = STRING_LIST_INIT_NODUP;
|
||||||
|
|
||||||
eol = memchr(snapshot->buf, '\n',
|
eol = memchr(snapshot->buf, '\n',
|
||||||
@ -647,7 +650,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
|
|||||||
/* perhaps other traits later as well */
|
/* perhaps other traits later as well */
|
||||||
|
|
||||||
/* The "+ 1" is for the LF character. */
|
/* The "+ 1" is for the LF character. */
|
||||||
snapshot->header_len = eol + 1 - snapshot->buf;
|
snapshot->start = eol + 1;
|
||||||
|
|
||||||
string_list_clear(&traits, 0);
|
string_list_clear(&traits, 0);
|
||||||
strbuf_release(&tmp);
|
strbuf_release(&tmp);
|
||||||
@ -671,13 +674,12 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
|
|||||||
* We don't want to leave the file mmapped, so we are
|
* We don't want to leave the file mmapped, so we are
|
||||||
* forced to make a copy now:
|
* forced to make a copy now:
|
||||||
*/
|
*/
|
||||||
size_t size = snapshot->eof -
|
size_t size = snapshot->eof - snapshot->start;
|
||||||
(snapshot->buf + snapshot->header_len);
|
|
||||||
char *buf_copy = xmalloc(size);
|
char *buf_copy = xmalloc(size);
|
||||||
|
|
||||||
memcpy(buf_copy, snapshot->buf + snapshot->header_len, size);
|
memcpy(buf_copy, snapshot->start, size);
|
||||||
clear_snapshot_buffer(snapshot);
|
clear_snapshot_buffer(snapshot);
|
||||||
snapshot->buf = buf_copy;
|
snapshot->buf = snapshot->start = buf_copy;
|
||||||
snapshot->eof = buf_copy + size;
|
snapshot->eof = buf_copy + size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -937,7 +939,7 @@ static struct ref_iterator *packed_ref_iterator_begin(
|
|||||||
if (prefix && *prefix)
|
if (prefix && *prefix)
|
||||||
start = find_reference_location(snapshot, prefix, 0);
|
start = find_reference_location(snapshot, prefix, 0);
|
||||||
else
|
else
|
||||||
start = snapshot->buf + snapshot->header_len;
|
start = snapshot->start;
|
||||||
|
|
||||||
iter->pos = start;
|
iter->pos = start;
|
||||||
iter->eof = snapshot->eof;
|
iter->eof = snapshot->eof;
|
||||||
|
Loading…
Reference in New Issue
Block a user