Commit 5367ec4b84dc3b4ff3ff441347ce07d6065dd759

Carlos Martín Nieto 2014-02-05T12:02:52

refs: add an unconditional delete Add it under the git_reference_remove() name, letting the user pass the repo and name, analogous to unconditional setting/creation.

diff --git a/include/git2/refs.h b/include/git2/refs.h
index 0e5f779..970faf7 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -407,6 +407,17 @@ GIT_EXTERN(int) git_reference_rename(
 GIT_EXTERN(int) git_reference_delete(git_reference *ref);
 
 /**
+ * Delete an existing reference by name
+ *
+ * This method removes the named reference from the repository without
+ * looking at its old value.
+ *
+ * @param ref The reference to remove
+ * @return 0, GIT_EMODIFIED or an error code
+ */
+GIT_EXTERN(int) git_reference_remove(git_repository *repo, const char *name);
+
+/**
  * Fill a list with all the references that can be found in a repository.
  *
  * The string array will be filled with the names of all references; these
diff --git a/src/refs.c b/src/refs.c
index 90340d0..bdf2da3 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -127,6 +127,17 @@ int git_reference_delete(git_reference *ref)
 	return git_refdb_delete(ref->db, ref->name, old_id, old_target);
 }
 
+int git_reference_remove(git_repository *repo, const char *name)
+{
+	git_refdb *db;
+	int error;
+
+	if ((error = git_repository_refdb__weakptr(&db, repo)) < 0)
+		return error;
+
+	return git_refdb_delete(db, name, NULL, NULL);
+}
+
 int git_reference_lookup(git_reference **ref_out,
 	git_repository *repo, const char *name)
 {
diff --git a/tests/refs/delete.c b/tests/refs/delete.c
index 5e4afb1..9d1c3fd 100644
--- a/tests/refs/delete.c
+++ b/tests/refs/delete.c
@@ -91,3 +91,17 @@ void test_refs_delete__packed_only(void)
 	git_reference_free(ref);
 	git_refdb_free(refdb);
 }
+
+void test_refs_delete__remove(void)
+{
+	git_reference *ref;
+
+	/* Check that passing no old values lets us delete */
+
+	cl_git_pass(git_reference_lookup(&ref, g_repo, packed_test_head_name));
+	git_reference_free(ref);
+
+	cl_git_pass(git_reference_remove(g_repo, packed_test_head_name));
+
+	cl_git_fail(git_reference_lookup(&ref, g_repo, packed_test_head_name));
+}