提交 9da31cb0 编写于 作者: M Michael Haggerty 提交者: Junio C Hamano

refs: handle the main ref_cache specially

Hold the ref_cache instance for the main repository in a dedicated,
statically-allocated instance to avoid the need for a function call
and a linked-list traversal when it is needed.

Suggested by: Heiko Voigt <hvoigt@hvoigt.net>
Signed-off-by: NMichael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 65cf102b
...@@ -811,9 +811,13 @@ static struct ref_cache { ...@@ -811,9 +811,13 @@ static struct ref_cache {
struct ref_cache *next; struct ref_cache *next;
struct ref_entry *loose; struct ref_entry *loose;
struct ref_entry *packed; struct ref_entry *packed;
/* The submodule name, or "" for the main repo. */ /*
char name[FLEX_ARRAY]; * The submodule name, or "" for the main repo. We allocate
} *ref_cache; * length 1 rather than FLEX_ARRAY so that the main ref_cache
* is initialized correctly.
*/
char name[1];
} ref_cache, *submodule_ref_caches;
static void clear_packed_ref_cache(struct ref_cache *refs) static void clear_packed_ref_cache(struct ref_cache *refs)
{ {
...@@ -851,18 +855,18 @@ static struct ref_cache *create_ref_cache(const char *submodule) ...@@ -851,18 +855,18 @@ static struct ref_cache *create_ref_cache(const char *submodule)
*/ */
static struct ref_cache *get_ref_cache(const char *submodule) static struct ref_cache *get_ref_cache(const char *submodule)
{ {
struct ref_cache *refs = ref_cache; struct ref_cache *refs;
if (!submodule)
submodule = ""; if (!submodule || !*submodule)
while (refs) { return &ref_cache;
for (refs = submodule_ref_caches; refs; refs = refs->next)
if (!strcmp(submodule, refs->name)) if (!strcmp(submodule, refs->name))
return refs; return refs;
refs = refs->next;
}
refs = create_ref_cache(submodule); refs = create_ref_cache(submodule);
refs->next = ref_cache; refs->next = submodule_ref_caches;
ref_cache = refs; submodule_ref_caches = refs;
return refs; return refs;
} }
...@@ -1011,8 +1015,8 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs) ...@@ -1011,8 +1015,8 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
void add_packed_ref(const char *refname, const unsigned char *sha1) void add_packed_ref(const char *refname, const unsigned char *sha1)
{ {
add_ref(get_packed_refs(get_ref_cache(NULL)), add_ref(get_packed_refs(&ref_cache),
create_ref_entry(refname, sha1, REF_ISPACKED, 1)); create_ref_entry(refname, sha1, REF_ISPACKED, 1));
} }
/* /*
...@@ -1187,7 +1191,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh ...@@ -1187,7 +1191,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
*/ */
static struct ref_entry *get_packed_ref(const char *refname) static struct ref_entry *get_packed_ref(const char *refname)
{ {
return find_ref(get_packed_refs(get_ref_cache(NULL)), refname); return find_ref(get_packed_refs(&ref_cache), refname);
} }
const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag) const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
...@@ -1592,7 +1596,7 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) ...@@ -1592,7 +1596,7 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
int for_each_ref(each_ref_fn fn, void *cb_data) int for_each_ref(each_ref_fn fn, void *cb_data)
{ {
return do_for_each_ref(get_ref_cache(NULL), "", fn, 0, 0, cb_data); return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data);
} }
int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
...@@ -1602,7 +1606,7 @@ int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) ...@@ -1602,7 +1606,7 @@ int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
{ {
return do_for_each_ref(get_ref_cache(NULL), prefix, fn, strlen(prefix), 0, cb_data); return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
} }
int for_each_ref_in_submodule(const char *submodule, const char *prefix, int for_each_ref_in_submodule(const char *submodule, const char *prefix,
...@@ -1643,7 +1647,7 @@ int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *c ...@@ -1643,7 +1647,7 @@ int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *c
int for_each_replace_ref(each_ref_fn fn, void *cb_data) int for_each_replace_ref(each_ref_fn fn, void *cb_data)
{ {
return do_for_each_ref(get_ref_cache(NULL), "refs/replace/", fn, 13, 0, cb_data); return do_for_each_ref(&ref_cache, "refs/replace/", fn, 13, 0, cb_data);
} }
int head_ref_namespaced(each_ref_fn fn, void *cb_data) int head_ref_namespaced(each_ref_fn fn, void *cb_data)
...@@ -1666,7 +1670,7 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) ...@@ -1666,7 +1670,7 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int ret; int ret;
strbuf_addf(&buf, "%srefs/", get_git_namespace()); strbuf_addf(&buf, "%srefs/", get_git_namespace());
ret = do_for_each_ref(get_ref_cache(NULL), buf.buf, fn, 0, 0, cb_data); ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data);
strbuf_release(&buf); strbuf_release(&buf);
return ret; return ret;
} }
...@@ -1708,7 +1712,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) ...@@ -1708,7 +1712,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
int for_each_rawref(each_ref_fn fn, void *cb_data) int for_each_rawref(each_ref_fn fn, void *cb_data)
{ {
return do_for_each_ref(get_ref_cache(NULL), "", fn, 0, return do_for_each_ref(&ref_cache, "", fn, 0,
DO_FOR_EACH_INCLUDE_BROKEN, cb_data); DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
} }
...@@ -1914,7 +1918,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname, ...@@ -1914,7 +1918,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
* name is a proper prefix of our refname. * name is a proper prefix of our refname.
*/ */
if (missing && if (missing &&
!is_refname_available(refname, NULL, get_packed_refs(get_ref_cache(NULL)))) { !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {
last_errno = ENOTDIR; last_errno = ENOTDIR;
goto error_return; goto error_return;
} }
...@@ -2103,7 +2107,7 @@ int pack_refs(unsigned int flags) ...@@ -2103,7 +2107,7 @@ int pack_refs(unsigned int flags)
write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER)); write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
do_for_each_entry(get_ref_cache(NULL), "", pack_one_ref, &cbdata); do_for_each_entry(&ref_cache, "", pack_one_ref, &cbdata);
if (commit_lock_file(&packlock) < 0) if (commit_lock_file(&packlock) < 0)
die_errno("unable to overwrite old ref-pack file"); die_errno("unable to overwrite old ref-pack file");
prune_refs(cbdata.ref_to_prune); prune_refs(cbdata.ref_to_prune);
...@@ -2161,7 +2165,6 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data) ...@@ -2161,7 +2165,6 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
static int repack_without_ref(const char *refname) static int repack_without_ref(const char *refname)
{ {
int fd; int fd;
struct ref_cache *refs = get_ref_cache(NULL);
struct ref_dir *packed; struct ref_dir *packed;
if (!get_packed_ref(refname)) if (!get_packed_ref(refname))
...@@ -2172,8 +2175,8 @@ static int repack_without_ref(const char *refname) ...@@ -2172,8 +2175,8 @@ static int repack_without_ref(const char *refname)
unable_to_lock_error(git_path("packed-refs"), errno); unable_to_lock_error(git_path("packed-refs"), errno);
return error("cannot delete '%s' from packed refs", refname); return error("cannot delete '%s' from packed refs", refname);
} }
clear_packed_ref_cache(refs); clear_packed_ref_cache(&ref_cache);
packed = get_packed_refs(refs); packed = get_packed_refs(&ref_cache);
/* Remove refname from the cache. */ /* Remove refname from the cache. */
if (remove_entry(packed, refname) == -1) { if (remove_entry(packed, refname) == -1) {
/* /*
...@@ -2213,7 +2216,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt) ...@@ -2213,7 +2216,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
ret |= repack_without_ref(lock->ref_name); ret |= repack_without_ref(lock->ref_name);
unlink_or_warn(git_path("logs/%s", lock->ref_name)); unlink_or_warn(git_path("logs/%s", lock->ref_name));
clear_loose_ref_cache(get_ref_cache(NULL)); clear_loose_ref_cache(&ref_cache);
unlock_ref(lock); unlock_ref(lock);
return ret; return ret;
} }
...@@ -2235,7 +2238,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms ...@@ -2235,7 +2238,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
struct stat loginfo; struct stat loginfo;
int log = !lstat(git_path("logs/%s", oldrefname), &loginfo); int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
const char *symref = NULL; const char *symref = NULL;
struct ref_cache *refs = get_ref_cache(NULL);
if (log && S_ISLNK(loginfo.st_mode)) if (log && S_ISLNK(loginfo.st_mode))
return error("reflog for %s is a symlink", oldrefname); return error("reflog for %s is a symlink", oldrefname);
...@@ -2247,10 +2249,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms ...@@ -2247,10 +2249,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
if (!symref) if (!symref)
return error("refname %s not found", oldrefname); return error("refname %s not found", oldrefname);
if (!is_refname_available(newrefname, oldrefname, get_packed_refs(refs))) if (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))
return 1; return 1;
if (!is_refname_available(newrefname, oldrefname, get_loose_refs(refs))) if (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))
return 1; return 1;
if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG))) if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
...@@ -2506,7 +2508,7 @@ int write_ref_sha1(struct ref_lock *lock, ...@@ -2506,7 +2508,7 @@ int write_ref_sha1(struct ref_lock *lock,
unlock_ref(lock); unlock_ref(lock);
return -1; return -1;
} }
clear_loose_ref_cache(get_ref_cache(NULL)); clear_loose_ref_cache(&ref_cache);
if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 || if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
(strcmp(lock->ref_name, lock->orig_ref_name) && (strcmp(lock->ref_name, lock->orig_ref_name) &&
log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) { log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册