refs: move resolve_ref_unsafe into common code
Now that resolve_ref_unsafe's only interaction with the backend is through read_raw_ref, we can move it into the common code. Later, we'll replace read_raw_ref with a backend function. Signed-off-by: David Turner <dturner@twopensource.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
David Turner committed
Apr 7, 2016 at 15:03 UTC
2d0663b21661f2677bce5742204ccdd73745a062
3 files changed
+83
-79
refs.c
+74
@@ -1155,3 +1155,77 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
1155
return do_for_each_ref(NULL, "", fn, 0,
1156
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1157
}
1158
+
1159
+/* This function needs to return a meaningful errno on failure */
1160
+const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1161
+ unsigned char *sha1, int *flags)
1162
+{
1163
+ static struct strbuf sb_refname = STRBUF_INIT;
1164
+ int unused_flags;
1165
+ int symref_count;
1166
+
1167
+ if (!flags)
1168
+ flags = &unused_flags;
1169
+
1170
+ *flags = 0;
1171
+
1172
+ if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1173
+ if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1174
+ !refname_is_safe(refname)) {
1175
+ errno = EINVAL;
1176
+ return NULL;
1177
+ }
1178
+
1179
+ /*
1180
+ * dwim_ref() uses REF_ISBROKEN to distinguish between
1181
+ * missing refs and refs that were present but invalid,
1182
+ * to complain about the latter to stderr.
1183
+ *
1184
+ * We don't know whether the ref exists, so don't set
1185
+ * REF_ISBROKEN yet.
1186
+ */
1187
+ *flags |= REF_BAD_NAME;
1188
+ }
1189
+
1190
+ for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1191
+ unsigned int read_flags = 0;
1192
+
1193
+ if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
1194
+ *flags |= read_flags;
1195
+ if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1196
+ return NULL;
1197
+ hashclr(sha1);
1198
+ if (*flags & REF_BAD_NAME)
1199
+ *flags |= REF_ISBROKEN;
1200
+ return refname;
1201
+ }
1202
+
1203
+ *flags |= read_flags;
1204
+
1205
+ if (!(read_flags & REF_ISSYMREF)) {
1206
+ if (*flags & REF_BAD_NAME) {
1207
+ hashclr(sha1);
1208
+ *flags |= REF_ISBROKEN;
1209
+ }
1210
+ return refname;
1211
+ }
1212
+
1213
+ refname = sb_refname.buf;
1214
+ if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1215
+ hashclr(sha1);
1216
+ return refname;
1217
+ }
1218
+ if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1219
+ if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1220
+ !refname_is_safe(refname)) {
1221
+ errno = EINVAL;
1222
+ return NULL;
1223
+ }
1224
+
1225
+ *flags |= REF_ISBROKEN | REF_BAD_NAME;
1226
+ }
1227
+ }
1228
+
1229
+ errno = ELOOP;
1230
+ return NULL;
1231
+}
refs/files-backend.c
+3
-79
@@ -1269,8 +1269,6 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)
1269
return get_ref_dir(refs->loose);
1270
}
1271
1272
-/* We allow "recursive" symbolic refs. Only within reason, though */
1273
-#define MAXDEPTH 5
1272
#define MAXREFLEN (1024)
1273
1274
/*
@@ -1300,7 +1298,7 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
1298
char buffer[128], *p;
1299
char *path;
1300
1303
- if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN)
1301
+ if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
1302
return -1;
1303
path = *refs->name
1304
? git_pathdup_submodule(refs->name, "%s", refname)
@@ -1420,8 +1418,8 @@ static int resolve_missing_loose_ref(const char *refname,
1418
* - in all other cases, symref will be untouched, and therefore
1419
* refname will still be valid and unchanged.
1420
*/
1423
-static int read_raw_ref(const char *refname, unsigned char *sha1,
1424
- struct strbuf *symref, unsigned int *flags)
1421
+int read_raw_ref(const char *refname, unsigned char *sha1,
1422
+ struct strbuf *symref, unsigned int *flags)
1423
{
1424
struct strbuf sb_contents = STRBUF_INIT;
1425
struct strbuf sb_path = STRBUF_INIT;
@@ -1538,80 +1536,6 @@ out:
1536
return ret;
1537
}
1538
1541
-/* This function needs to return a meaningful errno on failure */
1542
-const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1543
- unsigned char *sha1, int *flags)
1544
-{
1545
- static struct strbuf sb_refname = STRBUF_INIT;
1546
- int unused_flags;
1547
- int symref_count;
1548
-
1549
- if (!flags)
1550
- flags = &unused_flags;
1551
-
1552
- *flags = 0;
1553
-
1554
- if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1555
- if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1556
- !refname_is_safe(refname)) {
1557
- errno = EINVAL;
1558
- return NULL;
1559
- }
1560
-
1561
- /*
1562
- * dwim_ref() uses REF_ISBROKEN to distinguish between
1563
- * missing refs and refs that were present but invalid,
1564
- * to complain about the latter to stderr.
1565
- *
1566
- * We don't know whether the ref exists, so don't set
1567
- * REF_ISBROKEN yet.
1568
- */
1569
- *flags |= REF_BAD_NAME;
1570
- }
1571
-
1572
- for (symref_count = 0; symref_count < MAXDEPTH; symref_count++) {
1573
- unsigned int read_flags = 0;
1574
-
1575
- if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
1576
- *flags |= read_flags;
1577
- if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1578
- return NULL;
1579
- hashclr(sha1);
1580
- if (*flags & REF_BAD_NAME)
1581
- *flags |= REF_ISBROKEN;
1582
- return refname;
1583
- }
1584
-
1585
- *flags |= read_flags;
1586
-
1587
- if (!(read_flags & REF_ISSYMREF)) {
1588
- if (*flags & REF_BAD_NAME) {
1589
- hashclr(sha1);
1590
- *flags |= REF_ISBROKEN;
1591
- }
1592
- return refname;
1593
- }
1594
-
1595
- refname = sb_refname.buf;
1596
- if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1597
- hashclr(sha1);
1598
- return refname;
1599
- }
1600
- if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1601
- if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1602
- !refname_is_safe(refname)) {
1603
- errno = EINVAL;
1604
- return NULL;
1605
- }
1606
-
1607
- *flags |= REF_ISBROKEN | REF_BAD_NAME;
1608
- }
1609
- }
1610
-
1611
- errno = ELOOP;
1612
- return NULL;
1613
-}
1614
-
1539
/*
1540
* Peel the entry (if possible) and return its new peel_status. If
1541
* repeel is true, re-peel the entry even if there is an old peeled
refs/refs-internal.h
+6
@@ -197,6 +197,8 @@ const char *find_descendant_ref(const char *dirname,
197
198
int rename_ref_available(const char *oldname, const char *newname);
199
200
+/* We allow "recursive" symbolic refs. Only within reason, though */
201
+#define SYMREF_MAXDEPTH 5
202
203
/* Include broken references in a do_for_each_ref*() iteration: */
204
#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
@@ -206,4 +208,8 @@ int rename_ref_available(const char *oldname, const char *newname);
208
*/
209
int do_for_each_ref(const char *submodule, const char *base,
210
each_ref_fn fn, int trim, int flags, void *cb_data);
211
+
212
+int read_raw_ref(const char *refname, unsigned char *sha1,
213
+ struct strbuf *symref, unsigned int *flags);
214
+
215
#endif /* REFS_REFS_INTERNAL_H */