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