Commit Diff


commit - 8fa2f09641633918aea9c50370f672cbc1c37686
commit + 2d2e137815eff05e7c6e6c23416aa281ad17d8cc
blob - 6f15ea20735cffc58ec67eb8b358eecaab8360d5
blob + f5c67f29ec81ad8f3e403e7ed93087b2c9ca2bb6
--- include/got_reference.h
+++ include/got_reference.h
@@ -77,3 +77,7 @@ const struct got_error *got_ref_list(struct got_reflis
 /* 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 *);
blob - 09760ebef2b99d397fa8f74695952c212bdc77cb
blob + ae3b86b9ada95fc112444d7ffd884314f46f1821
--- lib/reference.c
+++ lib/reference.c
@@ -785,6 +785,44 @@ done:
 		if (unlink(tmppath) != 0 && err == NULL)
 			err = got_error_from_errno();
 		free(tmppath);
+	}
+	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;
 }