summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Michael Haggerty <mhagger@alum.mit.edu>2016-09-04 18:08:22 +0200
committerLibravatar Junio C Hamano <gitster@pobox.com>2016-09-09 15:28:13 -0700
commit424dcc7683a37d1f14aa0dd485300001cb854f6c (patch)
tree5dce65332dd68a05e624d597bfb1bf2e3893ac5b
parentresolve_ref_recursively(): new function (diff)
downloadtgif-424dcc7683a37d1f14aa0dd485300001cb854f6c.tar.xz
resolve_gitlink_ref(): implement using resolve_ref_recursively()
resolve_ref_recursively() can handle references in arbitrary files reference stores, so use it to resolve "gitlink" (i.e., submodule) references. Aside from removing redundant code, this allows submodule lookups to benefit from the much more robust code that we use for reading non-submodule references. And, since the code is now agnostic about reference backends, it will work for any future references backend (so move its definition to refs.c). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--refs.c24
-rw-r--r--refs/files-backend.c67
2 files changed, 24 insertions, 67 deletions
diff --git a/refs.c b/refs.c
index 464fe71928..7b86b4eab5 100644
--- a/refs.c
+++ b/refs.c
@@ -1299,6 +1299,30 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
resolve_flags, sha1, flags);
}
+int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
+{
+ int len = strlen(path);
+ struct strbuf submodule = STRBUF_INIT;
+ struct ref_store *refs;
+ int flags;
+
+ while (len && path[len-1] == '/')
+ len--;
+ if (!len)
+ return -1;
+
+ strbuf_add(&submodule, path, len);
+ refs = get_ref_store(submodule.buf);
+ strbuf_release(&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;
diff --git a/refs/files-backend.c b/refs/files-backend.c
index a743da4c1e..979cee884a 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1494,73 +1494,6 @@ static void unlock_ref(struct ref_lock *lock)
free(lock);
}
-#define MAXREFLEN (1024)
-
-static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,
- const char *refname, unsigned char *sha1,
- int recursion)
-{
- int fd, len;
- char buffer[128], *p;
- char *path;
-
- if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
- return -1;
- path = *refs->base.submodule
- ? git_pathdup_submodule(refs->base.submodule, "%s", refname)
- : git_pathdup("%s", refname);
- fd = open(path, O_RDONLY);
- free(path);
- if (fd < 0) {
- unsigned int flags;
-
- return resolve_packed_ref(refs, refname, sha1, &flags);
- }
-
- len = read(fd, buffer, sizeof(buffer)-1);
- close(fd);
- if (len < 0)
- return -1;
- while (len && isspace(buffer[len-1]))
- len--;
- buffer[len] = 0;
-
- /* Was it a detached head or an old-fashioned symlink? */
- if (!get_sha1_hex(buffer, sha1))
- return 0;
-
- /* Symref? */
- if (strncmp(buffer, "ref:", 4))
- return -1;
- p = buffer + 4;
- while (isspace(*p))
- p++;
-
- return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
-}
-
-int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
-{
- int len = strlen(path);
- struct strbuf submodule = STRBUF_INIT;
- struct files_ref_store *refs;
-
- while (len && path[len-1] == '/')
- len--;
- if (!len)
- return -1;
-
- strbuf_add(&submodule, path, len);
- refs = get_files_ref_store(submodule.buf, "resolve_gitlink_ref");
- if (!refs) {
- strbuf_release(&submodule);
- return -1;
- }
- strbuf_release(&submodule);
-
- return resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
-}
-
/*
* Lock refname, without following symrefs, and set *lock_p to point
* at a newly-allocated lock object. Fill in lock->old_oid, referent,