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