branch: introduce git_reference_is_branch()
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
diff --git a/include/git2/refs.h b/include/git2/refs.h
index b119e90..ac876eb 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -384,6 +384,16 @@ GIT_EXTERN(int) git_reference_remote_tracking_from_branch(
git_reference *branch_ref
);
+/**
+ * Check if a reference is a local branch.
+ *
+ * @param ref A git reference
+ *
+ * @return 1 when the reference lives in the refs/heads
+ * namespace; 0 otherwise.
+ */
+GIT_EXTERN(int) git_reference_is_branch(git_reference *ref);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/refs.c b/src/refs.c
index b3c140b..d08ea96 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1888,3 +1888,10 @@ cleanup:
git_buf_free(&buf);
return error;
}
+
+int git_reference_is_branch(git_reference *ref)
+{
+ assert(ref);
+
+ return git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) == 0;
+}
diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c
index ce4eefe..1948e0a 100644
--- a/tests-clar/refs/read.c
+++ b/tests-clar/refs/read.c
@@ -202,3 +202,19 @@ void test_refs_read__unfound_return_ENOTFOUND(void)
cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/tags/test/master"));
cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master"));
}
+
+static void assert_is_branch(const char *name, bool expected_branchness)
+{
+ git_reference *reference;
+ cl_git_pass(git_reference_lookup(&reference, g_repo, name));
+ cl_assert_equal_i(expected_branchness, git_reference_is_branch(reference));
+ git_reference_free(reference);
+}
+
+void test_refs_read__can_determine_if_a_reference_is_a_local_branch(void)
+{
+ assert_is_branch("refs/heads/master", true);
+ assert_is_branch("refs/heads/packed", true);
+ assert_is_branch("refs/remotes/test/master", false);
+ assert_is_branch("refs/tags/e90810b", false);
+}