refs: add new ref-store api

This is not meant to cover all existing API. It adds enough to test ref stores with the new test program test-ref-store, coming soon and to be used by files-backend.c. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 26, 2017 at 09:42 UTC 7d2df051d05bc65a0a540ba080f07b421b4d99d7
4 files changed +270 -99
refs.c
+186 -65
@@ -171,11 +171,23 @@ int refname_is_safe(const char *refname)
171 return 1;
172 }
173
174 +char *refs_resolve_refdup(struct ref_store *refs,
175 + const char *refname, int resolve_flags,
176 + unsigned char *sha1, int *flags)
177 +{
178 + const char *result;
179 +
180 + result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
181 + sha1, flags);
182 + return xstrdup_or_null(result);
183 +}
184 +
185 char *resolve_refdup(const char *refname, int resolve_flags,
186 unsigned char *sha1, int *flags)
187 {
177 - return xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,
178 - sha1, flags));
188 + return refs_resolve_refdup(get_main_ref_store(),
189 + refname, resolve_flags,
190 + sha1, flags);
191 }
192
193 /* The argument to filter_refs */
@@ -185,13 +197,20 @@ struct ref_filter {
197 void *cb_data;
198 };
199
188 -int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
200 +int refs_read_ref_full(struct ref_store *refs, const char *refname,
201 + int resolve_flags, unsigned char *sha1, int *flags)
202 {
190 - if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags))
203 + if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, sha1, flags))
204 return 0;
205 return -1;
206 }
207
208 +int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
209 +{
210 + return refs_read_ref_full(get_main_ref_store(), refname,
211 + resolve_flags, sha1, flags);
212 +}
213 +
214 int read_ref(const char *refname, unsigned char *sha1)
215 {
216 return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
@@ -286,34 +305,52 @@ void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_li
305 for_each_rawref(warn_if_dangling_symref, &data);
306 }
307
308 +int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
309 +{
310 + return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
311 +}
312 +
313 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
314 {
291 - return for_each_ref_in("refs/tags/", fn, cb_data);
315 + return refs_for_each_tag_ref(get_main_ref_store(), fn, cb_data);
316 }
317
318 int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
319 {
296 - return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
320 + return refs_for_each_tag_ref(get_submodule_ref_store(submodule),
321 + fn, cb_data);
322 +}
323 +
324 +int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
325 +{
326 + return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
327 }
328
329 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
330 {
301 - return for_each_ref_in("refs/heads/", fn, cb_data);
331 + return refs_for_each_branch_ref(get_main_ref_store(), fn, cb_data);
332 }
333
334 int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
335 {
306 - return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
336 + return refs_for_each_branch_ref(get_submodule_ref_store(submodule),
337 + fn, cb_data);
338 +}
339 +
340 +int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
341 +{
342 + return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
343 }
344
345 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
346 {
311 - return for_each_ref_in("refs/remotes/", fn, cb_data);
347 + return refs_for_each_remote_ref(get_main_ref_store(), fn, cb_data);
348 }
349
350 int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
351 {
316 - return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
352 + return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
353 + fn, cb_data);
354 }
355
356 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
@@ -1120,14 +1157,17 @@ const char *find_descendant_ref(const char *dirname,
1157 return NULL;
1158 }
1159
1123 -int rename_ref_available(const char *old_refname, const char *new_refname)
1160 +int refs_rename_ref_available(struct ref_store *refs,
1161 + const char *old_refname,
1162 + const char *new_refname)
1163 {
1164 struct string_list skip = STRING_LIST_INIT_NODUP;
1165 struct strbuf err = STRBUF_INIT;
1166 int ok;
1167
1168 string_list_insert(&skip, old_refname);
1130 - ok = !verify_refname_available(new_refname, NULL, &skip, &err);
1169 + ok = !refs_verify_refname_available(refs, new_refname,
1170 + NULL, &skip, &err);
1171 if (!ok)
1172 error("%s", err.buf);
1173
@@ -1168,10 +1208,9 @@ int head_ref(each_ref_fn fn, void *cb_data)
1208 * non-zero value, stop the iteration and return that value;
1209 * otherwise, return 0.
1210 */
1171 -static int do_for_each_ref(const char *submodule, const char *prefix,
1211 +static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1212 each_ref_fn fn, int trim, int flags, void *cb_data)
1213 {
1174 - struct ref_store *refs = get_submodule_ref_store(submodule);
1214 struct ref_iterator *iter;
1215
1216 if (!refs)
@@ -1183,19 +1222,30 @@ static int do_for_each_ref(const char *submodule, const char *prefix,
1222 return do_for_each_ref_iterator(iter, fn, cb_data);
1223 }
1224
1225 +int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1226 +{
1227 + return do_for_each_ref(refs, "", fn, 0, 0, cb_data);
1228 +}
1229 +
1230 int for_each_ref(each_ref_fn fn, void *cb_data)
1231 {
1188 - return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
1232 + return refs_for_each_ref(get_main_ref_store(), fn, cb_data);
1233 }
1234
1235 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1236 {
1193 - return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
1237 + return refs_for_each_ref(get_submodule_ref_store(submodule), fn, cb_data);
1238 +}
1239 +
1240 +int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1241 + each_ref_fn fn, void *cb_data)
1242 +{
1243 + return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data);
1244 }
1245
1246 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1247 {
1198 - return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
1248 + return refs_for_each_ref_in(get_main_ref_store(), prefix, fn, cb_data);
1249 }
1250
1251 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
@@ -1204,19 +1254,23 @@ int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsig
1254
1255 if (broken)
1256 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1207 - return do_for_each_ref(NULL, prefix, fn, 0, flag, cb_data);
1257 + return do_for_each_ref(get_main_ref_store(),
1258 + prefix, fn, 0, flag, cb_data);
1259 }
1260
1261 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1211 - each_ref_fn fn, void *cb_data)
1262 + each_ref_fn fn, void *cb_data)
1263 {
1213 - return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
1264 + return refs_for_each_ref_in(get_submodule_ref_store(submodule),
1265 + prefix, fn, cb_data);
1266 }
1267
1268 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1269 {
1218 - return do_for_each_ref(NULL, git_replace_ref_base, fn,
1219 - strlen(git_replace_ref_base), 0, cb_data);
1270 + return do_for_each_ref(get_main_ref_store(),
1271 + git_replace_ref_base, fn,
1272 + strlen(git_replace_ref_base),
1273 + 0, cb_data);
1274 }
1275
1276 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
@@ -1224,19 +1278,25 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1278 struct strbuf buf = STRBUF_INIT;
1279 int ret;
1280 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1227 - ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1281 + ret = do_for_each_ref(get_main_ref_store(),
1282 + buf.buf, fn, 0, 0, cb_data);
1283 strbuf_release(&buf);
1284 return ret;
1285 }
1286
1232 -int for_each_rawref(each_ref_fn fn, void *cb_data)
1287 +int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1288 {
1234 - return do_for_each_ref(NULL, "", fn, 0,
1289 + return do_for_each_ref(refs, "", fn, 0,
1290 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1291 }
1292
1293 +int for_each_rawref(each_ref_fn fn, void *cb_data)
1294 +{
1295 + return refs_for_each_rawref(get_main_ref_store(), fn, cb_data);
1296 +}
1297 +
1298 /* This function needs to return a meaningful errno on failure */
1239 -const char *resolve_ref_recursively(struct ref_store *refs,
1299 +const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1300 const char *refname,
1301 int resolve_flags,
1302 unsigned char *sha1, int *flags)
@@ -1323,7 +1383,7 @@ int refs_init_db(struct strbuf *err)
1383 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1384 unsigned char *sha1, int *flags)
1385 {
1326 - return resolve_ref_recursively(get_main_ref_store(), refname,
1386 + return refs_resolve_ref_unsafe(get_main_ref_store(), refname,
1387 resolve_flags, sha1, flags);
1388 }
1389
@@ -1353,7 +1413,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
1413 if (!refs)
1414 return -1;
1415
1356 - if (!resolve_ref_recursively(refs, refname, 0, sha1, &flags) ||
1416 + if (!refs_resolve_ref_unsafe(refs, refname, 0, sha1, &flags) ||
1417 is_null_sha1(sha1))
1418 return -1;
1419 return 0;
@@ -1506,27 +1566,42 @@ void base_ref_store_init(struct ref_store *refs,
1566 }
1567
1568 /* backend functions */
1569 +int refs_pack_refs(struct ref_store *refs, unsigned int flags)
1570 +{
1571 + return refs->be->pack_refs(refs, flags);
1572 +}
1573 +
1574 int pack_refs(unsigned int flags)
1575 {
1511 - struct ref_store *refs = get_main_ref_store();
1576 + return refs_pack_refs(get_main_ref_store(), flags);
1577 +}
1578
1513 - return refs->be->pack_refs(refs, flags);
1579 +int refs_peel_ref(struct ref_store *refs, const char *refname,
1580 + unsigned char *sha1)
1581 +{
1582 + return refs->be->peel_ref(refs, refname, sha1);
1583 }
1584
1585 int peel_ref(const char *refname, unsigned char *sha1)
1586 {
1518 - struct ref_store *refs = get_main_ref_store();
1587 + return refs_peel_ref(get_main_ref_store(), refname, sha1);
1588 +}
1589
1520 - return refs->be->peel_ref(refs, refname, sha1);
1590 +int refs_create_symref(struct ref_store *refs,
1591 + const char *ref_target,
1592 + const char *refs_heads_master,
1593 + const char *logmsg)
1594 +{
1595 + return refs->be->create_symref(refs, ref_target,
1596 + refs_heads_master,
1597 + logmsg);
1598 }
1599
1600 int create_symref(const char *ref_target, const char *refs_heads_master,
1601 const char *logmsg)
1602 {
1526 - struct ref_store *refs = get_main_ref_store();
1527 -
1528 - return refs->be->create_symref(refs, ref_target, refs_heads_master,
1529 - logmsg);
1603 + return refs_create_symref(get_main_ref_store(), ref_target,
1604 + refs_heads_master, logmsg);
1605 }
1606
1607 int ref_transaction_commit(struct ref_transaction *transaction,
@@ -1537,19 +1612,17 @@ int ref_transaction_commit(struct ref_transaction *transaction,
1612 return refs->be->transaction_commit(refs, transaction, err);
1613 }
1614
1540 -int verify_refname_available(const char *refname,
1541 - const struct string_list *extra,
1542 - const struct string_list *skip,
1543 - struct strbuf *err)
1615 +int refs_verify_refname_available(struct ref_store *refs,
1616 + const char *refname,
1617 + const struct string_list *extra,
1618 + const struct string_list *skip,
1619 + struct strbuf *err)
1620 {
1545 - struct ref_store *refs = get_main_ref_store();
1546 -
1621 return refs->be->verify_refname_available(refs, refname, extra, skip, err);
1622 }
1623
1550 -int for_each_reflog(each_ref_fn fn, void *cb_data)
1624 +int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1625 {
1552 - struct ref_store *refs = get_main_ref_store();
1626 struct ref_iterator *iter;
1627
1628 iter = refs->be->reflog_iterator_begin(refs);
@@ -1557,43 +1630,84 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
1630 return do_for_each_ref_iterator(iter, fn, cb_data);
1631 }
1632
1560 -int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
1561 - void *cb_data)
1633 +int for_each_reflog(each_ref_fn fn, void *cb_data)
1634 {
1563 - struct ref_store *refs = get_main_ref_store();
1635 + return refs_for_each_reflog(get_main_ref_store(), fn, cb_data);
1636 +}
1637
1638 +int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
1639 + const char *refname,
1640 + each_reflog_ent_fn fn,
1641 + void *cb_data)
1642 +{
1643 return refs->be->for_each_reflog_ent_reverse(refs, refname,
1644 fn, cb_data);
1645 }
1646
1647 +int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
1648 + void *cb_data)
1649 +{
1650 + return refs_for_each_reflog_ent_reverse(get_main_ref_store(),
1651 + refname, fn, cb_data);
1652 +}
1653 +
1654 +int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
1655 + each_reflog_ent_fn fn, void *cb_data)
1656 +{
1657 + return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
1658 +}
1659 +
1660 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
1661 void *cb_data)
1662 {
1572 - struct ref_store *refs = get_main_ref_store();
1663 + return refs_for_each_reflog_ent(get_main_ref_store(), refname,
1664 + fn, cb_data);
1665 +}
1666
1574 - return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
1667 +int refs_reflog_exists(struct ref_store *refs, const char *refname)
1668 +{
1669 + return refs->be->reflog_exists(refs, refname);
1670 }
1671
1672 int reflog_exists(const char *refname)
1673 {
1579 - struct ref_store *refs = get_main_ref_store();
1674 + return refs_reflog_exists(get_main_ref_store(), refname);
1675 +}
1676
1581 - return refs->be->reflog_exists(refs, refname);
1677 +int refs_create_reflog(struct ref_store *refs, const char *refname,
1678 + int force_create, struct strbuf *err)
1679 +{
1680 + return refs->be->create_reflog(refs, refname, force_create, err);
1681 }
1682
1683 int safe_create_reflog(const char *refname, int force_create,
1684 struct strbuf *err)
1685 {
1587 - struct ref_store *refs = get_main_ref_store();
1686 + return refs_create_reflog(get_main_ref_store(), refname,
1687 + force_create, err);
1688 +}
1689
1589 - return refs->be->create_reflog(refs, refname, force_create, err);
1690 +int refs_delete_reflog(struct ref_store *refs, const char *refname)
1691 +{
1692 + return refs->be->delete_reflog(refs, refname);
1693 }
1694
1695 int delete_reflog(const char *refname)
1696 {
1594 - struct ref_store *refs = get_main_ref_store();
1697 + return refs_delete_reflog(get_main_ref_store(), refname);
1698 +}
1699
1596 - return refs->be->delete_reflog(refs, refname);
1700 +int refs_reflog_expire(struct ref_store *refs,
1701 + const char *refname, const unsigned char *sha1,
1702 + unsigned int flags,
1703 + reflog_expiry_prepare_fn prepare_fn,
1704 + reflog_expiry_should_prune_fn should_prune_fn,
1705 + reflog_expiry_cleanup_fn cleanup_fn,
1706 + void *policy_cb_data)
1707 +{
1708 + return refs->be->reflog_expire(refs, refname, sha1, flags,
1709 + prepare_fn, should_prune_fn,
1710 + cleanup_fn, policy_cb_data);
1711 }
1712
1713 int reflog_expire(const char *refname, const unsigned char *sha1,
@@ -1603,11 +1717,10 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
1717 reflog_expiry_cleanup_fn cleanup_fn,
1718 void *policy_cb_data)
1719 {
1606 - struct ref_store *refs = get_main_ref_store();
1607 -
1608 - return refs->be->reflog_expire(refs, refname, sha1, flags,
1609 - prepare_fn, should_prune_fn,
1610 - cleanup_fn, policy_cb_data);
1720 + return refs_reflog_expire(get_main_ref_store(),
1721 + refname, sha1, flags,
1722 + prepare_fn, should_prune_fn,
1723 + cleanup_fn, policy_cb_data);
1724 }
1725
1726 int initial_ref_transaction_commit(struct ref_transaction *transaction,
@@ -1618,16 +1731,24 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
1731 return refs->be->initial_transaction_commit(refs, transaction, err);
1732 }
1733
1621 -int delete_refs(struct string_list *refnames, unsigned int flags)
1734 +int refs_delete_refs(struct ref_store *refs, struct string_list *refnames,
1735 + unsigned int flags)
1736 {
1623 - struct ref_store *refs = get_main_ref_store();
1624 -
1737 return refs->be->delete_refs(refs, refnames, flags);
1738 }
1739
1628 -int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1740 +int delete_refs(struct string_list *refnames, unsigned int flags)
1741 {
1630 - struct ref_store *refs = get_main_ref_store();
1742 + return refs_delete_refs(get_main_ref_store(), refnames, flags);
1743 +}
1744
1745 +int refs_rename_ref(struct ref_store *refs, const char *oldref,
1746 + const char *newref, const char *logmsg)
1747 +{
1748 return refs->be->rename_ref(refs, oldref, newref, logmsg);
1749 }
1750 +
1751 +int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1752 +{
1753 + return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg);
1754 +}
refs.h
+74
@@ -57,16 +57,50 @@ struct string_list;
57 #define RESOLVE_REF_NO_RECURSE 0x02
58 #define RESOLVE_REF_ALLOW_BAD_NAME 0x04
59
60 +const char *refs_resolve_ref_unsafe(struct ref_store *refs,
61 + const char *refname,
62 + int resolve_flags,
63 + unsigned char *sha1,
64 + int *flags);
65 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
66 unsigned char *sha1, int *flags);
67
68 +char *refs_resolve_refdup(struct ref_store *refs,
69 + const char *refname, int resolve_flags,
70 + unsigned char *sha1, int *flags);
71 char *resolve_refdup(const char *refname, int resolve_flags,
72 unsigned char *sha1, int *flags);
73
74 +int refs_read_ref_full(struct ref_store *refs, const char *refname,
75 + int resolve_flags, unsigned char *sha1, int *flags);
76 int read_ref_full(const char *refname, int resolve_flags,
77 unsigned char *sha1, int *flags);
78 int read_ref(const char *refname, unsigned char *sha1);
79
80 +/*
81 + * Return 0 if a reference named refname could be created without
82 + * conflicting with the name of an existing reference. Otherwise,
83 + * return a negative value and write an explanation to err. If extras
84 + * is non-NULL, it is a list of additional refnames with which refname
85 + * is not allowed to conflict. If skip is non-NULL, ignore potential
86 + * conflicts with refs in skip (e.g., because they are scheduled for
87 + * deletion in the same operation). Behavior is undefined if the same
88 + * name is listed in both extras and skip.
89 + *
90 + * Two reference names conflict if one of them exactly matches the
91 + * leading components of the other; e.g., "foo/bar" conflicts with
92 + * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
93 + * "foo/barbados".
94 + *
95 + * extras and skip must be sorted.
96 + */
97 +
98 +int refs_verify_refname_available(struct ref_store *refs,
99 + const char *refname,
100 + const struct string_list *extra,
101 + const struct string_list *skip,
102 + struct strbuf *err);
103 +
104 int ref_exists(const char *refname);
105
106 int should_autocreate_reflog(const char *refname);
@@ -83,6 +117,8 @@ extern int refs_init_db(struct strbuf *err);
117 * Symbolic references are considered unpeelable, even if they
118 * ultimately resolve to a peelable tag.
119 */
120 +int refs_peel_ref(struct ref_store *refs, const char *refname,
121 + unsigned char *sha1);
122 int peel_ref(const char *refname, unsigned char *sha1);
123
124 /**
@@ -196,6 +232,17 @@ typedef int each_ref_fn(const char *refname,
232 * modifies the reference also returns a nonzero value to immediately
233 * stop the iteration.
234 */
235 +int refs_for_each_ref(struct ref_store *refs,
236 + each_ref_fn fn, void *cb_data);
237 +int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
238 + each_ref_fn fn, void *cb_data);
239 +int refs_for_each_tag_ref(struct ref_store *refs,
240 + each_ref_fn fn, void *cb_data);
241 +int refs_for_each_branch_ref(struct ref_store *refs,
242 + each_ref_fn fn, void *cb_data);
243 +int refs_for_each_remote_ref(struct ref_store *refs,
244 + each_ref_fn fn, void *cb_data);
245 +
246 int head_ref(each_ref_fn fn, void *cb_data);
247 int for_each_ref(each_ref_fn fn, void *cb_data);
248 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
@@ -225,6 +272,7 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data);
272 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
273
274 /* can be used to learn about broken ref and symref */
275 +int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
276 int for_each_rawref(each_ref_fn fn, void *cb_data);
277
278 static inline const char *has_glob_specials(const char *pattern)
@@ -248,6 +296,7 @@ void warn_dangling_symrefs(FILE *fp, const char *msg_fmt,
296 * Write a packed-refs file for the current repository.
297 * flags: Combination of the above PACK_REFS_* flags.
298 */
299 +int refs_pack_refs(struct ref_store *refs, unsigned int flags);
300 int pack_refs(unsigned int flags);
301
302 /*
@@ -263,6 +312,8 @@ int pack_refs(unsigned int flags);
312 /*
313 * Setup reflog before using. Fill in err and return -1 on failure.
314 */
315 +int refs_create_reflog(struct ref_store *refs, const char *refname,
316 + int force_create, struct strbuf *err);
317 int safe_create_reflog(const char *refname, int force_create, struct strbuf *err);
318
319 /** Reads log for the value of ref during at_time. **/
@@ -272,6 +323,7 @@ int read_ref_at(const char *refname, unsigned int flags,
323 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
324
325 /** Check if a particular reflog exists */
326 +int refs_reflog_exists(struct ref_store *refs, const char *refname);
327 int reflog_exists(const char *refname);
328
329 /*
@@ -290,9 +342,12 @@ int delete_ref(const char *msg, const char *refname,
342 * an all-or-nothing transaction). flags is passed through to
343 * ref_transaction_delete().
344 */
345 +int refs_delete_refs(struct ref_store *refs, struct string_list *refnames,
346 + unsigned int flags);
347 int delete_refs(struct string_list *refnames, unsigned int flags);
348
349 /** Delete a reflog */
350 +int refs_delete_reflog(struct ref_store *refs, const char *refname);
351 int delete_reflog(const char *refname);
352
353 /* iterate over reflog entries */
@@ -301,6 +356,12 @@ typedef int each_reflog_ent_fn(
356 const char *committer, unsigned long timestamp,
357 int tz, const char *msg, void *cb_data);
358
359 +int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
360 + each_reflog_ent_fn fn, void *cb_data);
361 +int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
362 + const char *refname,
363 + each_reflog_ent_fn fn,
364 + void *cb_data);
365 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);
366 int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data);
367
@@ -308,6 +369,7 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
369 * Calls the specified function for each reflog file until it returns nonzero,
370 * and returns the value
371 */
372 +int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data);
373 int for_each_reflog(each_ref_fn fn, void *cb_data);
374
375 #define REFNAME_ALLOW_ONELEVEL 1
@@ -328,8 +390,12 @@ const char *prettify_refname(const char *refname);
390 char *shorten_unambiguous_ref(const char *refname, int strict);
391
392 /** rename ref, return 0 on success **/
393 +int refs_rename_ref(struct ref_store *refs, const char *oldref,
394 + const char *newref, const char *logmsg);
395 int rename_ref(const char *oldref, const char *newref, const char *logmsg);
396
397 +int refs_create_symref(struct ref_store *refs, const char *refname,
398 + const char *target, const char *logmsg);
399 int create_symref(const char *refname, const char *target, const char *logmsg);
400
401 /*
@@ -552,6 +618,14 @@ typedef void reflog_expiry_cleanup_fn(void *cb_data);
618 * enum expire_reflog_flags. The three function pointers are described
619 * above. On success, return zero.
620 */
621 +int refs_reflog_expire(struct ref_store *refs,
622 + const char *refname,
623 + const unsigned char *sha1,
624 + unsigned int flags,
625 + reflog_expiry_prepare_fn prepare_fn,
626 + reflog_expiry_should_prune_fn should_prune_fn,
627 + reflog_expiry_cleanup_fn cleanup_fn,
628 + void *policy_cb_data);
629 int reflog_expire(const char *refname, const unsigned char *sha1,
630 unsigned int flags,
631 reflog_expiry_prepare_fn prepare_fn,
refs/files-backend.c
+7 -6
@@ -1313,7 +1313,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
1313 create_dir_entry(refs, refname.buf,
1314 refname.len, 1));
1315 } else {
1316 - if (!resolve_ref_recursively(&refs->base,
1316 + if (!refs_resolve_ref_unsafe(&refs->base,
1317 refname.buf,
1318 RESOLVE_REF_READING,
1319 sha1, &flag)) {
@@ -1622,7 +1622,8 @@ retry:
1622 * another reference such as "refs/foo". There is no
1623 * reason to expect this error to be transitory.
1624 */
1625 - if (verify_refname_available(refname, extras, skip, err)) {
1625 + if (refs_verify_refname_available(&refs->base, refname,
1626 + extras, skip, err)) {
1627 if (mustexist) {
1628 /*
1629 * To the user the relevant error is
@@ -2670,7 +2671,7 @@ static int files_rename_ref(struct ref_store *ref_store,
2671 oldrefname);
2672 goto out;
2673 }
2673 - if (!rename_ref_available(oldrefname, newrefname)) {
2674 + if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
2675 ret = 1;
2676 goto out;
2677 }
@@ -4054,9 +4055,9 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
4055 if ((update->flags & REF_HAVE_OLD) &&
4056 !is_null_sha1(update->old_sha1))
4057 die("BUG: initial ref transaction with old_sha1 set");
4057 - if (verify_refname_available(update->refname,
4058 - &affected_refnames, NULL,
4059 - err)) {
4058 + if (refs_verify_refname_available(&refs->base, update->refname,
4059 + &affected_refnames, NULL,
4060 + err)) {
4061 ret = TRANSACTION_NAME_CONFLICT;
4062 goto cleanup;
4063 }
refs/refs-internal.h
+3 -28
@@ -111,28 +111,6 @@ enum peel_status {
111 */
112 enum peel_status peel_object(const unsigned char *name, unsigned char *sha1);
113
114 -/*
115 - * Return 0 if a reference named refname could be created without
116 - * conflicting with the name of an existing reference. Otherwise,
117 - * return a negative value and write an explanation to err. If extras
118 - * is non-NULL, it is a list of additional refnames with which refname
119 - * is not allowed to conflict. If skip is non-NULL, ignore potential
120 - * conflicts with refs in skip (e.g., because they are scheduled for
121 - * deletion in the same operation). Behavior is undefined if the same
122 - * name is listed in both extras and skip.
123 - *
124 - * Two reference names conflict if one of them exactly matches the
125 - * leading components of the other; e.g., "foo/bar" conflicts with
126 - * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
127 - * "foo/barbados".
128 - *
129 - * extras and skip must be sorted.
130 - */
131 -int verify_refname_available(const char *newname,
132 - const struct string_list *extras,
133 - const struct string_list *skip,
134 - struct strbuf *err);
135 -
114 /*
115 * Copy the reflog message msg to buf, which has been allocated sufficiently
116 * large, while cleaning up the whitespaces. Especially, convert LF to space,
@@ -252,7 +230,9 @@ const char *find_descendant_ref(const char *dirname,
230 * processes (though rename_ref() catches some races that might get by
231 * this check).
232 */
255 -int rename_ref_available(const char *old_refname, const char *new_refname);
233 +int refs_rename_ref_available(struct ref_store *refs,
234 + const char *old_refname,
235 + const char *new_refname);
236
237 /* We allow "recursive" symbolic refs. Only within reason, though */
238 #define SYMREF_MAXDEPTH 5
@@ -646,9 +626,4 @@ struct ref_store {
626 void base_ref_store_init(struct ref_store *refs,
627 const struct ref_storage_be *be);
628
649 -const char *resolve_ref_recursively(struct ref_store *refs,
650 - const char *refname,
651 - int resolve_flags,
652 - unsigned char *sha1, int *flags);
653 -
629 #endif /* REFS_REFS_INTERNAL_H */