300
size_t len;
301
};
302
303
-static int cmp_packed_ref_records(const void *v1, const void *v2,
304
- void *cb_data)
305
-{
306
- const struct snapshot *snapshot = cb_data;
307
- const struct snapshot_record *e1 = v1, *e2 = v2;
308
- const char *r1 = e1->start + snapshot_hexsz(snapshot) + 1;
309
- const char *r2 = e2->start + snapshot_hexsz(snapshot) + 1;
303
304
+static int cmp_packed_refname(const char *r1, const char *r2)
305
+{
306
while (1) {
307
if (*r1 == '\n')
308
return *r2 == '\n' ? 0 : -1;
317
}
318
}
319
320
+static int cmp_packed_ref_records(const void *v1, const void *v2,
321
+ void *cb_data)
322
+{
323
+ const struct snapshot *snapshot = cb_data;
324
+ const struct snapshot_record *e1 = v1, *e2 = v2;
325
+ const char *r1 = e1->start + snapshot_hexsz(snapshot) + 1;
326
+ const char *r2 = e2->start + snapshot_hexsz(snapshot) + 1;
327
+
328
+ return cmp_packed_refname(r1, r2);
329
+}
330
+
331
/*
332
* Compare a snapshot record at `rec` to the specified NUL-terminated
333
* refname.
1803
}
1804
1805
static int packed_fsck_ref_header(struct fsck_options *o,
1800
- const char *start, const char *eol)
1806
+ const char *start, const char *eol,
1807
+ unsigned int *sorted)
1808
{
1802
- if (!starts_with(start, "# pack-refs with: ")) {
1809
+ struct string_list traits = STRING_LIST_INIT_NODUP;
1810
+ char *tmp_line;
1811
+ int ret = 0;
1812
+ char *p;
1813
+
1814
+ tmp_line = xmemdupz(start, eol - start);
1815
+ if (!skip_prefix(tmp_line, "# pack-refs with: ", (const char **)&p)) {
1816
struct fsck_ref_report report = { 0 };
1817
report.path = "packed-refs.header";
1818
1806
- return fsck_report_ref(o, &report,
1807
- FSCK_MSG_BAD_PACKED_REF_HEADER,
1808
- "'%.*s' does not start with '# pack-refs with: '",
1809
- (int)(eol - start), start);
1819
+ ret = fsck_report_ref(o, &report,
1820
+ FSCK_MSG_BAD_PACKED_REF_HEADER,
1821
+ "'%.*s' does not start with '# pack-refs with: '",
1822
+ (int)(eol - start), start);
1823
+ goto cleanup;
1824
}
1825
1812
- return 0;
1826
+ string_list_split_in_place(&traits, p, " ", -1);
1827
+ *sorted = unsorted_string_list_has_string(&traits, "sorted");
1828
+
1829
+cleanup:
1830
+ free(tmp_line);
1831
+ string_list_clear(&traits, 0);
1832
+ return ret;
1833
}
1834
1835
static int packed_fsck_ref_peeled_line(struct fsck_options *o,
1935
return ret;
1936
}
1937
1938
+static int packed_fsck_ref_sorted(struct fsck_options *o,
1939
+ struct ref_store *ref_store,
1940
+ const char *start, const char *eof)
1941
+{
1942
+ size_t hexsz = ref_store->repo->hash_algo->hexsz;
1943
+ struct strbuf packed_entry = STRBUF_INIT;
1944
+ struct fsck_ref_report report = { 0 };
1945
+ struct strbuf refname1 = STRBUF_INIT;
1946
+ struct strbuf refname2 = STRBUF_INIT;
1947
+ unsigned long line_number = 1;
1948
+ const char *former = NULL;
1949
+ const char *current;
1950
+ const char *eol;
1951
+ int ret = 0;
1952
+
1953
+ if (*start == '#') {
1954
+ eol = memchr(start, '\n', eof - start);
1955
+ start = eol + 1;
1956
+ line_number++;
1957
+ }
1958
+
1959
+ for (; start < eof; line_number++, start = eol + 1) {
1960
+ eol = memchr(start, '\n', eof - start);
1961
+
1962
+ if (*start == '^')
1963
+ continue;
1964
+
1965
+ if (!former) {
1966
+ former = start + hexsz + 1;
1967
+ continue;
1968
+ }
1969
+
1970
+ current = start + hexsz + 1;
1971
+ if (cmp_packed_refname(former, current) >= 0) {
1972
+ const char *err_fmt =
1973
+ "refname '%s' is less than previous refname '%s'";
1974
+
1975
+ eol = memchr(former, '\n', eof - former);
1976
+ strbuf_add(&refname1, former, eol - former);
1977
+ eol = memchr(current, '\n', eof - current);
1978
+ strbuf_add(&refname2, current, eol - current);
1979
+
1980
+ strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1981
+ report.path = packed_entry.buf;
1982
+ ret = fsck_report_ref(o, &report,
1983
+ FSCK_MSG_PACKED_REF_UNSORTED,
1984
+ err_fmt, refname2.buf, refname1.buf);
1985
+ goto cleanup;
1986
+ }
1987
+ former = current;
1988
+ }
1989
+
1990
+cleanup:
1991
+ strbuf_release(&packed_entry);
1992
+ strbuf_release(&refname1);
1993
+ strbuf_release(&refname2);
1994
+ return ret;
1995
+}
1996
+
1997
static int packed_fsck_ref_content(struct fsck_options *o,
1998
struct ref_store *ref_store,
1999
+ unsigned int *sorted,
2000
const char *start, const char *eof)
2001
{
2002
struct strbuf refname = STRBUF_INIT;
2006
2007
ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol);
2008
if (*start == '#') {
1929
- ret |= packed_fsck_ref_header(o, start, eol);
2009
+ ret |= packed_fsck_ref_header(o, start, eol, sorted);
2010
2011
start = eol + 1;
2012
line_number++;
2037
struct packed_ref_store *refs = packed_downcast(ref_store,
2038
REF_STORE_READ, "fsck");
2039
struct strbuf packed_ref_content = STRBUF_INIT;
2040
+ unsigned int sorted = 0;
2041
struct stat st;
2042
int ret = 0;
2043
int fd = -1;
2085
goto cleanup;
2086
}
2087
2007
- ret = packed_fsck_ref_content(o, ref_store, packed_ref_content.buf,
2088
+ ret = packed_fsck_ref_content(o, ref_store, &sorted, packed_ref_content.buf,
2089
packed_ref_content.buf + packed_ref_content.len);
2090
+ if (!ret && sorted)
2091
+ ret = packed_fsck_ref_sorted(o, ref_store, packed_ref_content.buf,
2092
+ packed_ref_content.buf + packed_ref_content.len);
2093
2094
cleanup:
2095
if (fd >= 0)