@@ -269,9 +269,10 @@ char *refs_resolve_refdup(struct ref_store *refs,
269
269
struct object_id * oid , int * flags )
270
270
{
271
271
const char * result ;
272
+ int ignore_errno ;
272
273
273
274
result = refs_resolve_ref_unsafe (refs , refname , resolve_flags ,
274
- oid , flags );
275
+ oid , flags , & ignore_errno );
275
276
return xstrdup_or_null (result );
276
277
}
277
278
@@ -291,28 +292,27 @@ struct ref_filter {
291
292
void * cb_data ;
292
293
};
293
294
294
- int refs_read_ref_full (struct ref_store * refs , const char * refname ,
295
- int resolve_flags , struct object_id * oid , int * flags )
295
+ int read_ref_full (const char * refname , int resolve_flags , struct object_id * oid , int * flags )
296
296
{
297
- if (refs_resolve_ref_unsafe (refs , refname , resolve_flags , oid , flags ))
297
+ int ignore_errno ;
298
+ struct ref_store * refs = get_main_ref_store (the_repository );
299
+
300
+ if (refs_resolve_ref_unsafe (refs , refname , resolve_flags ,
301
+ oid , flags , & ignore_errno ))
298
302
return 0 ;
299
303
return -1 ;
300
304
}
301
305
302
- int read_ref_full (const char * refname , int resolve_flags , struct object_id * oid , int * flags )
303
- {
304
- return refs_read_ref_full (get_main_ref_store (the_repository ), refname ,
305
- resolve_flags , oid , flags );
306
- }
307
-
308
306
int read_ref (const char * refname , struct object_id * oid )
309
307
{
310
308
return read_ref_full (refname , RESOLVE_REF_READING , oid , NULL );
311
309
}
312
310
313
311
int refs_ref_exists (struct ref_store * refs , const char * refname )
314
312
{
315
- return !!refs_resolve_ref_unsafe (refs , refname , RESOLVE_REF_READING , NULL , NULL );
313
+ int ignore_errno ;
314
+ return !!refs_resolve_ref_unsafe (refs , refname , RESOLVE_REF_READING ,
315
+ NULL , NULL , & ignore_errno );
316
316
}
317
317
318
318
int ref_exists (const char * refname )
@@ -655,13 +655,16 @@ int expand_ref(struct repository *repo, const char *str, int len,
655
655
struct object_id oid_from_ref ;
656
656
struct object_id * this_result ;
657
657
int flag ;
658
+ struct ref_store * refs = get_main_ref_store (repo );
659
+ int ignore_errno ;
658
660
659
661
this_result = refs_found ? & oid_from_ref : oid ;
660
662
strbuf_reset (& fullref );
661
663
strbuf_addf (& fullref , * p , len , str );
662
- r = refs_resolve_ref_unsafe (get_main_ref_store (repo ),
663
- fullref .buf , RESOLVE_REF_READING ,
664
- this_result , & flag );
664
+ r = refs_resolve_ref_unsafe (refs , fullref .buf ,
665
+ RESOLVE_REF_READING ,
666
+ this_result , & flag ,
667
+ & ignore_errno );
665
668
if (r ) {
666
669
if (!refs_found ++ )
667
670
* ref = xstrdup (r );
@@ -690,12 +693,14 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
690
693
for (p = ref_rev_parse_rules ; * p ; p ++ ) {
691
694
struct object_id hash ;
692
695
const char * ref , * it ;
696
+ int ignore_errno ;
693
697
694
698
strbuf_reset (& path );
695
699
strbuf_addf (& path , * p , len , str );
696
700
ref = refs_resolve_ref_unsafe (refs , path .buf ,
697
701
RESOLVE_REF_READING ,
698
- oid ? & hash : NULL , NULL );
702
+ oid ? & hash : NULL , NULL ,
703
+ & ignore_errno );
699
704
if (!ref )
700
705
continue ;
701
706
if (refs_reflog_exists (refs , path .buf ))
@@ -1373,32 +1378,14 @@ const char *find_descendant_ref(const char *dirname,
1373
1378
return NULL ;
1374
1379
}
1375
1380
1376
- int refs_rename_ref_available (struct ref_store * refs ,
1377
- const char * old_refname ,
1378
- const char * new_refname )
1379
- {
1380
- struct string_list skip = STRING_LIST_INIT_NODUP ;
1381
- struct strbuf err = STRBUF_INIT ;
1382
- int ok ;
1383
-
1384
- string_list_insert (& skip , old_refname );
1385
- ok = !refs_verify_refname_available (refs , new_refname ,
1386
- NULL , & skip , & err );
1387
- if (!ok )
1388
- error ("%s" , err .buf );
1389
-
1390
- string_list_clear (& skip , 0 );
1391
- strbuf_release (& err );
1392
- return ok ;
1393
- }
1394
-
1395
1381
int refs_head_ref (struct ref_store * refs , each_ref_fn fn , void * cb_data )
1396
1382
{
1397
1383
struct object_id oid ;
1398
1384
int flag ;
1385
+ int ignore_errno ;
1399
1386
1400
- if (! refs_read_ref_full (refs , "HEAD" , RESOLVE_REF_READING ,
1401
- & oid , & flag ))
1387
+ if (refs_resolve_ref_unsafe (refs , "HEAD" , RESOLVE_REF_READING ,
1388
+ & oid , & flag , & ignore_errno ))
1402
1389
return fn ("HEAD" , & oid , flag , cb_data );
1403
1390
1404
1391
return 0 ;
@@ -1649,7 +1636,8 @@ int for_each_fullref_in_prefixes(const char *namespace,
1649
1636
1650
1637
static int refs_read_special_head (struct ref_store * ref_store ,
1651
1638
const char * refname , struct object_id * oid ,
1652
- struct strbuf * referent , unsigned int * type )
1639
+ struct strbuf * referent , unsigned int * type ,
1640
+ int * failure_errno )
1653
1641
{
1654
1642
struct strbuf full_path = STRBUF_INIT ;
1655
1643
struct strbuf content = STRBUF_INIT ;
@@ -1659,38 +1647,42 @@ static int refs_read_special_head(struct ref_store *ref_store,
1659
1647
if (strbuf_read_file (& content , full_path .buf , 0 ) < 0 )
1660
1648
goto done ;
1661
1649
1662
- result = parse_loose_ref_contents (content .buf , oid , referent , type );
1650
+ result = parse_loose_ref_contents (content .buf , oid , referent , type ,
1651
+ failure_errno );
1663
1652
1664
1653
done :
1665
1654
strbuf_release (& full_path );
1666
1655
strbuf_release (& content );
1667
1656
return result ;
1668
1657
}
1669
1658
1670
- int refs_read_raw_ref (struct ref_store * ref_store ,
1671
- const char * refname , struct object_id * oid ,
1672
- struct strbuf * referent , unsigned int * type )
1659
+ int refs_read_raw_ref (struct ref_store * ref_store , const char * refname ,
1660
+ struct object_id * oid , struct strbuf * referent ,
1661
+ unsigned int * type , int * failure_errno )
1673
1662
{
1663
+ assert (failure_errno );
1674
1664
if (!strcmp (refname , "FETCH_HEAD" ) || !strcmp (refname , "MERGE_HEAD" )) {
1675
1665
return refs_read_special_head (ref_store , refname , oid , referent ,
1676
- type );
1666
+ type , failure_errno );
1677
1667
}
1678
1668
1679
1669
return ref_store -> be -> read_raw_ref (ref_store , refname , oid , referent ,
1680
- type , & errno );
1670
+ type , failure_errno );
1681
1671
}
1682
1672
1683
- /* This function needs to return a meaningful errno on failure */
1684
1673
const char * refs_resolve_ref_unsafe (struct ref_store * refs ,
1685
1674
const char * refname ,
1686
1675
int resolve_flags ,
1687
- struct object_id * oid , int * flags )
1676
+ struct object_id * oid ,
1677
+ int * flags , int * failure_errno )
1688
1678
{
1689
1679
static struct strbuf sb_refname = STRBUF_INIT ;
1690
1680
struct object_id unused_oid ;
1691
1681
int unused_flags ;
1692
1682
int symref_count ;
1693
1683
1684
+ assert (failure_errno );
1685
+
1694
1686
if (!oid )
1695
1687
oid = & unused_oid ;
1696
1688
if (!flags )
@@ -1701,7 +1693,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1701
1693
if (check_refname_format (refname , REFNAME_ALLOW_ONELEVEL )) {
1702
1694
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME ) ||
1703
1695
!refname_is_safe (refname )) {
1704
- errno = EINVAL ;
1696
+ * failure_errno = EINVAL ;
1705
1697
return NULL ;
1706
1698
}
1707
1699
@@ -1719,9 +1711,11 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1719
1711
for (symref_count = 0 ; symref_count < SYMREF_MAXDEPTH ; symref_count ++ ) {
1720
1712
unsigned int read_flags = 0 ;
1721
1713
1722
- if (refs_read_raw_ref (refs , refname ,
1723
- oid , & sb_refname , & read_flags )) {
1714
+ if (refs_read_raw_ref (refs , refname , oid , & sb_refname ,
1715
+ & read_flags , failure_errno )) {
1724
1716
* flags |= read_flags ;
1717
+ if (errno )
1718
+ * failure_errno = errno ;
1725
1719
1726
1720
/* In reading mode, refs must eventually resolve */
1727
1721
if (resolve_flags & RESOLVE_REF_READING )
@@ -1732,9 +1726,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1732
1726
* may show errors besides ENOENT if there are
1733
1727
* similarly-named refs.
1734
1728
*/
1735
- if (errno != ENOENT &&
1736
- errno != EISDIR &&
1737
- errno != ENOTDIR )
1729
+ if (* failure_errno != ENOENT &&
1730
+ * failure_errno != EISDIR &&
1731
+ * failure_errno != ENOTDIR )
1738
1732
return NULL ;
1739
1733
1740
1734
oidclr (oid );
@@ -1761,15 +1755,15 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1761
1755
if (check_refname_format (refname , REFNAME_ALLOW_ONELEVEL )) {
1762
1756
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME ) ||
1763
1757
!refname_is_safe (refname )) {
1764
- errno = EINVAL ;
1758
+ * failure_errno = EINVAL ;
1765
1759
return NULL ;
1766
1760
}
1767
1761
1768
1762
* flags |= REF_ISBROKEN | REF_BAD_NAME ;
1769
1763
}
1770
1764
}
1771
1765
1772
- errno = ELOOP ;
1766
+ * failure_errno = ELOOP ;
1773
1767
return NULL ;
1774
1768
}
1775
1769
@@ -1784,23 +1778,26 @@ int refs_init_db(struct strbuf *err)
1784
1778
const char * resolve_ref_unsafe (const char * refname , int resolve_flags ,
1785
1779
struct object_id * oid , int * flags )
1786
1780
{
1781
+ int ignore_errno ;
1782
+
1787
1783
return refs_resolve_ref_unsafe (get_main_ref_store (the_repository ), refname ,
1788
- resolve_flags , oid , flags );
1784
+ resolve_flags , oid , flags , & ignore_errno );
1789
1785
}
1790
1786
1791
1787
int resolve_gitlink_ref (const char * submodule , const char * refname ,
1792
1788
struct object_id * oid )
1793
1789
{
1794
1790
struct ref_store * refs ;
1795
1791
int flags ;
1792
+ int ignore_errno ;
1796
1793
1797
1794
refs = get_submodule_ref_store (submodule );
1798
1795
1799
1796
if (!refs )
1800
1797
return -1 ;
1801
1798
1802
- if (!refs_resolve_ref_unsafe (refs , refname , 0 , oid , & flags ) ||
1803
- is_null_oid (oid ))
1799
+ if (!refs_resolve_ref_unsafe (refs , refname , 0 , oid , & flags ,
1800
+ & ignore_errno ) || is_null_oid (oid ))
1804
1801
return -1 ;
1805
1802
return 0 ;
1806
1803
}
@@ -2102,8 +2099,11 @@ static int run_transaction_hook(struct ref_transaction *transaction,
2102
2099
update -> refname );
2103
2100
2104
2101
if (write_in_full (proc .in , buf .buf , buf .len ) < 0 ) {
2105
- if (errno != EPIPE )
2102
+ if (errno != EPIPE ) {
2103
+ /* Don't leak errno outside this API */
2104
+ errno = 0 ;
2106
2105
ret = -1 ;
2106
+ }
2107
2107
break ;
2108
2108
}
2109
2109
}
@@ -2238,6 +2238,13 @@ int refs_verify_refname_available(struct ref_store *refs,
2238
2238
2239
2239
strbuf_grow (& dirname , strlen (refname ) + 1 );
2240
2240
for (slash = strchr (refname , '/' ); slash ; slash = strchr (slash + 1 , '/' )) {
2241
+ /*
2242
+ * Just saying "Is a directory" when we e.g. can't
2243
+ * lock some multi-level ref isn't very informative,
2244
+ * the user won't be told *what* is a directory, so
2245
+ * let's not use strerror() below.
2246
+ */
2247
+ int ignore_errno ;
2241
2248
/* Expand dirname to the new prefix, not including the trailing slash: */
2242
2249
strbuf_add (& dirname , refname + dirname .len , slash - refname - dirname .len );
2243
2250
@@ -2249,7 +2256,8 @@ int refs_verify_refname_available(struct ref_store *refs,
2249
2256
if (skip && string_list_has_string (skip , dirname .buf ))
2250
2257
continue ;
2251
2258
2252
- if (!refs_read_raw_ref (refs , dirname .buf , & oid , & referent , & type )) {
2259
+ if (!refs_read_raw_ref (refs , dirname .buf , & oid , & referent ,
2260
+ & type , & ignore_errno )) {
2253
2261
strbuf_addf (err , _ ("'%s' exists; cannot create '%s'" ),
2254
2262
dirname .buf , refname );
2255
2263
goto cleanup ;
0 commit comments