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