Commit 88bcd5153f6dc9e0c7ebc73a1e87c0da17e8df28

nulltoken 2012-07-20T16:27:56

branch: introduce git_reference_is_branch()

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);
+}