refs: split off reading loose ref data in separate function
This prepares for handling FETCH_HEAD (which is not a regular ref) separately from the ref backend. Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Han-Wen Nienhuys committed
Aug 19, 2020 at 14:27 UTC
e39620f07e092f36642ab97c6ced47be5b609d58
2 files changed
+25
-15
refs/files-backend.c
+19
-15
@@ -360,7 +360,6 @@ static int files_read_raw_ref(struct ref_store *ref_store,
360
struct strbuf sb_path = STRBUF_INIT;
361
const char *path;
362
const char *buf;
363
- const char *p;
363
struct stat st;
364
int fd;
365
int ret = -1;
@@ -465,6 +464,21 @@ stat_ref:
464
close(fd);
465
strbuf_rtrim(&sb_contents);
466
buf = sb_contents.buf;
467
+
468
+ ret = parse_loose_ref_contents(buf, oid, referent, type);
469
+
470
+out:
471
+ save_errno = errno;
472
+ strbuf_release(&sb_path);
473
+ strbuf_release(&sb_contents);
474
+ errno = save_errno;
475
+ return ret;
476
+}
477
+
478
+int parse_loose_ref_contents(const char *buf, struct object_id *oid,
479
+ struct strbuf *referent, unsigned int *type)
480
+{
481
+ const char *p;
482
if (skip_prefix(buf, "ref:", &buf)) {
483
while (isspace(*buf))
484
buf++;
@@ -472,29 +486,19 @@ stat_ref:
486
strbuf_reset(referent);
487
strbuf_addstr(referent, buf);
488
*type |= REF_ISSYMREF;
475
- ret = 0;
476
- goto out;
489
+ return 0;
490
}
491
492
/*
480
- * Please note that FETCH_HEAD has additional
481
- * data after the sha.
493
+ * FETCH_HEAD has additional data after the sha.
494
*/
495
if (parse_oid_hex(buf, oid, &p) ||
496
(*p != '\0' && !isspace(*p))) {
497
*type |= REF_ISBROKEN;
498
errno = EINVAL;
487
- goto out;
499
+ return -1;
500
}
489
-
490
- ret = 0;
491
-
492
-out:
493
- save_errno = errno;
494
- strbuf_release(&sb_path);
495
- strbuf_release(&sb_contents);
496
- errno = save_errno;
497
- return ret;
501
+ return 0;
502
}
503
504
static void unlock_ref(struct ref_lock *lock)
refs/refs-internal.h
+6
@@ -674,6 +674,12 @@ struct ref_store {
674
const struct ref_storage_be *be;
675
};
676
677
+/*
678
+ * Parse contents of a loose ref file.
679
+ */
680
+int parse_loose_ref_contents(const char *buf, struct object_id *oid,
681
+ struct strbuf *referent, unsigned int *type);
682
+
683
/*
684
* Fill in the generic part of refs and add it to our collection of
685
* reference stores.