Skip to content

Commit 9f16184

Browse files
committed
Merge branch 'jk/read-packed-refs-without-path-max' into maint
* jk/read-packed-refs-without-path-max: read_packed_refs: use skip_prefix instead of static array read_packed_refs: pass strbuf to parse_ref_line read_packed_refs: use a strbuf for reading lines
2 parents d0879b3 + ea41783 commit 9f16184

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

refs.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,10 @@ static const char PACKED_REFS_HEADER[] =
10681068
* Return a pointer to the refname within the line (null-terminated),
10691069
* or NULL if there was a problem.
10701070
*/
1071-
static const char *parse_ref_line(char *line, unsigned char *sha1)
1071+
static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
10721072
{
1073+
const char *ref;
1074+
10731075
/*
10741076
* 42: the answer to everything.
10751077
*
@@ -1078,22 +1080,23 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
10781080
* +1 (space in between hex and name)
10791081
* +1 (newline at the end of the line)
10801082
*/
1081-
int len = strlen(line) - 42;
1082-
1083-
if (len <= 0)
1083+
if (line->len <= 42)
10841084
return NULL;
1085-
if (get_sha1_hex(line, sha1) < 0)
1085+
1086+
if (get_sha1_hex(line->buf, sha1) < 0)
10861087
return NULL;
1087-
if (!isspace(line[40]))
1088+
if (!isspace(line->buf[40]))
10881089
return NULL;
1089-
line += 41;
1090-
if (isspace(*line))
1090+
1091+
ref = line->buf + 41;
1092+
if (isspace(*ref))
10911093
return NULL;
1092-
if (line[len] != '\n')
1094+
1095+
if (line->buf[line->len - 1] != '\n')
10931096
return NULL;
1094-
line[len] = 0;
1097+
line->buf[--line->len] = 0;
10951098

1096-
return line;
1099+
return ref;
10971100
}
10981101

10991102
/*
@@ -1126,16 +1129,15 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
11261129
static void read_packed_refs(FILE *f, struct ref_dir *dir)
11271130
{
11281131
struct ref_entry *last = NULL;
1129-
char refline[PATH_MAX];
1132+
struct strbuf line = STRBUF_INIT;
11301133
enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
11311134

1132-
while (fgets(refline, sizeof(refline), f)) {
1135+
while (strbuf_getwholeline(&line, f, '\n') != EOF) {
11331136
unsigned char sha1[20];
11341137
const char *refname;
1135-
static const char header[] = "# pack-refs with:";
1138+
const char *traits;
11361139

1137-
if (!strncmp(refline, header, sizeof(header)-1)) {
1138-
const char *traits = refline + sizeof(header) - 1;
1140+
if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
11391141
if (strstr(traits, " fully-peeled "))
11401142
peeled = PEELED_FULLY;
11411143
else if (strstr(traits, " peeled "))
@@ -1144,7 +1146,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
11441146
continue;
11451147
}
11461148

1147-
refname = parse_ref_line(refline, sha1);
1149+
refname = parse_ref_line(&line, sha1);
11481150
if (refname) {
11491151
int flag = REF_ISPACKED;
11501152

@@ -1160,10 +1162,10 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
11601162
continue;
11611163
}
11621164
if (last &&
1163-
refline[0] == '^' &&
1164-
strlen(refline) == PEELED_LINE_LENGTH &&
1165-
refline[PEELED_LINE_LENGTH - 1] == '\n' &&
1166-
!get_sha1_hex(refline + 1, sha1)) {
1165+
line.buf[0] == '^' &&
1166+
line.len == PEELED_LINE_LENGTH &&
1167+
line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
1168+
!get_sha1_hex(line.buf + 1, sha1)) {
11671169
hashcpy(last->u.value.peeled, sha1);
11681170
/*
11691171
* Regardless of what the file header said,
@@ -1173,6 +1175,8 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
11731175
last->flag |= REF_KNOWS_PEELED;
11741176
}
11751177
}
1178+
1179+
strbuf_release(&line);
11761180
}
11771181

11781182
/*

0 commit comments

Comments
 (0)