refs: read FETCH_HEAD and MERGE_HEAD generically
The FETCH_HEAD and MERGE_HEAD refs must be stored in a file, regardless of the type of ref backend. This is because they can hold more than just a single ref. To accomodate them for alternate ref backends, read them from a file generically in refs_read_raw_ref() 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
e81153027857b0b0ed3dfa6a544df2beeac0cfce
1 file changed
+27
-1
refs.c
+27
-1
@@ -1634,11 +1634,37 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
1634
return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data);
1635
}
1636
1637
+static int refs_read_special_head(struct ref_store *ref_store,
1638
+ const char *refname, struct object_id *oid,
1639
+ struct strbuf *referent, unsigned int *type)
1640
+{
1641
+ struct strbuf full_path = STRBUF_INIT;
1642
+ struct strbuf content = STRBUF_INIT;
1643
+ int result = -1;
1644
+ strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);
1645
+
1646
+ if (strbuf_read_file(&content, full_path.buf, 0) < 0)
1647
+ goto done;
1648
+
1649
+ result = parse_loose_ref_contents(content.buf, oid, referent, type);
1650
+
1651
+done:
1652
+ strbuf_release(&full_path);
1653
+ strbuf_release(&content);
1654
+ return result;
1655
+}
1656
+
1657
int refs_read_raw_ref(struct ref_store *ref_store,
1658
const char *refname, struct object_id *oid,
1659
struct strbuf *referent, unsigned int *type)
1660
{
1641
- return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, type);
1661
+ if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
1662
+ return refs_read_special_head(ref_store, refname, oid, referent,
1663
+ type);
1664
+ }
1665
+
1666
+ return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
1667
+ type);
1668
}
1669
1670
/* This function needs to return a meaningful errno on failure */