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>
Michael Haggerty committed
Sep 15, 2011 at 23:10 UTC
c224ca7f66bf88bf933d05ecbc163b5bbb152098
1 file changed
+34
-13
refs.c
+34
-13
@@ -465,6 +465,23 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
465
return retval;
466
}
467
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
+
485
/*
486
* If the "reading" argument is set, this function finds out what _object_
487
* 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 *
514
return NULL;
515
516
git_snpath(path, sizeof(path), "%s", ref);
500
- /* Special case: non-existing file. */
517
+
518
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;
529
}
512
- if (reading || errno != ENOENT)
530
+ /* The reference is not a packed reference, either. */
531
+ if (reading) {
532
return NULL;
514
- hashclr(sha1);
515
- return ref;
533
+ } else {
534
+ hashclr(sha1);
535
+ return ref;
536
+ }
537
}
538
539
/* Follow "normalized" - ie "refs/.." symlinks by hand */