Skip to content

Commit c224ca7

Browse files
mhaggergitster
authored andcommitted
resolve_ref(): extract a function get_packed_ref()
Making it a function and giving it a name makes the code clearer. I also have a strong suspicion that the function will find other uses in the future. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2877505 commit c224ca7

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

refs.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,23 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
465465
return retval;
466466
}
467467

468+
/*
469+
* Try to read ref from the packed references. On success, set sha1
470+
* and return 0; otherwise, return -1.
471+
*/
472+
static int get_packed_ref(const char *ref, unsigned char *sha1)
473+
{
474+
struct ref_list *list = get_packed_refs(NULL);
475+
while (list) {
476+
if (!strcmp(ref, list->name)) {
477+
hashcpy(sha1, list->sha1);
478+
return 0;
479+
}
480+
list = list->next;
481+
}
482+
return -1;
483+
}
484+
468485
/*
469486
* If the "reading" argument is set, this function finds out what _object_
470487
* the ref points at by "reading" the ref. The ref, if it is not symbolic,
@@ -497,22 +514,26 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
497514
return NULL;
498515

499516
git_snpath(path, sizeof(path), "%s", ref);
500-
/* Special case: non-existing file. */
517+
501518
if (lstat(path, &st) < 0) {
502-
struct ref_list *list = get_packed_refs(NULL);
503-
while (list) {
504-
if (!strcmp(ref, list->name)) {
505-
hashcpy(sha1, list->sha1);
506-
if (flag)
507-
*flag |= REF_ISPACKED;
508-
return ref;
509-
}
510-
list = list->next;
519+
if (errno != ENOENT)
520+
return NULL;
521+
/*
522+
* The loose reference file does not exist;
523+
* check for a packed reference.
524+
*/
525+
if (!get_packed_ref(ref, sha1)) {
526+
if (flag)
527+
*flag |= REF_ISPACKED;
528+
return ref;
511529
}
512-
if (reading || errno != ENOENT)
530+
/* The reference is not a packed reference, either. */
531+
if (reading) {
513532
return NULL;
514-
hashclr(sha1);
515-
return ref;
533+
} else {
534+
hashclr(sha1);
535+
return ref;
536+
}
516537
}
517538

518539
/* Follow "normalized" - ie "refs/.." symlinks by hand */

0 commit comments

Comments
 (0)