add got_ref_delete(); only loose refs supported for now
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
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;
+}