refs: add git_reference_has_log()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 2aa0ac2..7f6eb0e 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -353,6 +353,16 @@ GIT_EXTERN(int) git_reference_foreach_glob(
void *payload
);
+/**
+ * Check if a reflog exists for the specified reference.
+ *
+ * @param ref A git reference
+ *
+ * @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_END_DECL
#endif
diff --git a/src/refs.c b/src/refs.c
index 80349b7..2aba83e 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1802,3 +1802,20 @@ int git_reference_foreach_glob(
return git_reference_foreach(
repo, list_flags, fromglob_cb, &data);
}
+
+int git_reference_has_log(
+ git_reference *ref)
+{
+ git_buf path = GIT_BUF_INIT;
+ int result;
+
+ assert(ref);
+
+ if (git_buf_join_n(&path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name) < 0)
+ return -1;
+
+ result = git_path_isfile(git_buf_cstr(&path));
+ git_buf_free(&path);
+
+ return result;
+}
diff --git a/tests-clar/refs/reflog.c b/tests-clar/refs/reflog.c
index a945b47..05f3786 100644
--- a/tests-clar/refs/reflog.c
+++ b/tests-clar/refs/reflog.c
@@ -145,3 +145,20 @@ void test_refs_reflog__renaming_the_reference_moves_the_reflog(void)
git_buf_free(&moved_log_path);
git_buf_free(&master_log_path);
}
+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);
+}
+
+void test_refs_reflog__reference_has_reflog(void)
+{
+ assert_has_reflog(true, "HEAD");
+ assert_has_reflog(true, "refs/heads/master");
+ assert_has_reflog(false, "refs/heads/subtrees");
+}