Skip to content

Commit c018836

Browse files
shejialuogitster
authored andcommitted
packed-backend: create "fsck_packed_ref_entry" to store parsing info
We have already check whether the oid hash is correct by using `parse_oid_hex_algop`. However, we doesn't check whether the object exists. It may seem that we could do this when we are parsing the raw "packed-refs" file. But this is impossible. Let's analysis why. We will use "parse_object" function to get the "struct object". However, this function will eventually call the "create_snapshot" and "next_record" function in "packed-backend.c". If there is anything wrong, it will die the program. And we don't want to die the program during the check. So, we should store the information in the parsing process. And if there is nothing wrong in the parsing process, we could continue to check things. So, create "fsck_packed_ref_entry" to do this. Mentored-by: Patrick Steinhardt <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d87c126 commit c018836

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

refs/packed-backend.c

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,29 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
17691769
return empty_ref_iterator_begin();
17701770
}
17711771

1772+
struct fsck_packed_ref_entry {
1773+
int line_number;
1774+
1775+
int has_peeled;
1776+
struct object_id oid;
1777+
struct object_id peeled;
1778+
};
1779+
1780+
static struct fsck_packed_ref_entry *create_fsck_packed_ref_entry(int line_number)
1781+
{
1782+
struct fsck_packed_ref_entry *entry = xcalloc(1, sizeof(*entry));
1783+
entry->line_number = line_number;
1784+
entry->has_peeled = 0;
1785+
return entry;
1786+
}
1787+
1788+
static void free_fsck_packed_ref_entries(struct fsck_packed_ref_entry **entries, int nr)
1789+
{
1790+
for (int i = 0; i < nr; i++)
1791+
free(entries[i]);
1792+
free(entries);
1793+
}
1794+
17721795
static int packed_fsck_ref_next_line(struct fsck_options *o,
17731796
int line_number, const char *start,
17741797
const char *eof, const char **eol)
@@ -1824,20 +1847,20 @@ static int packed_fsck_ref_header(struct fsck_options *o, const char *start, con
18241847
}
18251848

18261849
static int packed_fsck_ref_peeled_line(struct fsck_options *o,
1827-
struct ref_store *ref_store, int line_number,
1850+
struct ref_store *ref_store,
1851+
struct fsck_packed_ref_entry *entry,
18281852
const char *start, const char *eol)
18291853
{
18301854
struct strbuf peeled_entry = STRBUF_INIT;
18311855
struct fsck_ref_report report = { 0 };
1832-
struct object_id peeled;
18331856
const char *p;
18341857
int ret = 0;
18351858

1836-
strbuf_addf(&peeled_entry, "packed-refs line %d", line_number);
1859+
strbuf_addf(&peeled_entry, "packed-refs line %d", entry->line_number + 1);
18371860
report.path = peeled_entry.buf;
18381861

18391862
start++;
1840-
if (parse_oid_hex_algop(start, &peeled, &p, ref_store->repo->hash_algo)) {
1863+
if (parse_oid_hex_algop(start, &entry->peeled, &p, ref_store->repo->hash_algo)) {
18411864
ret |= fsck_report_ref(o, &report,
18421865
FSCK_MSG_BAD_PACKED_REF_ENTRY,
18431866
"'%.*s' has invalid peeled oid",
@@ -1859,20 +1882,20 @@ static int packed_fsck_ref_peeled_line(struct fsck_options *o,
18591882
}
18601883

18611884
static int packed_fsck_ref_main_line(struct fsck_options *o,
1862-
struct ref_store *ref_store, int line_number,
1885+
struct ref_store *ref_store,
1886+
struct fsck_packed_ref_entry *entry,
18631887
const char *start, const char *eol)
18641888
{
18651889
struct strbuf packed_entry = STRBUF_INIT;
18661890
struct fsck_ref_report report = { 0 };
18671891
struct strbuf refname = STRBUF_INIT;
1868-
struct object_id oid;
18691892
const char *p;
18701893
int ret = 0;
18711894

1872-
strbuf_addf(&packed_entry, "packed-refs line %d", line_number);
1895+
strbuf_addf(&packed_entry, "packed-refs line %d", entry->line_number);
18731896
report.path = packed_entry.buf;
18741897

1875-
if (parse_oid_hex_algop(start, &oid, &p, ref_store->repo->hash_algo)) {
1898+
if (parse_oid_hex_algop(start, &entry->oid, &p, ref_store->repo->hash_algo)) {
18761899
ret |= fsck_report_ref(o, &report,
18771900
FSCK_MSG_BAD_PACKED_REF_ENTRY,
18781901
"'%.*s' has invalid oid",
@@ -1884,7 +1907,7 @@ static int packed_fsck_ref_main_line(struct fsck_options *o,
18841907
ret |= fsck_report_ref(o, &report,
18851908
FSCK_MSG_BAD_PACKED_REF_ENTRY,
18861909
"has no space after oid '%s' but with '%.*s'",
1887-
oid_to_hex(&oid), (int)(eol - p), p);
1910+
oid_to_hex(&entry->oid), (int)(eol - p), p);
18881911
goto cleanup;
18891912
}
18901913

@@ -1915,7 +1938,10 @@ static int packed_fsck_ref_content(struct fsck_options *o,
19151938
struct ref_store *ref_store,
19161939
const char *start, const char *eof)
19171940
{
1941+
struct fsck_packed_ref_entry **entries;
1942+
int entry_alloc = 20;
19181943
int line_number = 1;
1944+
int entry_nr = 0;
19191945
const char *eol;
19201946
int ret = 0;
19211947

@@ -1934,14 +1960,21 @@ static int packed_fsck_ref_content(struct fsck_options *o,
19341960
"missing header line");
19351961
}
19361962

1963+
ALLOC_ARRAY(entries, entry_alloc);
19371964
while (start < eof) {
1965+
struct fsck_packed_ref_entry *entry
1966+
= create_fsck_packed_ref_entry(line_number);
1967+
ALLOC_GROW(entries, entry_nr + 1, entry_alloc);
1968+
entries[entry_nr++] = entry;
1969+
19381970
ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol);
1939-
ret |= packed_fsck_ref_main_line(o, ref_store, line_number, start, eol);
1971+
ret |= packed_fsck_ref_main_line(o, ref_store, entry, start, eol);
19401972
start = eol + 1;
19411973
line_number++;
19421974
if (start < eof && *start == '^') {
1975+
entry->has_peeled = 1;
19431976
ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol);
1944-
ret |= packed_fsck_ref_peeled_line(o, ref_store, line_number,
1977+
ret |= packed_fsck_ref_peeled_line(o, ref_store, entry,
19451978
start, eol);
19461979
start = eol + 1;
19471980
line_number++;
@@ -1956,6 +1989,7 @@ static int packed_fsck_ref_content(struct fsck_options *o,
19561989
o->safe_object_check = 0;
19571990

19581991

1992+
free_fsck_packed_ref_entries(entries, entry_nr);
19591993
return ret;
19601994
}
19611995

0 commit comments

Comments
 (0)