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