Commit f21051297cc698644ea0dc9c7122ec944dba2863

Carlos Martín Nieto 2013-11-23T14:39:53

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.

diff --git a/include/git2/refs.h b/include/git2/refs.h
index 31bf997..f88f448 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 1485ed7..5bbd4ba 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -117,6 +117,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 4f3169f..411423d 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 12c15cb..91eecb7 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 2cdea82..e9ce648 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 902a17c..519770d 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;
-
-	assert(ref);
+	int error;
+	git_refdb *refdb;
 
-	if (git_buf_join_n(&path, '/', 3, ref->db->repo->path_repository,
-		GIT_REFLOG_DIR, ref->name) < 0)
-		return -1;
+	assert(repo && refname);
 
-	result = git_path_isfile(git_buf_cstr(&path));
-	git_buf_free(&path);
+	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
+		return error;
 
-	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 9f414f2..a1c5ada 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)