]>
Commit | Line | Data |
---|---|---|
67be7c5a | 1 | #include "../cache.h" |
44c2339e | 2 | #include "../config.h" |
67be7c5a MH |
3 | #include "../refs.h" |
4 | #include "refs-internal.h" | |
67be7c5a MH |
5 | #include "packed-backend.h" |
6 | #include "../iterator.h" | |
7 | #include "../lockfile.h" | |
fb9c2d27 | 8 | #include "../chdir-notify.h" |
67be7c5a | 9 | |
5b633610 MH |
10 | enum mmap_strategy { |
11 | /* | |
12 | * Don't use mmap() at all for reading `packed-refs`. | |
13 | */ | |
14 | MMAP_NONE, | |
67be7c5a MH |
15 | |
16 | /* | |
5b633610 MH |
17 | * Can use mmap() for reading `packed-refs`, but the file must |
18 | * not remain mmapped. This is the usual option on Windows, | |
19 | * where you cannot rename a new version of a file onto a file | |
20 | * that is currently mmapped. | |
67be7c5a | 21 | */ |
5b633610 | 22 | MMAP_TEMPORARY, |
67be7c5a | 23 | |
5b633610 MH |
24 | /* |
25 | * It is OK to leave the `packed-refs` file mmapped while | |
26 | * arbitrary other code is running. | |
27 | */ | |
28 | MMAP_OK | |
67be7c5a MH |
29 | }; |
30 | ||
5b633610 MH |
31 | #if defined(NO_MMAP) |
32 | static enum mmap_strategy mmap_strategy = MMAP_NONE; | |
33 | #elif defined(MMAP_PREVENTS_DELETE) | |
34 | static enum mmap_strategy mmap_strategy = MMAP_TEMPORARY; | |
35 | #else | |
36 | static enum mmap_strategy mmap_strategy = MMAP_OK; | |
37 | #endif | |
38 | ||
f0a7dc86 | 39 | struct packed_ref_store; |
67be7c5a MH |
40 | |
41 | /* | |
cff28ca9 MH |
42 | * A `snapshot` represents one snapshot of a `packed-refs` file. |
43 | * | |
44 | * Normally, this will be a mmapped view of the contents of the | |
45 | * `packed-refs` file at the time the snapshot was created. However, | |
46 | * if the `packed-refs` file was not sorted, this might point at heap | |
47 | * memory holding the contents of the `packed-refs` file with its | |
48 | * records sorted by refname. | |
49 | * | |
50 | * `snapshot` instances are reference counted (via | |
51 | * `acquire_snapshot()` and `release_snapshot()`). This is to prevent | |
52 | * an instance from disappearing while an iterator is still iterating | |
53 | * over it. Instances are garbage collected when their `referrers` | |
54 | * count goes to zero. | |
55 | * | |
56 | * The most recent `snapshot`, if available, is referenced by the | |
57 | * `packed_ref_store`. Its freshness is checked whenever | |
58 | * `get_snapshot()` is called; if the existing snapshot is obsolete, a | |
59 | * new snapshot is taken. | |
67be7c5a | 60 | */ |
cff28ca9 | 61 | struct snapshot { |
f0a7dc86 MH |
62 | /* |
63 | * A back-pointer to the packed_ref_store with which this | |
cff28ca9 | 64 | * snapshot is associated: |
f0a7dc86 MH |
65 | */ |
66 | struct packed_ref_store *refs; | |
67 | ||
5b633610 MH |
68 | /* Is the `packed-refs` file currently mmapped? */ |
69 | int mmapped; | |
70 | ||
71 | /* | |
4a2854f7 MH |
72 | * The contents of the `packed-refs` file: |
73 | * | |
74 | * - buf -- a pointer to the start of the memory | |
75 | * - start -- a pointer to the first byte of actual references | |
76 | * (i.e., after the header line, if one is present) | |
77 | * - eof -- a pointer just past the end of the reference | |
78 | * contents | |
79 | * | |
80 | * If the `packed-refs` file was already sorted, `buf` points | |
81 | * at the mmapped contents of the file. If not, it points at | |
82 | * heap-allocated memory containing the contents, sorted. If | |
83 | * there were no contents (e.g., because the file didn't | |
84 | * exist), `buf`, `start`, and `eof` are all NULL. | |
5b633610 | 85 | */ |
4a2854f7 | 86 | char *buf, *start, *eof; |
5b633610 | 87 | |
daa45408 | 88 | /* |
cff28ca9 MH |
89 | * What is the peeled state of the `packed-refs` file that |
90 | * this snapshot represents? (This is usually determined from | |
91 | * the file's header.) | |
daa45408 MH |
92 | */ |
93 | enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled; | |
94 | ||
67be7c5a | 95 | /* |
cff28ca9 MH |
96 | * Count of references to this instance, including the pointer |
97 | * from `packed_ref_store::snapshot`, if any. The instance | |
98 | * will not be freed as long as the reference count is | |
99 | * nonzero. | |
67be7c5a MH |
100 | */ |
101 | unsigned int referrers; | |
102 | ||
cff28ca9 MH |
103 | /* |
104 | * The metadata of the `packed-refs` file from which this | |
105 | * snapshot was created, used to tell if the file has been | |
106 | * replaced since we read it. | |
107 | */ | |
67be7c5a MH |
108 | struct stat_validity validity; |
109 | }; | |
67be7c5a MH |
110 | |
111 | /* | |
cff28ca9 MH |
112 | * A `ref_store` representing references stored in a `packed-refs` |
113 | * file. It implements the `ref_store` interface, though it has some | |
114 | * limitations: | |
115 | * | |
116 | * - It cannot store symbolic references. | |
117 | * | |
118 | * - It cannot store reflogs. | |
119 | * | |
120 | * - It does not support reference renaming (though it could). | |
121 | * | |
122 | * On the other hand, it can be locked outside of a reference | |
123 | * transaction. In that case, it remains locked even after the | |
124 | * transaction is done and the new `packed-refs` file is activated. | |
67be7c5a MH |
125 | */ |
126 | struct packed_ref_store { | |
e0cc8ac8 MH |
127 | struct ref_store base; |
128 | ||
67be7c5a MH |
129 | unsigned int store_flags; |
130 | ||
131 | /* The path of the "packed-refs" file: */ | |
132 | char *path; | |
133 | ||
134 | /* | |
cff28ca9 MH |
135 | * A snapshot of the values read from the `packed-refs` file, |
136 | * if it might still be current; otherwise, NULL. | |
67be7c5a | 137 | */ |
cff28ca9 | 138 | struct snapshot *snapshot; |
67be7c5a MH |
139 | |
140 | /* | |
141 | * Lock used for the "packed-refs" file. Note that this (and | |
142 | * thus the enclosing `packed_ref_store`) must not be freed. | |
143 | */ | |
144 | struct lock_file lock; | |
42dfa7ec MH |
145 | |
146 | /* | |
147 | * Temporary file used when rewriting new contents to the | |
148 | * "packed-refs" file. Note that this (and thus the enclosing | |
149 | * `packed_ref_store`) must not be freed. | |
150 | */ | |
076aa2cb | 151 | struct tempfile *tempfile; |
67be7c5a MH |
152 | }; |
153 | ||
14b3c344 | 154 | /* |
cff28ca9 | 155 | * Increment the reference count of `*snapshot`. |
14b3c344 | 156 | */ |
cff28ca9 | 157 | static void acquire_snapshot(struct snapshot *snapshot) |
14b3c344 | 158 | { |
cff28ca9 | 159 | snapshot->referrers++; |
14b3c344 MH |
160 | } |
161 | ||
5b633610 | 162 | /* |
cff28ca9 | 163 | * If the buffer in `snapshot` is active, then either munmap the |
5b633610 MH |
164 | * memory and close the file, or free the memory. Then set the buffer |
165 | * pointers to NULL. | |
166 | */ | |
cff28ca9 | 167 | static void clear_snapshot_buffer(struct snapshot *snapshot) |
5b633610 | 168 | { |
cff28ca9 MH |
169 | if (snapshot->mmapped) { |
170 | if (munmap(snapshot->buf, snapshot->eof - snapshot->buf)) | |
5b633610 | 171 | die_errno("error ummapping packed-refs file %s", |
cff28ca9 MH |
172 | snapshot->refs->path); |
173 | snapshot->mmapped = 0; | |
5b633610 | 174 | } else { |
cff28ca9 | 175 | free(snapshot->buf); |
5b633610 | 176 | } |
4a2854f7 | 177 | snapshot->buf = snapshot->start = snapshot->eof = NULL; |
5b633610 MH |
178 | } |
179 | ||
14b3c344 | 180 | /* |
cff28ca9 MH |
181 | * Decrease the reference count of `*snapshot`. If it goes to zero, |
182 | * free `*snapshot` and return true; otherwise return false. | |
14b3c344 | 183 | */ |
cff28ca9 | 184 | static int release_snapshot(struct snapshot *snapshot) |
14b3c344 | 185 | { |
cff28ca9 MH |
186 | if (!--snapshot->referrers) { |
187 | stat_validity_clear(&snapshot->validity); | |
188 | clear_snapshot_buffer(snapshot); | |
189 | free(snapshot); | |
14b3c344 MH |
190 | return 1; |
191 | } else { | |
192 | return 0; | |
193 | } | |
194 | } | |
195 | ||
34224e14 | 196 | struct ref_store *packed_ref_store_create(struct repository *repo, |
99f0d97b | 197 | const char *gitdir, |
e0cc8ac8 | 198 | unsigned int store_flags) |
67be7c5a MH |
199 | { |
200 | struct packed_ref_store *refs = xcalloc(1, sizeof(*refs)); | |
e0cc8ac8 | 201 | struct ref_store *ref_store = (struct ref_store *)refs; |
99f0d97b | 202 | struct strbuf sb = STRBUF_INIT; |
67be7c5a | 203 | |
f9f7fd3b | 204 | base_ref_store_init(ref_store, repo, gitdir, &refs_be_packed); |
67be7c5a | 205 | refs->store_flags = store_flags; |
f9f7fd3b | 206 | |
99f0d97b HWN |
207 | strbuf_addf(&sb, "%s/packed-refs", gitdir); |
208 | refs->path = strbuf_detach(&sb, NULL); | |
fb9c2d27 | 209 | chdir_notify_reparent("packed-refs", &refs->path); |
e0cc8ac8 | 210 | return ref_store; |
67be7c5a MH |
211 | } |
212 | ||
e0cc8ac8 MH |
213 | /* |
214 | * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is | |
215 | * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't | |
216 | * support at least the flags specified in `required_flags`. `caller` | |
217 | * is used in any necessary error messages. | |
218 | */ | |
219 | static struct packed_ref_store *packed_downcast(struct ref_store *ref_store, | |
220 | unsigned int required_flags, | |
221 | const char *caller) | |
222 | { | |
223 | struct packed_ref_store *refs; | |
224 | ||
225 | if (ref_store->be != &refs_be_packed) | |
033abf97 | 226 | BUG("ref_store is type \"%s\" not \"packed\" in %s", |
e0cc8ac8 MH |
227 | ref_store->be->name, caller); |
228 | ||
229 | refs = (struct packed_ref_store *)ref_store; | |
230 | ||
231 | if ((refs->store_flags & required_flags) != required_flags) | |
033abf97 | 232 | BUG("unallowed operation (%s), requires %x, has %x\n", |
e0cc8ac8 MH |
233 | caller, required_flags, refs->store_flags); |
234 | ||
235 | return refs; | |
236 | } | |
237 | ||
cff28ca9 | 238 | static void clear_snapshot(struct packed_ref_store *refs) |
67be7c5a | 239 | { |
cff28ca9 MH |
240 | if (refs->snapshot) { |
241 | struct snapshot *snapshot = refs->snapshot; | |
67be7c5a | 242 | |
cff28ca9 MH |
243 | refs->snapshot = NULL; |
244 | release_snapshot(snapshot); | |
67be7c5a MH |
245 | } |
246 | } | |
247 | ||
735267aa MH |
248 | static NORETURN void die_unterminated_line(const char *path, |
249 | const char *p, size_t len) | |
67be7c5a | 250 | { |
735267aa MH |
251 | if (len < 80) |
252 | die("unterminated line in %s: %.*s", path, (int)len, p); | |
253 | else | |
254 | die("unterminated line in %s: %.75s...", path, p); | |
255 | } | |
67be7c5a | 256 | |
735267aa MH |
257 | static NORETURN void die_invalid_line(const char *path, |
258 | const char *p, size_t len) | |
259 | { | |
260 | const char *eol = memchr(p, '\n', len); | |
261 | ||
262 | if (!eol) | |
263 | die_unterminated_line(path, p, len); | |
264 | else if (eol - p < 80) | |
265 | die("unexpected line in %s: %.*s", path, (int)(eol - p), p); | |
266 | else | |
267 | die("unexpected line in %s: %.75s...", path, p); | |
268 | ||
269 | } | |
270 | ||
cff28ca9 | 271 | struct snapshot_record { |
02b920f3 MH |
272 | const char *start; |
273 | size_t len; | |
274 | }; | |
275 | ||
cff28ca9 | 276 | static int cmp_packed_ref_records(const void *v1, const void *v2) |
02b920f3 | 277 | { |
cff28ca9 | 278 | const struct snapshot_record *e1 = v1, *e2 = v2; |
49d16608 | 279 | const char *r1 = e1->start + the_hash_algo->hexsz + 1; |
280 | const char *r2 = e2->start + the_hash_algo->hexsz + 1; | |
02b920f3 MH |
281 | |
282 | while (1) { | |
283 | if (*r1 == '\n') | |
284 | return *r2 == '\n' ? 0 : -1; | |
285 | if (*r1 != *r2) { | |
286 | if (*r2 == '\n') | |
287 | return 1; | |
288 | else | |
289 | return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1; | |
290 | } | |
291 | r1++; | |
292 | r2++; | |
67be7c5a MH |
293 | } |
294 | } | |
295 | ||
d1cf1551 | 296 | /* |
cff28ca9 MH |
297 | * Compare a snapshot record at `rec` to the specified NUL-terminated |
298 | * refname. | |
d1cf1551 | 299 | */ |
cff28ca9 | 300 | static int cmp_record_to_refname(const char *rec, const char *refname) |
d1cf1551 | 301 | { |
49d16608 | 302 | const char *r1 = rec + the_hash_algo->hexsz + 1; |
d1cf1551 MH |
303 | const char *r2 = refname; |
304 | ||
305 | while (1) { | |
306 | if (*r1 == '\n') | |
307 | return *r2 ? -1 : 0; | |
308 | if (!*r2) | |
309 | return 1; | |
310 | if (*r1 != *r2) | |
311 | return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1; | |
312 | r1++; | |
313 | r2++; | |
314 | } | |
315 | } | |
67be7c5a MH |
316 | |
317 | /* | |
cff28ca9 MH |
318 | * `snapshot->buf` is not known to be sorted. Check whether it is, and |
319 | * if not, sort it into new memory and munmap/free the old storage. | |
67be7c5a | 320 | */ |
cff28ca9 | 321 | static void sort_snapshot(struct snapshot *snapshot) |
67be7c5a | 322 | { |
cff28ca9 | 323 | struct snapshot_record *records = NULL; |
02b920f3 MH |
324 | size_t alloc = 0, nr = 0; |
325 | int sorted = 1; | |
326 | const char *pos, *eof, *eol; | |
327 | size_t len, i; | |
328 | char *new_buffer, *dst; | |
67be7c5a | 329 | |
4a2854f7 | 330 | pos = snapshot->start; |
cff28ca9 | 331 | eof = snapshot->eof; |
67be7c5a | 332 | |
4a2854f7 | 333 | if (pos == eof) |
02b920f3 | 334 | return; |
67be7c5a | 335 | |
4a2854f7 MH |
336 | len = eof - pos; |
337 | ||
02b920f3 | 338 | /* |
cff28ca9 | 339 | * Initialize records based on a crude estimate of the number |
02b920f3 MH |
340 | * of references in the file (we'll grow it below if needed): |
341 | */ | |
cff28ca9 | 342 | ALLOC_GROW(records, len / 80 + 20, alloc); |
02b920f3 MH |
343 | |
344 | while (pos < eof) { | |
345 | eol = memchr(pos, '\n', eof - pos); | |
346 | if (!eol) | |
347 | /* The safety check should prevent this. */ | |
348 | BUG("unterminated line found in packed-refs"); | |
49d16608 | 349 | if (eol - pos < the_hash_algo->hexsz + 2) |
cff28ca9 | 350 | die_invalid_line(snapshot->refs->path, |
02b920f3 MH |
351 | pos, eof - pos); |
352 | eol++; | |
353 | if (eol < eof && *eol == '^') { | |
354 | /* | |
355 | * Keep any peeled line together with its | |
356 | * reference: | |
357 | */ | |
358 | const char *peeled_start = eol; | |
359 | ||
360 | eol = memchr(peeled_start, '\n', eof - peeled_start); | |
361 | if (!eol) | |
362 | /* The safety check should prevent this. */ | |
363 | BUG("unterminated peeled line found in packed-refs"); | |
364 | eol++; | |
365 | } | |
366 | ||
cff28ca9 MH |
367 | ALLOC_GROW(records, nr + 1, alloc); |
368 | records[nr].start = pos; | |
369 | records[nr].len = eol - pos; | |
02b920f3 MH |
370 | nr++; |
371 | ||
372 | if (sorted && | |
373 | nr > 1 && | |
cff28ca9 MH |
374 | cmp_packed_ref_records(&records[nr - 2], |
375 | &records[nr - 1]) >= 0) | |
02b920f3 | 376 | sorted = 0; |
67be7c5a | 377 | |
02b920f3 MH |
378 | pos = eol; |
379 | } | |
380 | ||
381 | if (sorted) | |
382 | goto cleanup; | |
383 | ||
cff28ca9 MH |
384 | /* We need to sort the memory. First we sort the records array: */ |
385 | QSORT(records, nr, cmp_packed_ref_records); | |
02b920f3 MH |
386 | |
387 | /* | |
388 | * Allocate a new chunk of memory, and copy the old memory to | |
cff28ca9 | 389 | * the new in the order indicated by `records` (not bothering |
02b920f3 MH |
390 | * with the header line): |
391 | */ | |
392 | new_buffer = xmalloc(len); | |
393 | for (dst = new_buffer, i = 0; i < nr; i++) { | |
cff28ca9 MH |
394 | memcpy(dst, records[i].start, records[i].len); |
395 | dst += records[i].len; | |
02b920f3 MH |
396 | } |
397 | ||
398 | /* | |
399 | * Now munmap the old buffer and use the sorted buffer in its | |
400 | * place: | |
401 | */ | |
cff28ca9 | 402 | clear_snapshot_buffer(snapshot); |
4a2854f7 | 403 | snapshot->buf = snapshot->start = new_buffer; |
cff28ca9 | 404 | snapshot->eof = new_buffer + len; |
02b920f3 MH |
405 | |
406 | cleanup: | |
cff28ca9 | 407 | free(records); |
67be7c5a MH |
408 | } |
409 | ||
410 | /* | |
02b920f3 MH |
411 | * Return a pointer to the start of the record that contains the |
412 | * character `*p` (which must be within the buffer). If no other | |
413 | * record start is found, return `buf`. | |
414 | */ | |
415 | static const char *find_start_of_record(const char *buf, const char *p) | |
416 | { | |
417 | while (p > buf && (p[-1] != '\n' || p[0] == '^')) | |
418 | p--; | |
419 | return p; | |
420 | } | |
421 | ||
d1cf1551 MH |
422 | /* |
423 | * Return a pointer to the start of the record following the record | |
424 | * that contains `*p`. If none is found before `end`, return `end`. | |
425 | */ | |
426 | static const char *find_end_of_record(const char *p, const char *end) | |
427 | { | |
428 | while (++p < end && (p[-1] != '\n' || p[0] == '^')) | |
429 | ; | |
430 | return p; | |
431 | } | |
432 | ||
02b920f3 MH |
433 | /* |
434 | * We want to be able to compare mmapped reference records quickly, | |
435 | * without totally parsing them. We can do so because the records are | |
436 | * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ | |
437 | * + 1) bytes past the beginning of the record. | |
438 | * | |
439 | * But what if the `packed-refs` file contains garbage? We're willing | |
440 | * to tolerate not detecting the problem, as long as we don't produce | |
441 | * totally garbled output (we can't afford to check the integrity of | |
442 | * the whole file during every Git invocation). But we do want to be | |
443 | * sure that we never read past the end of the buffer in memory and | |
444 | * perform an illegal memory access. | |
445 | * | |
446 | * Guarantee that minimum level of safety by verifying that the last | |
447 | * record in the file is LF-terminated, and that it has at least | |
448 | * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of | |
449 | * these checks fails. | |
450 | */ | |
cff28ca9 | 451 | static void verify_buffer_safe(struct snapshot *snapshot) |
02b920f3 | 452 | { |
4a2854f7 | 453 | const char *start = snapshot->start; |
cff28ca9 | 454 | const char *eof = snapshot->eof; |
02b920f3 MH |
455 | const char *last_line; |
456 | ||
4a2854f7 | 457 | if (start == eof) |
02b920f3 MH |
458 | return; |
459 | ||
4a2854f7 | 460 | last_line = find_start_of_record(start, eof - 1); |
49d16608 | 461 | if (*(eof - 1) != '\n' || eof - last_line < the_hash_algo->hexsz + 2) |
cff28ca9 | 462 | die_invalid_line(snapshot->refs->path, |
02b920f3 MH |
463 | last_line, eof - last_line); |
464 | } | |
465 | ||
ba41a8b6 KG |
466 | #define SMALL_FILE_SIZE (32*1024) |
467 | ||
5b633610 MH |
468 | /* |
469 | * Depending on `mmap_strategy`, either mmap or read the contents of | |
cff28ca9 | 470 | * the `packed-refs` file into the snapshot. Return 1 if the file |
01caf20d MH |
471 | * existed and was read, or 0 if the file was absent or empty. Die on |
472 | * errors. | |
5b633610 | 473 | */ |
cff28ca9 | 474 | static int load_contents(struct snapshot *snapshot) |
5b633610 MH |
475 | { |
476 | int fd; | |
477 | struct stat st; | |
478 | size_t size; | |
479 | ssize_t bytes_read; | |
480 | ||
cff28ca9 | 481 | fd = open(snapshot->refs->path, O_RDONLY); |
5b633610 MH |
482 | if (fd < 0) { |
483 | if (errno == ENOENT) { | |
484 | /* | |
485 | * This is OK; it just means that no | |
486 | * "packed-refs" file has been written yet, | |
487 | * which is equivalent to it being empty, | |
488 | * which is its state when initialized with | |
489 | * zeros. | |
490 | */ | |
491 | return 0; | |
492 | } else { | |
cff28ca9 | 493 | die_errno("couldn't read %s", snapshot->refs->path); |
5b633610 MH |
494 | } |
495 | } | |
496 | ||
cff28ca9 | 497 | stat_validity_update(&snapshot->validity, fd); |
5b633610 MH |
498 | |
499 | if (fstat(fd, &st) < 0) | |
cff28ca9 | 500 | die_errno("couldn't stat %s", snapshot->refs->path); |
5b633610 MH |
501 | size = xsize_t(st.st_size); |
502 | ||
01caf20d | 503 | if (!size) { |
09427e83 | 504 | close(fd); |
01caf20d | 505 | return 0; |
ba41a8b6 | 506 | } else if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE) { |
cff28ca9 MH |
507 | snapshot->buf = xmalloc(size); |
508 | bytes_read = read_in_full(fd, snapshot->buf, size); | |
5b633610 | 509 | if (bytes_read < 0 || bytes_read != size) |
cff28ca9 | 510 | die_errno("couldn't read %s", snapshot->refs->path); |
cff28ca9 | 511 | snapshot->mmapped = 0; |
01caf20d | 512 | } else { |
cff28ca9 | 513 | snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
cff28ca9 | 514 | snapshot->mmapped = 1; |
5b633610 MH |
515 | } |
516 | close(fd); | |
517 | ||
4a2854f7 MH |
518 | snapshot->start = snapshot->buf; |
519 | snapshot->eof = snapshot->buf + size; | |
520 | ||
5b633610 MH |
521 | return 1; |
522 | } | |
523 | ||
d1cf1551 | 524 | /* |
cff28ca9 | 525 | * Find the place in `snapshot->buf` where the start of the record for |
d1cf1551 MH |
526 | * `refname` starts. If `mustexist` is true and the reference doesn't |
527 | * exist, then return NULL. If `mustexist` is false and the reference | |
528 | * doesn't exist, then return the point where that reference would be | |
4a14f8d0 MH |
529 | * inserted, or `snapshot->eof` (which might be NULL) if it would be |
530 | * inserted at the end of the file. In the latter mode, `refname` | |
531 | * doesn't have to be a proper reference name; for example, one could | |
532 | * search for "refs/replace/" to find the start of any replace | |
533 | * references. | |
d1cf1551 | 534 | * |
cff28ca9 MH |
535 | * The record is sought using a binary search, so `snapshot->buf` must |
536 | * be sorted. | |
d1cf1551 | 537 | */ |
cff28ca9 | 538 | static const char *find_reference_location(struct snapshot *snapshot, |
d1cf1551 MH |
539 | const char *refname, int mustexist) |
540 | { | |
541 | /* | |
542 | * This is not *quite* a garden-variety binary search, because | |
543 | * the data we're searching is made up of records, and we | |
544 | * always need to find the beginning of a record to do a | |
545 | * comparison. A "record" here is one line for the reference | |
546 | * itself and zero or one peel lines that start with '^'. Our | |
547 | * loop invariant is described in the next two comments. | |
548 | */ | |
549 | ||
550 | /* | |
551 | * A pointer to the character at the start of a record whose | |
552 | * preceding records all have reference names that come | |
553 | * *before* `refname`. | |
554 | */ | |
4a2854f7 | 555 | const char *lo = snapshot->start; |
d1cf1551 MH |
556 | |
557 | /* | |
558 | * A pointer to a the first character of a record whose | |
559 | * reference name comes *after* `refname`. | |
560 | */ | |
cff28ca9 | 561 | const char *hi = snapshot->eof; |
d1cf1551 | 562 | |
4a14f8d0 | 563 | while (lo != hi) { |
d1cf1551 MH |
564 | const char *mid, *rec; |
565 | int cmp; | |
566 | ||
567 | mid = lo + (hi - lo) / 2; | |
568 | rec = find_start_of_record(lo, mid); | |
cff28ca9 | 569 | cmp = cmp_record_to_refname(rec, refname); |
d1cf1551 MH |
570 | if (cmp < 0) { |
571 | lo = find_end_of_record(mid, hi); | |
572 | } else if (cmp > 0) { | |
573 | hi = rec; | |
574 | } else { | |
575 | return rec; | |
576 | } | |
577 | } | |
578 | ||
579 | if (mustexist) | |
580 | return NULL; | |
581 | else | |
582 | return lo; | |
583 | } | |
584 | ||
67be7c5a | 585 | /* |
cff28ca9 MH |
586 | * Create a newly-allocated `snapshot` of the `packed-refs` file in |
587 | * its current state and return it. The return value will already have | |
588 | * its reference count incremented. | |
67be7c5a MH |
589 | * |
590 | * A comment line of the form "# pack-refs with: " may contain zero or | |
591 | * more traits. We interpret the traits as follows: | |
592 | * | |
02b920f3 | 593 | * Neither `peeled` nor `fully-peeled`: |
67be7c5a MH |
594 | * |
595 | * Probably no references are peeled. But if the file contains a | |
596 | * peeled value for a reference, we will use it. | |
597 | * | |
02b920f3 | 598 | * `peeled`: |
67be7c5a MH |
599 | * |
600 | * References under "refs/tags/", if they *can* be peeled, *are* | |
601 | * peeled in this file. References outside of "refs/tags/" are | |
602 | * probably not peeled even if they could have been, but if we find | |
603 | * a peeled value for such a reference we will use it. | |
604 | * | |
02b920f3 | 605 | * `fully-peeled`: |
67be7c5a MH |
606 | * |
607 | * All references in the file that can be peeled are peeled. | |
608 | * Inversely (and this is more important), any references in the | |
609 | * file for which no peeled value is recorded is not peelable. This | |
610 | * trait should typically be written alongside "peeled" for | |
611 | * compatibility with older clients, but we do not require it | |
612 | * (i.e., "peeled" is a no-op if "fully-peeled" is set). | |
02b920f3 MH |
613 | * |
614 | * `sorted`: | |
615 | * | |
616 | * The references in this file are known to be sorted by refname. | |
67be7c5a | 617 | */ |
cff28ca9 | 618 | static struct snapshot *create_snapshot(struct packed_ref_store *refs) |
67be7c5a | 619 | { |
cff28ca9 | 620 | struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot)); |
02b920f3 | 621 | int sorted = 0; |
67be7c5a | 622 | |
cff28ca9 MH |
623 | snapshot->refs = refs; |
624 | acquire_snapshot(snapshot); | |
625 | snapshot->peeled = PEELED_NONE; | |
67be7c5a | 626 | |
cff28ca9 MH |
627 | if (!load_contents(snapshot)) |
628 | return snapshot; | |
67be7c5a | 629 | |
36f23534 | 630 | /* If the file has a header line, process it: */ |
cff28ca9 | 631 | if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') { |
27a41841 | 632 | char *tmp, *p, *eol; |
a8811695 | 633 | struct string_list traits = STRING_LIST_INIT_NODUP; |
9308b7f3 | 634 | |
cff28ca9 MH |
635 | eol = memchr(snapshot->buf, '\n', |
636 | snapshot->eof - snapshot->buf); | |
36f23534 | 637 | if (!eol) |
5b633610 | 638 | die_unterminated_line(refs->path, |
cff28ca9 MH |
639 | snapshot->buf, |
640 | snapshot->eof - snapshot->buf); | |
67be7c5a | 641 | |
27a41841 | 642 | tmp = xmemdupz(snapshot->buf, eol - snapshot->buf); |
67be7c5a | 643 | |
27a41841 | 644 | if (!skip_prefix(tmp, "# pack-refs with:", (const char **)&p)) |
5b633610 | 645 | die_invalid_line(refs->path, |
cff28ca9 MH |
646 | snapshot->buf, |
647 | snapshot->eof - snapshot->buf); | |
36f23534 | 648 | |
a8811695 MH |
649 | string_list_split_in_place(&traits, p, ' ', -1); |
650 | ||
651 | if (unsorted_string_list_has_string(&traits, "fully-peeled")) | |
cff28ca9 | 652 | snapshot->peeled = PEELED_FULLY; |
a8811695 | 653 | else if (unsorted_string_list_has_string(&traits, "peeled")) |
cff28ca9 | 654 | snapshot->peeled = PEELED_TAGS; |
02b920f3 MH |
655 | |
656 | sorted = unsorted_string_list_has_string(&traits, "sorted"); | |
657 | ||
36f23534 MH |
658 | /* perhaps other traits later as well */ |
659 | ||
660 | /* The "+ 1" is for the LF character. */ | |
4a2854f7 | 661 | snapshot->start = eol + 1; |
a8811695 MH |
662 | |
663 | string_list_clear(&traits, 0); | |
27a41841 | 664 | free(tmp); |
67be7c5a MH |
665 | } |
666 | ||
cff28ca9 | 667 | verify_buffer_safe(snapshot); |
67be7c5a | 668 | |
02b920f3 | 669 | if (!sorted) { |
cff28ca9 | 670 | sort_snapshot(snapshot); |
02b920f3 MH |
671 | |
672 | /* | |
673 | * Reordering the records might have moved a short one | |
674 | * to the end of the buffer, so verify the buffer's | |
675 | * safety again: | |
676 | */ | |
cff28ca9 | 677 | verify_buffer_safe(snapshot); |
02b920f3 MH |
678 | } |
679 | ||
cff28ca9 | 680 | if (mmap_strategy != MMAP_OK && snapshot->mmapped) { |
02b920f3 MH |
681 | /* |
682 | * We don't want to leave the file mmapped, so we are | |
683 | * forced to make a copy now: | |
684 | */ | |
4a2854f7 | 685 | size_t size = snapshot->eof - snapshot->start; |
02b920f3 MH |
686 | char *buf_copy = xmalloc(size); |
687 | ||
4a2854f7 | 688 | memcpy(buf_copy, snapshot->start, size); |
cff28ca9 | 689 | clear_snapshot_buffer(snapshot); |
4a2854f7 | 690 | snapshot->buf = snapshot->start = buf_copy; |
cff28ca9 | 691 | snapshot->eof = buf_copy + size; |
02b920f3 MH |
692 | } |
693 | ||
cff28ca9 | 694 | return snapshot; |
67be7c5a MH |
695 | } |
696 | ||
697 | /* | |
cff28ca9 MH |
698 | * Check that `refs->snapshot` (if present) still reflects the |
699 | * contents of the `packed-refs` file. If not, clear the snapshot. | |
67be7c5a | 700 | */ |
cff28ca9 | 701 | static void validate_snapshot(struct packed_ref_store *refs) |
67be7c5a | 702 | { |
cff28ca9 MH |
703 | if (refs->snapshot && |
704 | !stat_validity_check(&refs->snapshot->validity, refs->path)) | |
705 | clear_snapshot(refs); | |
67be7c5a MH |
706 | } |
707 | ||
708 | /* | |
cff28ca9 MH |
709 | * Get the `snapshot` for the specified packed_ref_store, creating and |
710 | * populating it if it hasn't been read before or if the file has been | |
711 | * changed (according to its `validity` field) since it was last read. | |
712 | * On the other hand, if we hold the lock, then assume that the file | |
713 | * hasn't been changed out from under us, so skip the extra `stat()` | |
714 | * call in `stat_validity_check()`. This function does *not* increase | |
715 | * the snapshot's reference count on behalf of the caller. | |
67be7c5a | 716 | */ |
cff28ca9 | 717 | static struct snapshot *get_snapshot(struct packed_ref_store *refs) |
67be7c5a MH |
718 | { |
719 | if (!is_lock_file_locked(&refs->lock)) | |
cff28ca9 | 720 | validate_snapshot(refs); |
67be7c5a | 721 | |
cff28ca9 MH |
722 | if (!refs->snapshot) |
723 | refs->snapshot = create_snapshot(refs); | |
67be7c5a | 724 | |
cff28ca9 | 725 | return refs->snapshot; |
67be7c5a MH |
726 | } |
727 | ||
5b12e16b HWN |
728 | static int packed_read_raw_ref(struct ref_store *ref_store, const char *refname, |
729 | struct object_id *oid, struct strbuf *referent, | |
730 | unsigned int *type, int *failure_errno) | |
67be7c5a | 731 | { |
e0cc8ac8 MH |
732 | struct packed_ref_store *refs = |
733 | packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); | |
cff28ca9 | 734 | struct snapshot *snapshot = get_snapshot(refs); |
f3987ab3 | 735 | const char *rec; |
67be7c5a MH |
736 | |
737 | *type = 0; | |
738 | ||
cff28ca9 | 739 | rec = find_reference_location(snapshot, refname, 1); |
f3987ab3 MH |
740 | |
741 | if (!rec) { | |
742 | /* refname is not a packed reference. */ | |
5b12e16b | 743 | *failure_errno = ENOENT; |
67be7c5a MH |
744 | return -1; |
745 | } | |
746 | ||
99afe91a | 747 | if (get_oid_hex(rec, oid)) |
cff28ca9 | 748 | die_invalid_line(refs->path, rec, snapshot->eof - rec); |
f3987ab3 | 749 | |
67be7c5a MH |
750 | *type = REF_ISPACKED; |
751 | return 0; | |
752 | } | |
753 | ||
523ee2d7 MH |
754 | /* |
755 | * This value is set in `base.flags` if the peeled value of the | |
756 | * current reference is known. In that case, `peeled` contains the | |
78fb4579 | 757 | * correct peeled value for the reference, which might be `null_oid` |
523ee2d7 MH |
758 | * if the reference is not a tag or if it is broken. |
759 | */ | |
760 | #define REF_KNOWS_PEELED 0x40 | |
67be7c5a | 761 | |
523ee2d7 | 762 | /* |
cff28ca9 | 763 | * An iterator over a snapshot of a `packed-refs` file. |
523ee2d7 | 764 | */ |
67be7c5a MH |
765 | struct packed_ref_iterator { |
766 | struct ref_iterator base; | |
767 | ||
cff28ca9 | 768 | struct snapshot *snapshot; |
523ee2d7 | 769 | |
cff28ca9 | 770 | /* The current position in the snapshot's buffer: */ |
523ee2d7 MH |
771 | const char *pos; |
772 | ||
cff28ca9 | 773 | /* The end of the part of the buffer that will be iterated over: */ |
523ee2d7 MH |
774 | const char *eof; |
775 | ||
cff28ca9 | 776 | /* Scratch space for current values: */ |
523ee2d7 | 777 | struct object_id oid, peeled; |
523ee2d7 MH |
778 | struct strbuf refname_buf; |
779 | ||
9bc45a28 | 780 | struct repository *repo; |
67be7c5a MH |
781 | unsigned int flags; |
782 | }; | |
783 | ||
cff28ca9 MH |
784 | /* |
785 | * Move the iterator to the next record in the snapshot, without | |
786 | * respect for whether the record is actually required by the current | |
787 | * iteration. Adjust the fields in `iter` and return `ITER_OK` or | |
788 | * `ITER_DONE`. This function does not free the iterator in the case | |
789 | * of `ITER_DONE`. | |
790 | */ | |
523ee2d7 MH |
791 | static int next_record(struct packed_ref_iterator *iter) |
792 | { | |
793 | const char *p = iter->pos, *eol; | |
794 | ||
795 | strbuf_reset(&iter->refname_buf); | |
796 | ||
797 | if (iter->pos == iter->eof) | |
798 | return ITER_DONE; | |
799 | ||
800 | iter->base.flags = REF_ISPACKED; | |
801 | ||
49d16608 | 802 | if (iter->eof - p < the_hash_algo->hexsz + 2 || |
523ee2d7 MH |
803 | parse_oid_hex(p, &iter->oid, &p) || |
804 | !isspace(*p++)) | |
cff28ca9 | 805 | die_invalid_line(iter->snapshot->refs->path, |
523ee2d7 MH |
806 | iter->pos, iter->eof - iter->pos); |
807 | ||
808 | eol = memchr(p, '\n', iter->eof - p); | |
809 | if (!eol) | |
cff28ca9 | 810 | die_unterminated_line(iter->snapshot->refs->path, |
523ee2d7 MH |
811 | iter->pos, iter->eof - iter->pos); |
812 | ||
813 | strbuf_add(&iter->refname_buf, p, eol - p); | |
814 | iter->base.refname = iter->refname_buf.buf; | |
815 | ||
816 | if (check_refname_format(iter->base.refname, REFNAME_ALLOW_ONELEVEL)) { | |
817 | if (!refname_is_safe(iter->base.refname)) | |
818 | die("packed refname is dangerous: %s", | |
819 | iter->base.refname); | |
820 | oidclr(&iter->oid); | |
821 | iter->base.flags |= REF_BAD_NAME | REF_ISBROKEN; | |
822 | } | |
cff28ca9 MH |
823 | if (iter->snapshot->peeled == PEELED_FULLY || |
824 | (iter->snapshot->peeled == PEELED_TAGS && | |
523ee2d7 MH |
825 | starts_with(iter->base.refname, "refs/tags/"))) |
826 | iter->base.flags |= REF_KNOWS_PEELED; | |
827 | ||
828 | iter->pos = eol + 1; | |
829 | ||
830 | if (iter->pos < iter->eof && *iter->pos == '^') { | |
831 | p = iter->pos + 1; | |
49d16608 | 832 | if (iter->eof - p < the_hash_algo->hexsz + 1 || |
523ee2d7 MH |
833 | parse_oid_hex(p, &iter->peeled, &p) || |
834 | *p++ != '\n') | |
cff28ca9 | 835 | die_invalid_line(iter->snapshot->refs->path, |
523ee2d7 MH |
836 | iter->pos, iter->eof - iter->pos); |
837 | iter->pos = p; | |
838 | ||
839 | /* | |
840 | * Regardless of what the file header said, we | |
841 | * definitely know the value of *this* reference. But | |
842 | * we suppress it if the reference is broken: | |
843 | */ | |
844 | if ((iter->base.flags & REF_ISBROKEN)) { | |
845 | oidclr(&iter->peeled); | |
846 | iter->base.flags &= ~REF_KNOWS_PEELED; | |
847 | } else { | |
848 | iter->base.flags |= REF_KNOWS_PEELED; | |
849 | } | |
850 | } else { | |
851 | oidclr(&iter->peeled); | |
852 | } | |
853 | ||
854 | return ITER_OK; | |
855 | } | |
856 | ||
67be7c5a MH |
857 | static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator) |
858 | { | |
859 | struct packed_ref_iterator *iter = | |
860 | (struct packed_ref_iterator *)ref_iterator; | |
861 | int ok; | |
862 | ||
523ee2d7 | 863 | while ((ok = next_record(iter)) == ITER_OK) { |
67be7c5a | 864 | if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && |
523ee2d7 | 865 | ref_type(iter->base.refname) != REF_TYPE_PER_WORKTREE) |
67be7c5a MH |
866 | continue; |
867 | ||
868 | if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && | |
9bc45a28 JT |
869 | !ref_resolves_to_object(iter->base.refname, iter->repo, |
870 | &iter->oid, iter->flags)) | |
67be7c5a MH |
871 | continue; |
872 | ||
67be7c5a MH |
873 | return ITER_OK; |
874 | } | |
875 | ||
67be7c5a MH |
876 | if (ref_iterator_abort(ref_iterator) != ITER_DONE) |
877 | ok = ITER_ERROR; | |
878 | ||
879 | return ok; | |
880 | } | |
881 | ||
882 | static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator, | |
883 | struct object_id *peeled) | |
884 | { | |
885 | struct packed_ref_iterator *iter = | |
886 | (struct packed_ref_iterator *)ref_iterator; | |
887 | ||
8788195c JT |
888 | if (iter->repo != the_repository) |
889 | BUG("peeling for non-the_repository is not supported"); | |
890 | ||
523ee2d7 MH |
891 | if ((iter->base.flags & REF_KNOWS_PEELED)) { |
892 | oidcpy(peeled, &iter->peeled); | |
893 | return is_null_oid(&iter->peeled) ? -1 : 0; | |
894 | } else if ((iter->base.flags & (REF_ISBROKEN | REF_ISSYMREF))) { | |
895 | return -1; | |
896 | } else { | |
617480d7 | 897 | return peel_object(&iter->oid, peeled) ? -1 : 0; |
523ee2d7 | 898 | } |
67be7c5a MH |
899 | } |
900 | ||
901 | static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator) | |
902 | { | |
903 | struct packed_ref_iterator *iter = | |
904 | (struct packed_ref_iterator *)ref_iterator; | |
905 | int ok = ITER_DONE; | |
906 | ||
523ee2d7 | 907 | strbuf_release(&iter->refname_buf); |
cff28ca9 | 908 | release_snapshot(iter->snapshot); |
67be7c5a MH |
909 | base_ref_iterator_free(ref_iterator); |
910 | return ok; | |
911 | } | |
912 | ||
913 | static struct ref_iterator_vtable packed_ref_iterator_vtable = { | |
914 | packed_ref_iterator_advance, | |
915 | packed_ref_iterator_peel, | |
916 | packed_ref_iterator_abort | |
917 | }; | |
918 | ||
e0cc8ac8 MH |
919 | static struct ref_iterator *packed_ref_iterator_begin( |
920 | struct ref_store *ref_store, | |
67be7c5a MH |
921 | const char *prefix, unsigned int flags) |
922 | { | |
e0cc8ac8 | 923 | struct packed_ref_store *refs; |
cff28ca9 | 924 | struct snapshot *snapshot; |
d1cf1551 | 925 | const char *start; |
67be7c5a MH |
926 | struct packed_ref_iterator *iter; |
927 | struct ref_iterator *ref_iterator; | |
e0cc8ac8 MH |
928 | unsigned int required_flags = REF_STORE_READ; |
929 | ||
930 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) | |
931 | required_flags |= REF_STORE_ODB; | |
932 | refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin"); | |
67be7c5a | 933 | |
cff28ca9 MH |
934 | /* |
935 | * Note that `get_snapshot()` internally checks whether the | |
936 | * snapshot is up to date with what is on disk, and re-reads | |
937 | * it if not. | |
938 | */ | |
939 | snapshot = get_snapshot(refs); | |
523ee2d7 | 940 | |
f3424297 MH |
941 | if (prefix && *prefix) |
942 | start = find_reference_location(snapshot, prefix, 0); | |
943 | else | |
944 | start = snapshot->start; | |
945 | ||
946 | if (start == snapshot->eof) | |
523ee2d7 MH |
947 | return empty_ref_iterator_begin(); |
948 | ||
ca56dadb | 949 | CALLOC_ARRAY(iter, 1); |
67be7c5a | 950 | ref_iterator = &iter->base; |
8738a8a4 | 951 | base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1); |
67be7c5a | 952 | |
cff28ca9 MH |
953 | iter->snapshot = snapshot; |
954 | acquire_snapshot(snapshot); | |
67be7c5a | 955 | |
523ee2d7 | 956 | iter->pos = start; |
cff28ca9 | 957 | iter->eof = snapshot->eof; |
523ee2d7 MH |
958 | strbuf_init(&iter->refname_buf, 0); |
959 | ||
960 | iter->base.oid = &iter->oid; | |
67be7c5a | 961 | |
9bc45a28 | 962 | iter->repo = ref_store->repo; |
67be7c5a MH |
963 | iter->flags = flags; |
964 | ||
d1cf1551 MH |
965 | if (prefix && *prefix) |
966 | /* Stop iteration after we've gone *past* prefix: */ | |
967 | ref_iterator = prefix_ref_iterator_begin(ref_iterator, prefix, 0); | |
968 | ||
67be7c5a MH |
969 | return ref_iterator; |
970 | } | |
971 | ||
972 | /* | |
973 | * Write an entry to the packed-refs file for the specified refname. | |
3478983b MH |
974 | * If peeled is non-NULL, write it as the entry's peeled value. On |
975 | * error, return a nonzero value and leave errno set at the value left | |
976 | * by the failing call to `fprintf()`. | |
67be7c5a | 977 | */ |
3478983b | 978 | static int write_packed_entry(FILE *fh, const char *refname, |
41701882 MH |
979 | const struct object_id *oid, |
980 | const struct object_id *peeled) | |
67be7c5a | 981 | { |
41701882 MH |
982 | if (fprintf(fh, "%s %s\n", oid_to_hex(oid), refname) < 0 || |
983 | (peeled && fprintf(fh, "^%s\n", oid_to_hex(peeled)) < 0)) | |
3478983b MH |
984 | return -1; |
985 | ||
986 | return 0; | |
67be7c5a MH |
987 | } |
988 | ||
c8bed835 | 989 | int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err) |
67be7c5a | 990 | { |
e0cc8ac8 MH |
991 | struct packed_ref_store *refs = |
992 | packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN, | |
b7de57d8 | 993 | "packed_refs_lock"); |
67be7c5a MH |
994 | static int timeout_configured = 0; |
995 | static int timeout_value = 1000; | |
67be7c5a | 996 | |
67be7c5a MH |
997 | if (!timeout_configured) { |
998 | git_config_get_int("core.packedrefstimeout", &timeout_value); | |
999 | timeout_configured = 1; | |
1000 | } | |
1001 | ||
42dfa7ec MH |
1002 | /* |
1003 | * Note that we close the lockfile immediately because we | |
1004 | * don't write new content to it, but rather to a separate | |
1005 | * tempfile. | |
1006 | */ | |
67be7c5a MH |
1007 | if (hold_lock_file_for_update_timeout( |
1008 | &refs->lock, | |
1009 | refs->path, | |
c8bed835 MH |
1010 | flags, timeout_value) < 0) { |
1011 | unable_to_lock_message(refs->path, errno, err); | |
1012 | return -1; | |
1013 | } | |
1014 | ||
83a3069a | 1015 | if (close_lock_file_gently(&refs->lock)) { |
c8bed835 | 1016 | strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno)); |
83a3069a | 1017 | rollback_lock_file(&refs->lock); |
67be7c5a | 1018 | return -1; |
c8bed835 | 1019 | } |
67be7c5a MH |
1020 | |
1021 | /* | |
a613d4f8 SC |
1022 | * There is a stat-validity problem might cause `update-ref -d` |
1023 | * lost the newly commit of a ref, because a new `packed-refs` | |
1024 | * file might has the same on-disk file attributes such as | |
1025 | * timestamp, file size and inode value, but has a changed | |
1026 | * ref value. | |
1027 | * | |
1028 | * This could happen with a very small chance when | |
1029 | * `update-ref -d` is called and at the same time another | |
1030 | * `pack-refs --all` process is running. | |
1031 | * | |
1032 | * Now that we hold the `packed-refs` lock, it is important | |
1033 | * to make sure we could read the latest version of | |
1034 | * `packed-refs` file no matter we have just mmap it or not. | |
1035 | * So what need to do is clear the snapshot if we hold it | |
1036 | * already. | |
67be7c5a | 1037 | */ |
a613d4f8 | 1038 | clear_snapshot(refs); |
67be7c5a | 1039 | |
39c8df0c MH |
1040 | /* |
1041 | * Now make sure that the packed-refs file as it exists in the | |
cff28ca9 | 1042 | * locked state is loaded into the snapshot: |
39c8df0c | 1043 | */ |
cff28ca9 | 1044 | get_snapshot(refs); |
67be7c5a MH |
1045 | return 0; |
1046 | } | |
1047 | ||
49aebcf4 MH |
1048 | void packed_refs_unlock(struct ref_store *ref_store) |
1049 | { | |
1050 | struct packed_ref_store *refs = packed_downcast( | |
1051 | ref_store, | |
1052 | REF_STORE_READ | REF_STORE_WRITE, | |
1053 | "packed_refs_unlock"); | |
1054 | ||
1055 | if (!is_lock_file_locked(&refs->lock)) | |
033abf97 | 1056 | BUG("packed_refs_unlock() called when not locked"); |
49aebcf4 | 1057 | rollback_lock_file(&refs->lock); |
49aebcf4 MH |
1058 | } |
1059 | ||
1060 | int packed_refs_is_locked(struct ref_store *ref_store) | |
1061 | { | |
1062 | struct packed_ref_store *refs = packed_downcast( | |
1063 | ref_store, | |
1064 | REF_STORE_READ | REF_STORE_WRITE, | |
1065 | "packed_refs_is_locked"); | |
1066 | ||
1067 | return is_lock_file_locked(&refs->lock); | |
1068 | } | |
1069 | ||
67be7c5a | 1070 | /* |
cff28ca9 MH |
1071 | * The packed-refs header line that we write out. Perhaps other traits |
1072 | * will be added later. | |
a8811695 MH |
1073 | * |
1074 | * Note that earlier versions of Git used to parse these traits by | |
1075 | * looking for " trait " in the line. For this reason, the space after | |
1076 | * the colon and the trailing space are required. | |
67be7c5a MH |
1077 | */ |
1078 | static const char PACKED_REFS_HEADER[] = | |
02b920f3 | 1079 | "# pack-refs with: peeled fully-peeled sorted \n"; |
67be7c5a | 1080 | |
e0cc8ac8 MH |
1081 | static int packed_init_db(struct ref_store *ref_store, struct strbuf *err) |
1082 | { | |
1083 | /* Nothing to do. */ | |
1084 | return 0; | |
1085 | } | |
1086 | ||
67be7c5a | 1087 | /* |
cff28ca9 MH |
1088 | * Write the packed refs from the current snapshot to the packed-refs |
1089 | * tempfile, incorporating any changes from `updates`. `updates` must | |
1090 | * be a sorted string list whose keys are the refnames and whose util | |
2775d872 MH |
1091 | * values are `struct ref_update *`. On error, rollback the tempfile, |
1092 | * write an error message to `err`, and return a nonzero value. | |
1093 | * | |
1094 | * The packfile must be locked before calling this function and will | |
1095 | * remain locked when it is done. | |
67be7c5a | 1096 | */ |
2775d872 MH |
1097 | static int write_with_updates(struct packed_ref_store *refs, |
1098 | struct string_list *updates, | |
1099 | struct strbuf *err) | |
67be7c5a | 1100 | { |
2775d872 MH |
1101 | struct ref_iterator *iter = NULL; |
1102 | size_t i; | |
3478983b | 1103 | int ok; |
67be7c5a | 1104 | FILE *out; |
2775d872 | 1105 | struct strbuf sb = STRBUF_INIT; |
198b808e | 1106 | char *packed_refs_path; |
67be7c5a | 1107 | |
67be7c5a | 1108 | if (!is_lock_file_locked(&refs->lock)) |
033abf97 | 1109 | BUG("write_with_updates() called while unlocked"); |
67be7c5a | 1110 | |
198b808e MH |
1111 | /* |
1112 | * If packed-refs is a symlink, we want to overwrite the | |
1113 | * symlinked-to file, not the symlink itself. Also, put the | |
1114 | * staging file next to it: | |
1115 | */ | |
1116 | packed_refs_path = get_locked_file_path(&refs->lock); | |
1117 | strbuf_addf(&sb, "%s.new", packed_refs_path); | |
2775d872 | 1118 | free(packed_refs_path); |
076aa2cb JK |
1119 | refs->tempfile = create_tempfile(sb.buf); |
1120 | if (!refs->tempfile) { | |
42dfa7ec MH |
1121 | strbuf_addf(err, "unable to create file %s: %s", |
1122 | sb.buf, strerror(errno)); | |
1123 | strbuf_release(&sb); | |
2775d872 | 1124 | return -1; |
42dfa7ec MH |
1125 | } |
1126 | strbuf_release(&sb); | |
1127 | ||
076aa2cb | 1128 | out = fdopen_tempfile(refs->tempfile, "w"); |
3478983b MH |
1129 | if (!out) { |
1130 | strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s", | |
1131 | strerror(errno)); | |
1132 | goto error; | |
1133 | } | |
67be7c5a | 1134 | |
2775d872 MH |
1135 | if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) |
1136 | goto write_error; | |
1137 | ||
1138 | /* | |
1139 | * We iterate in parallel through the current list of refs and | |
1140 | * the list of updates, processing an entry from at least one | |
1141 | * of the lists each time through the loop. When the current | |
1142 | * list of refs is exhausted, set iter to NULL. When the list | |
1143 | * of updates is exhausted, leave i set to updates->nr. | |
1144 | */ | |
1145 | iter = packed_ref_iterator_begin(&refs->base, "", | |
1146 | DO_FOR_EACH_INCLUDE_BROKEN); | |
1147 | if ((ok = ref_iterator_advance(iter)) != ITER_OK) | |
1148 | iter = NULL; | |
1149 | ||
1150 | i = 0; | |
67be7c5a | 1151 | |
2775d872 MH |
1152 | while (iter || i < updates->nr) { |
1153 | struct ref_update *update = NULL; | |
1154 | int cmp; | |
1155 | ||
1156 | if (i >= updates->nr) { | |
1157 | cmp = -1; | |
1158 | } else { | |
1159 | update = updates->items[i].util; | |
1160 | ||
1161 | if (!iter) | |
1162 | cmp = +1; | |
1163 | else | |
1164 | cmp = strcmp(iter->refname, update->refname); | |
1165 | } | |
1166 | ||
1167 | if (!cmp) { | |
1168 | /* | |
1169 | * There is both an old value and an update | |
1170 | * for this reference. Check the old value if | |
1171 | * necessary: | |
1172 | */ | |
1173 | if ((update->flags & REF_HAVE_OLD)) { | |
1174 | if (is_null_oid(&update->old_oid)) { | |
1175 | strbuf_addf(err, "cannot update ref '%s': " | |
1176 | "reference already exists", | |
1177 | update->refname); | |
1178 | goto error; | |
9001dc2a | 1179 | } else if (!oideq(&update->old_oid, iter->oid)) { |
2775d872 MH |
1180 | strbuf_addf(err, "cannot update ref '%s': " |
1181 | "is at %s but expected %s", | |
1182 | update->refname, | |
1183 | oid_to_hex(iter->oid), | |
1184 | oid_to_hex(&update->old_oid)); | |
1185 | goto error; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | /* Now figure out what to use for the new value: */ | |
1190 | if ((update->flags & REF_HAVE_NEW)) { | |
1191 | /* | |
1192 | * The update takes precedence. Skip | |
1193 | * the iterator over the unneeded | |
1194 | * value. | |
1195 | */ | |
1196 | if ((ok = ref_iterator_advance(iter)) != ITER_OK) | |
1197 | iter = NULL; | |
1198 | cmp = +1; | |
1199 | } else { | |
1200 | /* | |
1201 | * The update doesn't actually want to | |
1202 | * change anything. We're done with it. | |
1203 | */ | |
1204 | i++; | |
1205 | cmp = -1; | |
1206 | } | |
1207 | } else if (cmp > 0) { | |
1208 | /* | |
1209 | * There is no old value but there is an | |
1210 | * update for this reference. Make sure that | |
1211 | * the update didn't expect an existing value: | |
1212 | */ | |
1213 | if ((update->flags & REF_HAVE_OLD) && | |
1214 | !is_null_oid(&update->old_oid)) { | |
1215 | strbuf_addf(err, "cannot update ref '%s': " | |
1216 | "reference is missing but expected %s", | |
1217 | update->refname, | |
1218 | oid_to_hex(&update->old_oid)); | |
1219 | goto error; | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | if (cmp < 0) { | |
1224 | /* Pass the old reference through. */ | |
1225 | ||
1226 | struct object_id peeled; | |
1227 | int peel_error = ref_iterator_peel(iter, &peeled); | |
1228 | ||
1229 | if (write_packed_entry(out, iter->refname, | |
41701882 MH |
1230 | iter->oid, |
1231 | peel_error ? NULL : &peeled)) | |
2775d872 MH |
1232 | goto write_error; |
1233 | ||
1234 | if ((ok = ref_iterator_advance(iter)) != ITER_OK) | |
1235 | iter = NULL; | |
1236 | } else if (is_null_oid(&update->new_oid)) { | |
1237 | /* | |
1238 | * The update wants to delete the reference, | |
1239 | * and the reference either didn't exist or we | |
1240 | * have already skipped it. So we're done with | |
1241 | * the update (and don't have to write | |
1242 | * anything). | |
1243 | */ | |
1244 | i++; | |
1245 | } else { | |
1246 | struct object_id peeled; | |
ac2ed0d7 | 1247 | int peel_error = peel_object(&update->new_oid, |
1248 | &peeled); | |
2775d872 MH |
1249 | |
1250 | if (write_packed_entry(out, update->refname, | |
41701882 MH |
1251 | &update->new_oid, |
1252 | peel_error ? NULL : &peeled)) | |
2775d872 MH |
1253 | goto write_error; |
1254 | ||
1255 | i++; | |
3478983b | 1256 | } |
67be7c5a MH |
1257 | } |
1258 | ||
3478983b | 1259 | if (ok != ITER_DONE) { |
72d4a9a7 RS |
1260 | strbuf_addstr(err, "unable to write packed-refs file: " |
1261 | "error iterating over old contents"); | |
3478983b MH |
1262 | goto error; |
1263 | } | |
67be7c5a | 1264 | |
07f0542d | 1265 | if (close_tempfile_gently(refs->tempfile)) { |
2775d872 | 1266 | strbuf_addf(err, "error closing file %s: %s", |
07f0542d | 1267 | get_tempfile_path(refs->tempfile), |
2775d872 MH |
1268 | strerror(errno)); |
1269 | strbuf_release(&sb); | |
07f0542d | 1270 | delete_tempfile(&refs->tempfile); |
2775d872 | 1271 | return -1; |
67be7c5a | 1272 | } |
3478983b | 1273 | |
2775d872 MH |
1274 | return 0; |
1275 | ||
1276 | write_error: | |
1277 | strbuf_addf(err, "error writing to %s: %s", | |
07f0542d | 1278 | get_tempfile_path(refs->tempfile), strerror(errno)); |
3478983b MH |
1279 | |
1280 | error: | |
2775d872 MH |
1281 | if (iter) |
1282 | ref_iterator_abort(iter); | |
198b808e | 1283 | |
2775d872 MH |
1284 | delete_tempfile(&refs->tempfile); |
1285 | return -1; | |
67be7c5a MH |
1286 | } |
1287 | ||
7c6bd25c MH |
1288 | int is_packed_transaction_needed(struct ref_store *ref_store, |
1289 | struct ref_transaction *transaction) | |
1290 | { | |
1291 | struct packed_ref_store *refs = packed_downcast( | |
1292 | ref_store, | |
1293 | REF_STORE_READ, | |
1294 | "is_packed_transaction_needed"); | |
1295 | struct strbuf referent = STRBUF_INIT; | |
1296 | size_t i; | |
1297 | int ret; | |
1298 | ||
1299 | if (!is_lock_file_locked(&refs->lock)) | |
1300 | BUG("is_packed_transaction_needed() called while unlocked"); | |
1301 | ||
1302 | /* | |
1303 | * We're only going to bother returning false for the common, | |
1304 | * trivial case that references are only being deleted, their | |
1305 | * old values are not being checked, and the old `packed-refs` | |
1306 | * file doesn't contain any of those reference(s). This gives | |
1307 | * false positives for some other cases that could | |
1308 | * theoretically be optimized away: | |
1309 | * | |
1310 | * 1. It could be that the old value is being verified without | |
1311 | * setting a new value. In this case, we could verify the | |
1312 | * old value here and skip the update if it agrees. If it | |
1313 | * disagrees, we could either let the update go through | |
1314 | * (the actual commit would re-detect and report the | |
1315 | * problem), or come up with a way of reporting such an | |
1316 | * error to *our* caller. | |
1317 | * | |
1318 | * 2. It could be that a new value is being set, but that it | |
1319 | * is identical to the current packed value of the | |
1320 | * reference. | |
1321 | * | |
1322 | * Neither of these cases will come up in the current code, | |
1323 | * because the only caller of this function passes to it a | |
1324 | * transaction that only includes `delete` updates with no | |
1325 | * `old_id`. Even if that ever changes, false positives only | |
1326 | * cause an optimization to be missed; they do not affect | |
1327 | * correctness. | |
1328 | */ | |
1329 | ||
1330 | /* | |
1331 | * Start with the cheap checks that don't require old | |
1332 | * reference values to be read: | |
1333 | */ | |
1334 | for (i = 0; i < transaction->nr; i++) { | |
1335 | struct ref_update *update = transaction->updates[i]; | |
1336 | ||
1337 | if (update->flags & REF_HAVE_OLD) | |
1338 | /* Have to check the old value -> needed. */ | |
1339 | return 1; | |
1340 | ||
1341 | if ((update->flags & REF_HAVE_NEW) && !is_null_oid(&update->new_oid)) | |
1342 | /* Have to set a new value -> needed. */ | |
1343 | return 1; | |
1344 | } | |
1345 | ||
1346 | /* | |
1347 | * The transaction isn't checking any old values nor is it | |
1348 | * setting any nonzero new values, so it still might be able | |
1349 | * to be skipped. Now do the more expensive check: the update | |
1350 | * is needed if any of the updates is a delete, and the old | |
1351 | * `packed-refs` file contains a value for that reference. | |
1352 | */ | |
1353 | ret = 0; | |
1354 | for (i = 0; i < transaction->nr; i++) { | |
1355 | struct ref_update *update = transaction->updates[i]; | |
8b72fea7 | 1356 | int failure_errno; |
7c6bd25c MH |
1357 | unsigned int type; |
1358 | struct object_id oid; | |
1359 | ||
1360 | if (!(update->flags & REF_HAVE_NEW)) | |
1361 | /* | |
1362 | * This reference isn't being deleted -> not | |
1363 | * needed. | |
1364 | */ | |
1365 | continue; | |
1366 | ||
8b72fea7 HWN |
1367 | if (!refs_read_raw_ref(ref_store, update->refname, &oid, |
1368 | &referent, &type, &failure_errno) || | |
1369 | failure_errno != ENOENT) { | |
7c6bd25c MH |
1370 | /* |
1371 | * We have to actually delete that reference | |
1372 | * -> this transaction is needed. | |
1373 | */ | |
1374 | ret = 1; | |
1375 | break; | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | strbuf_release(&referent); | |
1380 | return ret; | |
1381 | } | |
1382 | ||
2775d872 MH |
1383 | struct packed_transaction_backend_data { |
1384 | /* True iff the transaction owns the packed-refs lock. */ | |
1385 | int own_lock; | |
1386 | ||
1387 | struct string_list updates; | |
1388 | }; | |
1389 | ||
1390 | static void packed_transaction_cleanup(struct packed_ref_store *refs, | |
1391 | struct ref_transaction *transaction) | |
67be7c5a | 1392 | { |
2775d872 | 1393 | struct packed_transaction_backend_data *data = transaction->backend_data; |
67be7c5a | 1394 | |
2775d872 MH |
1395 | if (data) { |
1396 | string_list_clear(&data->updates, 0); | |
67be7c5a | 1397 | |
07f0542d | 1398 | if (is_tempfile_active(refs->tempfile)) |
2775d872 | 1399 | delete_tempfile(&refs->tempfile); |
e5cc7d7d | 1400 | |
2775d872 MH |
1401 | if (data->own_lock && is_lock_file_locked(&refs->lock)) { |
1402 | packed_refs_unlock(&refs->base); | |
1403 | data->own_lock = 0; | |
67be7c5a | 1404 | } |
67be7c5a | 1405 | |
2775d872 MH |
1406 | free(data); |
1407 | transaction->backend_data = NULL; | |
67be7c5a MH |
1408 | } |
1409 | ||
2775d872 | 1410 | transaction->state = REF_TRANSACTION_CLOSED; |
e0cc8ac8 MH |
1411 | } |
1412 | ||
1413 | static int packed_transaction_prepare(struct ref_store *ref_store, | |
1414 | struct ref_transaction *transaction, | |
1415 | struct strbuf *err) | |
1416 | { | |
2775d872 MH |
1417 | struct packed_ref_store *refs = packed_downcast( |
1418 | ref_store, | |
1419 | REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, | |
1420 | "ref_transaction_prepare"); | |
1421 | struct packed_transaction_backend_data *data; | |
1422 | size_t i; | |
1423 | int ret = TRANSACTION_GENERIC_ERROR; | |
1424 | ||
1425 | /* | |
1426 | * Note that we *don't* skip transactions with zero updates, | |
1427 | * because such a transaction might be executed for the side | |
cff28ca9 MH |
1428 | * effect of ensuring that all of the references are peeled or |
1429 | * ensuring that the `packed-refs` file is sorted. If the | |
1430 | * caller wants to optimize away empty transactions, it should | |
1431 | * do so itself. | |
2775d872 MH |
1432 | */ |
1433 | ||
ca56dadb | 1434 | CALLOC_ARRAY(data, 1); |
bc40dfb1 | 1435 | string_list_init_nodup(&data->updates); |
2775d872 MH |
1436 | |
1437 | transaction->backend_data = data; | |
1438 | ||
1439 | /* | |
1440 | * Stick the updates in a string list by refname so that we | |
1441 | * can sort them: | |
1442 | */ | |
1443 | for (i = 0; i < transaction->nr; i++) { | |
1444 | struct ref_update *update = transaction->updates[i]; | |
1445 | struct string_list_item *item = | |
1446 | string_list_append(&data->updates, update->refname); | |
1447 | ||
1448 | /* Store a pointer to update in item->util: */ | |
1449 | item->util = update; | |
1450 | } | |
1451 | string_list_sort(&data->updates); | |
1452 | ||
1453 | if (ref_update_reject_duplicates(&data->updates, err)) | |
1454 | goto failure; | |
1455 | ||
1456 | if (!is_lock_file_locked(&refs->lock)) { | |
1457 | if (packed_refs_lock(ref_store, 0, err)) | |
1458 | goto failure; | |
1459 | data->own_lock = 1; | |
1460 | } | |
1461 | ||
1462 | if (write_with_updates(refs, &data->updates, err)) | |
1463 | goto failure; | |
1464 | ||
1465 | transaction->state = REF_TRANSACTION_PREPARED; | |
1466 | return 0; | |
1467 | ||
1468 | failure: | |
1469 | packed_transaction_cleanup(refs, transaction); | |
1470 | return ret; | |
e0cc8ac8 MH |
1471 | } |
1472 | ||
1473 | static int packed_transaction_abort(struct ref_store *ref_store, | |
1474 | struct ref_transaction *transaction, | |
1475 | struct strbuf *err) | |
1476 | { | |
2775d872 MH |
1477 | struct packed_ref_store *refs = packed_downcast( |
1478 | ref_store, | |
1479 | REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, | |
1480 | "ref_transaction_abort"); | |
1481 | ||
1482 | packed_transaction_cleanup(refs, transaction); | |
1483 | return 0; | |
e0cc8ac8 MH |
1484 | } |
1485 | ||
1486 | static int packed_transaction_finish(struct ref_store *ref_store, | |
1487 | struct ref_transaction *transaction, | |
1488 | struct strbuf *err) | |
1489 | { | |
2775d872 MH |
1490 | struct packed_ref_store *refs = packed_downcast( |
1491 | ref_store, | |
1492 | REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, | |
1493 | "ref_transaction_finish"); | |
1494 | int ret = TRANSACTION_GENERIC_ERROR; | |
1495 | char *packed_refs_path; | |
1496 | ||
cff28ca9 | 1497 | clear_snapshot(refs); |
5b633610 | 1498 | |
2775d872 MH |
1499 | packed_refs_path = get_locked_file_path(&refs->lock); |
1500 | if (rename_tempfile(&refs->tempfile, packed_refs_path)) { | |
1501 | strbuf_addf(err, "error replacing %s: %s", | |
1502 | refs->path, strerror(errno)); | |
1503 | goto cleanup; | |
1504 | } | |
1505 | ||
2775d872 MH |
1506 | ret = 0; |
1507 | ||
1508 | cleanup: | |
1509 | free(packed_refs_path); | |
1510 | packed_transaction_cleanup(refs, transaction); | |
1511 | return ret; | |
e0cc8ac8 MH |
1512 | } |
1513 | ||
1514 | static int packed_initial_transaction_commit(struct ref_store *ref_store, | |
1515 | struct ref_transaction *transaction, | |
1516 | struct strbuf *err) | |
1517 | { | |
1518 | return ref_transaction_commit(transaction, err); | |
1519 | } | |
1520 | ||
1521 | static int packed_delete_refs(struct ref_store *ref_store, const char *msg, | |
1522 | struct string_list *refnames, unsigned int flags) | |
1523 | { | |
2fb330ca MH |
1524 | struct strbuf err = STRBUF_INIT; |
1525 | struct ref_transaction *transaction; | |
2fb330ca MH |
1526 | int ret; |
1527 | ||
2fb330ca MH |
1528 | if (!refnames->nr) |
1529 | return 0; | |
1530 | ||
1531 | /* | |
1532 | * Since we don't check the references' old_oids, the | |
1533 | * individual updates can't fail, so we can pack all of the | |
1534 | * updates into a single transaction. | |
1535 | */ | |
1536 | ||
fbe73f61 | 1537 | transaction = ref_store_transaction_begin(ref_store, 0, &err); |
2fb330ca MH |
1538 | if (!transaction) |
1539 | return -1; | |
1540 | ||
69840cc0 PS |
1541 | ret = packed_refs_delete_refs(ref_store, transaction, |
1542 | msg, refnames, flags); | |
1543 | ||
1544 | ref_transaction_free(transaction); | |
1545 | return ret; | |
1546 | } | |
1547 | ||
1548 | int packed_refs_delete_refs(struct ref_store *ref_store, | |
1549 | struct ref_transaction *transaction, | |
1550 | const char *msg, | |
1551 | struct string_list *refnames, | |
1552 | unsigned int flags) | |
1553 | { | |
1554 | struct strbuf err = STRBUF_INIT; | |
1555 | struct string_list_item *item; | |
1556 | int ret; | |
1557 | ||
1558 | /* Assert that the ref store refers to a packed backend. */ | |
1559 | packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs"); | |
1560 | ||
2fb330ca MH |
1561 | for_each_string_list_item(item, refnames) { |
1562 | if (ref_transaction_delete(transaction, item->string, NULL, | |
1563 | flags, msg, &err)) { | |
1564 | warning(_("could not delete reference %s: %s"), | |
1565 | item->string, err.buf); | |
1566 | strbuf_reset(&err); | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | ret = ref_transaction_commit(transaction, &err); | |
1571 | ||
1572 | if (ret) { | |
1573 | if (refnames->nr == 1) | |
1574 | error(_("could not delete reference %s: %s"), | |
1575 | refnames->items[0].string, err.buf); | |
1576 | else | |
1577 | error(_("could not delete references: %s"), err.buf); | |
1578 | } | |
1579 | ||
2fb330ca MH |
1580 | strbuf_release(&err); |
1581 | return ret; | |
e0cc8ac8 MH |
1582 | } |
1583 | ||
1584 | static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags) | |
1585 | { | |
1586 | /* | |
1587 | * Packed refs are already packed. It might be that loose refs | |
1588 | * are packed *into* a packed refs store, but that is done by | |
1589 | * updating the packed references via a transaction. | |
1590 | */ | |
1591 | return 0; | |
1592 | } | |
1593 | ||
1594 | static int packed_create_symref(struct ref_store *ref_store, | |
1595 | const char *refname, const char *target, | |
1596 | const char *logmsg) | |
1597 | { | |
033abf97 | 1598 | BUG("packed reference store does not support symrefs"); |
e0cc8ac8 MH |
1599 | } |
1600 | ||
1601 | static int packed_rename_ref(struct ref_store *ref_store, | |
1602 | const char *oldrefname, const char *newrefname, | |
1603 | const char *logmsg) | |
1604 | { | |
033abf97 | 1605 | BUG("packed reference store does not support renaming references"); |
e0cc8ac8 MH |
1606 | } |
1607 | ||
3b48045c JH |
1608 | static int packed_copy_ref(struct ref_store *ref_store, |
1609 | const char *oldrefname, const char *newrefname, | |
1610 | const char *logmsg) | |
1611 | { | |
033abf97 | 1612 | BUG("packed reference store does not support copying references"); |
3b48045c JH |
1613 | } |
1614 | ||
e0cc8ac8 MH |
1615 | static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store) |
1616 | { | |
1617 | return empty_ref_iterator_begin(); | |
1618 | } | |
1619 | ||
1620 | static int packed_for_each_reflog_ent(struct ref_store *ref_store, | |
1621 | const char *refname, | |
1622 | each_reflog_ent_fn fn, void *cb_data) | |
1623 | { | |
881aebff | 1624 | BUG("packed reference store does not support reflogs"); |
e0cc8ac8 MH |
1625 | return 0; |
1626 | } | |
1627 | ||
1628 | static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store, | |
1629 | const char *refname, | |
1630 | each_reflog_ent_fn fn, | |
1631 | void *cb_data) | |
1632 | { | |
881aebff | 1633 | BUG("packed reference store does not support reflogs"); |
e0cc8ac8 MH |
1634 | return 0; |
1635 | } | |
1636 | ||
1637 | static int packed_reflog_exists(struct ref_store *ref_store, | |
1638 | const char *refname) | |
1639 | { | |
881aebff | 1640 | BUG("packed reference store does not support reflogs"); |
e0cc8ac8 MH |
1641 | return 0; |
1642 | } | |
1643 | ||
1644 | static int packed_create_reflog(struct ref_store *ref_store, | |
7b089120 | 1645 | const char *refname, struct strbuf *err) |
e0cc8ac8 | 1646 | { |
033abf97 | 1647 | BUG("packed reference store does not support reflogs"); |
e0cc8ac8 MH |
1648 | } |
1649 | ||
1650 | static int packed_delete_reflog(struct ref_store *ref_store, | |
1651 | const char *refname) | |
1652 | { | |
881aebff | 1653 | BUG("packed reference store does not support reflogs"); |
e0cc8ac8 MH |
1654 | return 0; |
1655 | } | |
1656 | ||
1657 | static int packed_reflog_expire(struct ref_store *ref_store, | |
cc40b5ce | 1658 | const char *refname, |
e0cc8ac8 MH |
1659 | unsigned int flags, |
1660 | reflog_expiry_prepare_fn prepare_fn, | |
1661 | reflog_expiry_should_prune_fn should_prune_fn, | |
1662 | reflog_expiry_cleanup_fn cleanup_fn, | |
1663 | void *policy_cb_data) | |
1664 | { | |
881aebff | 1665 | BUG("packed reference store does not support reflogs"); |
e0cc8ac8 MH |
1666 | return 0; |
1667 | } | |
1668 | ||
1669 | struct ref_storage_be refs_be_packed = { | |
1670 | NULL, | |
1671 | "packed", | |
1672 | packed_ref_store_create, | |
1673 | packed_init_db, | |
1674 | packed_transaction_prepare, | |
1675 | packed_transaction_finish, | |
1676 | packed_transaction_abort, | |
1677 | packed_initial_transaction_commit, | |
1678 | ||
1679 | packed_pack_refs, | |
e0cc8ac8 MH |
1680 | packed_create_symref, |
1681 | packed_delete_refs, | |
1682 | packed_rename_ref, | |
3b48045c | 1683 | packed_copy_ref, |
e0cc8ac8 MH |
1684 | |
1685 | packed_ref_iterator_begin, | |
1686 | packed_read_raw_ref, | |
1687 | ||
1688 | packed_reflog_iterator_begin, | |
1689 | packed_for_each_reflog_ent, | |
1690 | packed_for_each_reflog_ent_reverse, | |
1691 | packed_reflog_exists, | |
1692 | packed_create_reflog, | |
1693 | packed_delete_reflog, | |
1694 | packed_reflog_expire | |
1695 | }; |