Commit 2d2e137815eff05e7c6e6c23416aa281ad17d8cc

Stefan Sperling 2019-03-11T16:34:33

add got_ref_delete(); only loose refs supported for now

diff --git a/include/got_reference.h b/include/got_reference.h
index 6f15ea2..f5c67f2 100644
--- a/include/got_reference.h
+++ b/include/got_reference.h
@@ -77,3 +77,7 @@ const struct got_error *got_ref_list(struct got_reflist_head *,
 /* Write a reference to its on-disk path in the repository. */
 const struct got_error *got_ref_write(struct got_reference *,
     struct got_repository *);
+
+/* Delete a reference from its on-disk path in the repository. */
+const struct got_error *got_ref_delete(struct got_reference *,
+    struct got_repository *);
diff --git a/lib/reference.c b/lib/reference.c
index 09760eb..ae3b86b 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -788,3 +788,41 @@ done:
 	}
 	return err ? err : unlock_err;
 }
+
+const struct got_error *
+got_ref_delete(struct got_reference *ref, struct got_repository *repo)
+{
+	const struct got_error *err = NULL, *unlock_err = NULL;
+	const char *name = got_ref_get_name(ref);
+	char *path_refs = NULL, *path = NULL;
+	struct got_lockfile *lf = NULL;
+
+	/* TODO: handle packed refs ! */
+
+	path_refs = get_refs_dir_path(repo, name);
+	if (path_refs == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	err = got_lockfile_lock(&lf, path);
+	if (err)
+		goto done;
+
+	/* XXX: check if old content matches our expectations? */
+
+	if (unlink(path) != 0)
+		err = got_error_from_errno();
+done:
+	if (lf)
+		unlock_err = got_lockfile_unlock(lf);
+
+	free(path_refs);
+	free(path);
+	return err ? err : unlock_err;
+}