From f21051297cc698644ea0dc9c7122ec944dba2863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 23 Nov 2013 14:39:53 +0100 Subject: [PATCH] refs: expose has_log() on the backend The frontend used to look at the file directly, but that's obviously not the right thing to do. Expose it on the backend and use that function instead. --- include/git2/refs.h | 6 +++--- include/git2/sys/refdb_backend.h | 5 +++++ src/refdb.c | 7 +++++++ src/refdb.h | 1 + src/refdb_fs.c | 12 ++++++++++++ src/refs.c | 19 +++++++------------ tests/refs/reflog/reflog.c | 8 +------- 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/include/git2/refs.h b/include/git2/refs.h index 31bf997fe..f88f448f0 100644 --- a/include/git2/refs.h +++ b/include/git2/refs.h @@ -548,12 +548,12 @@ GIT_EXTERN(int) git_reference_foreach_glob( /** * Check if a reflog exists for the specified reference. * - * @param ref A git reference - * + * @param repo the repository + * @param refname the reference's name * @return 0 when no reflog can be found, 1 when it exists; * otherwise an error code. */ -GIT_EXTERN(int) git_reference_has_log(git_reference *ref); +GIT_EXTERN(int) git_reference_has_log(git_repository *repo, const char *refname); /** * Ensure there is a reflog for a particular reference. diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h index 1485ed73a..5bbd4ba4c 100644 --- a/include/git2/sys/refdb_backend.h +++ b/include/git2/sys/refdb_backend.h @@ -116,6 +116,11 @@ struct git_refdb_backend { */ int (*compress)(git_refdb_backend *backend); + /** + * Query whether a particular reference has a log (may be empty) + */ + int (*has_log)(git_refdb_backend *backend, const char *refname); + /** * Make sure a particular reference will have a reflog which * will be appended to on writes. diff --git a/src/refdb.c b/src/refdb.c index 4f3169f88..411423d57 100644 --- a/src/refdb.c +++ b/src/refdb.c @@ -222,6 +222,13 @@ int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name) return 0; } +int git_refdb_has_log(git_refdb *db, const char *refname) +{ + assert(db && refname); + + return db->backend->has_log(db->backend, refname); +} + int git_refdb_ensure_log(git_refdb *db, const char *refname) { assert(db && refname); diff --git a/src/refdb.h b/src/refdb.h index 12c15cb9f..91eecb782 100644 --- a/src/refdb.h +++ b/src/refdb.h @@ -48,6 +48,7 @@ int git_refdb_delete(git_refdb *refdb, const char *ref_name); int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name); int git_refdb_reflog_write(git_reflog *reflog); +int git_refdb_has_log(git_refdb *db, const char *refname); int git_refdb_ensure_log(git_refdb *refdb, const char *refname); diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 2cdea8274..e9ce648e1 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -1278,6 +1278,17 @@ cleanup: return ret; } +static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name) +{ + refdb_fs_backend *backend; + + assert(_backend && name); + + backend = (refdb_fs_backend *) _backend; + + return has_reflog(backend->repo, name); +} + static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name) { int error = -1; @@ -1608,6 +1619,7 @@ int git_refdb_backend_fs( backend->parent.del = &refdb_fs_backend__delete; backend->parent.rename = &refdb_fs_backend__rename; backend->parent.compress = &refdb_fs_backend__compress; + backend->parent.has_log = &refdb_reflog_fs__has_log; backend->parent.ensure_log = &refdb_reflog_fs__ensure_log; backend->parent.free = &refdb_fs_backend__free; backend->parent.reflog_read = &refdb_reflog_fs__read; diff --git a/src/refs.c b/src/refs.c index 902a17cfc..519770d2d 100644 --- a/src/refs.c +++ b/src/refs.c @@ -1050,22 +1050,17 @@ int git_reference__update_terminal( return reference__update_terminal(repo, ref_name, oid, 0); } -int git_reference_has_log( - git_reference *ref) +int git_reference_has_log(git_repository *repo, const char *refname) { - git_buf path = GIT_BUF_INIT; - int result; + int error; + git_refdb *refdb; - assert(ref); + assert(repo && refname); - if (git_buf_join_n(&path, '/', 3, ref->db->repo->path_repository, - GIT_REFLOG_DIR, ref->name) < 0) - return -1; + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + return error; - result = git_path_isfile(git_buf_cstr(&path)); - git_buf_free(&path); - - return result; + return git_refdb_has_log(refdb, refname); } int git_reference_ensure_log(git_repository *repo, const char *refname) diff --git a/tests/refs/reflog/reflog.c b/tests/refs/reflog/reflog.c index 9f414f21d..a1c5adaf4 100644 --- a/tests/refs/reflog/reflog.c +++ b/tests/refs/reflog/reflog.c @@ -127,13 +127,7 @@ void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void) static void assert_has_reflog(bool expected_result, const char *name) { - git_reference *ref; - - cl_git_pass(git_reference_lookup(&ref, g_repo, name)); - - cl_assert_equal_i(expected_result, git_reference_has_log(ref)); - - git_reference_free(ref); + cl_assert_equal_i(expected_result, git_reference_has_log(g_repo, name)); } void test_refs_reflog_reflog__reference_has_reflog(void)