git-2.22.0/refs/packed-backend.c
/*
 * `snapshot->buf` is not known to be sorted. Check whether it is, and
 * if not, sort it into new memory and munmap/free the old storage.
 */
`snapshot->buf` is not known to be sorted. Check whether it is, and
if not, sort it into new memory and munmap/free the old storage.
` snapshot - > buf ` is not known to be sorted . Check whether it is and if not sort it into new memory and munmap / free the old storage .
static void sort_snapshot(struct snapshot *snapshot)
{
	struct snapshot_record *records = NULL;
	size_t alloc = 0, nr = 0;
	int sorted = 1;
	const char *pos, *eof, *eol;
	size_t len, i;
	char *new_buffer, *dst;
	pos = snapshot->start;
	eof = snapshot->eof;
	if (pos == eof)
		return;
	len = eof - pos;
	ALLOC_GROW(records, len / 80 + 20, alloc);
	while (pos < eof) {
		eol = memchr(pos, '\n', eof - pos);
		if (!eol)
			BUG("unterminated line found in packed-refs");
		if (eol - pos < the_hash_algo->hexsz + 2)
			die_invalid_line(snapshot->refs->path,
					 pos, eof - pos);
		eol++;
		if (eol < eof && *eol == '^') {
			const char *peeled_start = eol;
			eol = memchr(peeled_start, '\n', eof - peeled_start);
			if (!eol)
				BUG("unterminated peeled line found in packed-refs");
			eol++;
		}
		ALLOC_GROW(records, nr + 1, alloc);
		records[nr].start = pos;
		records[nr].len = eol - pos;
		nr++;
		if (sorted &&
		    nr > 1 &&
		    cmp_packed_ref_records(&records[nr - 2],
					   &records[nr - 1]) >= 0)
			sorted = 0;
		pos = eol;
	}
	if (sorted)
		goto cleanup;
	QSORT(records, nr, cmp_packed_ref_records);
	new_buffer = xmalloc(len);
	for (dst = new_buffer, i = 0; i < nr; i++) {
		memcpy(dst, records[i].start, records[i].len);
		dst += records[i].len;
	}
	clear_snapshot_buffer(snapshot);
	snapshot->buf = snapshot->start = new_buffer;
	snapshot->eof = new_buffer + len;
cleanup:
	free(records);
}
static void sort_snapshot ( struct snapshot * snapshot ) { struct snapshot_record * records = NULL ; size_t alloc = 0 , nr = 0 ; int sorted = 1 ; const char * pos , * eof , * eol ; size_t len , i ; char * new_buffer , * dst ; pos = snapshot -> start ; eof = snapshot -> eof ; if ( pos == eof ) return ; len = eof - pos ; ALLOC_GROW ( records , len / 80 + 20 , alloc ) ; while ( pos < eof ) { eol = memchr ( pos , ' \n ' , eof - pos ) ; if ( ! eol ) BUG ( " " ) ; if ( eol - pos < the_hash_algo -> hexsz + 2 ) die_invalid_line ( snapshot -> refs -> path , pos , eof - pos ) ; eol ++ ; if ( eol < eof && * eol == ' ' ) { const char * peeled_start = eol ; eol = memchr ( peeled_start , ' \n ' , eof - peeled_start ) ; if ( ! eol ) BUG ( " " ) ; eol ++ ; } ALLOC_GROW ( records , nr + 1 , alloc ) ; records [ nr ] . start = pos ; records [ nr ] . len = eol - pos ; nr ++ ; if ( sorted && nr > 1 && cmp_packed_ref_records ( & records [ nr - 2 ] , & records [ nr - 1 ] ) >= 0 ) sorted = 0 ; pos = eol ; } if ( sorted ) goto cleanup ; QSORT ( records , nr , cmp_packed_ref_records ) ; new_buffer = xmalloc ( len ) ; for ( dst = new_buffer , i = 0 ; i < nr ; i ++ ) { memcpy ( dst , records [ i ] . start , records [ i ] . len ) ; dst += records [ i ] . len ; } clear_snapshot_buffer ( snapshot ) ; snapshot -> buf = snapshot -> start = new_buffer ; snapshot -> eof = new_buffer + len ; cleanup : free ( records ) ; }
`snapshot->buf` is not known to be sorted.
` snapshot - > buf ` is not known to be sorted .
/*
	 * Initialize records based on a crude estimate of the number
	 * of references in the file (we'll grow it below if needed):
	 */ /* The safety check should prevent this. */ /*
			 * Keep any peeled line together with its
			 * reference:
			 */ /* The safety check should prevent this. */ /* We need to sort the memory. First we sort the records array: */ /*
	 * Allocate a new chunk of memory, and copy the old memory to
	 * the new in the order indicated by `records` (not bothering
	 * with the header line):
	 */ /*
	 * Now munmap the old buffer and use the sorted buffer in its
	 * place:
	 */