@@ -1806,8 +1806,10 @@ static int refs_read_special_head(struct ref_store *ref_store,
18061806 int result = -1 ;
18071807 strbuf_addf (& full_path , "%s/%s" , ref_store -> gitdir , refname );
18081808
1809- if (strbuf_read_file (& content , full_path .buf , 0 ) < 0 )
1809+ if (strbuf_read_file (& content , full_path .buf , 0 ) < 0 ) {
1810+ * failure_errno = errno ;
18101811 goto done ;
1812+ }
18111813
18121814 result = parse_loose_ref_contents (content .buf , oid , referent , type ,
18131815 failure_errno );
@@ -1818,15 +1820,66 @@ static int refs_read_special_head(struct ref_store *ref_store,
18181820 return result ;
18191821}
18201822
1823+ static int is_special_ref (const char * refname )
1824+ {
1825+ /*
1826+ * Special references get written and read directly via the filesystem
1827+ * by the subsystems that create them. Thus, they must not go through
1828+ * the reference backend but must instead be read directly. It is
1829+ * arguable whether this behaviour is sensible, or whether it's simply
1830+ * a leaky abstraction enabled by us only having a single reference
1831+ * backend implementation. But at least for a subset of references it
1832+ * indeed does make sense to treat them specially:
1833+ *
1834+ * - FETCH_HEAD may contain multiple object IDs, and each one of them
1835+ * carries additional metadata like where it came from.
1836+ *
1837+ * - MERGE_HEAD may contain multiple object IDs when merging multiple
1838+ * heads.
1839+ *
1840+ * There are some exceptions that you might expect to see on this list
1841+ * but which are handled exclusively via the reference backend:
1842+ *
1843+ * - BISECT_EXPECTED_REV
1844+ *
1845+ * - CHERRY_PICK_HEAD
1846+ *
1847+ * - HEAD
1848+ *
1849+ * - ORIG_HEAD
1850+ *
1851+ * - "rebase-apply/" and "rebase-merge/" contain all of the state for
1852+ * rebases, including some reference-like files. These are
1853+ * exclusively read and written via the filesystem and never go
1854+ * through the refdb.
1855+ *
1856+ * Writing or deleting references must consistently go either through
1857+ * the filesystem (special refs) or through the reference backend
1858+ * (normal ones).
1859+ */
1860+ static const char * const special_refs [] = {
1861+ "AUTO_MERGE" ,
1862+ "FETCH_HEAD" ,
1863+ "MERGE_AUTOSTASH" ,
1864+ "MERGE_HEAD" ,
1865+ };
1866+ size_t i ;
1867+
1868+ for (i = 0 ; i < ARRAY_SIZE (special_refs ); i ++ )
1869+ if (!strcmp (refname , special_refs [i ]))
1870+ return 1 ;
1871+
1872+ return 0 ;
1873+ }
1874+
18211875int refs_read_raw_ref (struct ref_store * ref_store , const char * refname ,
18221876 struct object_id * oid , struct strbuf * referent ,
18231877 unsigned int * type , int * failure_errno )
18241878{
18251879 assert (failure_errno );
1826- if (! strcmp (refname , "FETCH_HEAD" ) || ! strcmp ( refname , "MERGE_HEAD" )) {
1880+ if (is_special_ref (refname ))
18271881 return refs_read_special_head (ref_store , refname , oid , referent ,
18281882 type , failure_errno );
1829- }
18301883
18311884 return ref_store -> be -> read_raw_ref (ref_store , refname , oid , referent ,
18321885 type , failure_errno );
0 commit comments