refs: make create_symref() virtual
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index ca57a6e1e5e6837e75d6bb7068b7467d50c56390..312c76619f75846aa1c4c363430a3ff929bf3ac3 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1216,8 +1216,10 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
 }
 
 /* This function needs to return a meaningful errno on failure */
-const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
-                              unsigned char *sha1, int *flags)
+static const char *resolve_ref_recursively(struct ref_store *refs,
+                                          const char *refname,
+                                          int resolve_flags,
+                                          unsigned char *sha1, int *flags)
 {
        static struct strbuf sb_refname = STRBUF_INIT;
        int unused_flags;
@@ -1249,7 +1251,8 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
        for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
                unsigned int read_flags = 0;
 
-               if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
+               if (refs->be->read_raw_ref(refs, refname,
+                                          sha1, &sb_refname, &read_flags)) {
                        *flags |= read_flags;
                        if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
                                return NULL;
@@ -1289,6 +1292,45 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
        return NULL;
 }
 
+const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
+                              unsigned char *sha1, int *flags)
+{
+       return resolve_ref_recursively(get_ref_store(NULL), refname,
+                                      resolve_flags, sha1, flags);
+}
+
+int resolve_gitlink_ref(const char *submodule, const char *refname,
+                       unsigned char *sha1)
+{
+       size_t len = strlen(submodule);
+       struct ref_store *refs;
+       int flags;
+
+       while (len && submodule[len - 1] == '/')
+               len--;
+
+       if (!len)
+               return -1;
+
+       if (submodule[len]) {
+               /* We need to strip off one or more trailing slashes */
+               char *stripped = xmemdupz(submodule, len);
+
+               refs = get_ref_store(stripped);
+               free(stripped);
+       } else {
+               refs = get_ref_store(submodule);
+       }
+
+       if (!refs)
+               return -1;
+
+       if (!resolve_ref_recursively(refs, refname, 0, sha1, &flags) ||
+           is_null_sha1(sha1))
+               return -1;
+       return 0;
+}
+
 /* A pointer to the ref_store for the main repository: */
 static struct ref_store *main_ref_store;
 
@@ -1377,3 +1419,38 @@ void assert_main_repository(struct ref_store *refs, const char *caller)
        if (*refs->submodule)
                die("BUG: %s called for a submodule", caller);
 }
+
+/* backend functions */
+int pack_refs(unsigned int flags)
+{
+       struct ref_store *refs = get_ref_store(NULL);
+
+       return refs->be->pack_refs(refs, flags);
+}
+
+int create_symref(const char *ref_target, const char *refs_heads_master,
+                 const char *logmsg)
+{
+       struct ref_store *refs = get_ref_store(NULL);
+
+       return refs->be->create_symref(refs, ref_target, refs_heads_master,
+                                      logmsg);
+}
+
+int ref_transaction_commit(struct ref_transaction *transaction,
+                          struct strbuf *err)
+{
+       struct ref_store *refs = get_ref_store(NULL);
+
+       return refs->be->transaction_commit(refs, transaction, err);
+}
+
+int verify_refname_available(const char *refname,
+                            const struct string_list *extra,
+                            const struct string_list *skip,
+                            struct strbuf *err)
+{
+       struct ref_store *refs = get_ref_store(NULL);
+
+       return refs->be->verify_refname_available(refs, refname, extra, skip, err);
+}