git-2.22.0/refs/packed-backend.c
/*
 * Find the place in `snapshot->buf` where the start of the record for
 * `refname` starts. If `mustexist` is true and the reference doesn't
 * exist, then return NULL. If `mustexist` is false and the reference
 * doesn't exist, then return the point where that reference would be
 * inserted, or `snapshot->eof` (which might be NULL) if it would be
 * inserted at the end of the file. In the latter mode, `refname`
 * doesn't have to be a proper reference name; for example, one could
 * search for "refs/replace/" to find the start of any replace
 * references.
 *
 * The record is sought using a binary search, so `snapshot->buf` must
 * be sorted.
 */
Find the place in `snapshot->buf` where the start of the record for
`refname` starts. If `mustexist` is true and the reference doesn't
exist, then return NULL. If `mustexist` is false and the reference
doesn't exist, then return the point where that reference would be
inserted, or `snapshot->eof` (which might be NULL) if it would be
inserted at the end of the file. In the latter mode, `refname`
doesn't have to be a proper reference name; for example, one could
search for "refs/replace/" to find the start of any replace
references.

The record is sought using a binary search, so `snapshot->buf` must
be sorted.
Find the place in ` snapshot - > buf ` where the start of the record for ` refname ` starts . If ` mustexist ` is true and the reference doesn ' t exist then return NULL . If ` mustexist ` is false and the reference doesn ' t exist then return the point where that reference would be inserted or ` snapshot - > eof ` ( which might be NULL ) if it would be inserted at the end of the file . In the latter mode ` refname ` doesn ' t have to be a proper reference name ; for example one could search for " refs / replace / " to find the start of any replace references . The record is sought using a binary search so ` snapshot - > buf ` must be sorted .
static const char *find_reference_location(struct snapshot *snapshot,
					   const char *refname, int mustexist)
{
	const char *lo = snapshot->start;
	const char *hi = snapshot->eof;
	while (lo != hi) {
		const char *mid, *rec;
		int cmp;
		mid = lo + (hi - lo) / 2;
		rec = find_start_of_record(lo, mid);
		cmp = cmp_record_to_refname(rec, refname);
		if (cmp < 0) {
			lo = find_end_of_record(mid, hi);
		} else if (cmp > 0) {
			hi = rec;
		} else {
			return rec;
		}
	}
	if (mustexist)
		return NULL;
	else
		return lo;
}
static const char * find_reference_location ( struct snapshot * snapshot , const char * refname , int mustexist ) { const char * lo = snapshot -> start ; const char * hi = snapshot -> eof ; while ( lo != hi ) { const char * mid , * rec ; int cmp ; mid = lo + ( hi - lo ) / 2 ; rec = find_start_of_record ( lo , mid ) ; cmp = cmp_record_to_refname ( rec , refname ) ; if ( cmp < 0 ) { lo = find_end_of_record ( mid , hi ) ; } else if ( cmp > 0 ) { hi = rec ; } else { return rec ; } } if ( mustexist ) return NULL ; else return lo ; }
Find the place in `snapshot->buf` where the start of the record for
 `refname` starts.
Find the place in ` snapshot - > buf ` where the start of the record for ` refname ` starts .
/*
	 * This is not *quite* a garden-variety binary search, because
	 * the data we're searching is made up of records, and we
	 * always need to find the beginning of a record to do a
	 * comparison. A "record" here is one line for the reference
	 * itself and zero or one peel lines that start with '^'. Our
	 * loop invariant is described in the next two comments.
	 */ /*
	 * A pointer to the character at the start of a record whose
	 * preceding records all have reference names that come
	 * *before* `refname`.
	 */ /*
	 * A pointer to a the first character of a record whose
	 * reference name comes *after* `refname`.
	 */