implement support for packed refs in 'got ref -d'
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
diff --git a/lib/reference.c b/lib/reference.c
index 16f6a62..0a2b43b 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -51,6 +51,13 @@
#define GOT_REF_TAGS "tags"
#define GOT_REF_REMOTES "remotes"
+/*
+ * We do not resolve tags yet, and don't yet care about sorting refs either,
+ * so packed-refs files we write contain a minimal header which disables all
+ * packed-refs "traits" supported by Git.
+ */
+#define GOT_PACKED_REFS_HEADER "# pack-refs with:"
+
/* A symbolic reference. */
struct got_symref {
char *name;
@@ -858,6 +865,147 @@ done:
return err ? err : unlock_err;
}
+static const struct got_error *
+delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
+{
+ const struct got_error *err = NULL, *unlock_err = NULL;
+ struct got_lockfile *lf = NULL;
+ FILE *f = NULL, *tmpf = NULL;
+ char *packed_refs_path, *tmppath = NULL;
+ struct got_reflist_head refs;
+ int found_delref = 0;
+
+ /* The packed-refs file does not cotain symbolic references. */
+ if (delref->flags & GOT_REF_IS_SYMBOLIC)
+ return got_error(GOT_ERR_BAD_REF_DATA);
+
+ SIMPLEQ_INIT(&refs);
+
+ packed_refs_path = got_repo_get_path_packed_refs(repo);
+ if (packed_refs_path == NULL)
+ return got_error_from_errno();
+
+ err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
+ if (err)
+ goto done;
+
+ err = got_lockfile_lock(&lf, packed_refs_path);
+ if (err)
+ goto done;
+
+ f = fopen(packed_refs_path, "r");
+ if (f == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ while (1) {
+ char *line;
+ size_t len;
+ const char delim[3] = {'\0', '\0', '\0'};
+ struct got_reference *ref;
+ struct got_reflist_entry *new;
+
+ line = fparseln(f, &len, NULL, delim, 0);
+ if (line == NULL) {
+ if (feof(f))
+ break;
+ err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
+ goto done;
+ }
+ err = parse_packed_ref_line(&ref, NULL, line);
+ free(line);
+ if (err)
+ goto done;
+ if (ref == NULL)
+ continue;
+
+ if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
+ memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
+ sizeof(delref->ref.ref.sha1)) == 0) {
+ found_delref = 1;
+ got_ref_close(ref);
+ continue;
+ }
+
+ err = insert_ref(&new, &refs, ref, repo);
+ if (err || new == NULL /* duplicate */)
+ got_ref_close(ref);
+ if (err)
+ goto done;
+ }
+
+ if (found_delref) {
+ struct got_reflist_entry *re;
+ size_t n;
+ struct stat sb;
+
+ n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
+ if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
+ err = got_ferror(f, GOT_ERR_IO);
+ goto done;
+ }
+
+ SIMPLEQ_FOREACH(re, &refs, entry) {
+ uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
+
+ if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
+ sizeof(hex)) == NULL) {
+ err = got_error(GOT_ERR_BAD_REF_DATA);
+ goto done;
+ }
+ n = fprintf(tmpf, "%s ", hex);
+ if (n != sizeof(hex)) {
+ err = got_ferror(f, GOT_ERR_IO);
+ goto done;
+ }
+ n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
+ if (n != strlen(re->ref->ref.ref.name) + 1) {
+ err = got_ferror(f, GOT_ERR_IO);
+ goto done;
+ }
+ }
+
+ if (fflush(tmpf) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ if (stat(packed_refs_path, &sb) != 0) {
+ if (errno != ENOENT) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ sb.st_mode = GOT_DEFAULT_FILE_MODE;
+ }
+
+ if (rename(tmppath, packed_refs_path) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ if (chmod(packed_refs_path, sb.st_mode) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ }
+done:
+ if (lf)
+ unlock_err = got_lockfile_unlock(lf);
+ if (f) {
+ if (fclose(f) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
+ if (tmpf) {
+ unlink(tmppath);
+ if (fclose(tmpf) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
+ free(tmppath);
+ free(packed_refs_path);
+ got_ref_list_free(&refs);
+ return err ? err : unlock_err;
+}
+
const struct got_error *
got_ref_delete(struct got_reference *ref, struct got_repository *repo)
{
@@ -866,7 +1014,8 @@ got_ref_delete(struct got_reference *ref, struct got_repository *repo)
char *path_refs = NULL, *path = NULL;
struct got_lockfile *lf = NULL;
- /* TODO: handle packed refs ! */
+ if (ref->flags & GOT_REF_IS_PACKED)
+ return delete_packed_ref(ref, repo);
path_refs = get_refs_dir_path(repo, name);
if (path_refs == NULL) {