ref_store: implement `refs_peel_ref()` generically

We're about to stop storing packed refs in a `ref_cache`. That means that the only way we have left to optimize `peel_ref()` is by checking whether the reference being peeled is the one currently being iterated over (in `current_ref_iter`), and if so, using `ref_iterator_peel()`. But this can be done generically; it doesn't have to be implemented per-backend. So implement `refs_peel_ref()` in `refs.c` and remove the `peel_ref()` method from the refs API. This removes the last callers of a couple of functions, so delete them. More cleanup to come... Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 25, 2017 at 10:00 UTC ba1c052fa616eb93a654375e8b9d59daa47c28a8
4 files changed +17 -78
refs.c
+17 -1
@@ -1735,7 +1735,23 @@ int refs_pack_refs(struct ref_store *refs, unsigned int flags)
1735 int refs_peel_ref(struct ref_store *refs, const char *refname,
1736 unsigned char *sha1)
1737 {
1738 - return refs->be->peel_ref(refs, refname, sha1);
1738 + int flag;
1739 + unsigned char base[20];
1740 +
1741 + if (current_ref_iter && current_ref_iter->refname == refname) {
1742 + struct object_id peeled;
1743 +
1744 + if (ref_iterator_peel(current_ref_iter, &peeled))
1745 + return -1;
1746 + hashcpy(sha1, peeled.hash);
1747 + return 0;
1748 + }
1749 +
1750 + if (refs_read_ref_full(refs, refname,
1751 + RESOLVE_REF_READING, base, &flag))
1752 + return -1;
1753 +
1754 + return peel_object(base, sha1);
1755 }
1756
1757 int peel_ref(const char *refname, unsigned char *sha1)
refs/files-backend.c
-38
@@ -655,43 +655,6 @@ out:
655 return ret;
656 }
657
658 -static int files_peel_ref(struct ref_store *ref_store,
659 - const char *refname, unsigned char *sha1)
660 -{
661 - struct files_ref_store *refs =
662 - files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
663 - "peel_ref");
664 - int flag;
665 - unsigned char base[20];
666 -
667 - if (current_ref_iter && current_ref_iter->refname == refname) {
668 - struct object_id peeled;
669 -
670 - if (ref_iterator_peel(current_ref_iter, &peeled))
671 - return -1;
672 - hashcpy(sha1, peeled.hash);
673 - return 0;
674 - }
675 -
676 - if (refs_read_ref_full(ref_store, refname,
677 - RESOLVE_REF_READING, base, &flag))
678 - return -1;
679 -
680 - /*
681 - * If the reference is packed, read its ref_entry from the
682 - * cache in the hope that we already know its peeled value.
683 - * We only try this optimization on packed references because
684 - * (a) forcing the filling of the loose reference cache could
685 - * be expensive and (b) loose references anyway usually do not
686 - * have REF_KNOWS_PEELED.
687 - */
688 - if (flag & REF_ISPACKED &&
689 - !refs_peel_ref(refs->packed_ref_store, refname, sha1))
690 - return 0;
691 -
692 - return peel_object(base, sha1);
693 -}
694 -
658 struct files_ref_iterator {
659 struct ref_iterator base;
660
@@ -3012,7 +2975,6 @@ struct ref_storage_be refs_be_files = {
2975 files_initial_transaction_commit,
2976
2977 files_pack_refs,
3015 - files_peel_ref,
2978 files_create_symref,
2979 files_delete_refs,
2980 files_rename_ref,
refs/packed-backend.c
-36
@@ -850,26 +850,6 @@ static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *re
850 return refs->cache;
851 }
852
853 -static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
854 -{
855 - return get_ref_dir(packed_ref_cache->cache->root);
856 -}
857 -
858 -static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
859 -{
860 - return get_packed_ref_dir(get_packed_ref_cache(refs));
861 -}
862 -
863 -/*
864 - * Return the ref_entry for the given refname from the packed
865 - * references. If it does not exist, return NULL.
866 - */
867 -static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
868 - const char *refname)
869 -{
870 - return find_ref_entry(get_packed_refs(refs), refname);
871 -}
872 -
853 static int packed_read_raw_ref(struct ref_store *ref_store,
854 const char *refname, unsigned char *sha1,
855 struct strbuf *referent, unsigned int *type)
@@ -896,21 +876,6 @@ static int packed_read_raw_ref(struct ref_store *ref_store,
876 return 0;
877 }
878
899 -static int packed_peel_ref(struct ref_store *ref_store,
900 - const char *refname, unsigned char *sha1)
901 -{
902 - struct packed_ref_store *refs =
903 - packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
904 - "peel_ref");
905 - struct ref_entry *r = get_packed_ref(refs, refname);
906 -
907 - if (!r || peel_entry(r, 0))
908 - return -1;
909 -
910 - hashcpy(sha1, r->u.value.peeled.hash);
911 - return 0;
912 -}
913 -
879 struct packed_ref_iterator {
880 struct ref_iterator base;
881
@@ -1597,7 +1562,6 @@ struct ref_storage_be refs_be_packed = {
1562 packed_initial_transaction_commit,
1563
1564 packed_pack_refs,
1600 - packed_peel_ref,
1565 packed_create_symref,
1566 packed_delete_refs,
1567 packed_rename_ref,
refs/refs-internal.h
-3
@@ -562,8 +562,6 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,
562 struct strbuf *err);
563
564 typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);
565 -typedef int peel_ref_fn(struct ref_store *ref_store,
566 - const char *refname, unsigned char *sha1);
565 typedef int create_symref_fn(struct ref_store *ref_store,
566 const char *ref_target,
567 const char *refs_heads_master,
@@ -668,7 +666,6 @@ struct ref_storage_be {
666 ref_transaction_commit_fn *initial_transaction_commit;
667
668 pack_refs_fn *pack_refs;
671 - peel_ref_fn *peel_ref;
669 create_symref_fn *create_symref;
670 delete_refs_fn *delete_refs;
671 rename_ref_fn *rename_ref;