read_raw_ref(): take a (struct ref_store *) argument

And make the function work for submodules. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 4, 2016 at 18:08 UTC 34c7ad8ffc79c64d7f2261e6bcf6efd3adb16e7e
3 files changed +22 -9
refs.c
+3 -1
@@ -1222,6 +1222,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1222 static struct strbuf sb_refname = STRBUF_INIT;
1223 int unused_flags;
1224 int symref_count;
1225 + struct ref_store *refs = get_ref_store(NULL);
1226
1227 if (!flags)
1228 flags = &unused_flags;
@@ -1249,7 +1250,8 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1250 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1251 unsigned int read_flags = 0;
1252
1252 - if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
1253 + if (read_raw_ref(refs, refname,
1254 + sha1, &sb_refname, &read_flags)) {
1255 *flags |= read_flags;
1256 if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1257 return NULL;
refs/files-backend.c
+13 -5
@@ -1349,11 +1349,12 @@ static int resolve_packed_ref(struct files_ref_store *refs,
1349 return -1;
1350 }
1351
1352 -int read_raw_ref(const char *refname, unsigned char *sha1,
1352 +int read_raw_ref(struct ref_store *ref_store,
1353 + const char *refname, unsigned char *sha1,
1354 struct strbuf *referent, unsigned int *type)
1355 {
1356 struct files_ref_store *refs =
1356 - get_files_ref_store(NULL, "read_raw_ref");
1357 + files_downcast(ref_store, 1, "read_raw_ref");
1358 struct strbuf sb_contents = STRBUF_INIT;
1359 struct strbuf sb_path = STRBUF_INIT;
1360 const char *path;
@@ -1365,7 +1366,12 @@ int read_raw_ref(const char *refname, unsigned char *sha1,
1366
1367 *type = 0;
1368 strbuf_reset(&sb_path);
1368 - strbuf_git_path(&sb_path, "%s", refname);
1369 +
1370 + if (*refs->base.submodule)
1371 + strbuf_git_path_submodule(&sb_path, refs->base.submodule, "%s", refname);
1372 + else
1373 + strbuf_git_path(&sb_path, "%s", refname);
1374 +
1375 path = sb_path.buf;
1376
1377 stat_ref:
@@ -1592,8 +1598,9 @@ static int lock_raw_ref(const char *refname, int mustexist,
1598 unsigned int *type,
1599 struct strbuf *err)
1600 {
1601 + struct ref_store *ref_store = get_ref_store(NULL);
1602 struct files_ref_store *refs =
1596 - get_files_ref_store(NULL, "lock_raw_ref");
1603 + files_downcast(ref_store, 0, "lock_raw_ref");
1604 struct ref_lock *lock;
1605 struct strbuf ref_file = STRBUF_INIT;
1606 int attempts_remaining = 3;
@@ -1683,7 +1690,8 @@ retry:
1690 * fear that its value will change.
1691 */
1692
1686 - if (read_raw_ref(refname, lock->old_oid.hash, referent, type)) {
1693 + if (read_raw_ref(ref_store, refname,
1694 + lock->old_oid.hash, referent, type)) {
1695 if (errno == ENOENT) {
1696 if (mustexist) {
1697 /* Garden variety missing reference. */
refs/refs-internal.h
+6 -3
@@ -484,9 +484,11 @@ extern struct ref_iterator *current_ref_iter;
484 int do_for_each_ref_iterator(struct ref_iterator *iter,
485 each_ref_fn fn, void *cb_data);
486
487 +struct ref_store;
488 +
489 /*
488 - * Read the specified reference from the filesystem or packed refs
489 - * file, non-recursively. Set type to describe the reference, and:
490 + * Read a reference from the specified reference store, non-recursively.
491 + * Set type to describe the reference, and:
492 *
493 * - If refname is the name of a normal reference, fill in sha1
494 * (leaving referent unchanged).
@@ -522,7 +524,8 @@ int do_for_each_ref_iterator(struct ref_iterator *iter,
524 * - in all other cases, referent will be untouched, and therefore
525 * refname will still be valid and unchanged.
526 */
525 -int read_raw_ref(const char *refname, unsigned char *sha1,
527 +int read_raw_ref(struct ref_store *ref_store,
528 + const char *refname, unsigned char *sha1,
529 struct strbuf *referent, unsigned int *type);
530
531 /* refs backends */