]>
Commit | Line | Data |
---|---|---|
67be7c5a | 1 | #include "../cache.h" |
44c2339e | 2 | #include "../config.h" |
67be7c5a MH |
3 | #include "../refs.h" |
4 | #include "refs-internal.h" | |
5 | #include "ref-cache.h" | |
6 | #include "packed-backend.h" | |
7 | #include "../iterator.h" | |
8 | #include "../lockfile.h" | |
9 | ||
10 | struct packed_ref_cache { | |
11 | struct ref_cache *cache; | |
12 | ||
13 | /* | |
14 | * Count of references to the data structure in this instance, | |
15 | * including the pointer from files_ref_store::packed if any. | |
16 | * The data will not be freed as long as the reference count | |
17 | * is nonzero. | |
18 | */ | |
19 | unsigned int referrers; | |
20 | ||
21 | /* The metadata from when this packed-refs cache was read */ | |
22 | struct stat_validity validity; | |
23 | }; | |
24 | ||
25 | /* | |
26 | * Increment the reference count of *packed_refs. | |
27 | */ | |
28 | static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs) | |
29 | { | |
30 | packed_refs->referrers++; | |
31 | } | |
32 | ||
33 | /* | |
34 | * Decrease the reference count of *packed_refs. If it goes to zero, | |
35 | * free *packed_refs and return true; otherwise return false. | |
36 | */ | |
37 | static int release_packed_ref_cache(struct packed_ref_cache *packed_refs) | |
38 | { | |
39 | if (!--packed_refs->referrers) { | |
40 | free_ref_cache(packed_refs->cache); | |
41 | stat_validity_clear(&packed_refs->validity); | |
42 | free(packed_refs); | |
43 | return 1; | |
44 | } else { | |
45 | return 0; | |
46 | } | |
47 | } | |
48 | ||
49 | /* | |
50 | * A container for `packed-refs`-related data. It is not (yet) a | |
51 | * `ref_store`. | |
52 | */ | |
53 | struct packed_ref_store { | |
e0cc8ac8 MH |
54 | struct ref_store base; |
55 | ||
67be7c5a MH |
56 | unsigned int store_flags; |
57 | ||
58 | /* The path of the "packed-refs" file: */ | |
59 | char *path; | |
60 | ||
61 | /* | |
62 | * A cache of the values read from the `packed-refs` file, if | |
63 | * it might still be current; otherwise, NULL. | |
64 | */ | |
65 | struct packed_ref_cache *cache; | |
66 | ||
67 | /* | |
68 | * Lock used for the "packed-refs" file. Note that this (and | |
69 | * thus the enclosing `packed_ref_store`) must not be freed. | |
70 | */ | |
71 | struct lock_file lock; | |
42dfa7ec MH |
72 | |
73 | /* | |
74 | * Temporary file used when rewriting new contents to the | |
75 | * "packed-refs" file. Note that this (and thus the enclosing | |
76 | * `packed_ref_store`) must not be freed. | |
77 | */ | |
78 | struct tempfile tempfile; | |
67be7c5a MH |
79 | }; |
80 | ||
e0cc8ac8 MH |
81 | struct ref_store *packed_ref_store_create(const char *path, |
82 | unsigned int store_flags) | |
67be7c5a MH |
83 | { |
84 | struct packed_ref_store *refs = xcalloc(1, sizeof(*refs)); | |
e0cc8ac8 | 85 | struct ref_store *ref_store = (struct ref_store *)refs; |
67be7c5a | 86 | |
e0cc8ac8 | 87 | base_ref_store_init(ref_store, &refs_be_packed); |
67be7c5a | 88 | refs->store_flags = store_flags; |
e0cc8ac8 | 89 | |
67be7c5a | 90 | refs->path = xstrdup(path); |
e0cc8ac8 | 91 | return ref_store; |
67be7c5a MH |
92 | } |
93 | ||
94 | /* | |
95 | * Die if refs is not the main ref store. caller is used in any | |
96 | * necessary error messages. | |
97 | */ | |
98 | static void packed_assert_main_repository(struct packed_ref_store *refs, | |
99 | const char *caller) | |
100 | { | |
101 | if (refs->store_flags & REF_STORE_MAIN) | |
102 | return; | |
103 | ||
104 | die("BUG: operation %s only allowed for main ref store", caller); | |
105 | } | |
106 | ||
e0cc8ac8 MH |
107 | /* |
108 | * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is | |
109 | * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't | |
110 | * support at least the flags specified in `required_flags`. `caller` | |
111 | * is used in any necessary error messages. | |
112 | */ | |
113 | static struct packed_ref_store *packed_downcast(struct ref_store *ref_store, | |
114 | unsigned int required_flags, | |
115 | const char *caller) | |
116 | { | |
117 | struct packed_ref_store *refs; | |
118 | ||
119 | if (ref_store->be != &refs_be_packed) | |
120 | die("BUG: ref_store is type \"%s\" not \"packed\" in %s", | |
121 | ref_store->be->name, caller); | |
122 | ||
123 | refs = (struct packed_ref_store *)ref_store; | |
124 | ||
125 | if ((refs->store_flags & required_flags) != required_flags) | |
126 | die("BUG: unallowed operation (%s), requires %x, has %x\n", | |
127 | caller, required_flags, refs->store_flags); | |
128 | ||
129 | return refs; | |
130 | } | |
131 | ||
67be7c5a MH |
132 | static void clear_packed_ref_cache(struct packed_ref_store *refs) |
133 | { | |
134 | if (refs->cache) { | |
135 | struct packed_ref_cache *cache = refs->cache; | |
136 | ||
67be7c5a MH |
137 | refs->cache = NULL; |
138 | release_packed_ref_cache(cache); | |
139 | } | |
140 | } | |
141 | ||
142 | /* The length of a peeled reference line in packed-refs, including EOL: */ | |
143 | #define PEELED_LINE_LENGTH 42 | |
144 | ||
145 | /* | |
146 | * Parse one line from a packed-refs file. Write the SHA1 to sha1. | |
147 | * Return a pointer to the refname within the line (null-terminated), | |
148 | * or NULL if there was a problem. | |
149 | */ | |
150 | static const char *parse_ref_line(struct strbuf *line, struct object_id *oid) | |
151 | { | |
152 | const char *ref; | |
153 | ||
154 | if (parse_oid_hex(line->buf, oid, &ref) < 0) | |
155 | return NULL; | |
156 | if (!isspace(*ref++)) | |
157 | return NULL; | |
158 | ||
159 | if (isspace(*ref)) | |
160 | return NULL; | |
161 | ||
162 | if (line->buf[line->len - 1] != '\n') | |
163 | return NULL; | |
164 | line->buf[--line->len] = 0; | |
165 | ||
166 | return ref; | |
167 | } | |
168 | ||
169 | /* | |
170 | * Read from `packed_refs_file` into a newly-allocated | |
171 | * `packed_ref_cache` and return it. The return value will already | |
172 | * have its reference count incremented. | |
173 | * | |
174 | * A comment line of the form "# pack-refs with: " may contain zero or | |
175 | * more traits. We interpret the traits as follows: | |
176 | * | |
177 | * No traits: | |
178 | * | |
179 | * Probably no references are peeled. But if the file contains a | |
180 | * peeled value for a reference, we will use it. | |
181 | * | |
182 | * peeled: | |
183 | * | |
184 | * References under "refs/tags/", if they *can* be peeled, *are* | |
185 | * peeled in this file. References outside of "refs/tags/" are | |
186 | * probably not peeled even if they could have been, but if we find | |
187 | * a peeled value for such a reference we will use it. | |
188 | * | |
189 | * fully-peeled: | |
190 | * | |
191 | * All references in the file that can be peeled are peeled. | |
192 | * Inversely (and this is more important), any references in the | |
193 | * file for which no peeled value is recorded is not peelable. This | |
194 | * trait should typically be written alongside "peeled" for | |
195 | * compatibility with older clients, but we do not require it | |
196 | * (i.e., "peeled" is a no-op if "fully-peeled" is set). | |
197 | */ | |
198 | static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file) | |
199 | { | |
200 | FILE *f; | |
201 | struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs)); | |
202 | struct ref_entry *last = NULL; | |
203 | struct strbuf line = STRBUF_INIT; | |
204 | enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; | |
205 | struct ref_dir *dir; | |
206 | ||
207 | acquire_packed_ref_cache(packed_refs); | |
208 | packed_refs->cache = create_ref_cache(NULL, NULL); | |
209 | packed_refs->cache->root->flag &= ~REF_INCOMPLETE; | |
210 | ||
211 | f = fopen(packed_refs_file, "r"); | |
212 | if (!f) { | |
213 | if (errno == ENOENT) { | |
214 | /* | |
215 | * This is OK; it just means that no | |
216 | * "packed-refs" file has been written yet, | |
217 | * which is equivalent to it being empty. | |
218 | */ | |
219 | return packed_refs; | |
220 | } else { | |
221 | die_errno("couldn't read %s", packed_refs_file); | |
222 | } | |
223 | } | |
224 | ||
225 | stat_validity_update(&packed_refs->validity, fileno(f)); | |
226 | ||
227 | dir = get_ref_dir(packed_refs->cache->root); | |
228 | while (strbuf_getwholeline(&line, f, '\n') != EOF) { | |
229 | struct object_id oid; | |
230 | const char *refname; | |
231 | const char *traits; | |
232 | ||
9308b7f3 MH |
233 | if (!line.len || line.buf[line.len - 1] != '\n') |
234 | die("unterminated line in %s: %s", packed_refs_file, line.buf); | |
235 | ||
67be7c5a MH |
236 | if (skip_prefix(line.buf, "# pack-refs with:", &traits)) { |
237 | if (strstr(traits, " fully-peeled ")) | |
238 | peeled = PEELED_FULLY; | |
239 | else if (strstr(traits, " peeled ")) | |
240 | peeled = PEELED_TAGS; | |
241 | /* perhaps other traits later as well */ | |
242 | continue; | |
243 | } | |
244 | ||
245 | refname = parse_ref_line(&line, &oid); | |
246 | if (refname) { | |
247 | int flag = REF_ISPACKED; | |
248 | ||
249 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
250 | if (!refname_is_safe(refname)) | |
251 | die("packed refname is dangerous: %s", refname); | |
252 | oidclr(&oid); | |
253 | flag |= REF_BAD_NAME | REF_ISBROKEN; | |
254 | } | |
255 | last = create_ref_entry(refname, &oid, flag); | |
256 | if (peeled == PEELED_FULLY || | |
257 | (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/"))) | |
258 | last->flag |= REF_KNOWS_PEELED; | |
259 | add_ref_entry(dir, last); | |
9308b7f3 | 260 | } else if (last && |
67be7c5a MH |
261 | line.buf[0] == '^' && |
262 | line.len == PEELED_LINE_LENGTH && | |
263 | line.buf[PEELED_LINE_LENGTH - 1] == '\n' && | |
264 | !get_oid_hex(line.buf + 1, &oid)) { | |
265 | oidcpy(&last->u.value.peeled, &oid); | |
266 | /* | |
267 | * Regardless of what the file header said, | |
268 | * we definitely know the value of *this* | |
269 | * reference: | |
270 | */ | |
271 | last->flag |= REF_KNOWS_PEELED; | |
9308b7f3 MH |
272 | } else { |
273 | strbuf_setlen(&line, line.len - 1); | |
274 | die("unexpected line in %s: %s", packed_refs_file, line.buf); | |
67be7c5a MH |
275 | } |
276 | } | |
277 | ||
278 | fclose(f); | |
279 | strbuf_release(&line); | |
280 | ||
281 | return packed_refs; | |
282 | } | |
283 | ||
284 | /* | |
285 | * Check that the packed refs cache (if any) still reflects the | |
286 | * contents of the file. If not, clear the cache. | |
287 | */ | |
288 | static void validate_packed_ref_cache(struct packed_ref_store *refs) | |
289 | { | |
290 | if (refs->cache && | |
291 | !stat_validity_check(&refs->cache->validity, refs->path)) | |
292 | clear_packed_ref_cache(refs); | |
293 | } | |
294 | ||
295 | /* | |
296 | * Get the packed_ref_cache for the specified packed_ref_store, | |
297 | * creating and populating it if it hasn't been read before or if the | |
298 | * file has been changed (according to its `validity` field) since it | |
299 | * was last read. On the other hand, if we hold the lock, then assume | |
300 | * that the file hasn't been changed out from under us, so skip the | |
301 | * extra `stat()` call in `stat_validity_check()`. | |
302 | */ | |
303 | static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs) | |
304 | { | |
305 | if (!is_lock_file_locked(&refs->lock)) | |
306 | validate_packed_ref_cache(refs); | |
307 | ||
308 | if (!refs->cache) | |
309 | refs->cache = read_packed_refs(refs->path); | |
310 | ||
311 | return refs->cache; | |
312 | } | |
313 | ||
314 | static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) | |
315 | { | |
316 | return get_ref_dir(packed_ref_cache->cache->root); | |
317 | } | |
318 | ||
319 | static struct ref_dir *get_packed_refs(struct packed_ref_store *refs) | |
320 | { | |
321 | return get_packed_ref_dir(get_packed_ref_cache(refs)); | |
322 | } | |
323 | ||
324 | /* | |
325 | * Add or overwrite a reference in the in-memory packed reference | |
326 | * cache. This may only be called while the packed-refs file is locked | |
b7de57d8 | 327 | * (see packed_refs_lock()). To actually write the packed-refs file, |
67be7c5a MH |
328 | * call commit_packed_refs(). |
329 | */ | |
e0cc8ac8 | 330 | void add_packed_ref(struct ref_store *ref_store, |
67be7c5a MH |
331 | const char *refname, const struct object_id *oid) |
332 | { | |
e0cc8ac8 MH |
333 | struct packed_ref_store *refs = |
334 | packed_downcast(ref_store, REF_STORE_WRITE, | |
335 | "add_packed_ref"); | |
67be7c5a MH |
336 | struct ref_dir *packed_refs; |
337 | struct ref_entry *packed_entry; | |
338 | ||
339 | if (!is_lock_file_locked(&refs->lock)) | |
340 | die("BUG: packed refs not locked"); | |
341 | ||
342 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) | |
343 | die("Reference has invalid format: '%s'", refname); | |
344 | ||
345 | packed_refs = get_packed_refs(refs); | |
346 | packed_entry = find_ref_entry(packed_refs, refname); | |
347 | if (packed_entry) { | |
348 | /* Overwrite the existing entry: */ | |
349 | oidcpy(&packed_entry->u.value.oid, oid); | |
350 | packed_entry->flag = REF_ISPACKED; | |
351 | oidclr(&packed_entry->u.value.peeled); | |
352 | } else { | |
353 | packed_entry = create_ref_entry(refname, oid, REF_ISPACKED); | |
354 | add_ref_entry(packed_refs, packed_entry); | |
355 | } | |
356 | } | |
357 | ||
358 | /* | |
359 | * Return the ref_entry for the given refname from the packed | |
360 | * references. If it does not exist, return NULL. | |
361 | */ | |
362 | static struct ref_entry *get_packed_ref(struct packed_ref_store *refs, | |
363 | const char *refname) | |
364 | { | |
365 | return find_ref_entry(get_packed_refs(refs), refname); | |
366 | } | |
367 | ||
e0cc8ac8 MH |
368 | static int packed_read_raw_ref(struct ref_store *ref_store, |
369 | const char *refname, unsigned char *sha1, | |
370 | struct strbuf *referent, unsigned int *type) | |
67be7c5a | 371 | { |
e0cc8ac8 MH |
372 | struct packed_ref_store *refs = |
373 | packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); | |
374 | ||
67be7c5a MH |
375 | struct ref_entry *entry; |
376 | ||
377 | *type = 0; | |
378 | ||
379 | entry = get_packed_ref(refs, refname); | |
380 | if (!entry) { | |
381 | errno = ENOENT; | |
382 | return -1; | |
383 | } | |
384 | ||
385 | hashcpy(sha1, entry->u.value.oid.hash); | |
386 | *type = REF_ISPACKED; | |
387 | return 0; | |
388 | } | |
389 | ||
e0cc8ac8 MH |
390 | static int packed_peel_ref(struct ref_store *ref_store, |
391 | const char *refname, unsigned char *sha1) | |
67be7c5a | 392 | { |
e0cc8ac8 MH |
393 | struct packed_ref_store *refs = |
394 | packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB, | |
395 | "peel_ref"); | |
67be7c5a MH |
396 | struct ref_entry *r = get_packed_ref(refs, refname); |
397 | ||
398 | if (!r || peel_entry(r, 0)) | |
399 | return -1; | |
400 | ||
401 | hashcpy(sha1, r->u.value.peeled.hash); | |
402 | return 0; | |
403 | } | |
404 | ||
405 | struct packed_ref_iterator { | |
406 | struct ref_iterator base; | |
407 | ||
408 | struct packed_ref_cache *cache; | |
409 | struct ref_iterator *iter0; | |
410 | unsigned int flags; | |
411 | }; | |
412 | ||
413 | static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator) | |
414 | { | |
415 | struct packed_ref_iterator *iter = | |
416 | (struct packed_ref_iterator *)ref_iterator; | |
417 | int ok; | |
418 | ||
419 | while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) { | |
420 | if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && | |
421 | ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE) | |
422 | continue; | |
423 | ||
424 | if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && | |
425 | !ref_resolves_to_object(iter->iter0->refname, | |
426 | iter->iter0->oid, | |
427 | iter->iter0->flags)) | |
428 | continue; | |
429 | ||
430 | iter->base.refname = iter->iter0->refname; | |
431 | iter->base.oid = iter->iter0->oid; | |
432 | iter->base.flags = iter->iter0->flags; | |
433 | return ITER_OK; | |
434 | } | |
435 | ||
436 | iter->iter0 = NULL; | |
437 | if (ref_iterator_abort(ref_iterator) != ITER_DONE) | |
438 | ok = ITER_ERROR; | |
439 | ||
440 | return ok; | |
441 | } | |
442 | ||
443 | static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator, | |
444 | struct object_id *peeled) | |
445 | { | |
446 | struct packed_ref_iterator *iter = | |
447 | (struct packed_ref_iterator *)ref_iterator; | |
448 | ||
449 | return ref_iterator_peel(iter->iter0, peeled); | |
450 | } | |
451 | ||
452 | static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator) | |
453 | { | |
454 | struct packed_ref_iterator *iter = | |
455 | (struct packed_ref_iterator *)ref_iterator; | |
456 | int ok = ITER_DONE; | |
457 | ||
458 | if (iter->iter0) | |
459 | ok = ref_iterator_abort(iter->iter0); | |
460 | ||
461 | release_packed_ref_cache(iter->cache); | |
462 | base_ref_iterator_free(ref_iterator); | |
463 | return ok; | |
464 | } | |
465 | ||
466 | static struct ref_iterator_vtable packed_ref_iterator_vtable = { | |
467 | packed_ref_iterator_advance, | |
468 | packed_ref_iterator_peel, | |
469 | packed_ref_iterator_abort | |
470 | }; | |
471 | ||
e0cc8ac8 MH |
472 | static struct ref_iterator *packed_ref_iterator_begin( |
473 | struct ref_store *ref_store, | |
67be7c5a MH |
474 | const char *prefix, unsigned int flags) |
475 | { | |
e0cc8ac8 | 476 | struct packed_ref_store *refs; |
67be7c5a MH |
477 | struct packed_ref_iterator *iter; |
478 | struct ref_iterator *ref_iterator; | |
e0cc8ac8 MH |
479 | unsigned int required_flags = REF_STORE_READ; |
480 | ||
481 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) | |
482 | required_flags |= REF_STORE_ODB; | |
483 | refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin"); | |
67be7c5a MH |
484 | |
485 | iter = xcalloc(1, sizeof(*iter)); | |
486 | ref_iterator = &iter->base; | |
487 | base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable); | |
488 | ||
489 | /* | |
490 | * Note that get_packed_ref_cache() internally checks whether | |
491 | * the packed-ref cache is up to date with what is on disk, | |
492 | * and re-reads it if not. | |
493 | */ | |
494 | ||
495 | iter->cache = get_packed_ref_cache(refs); | |
496 | acquire_packed_ref_cache(iter->cache); | |
497 | iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0); | |
498 | ||
499 | iter->flags = flags; | |
500 | ||
501 | return ref_iterator; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Write an entry to the packed-refs file for the specified refname. | |
3478983b MH |
506 | * If peeled is non-NULL, write it as the entry's peeled value. On |
507 | * error, return a nonzero value and leave errno set at the value left | |
508 | * by the failing call to `fprintf()`. | |
67be7c5a | 509 | */ |
3478983b MH |
510 | static int write_packed_entry(FILE *fh, const char *refname, |
511 | const unsigned char *sha1, | |
512 | const unsigned char *peeled) | |
67be7c5a | 513 | { |
3478983b MH |
514 | if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 || |
515 | (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0)) | |
516 | return -1; | |
517 | ||
518 | return 0; | |
67be7c5a MH |
519 | } |
520 | ||
c8bed835 | 521 | int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err) |
67be7c5a | 522 | { |
e0cc8ac8 MH |
523 | struct packed_ref_store *refs = |
524 | packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN, | |
b7de57d8 | 525 | "packed_refs_lock"); |
67be7c5a MH |
526 | static int timeout_configured = 0; |
527 | static int timeout_value = 1000; | |
528 | struct packed_ref_cache *packed_ref_cache; | |
529 | ||
67be7c5a MH |
530 | if (!timeout_configured) { |
531 | git_config_get_int("core.packedrefstimeout", &timeout_value); | |
532 | timeout_configured = 1; | |
533 | } | |
534 | ||
42dfa7ec MH |
535 | /* |
536 | * Note that we close the lockfile immediately because we | |
537 | * don't write new content to it, but rather to a separate | |
538 | * tempfile. | |
539 | */ | |
67be7c5a MH |
540 | if (hold_lock_file_for_update_timeout( |
541 | &refs->lock, | |
542 | refs->path, | |
c8bed835 MH |
543 | flags, timeout_value) < 0) { |
544 | unable_to_lock_message(refs->path, errno, err); | |
545 | return -1; | |
546 | } | |
547 | ||
548 | if (close_lock_file(&refs->lock)) { | |
549 | strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno)); | |
67be7c5a | 550 | return -1; |
c8bed835 | 551 | } |
67be7c5a MH |
552 | |
553 | /* | |
554 | * Now that we hold the `packed-refs` lock, make sure that our | |
555 | * cache matches the current version of the file. Normally | |
556 | * `get_packed_ref_cache()` does that for us, but that | |
557 | * function assumes that when the file is locked, any existing | |
558 | * cache is still valid. We've just locked the file, but it | |
559 | * might have changed the moment *before* we locked it. | |
560 | */ | |
561 | validate_packed_ref_cache(refs); | |
562 | ||
563 | packed_ref_cache = get_packed_ref_cache(refs); | |
564 | /* Increment the reference count to prevent it from being freed: */ | |
565 | acquire_packed_ref_cache(packed_ref_cache); | |
566 | return 0; | |
567 | } | |
568 | ||
49aebcf4 MH |
569 | void packed_refs_unlock(struct ref_store *ref_store) |
570 | { | |
571 | struct packed_ref_store *refs = packed_downcast( | |
572 | ref_store, | |
573 | REF_STORE_READ | REF_STORE_WRITE, | |
574 | "packed_refs_unlock"); | |
575 | ||
576 | if (!is_lock_file_locked(&refs->lock)) | |
577 | die("BUG: packed_refs_unlock() called when not locked"); | |
578 | rollback_lock_file(&refs->lock); | |
579 | release_packed_ref_cache(refs->cache); | |
580 | } | |
581 | ||
582 | int packed_refs_is_locked(struct ref_store *ref_store) | |
583 | { | |
584 | struct packed_ref_store *refs = packed_downcast( | |
585 | ref_store, | |
586 | REF_STORE_READ | REF_STORE_WRITE, | |
587 | "packed_refs_is_locked"); | |
588 | ||
589 | return is_lock_file_locked(&refs->lock); | |
590 | } | |
591 | ||
67be7c5a MH |
592 | /* |
593 | * The packed-refs header line that we write out. Perhaps other | |
594 | * traits will be added later. The trailing space is required. | |
595 | */ | |
596 | static const char PACKED_REFS_HEADER[] = | |
597 | "# pack-refs with: peeled fully-peeled \n"; | |
598 | ||
599 | /* | |
600 | * Write the current version of the packed refs cache from memory to | |
601 | * disk. The packed-refs file must already be locked for writing (see | |
b7de57d8 | 602 | * packed_refs_lock()). Return zero on success. On errors, rollback |
3478983b MH |
603 | * the lockfile, write an error message to `err`, and return a nonzero |
604 | * value. | |
67be7c5a | 605 | */ |
3478983b | 606 | int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err) |
67be7c5a | 607 | { |
e0cc8ac8 MH |
608 | struct packed_ref_store *refs = |
609 | packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN, | |
610 | "commit_packed_refs"); | |
67be7c5a MH |
611 | struct packed_ref_cache *packed_ref_cache = |
612 | get_packed_ref_cache(refs); | |
3478983b | 613 | int ok; |
198b808e | 614 | int ret = -1; |
42dfa7ec | 615 | struct strbuf sb = STRBUF_INIT; |
67be7c5a MH |
616 | FILE *out; |
617 | struct ref_iterator *iter; | |
198b808e | 618 | char *packed_refs_path; |
67be7c5a | 619 | |
67be7c5a | 620 | if (!is_lock_file_locked(&refs->lock)) |
3478983b | 621 | die("BUG: commit_packed_refs() called when unlocked"); |
67be7c5a | 622 | |
198b808e MH |
623 | /* |
624 | * If packed-refs is a symlink, we want to overwrite the | |
625 | * symlinked-to file, not the symlink itself. Also, put the | |
626 | * staging file next to it: | |
627 | */ | |
628 | packed_refs_path = get_locked_file_path(&refs->lock); | |
629 | strbuf_addf(&sb, "%s.new", packed_refs_path); | |
42dfa7ec MH |
630 | if (create_tempfile(&refs->tempfile, sb.buf) < 0) { |
631 | strbuf_addf(err, "unable to create file %s: %s", | |
632 | sb.buf, strerror(errno)); | |
633 | strbuf_release(&sb); | |
198b808e | 634 | goto out; |
42dfa7ec MH |
635 | } |
636 | strbuf_release(&sb); | |
637 | ||
638 | out = fdopen_tempfile(&refs->tempfile, "w"); | |
3478983b MH |
639 | if (!out) { |
640 | strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s", | |
641 | strerror(errno)); | |
642 | goto error; | |
643 | } | |
67be7c5a | 644 | |
3478983b MH |
645 | if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) { |
646 | strbuf_addf(err, "error writing to %s: %s", | |
42dfa7ec | 647 | get_tempfile_path(&refs->tempfile), strerror(errno)); |
3478983b MH |
648 | goto error; |
649 | } | |
67be7c5a MH |
650 | |
651 | iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0); | |
652 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
653 | struct object_id peeled; | |
654 | int peel_error = ref_iterator_peel(iter, &peeled); | |
655 | ||
3478983b MH |
656 | if (write_packed_entry(out, iter->refname, iter->oid->hash, |
657 | peel_error ? NULL : peeled.hash)) { | |
658 | strbuf_addf(err, "error writing to %s: %s", | |
42dfa7ec | 659 | get_tempfile_path(&refs->tempfile), |
3478983b MH |
660 | strerror(errno)); |
661 | ref_iterator_abort(iter); | |
662 | goto error; | |
663 | } | |
67be7c5a MH |
664 | } |
665 | ||
3478983b | 666 | if (ok != ITER_DONE) { |
42dfa7ec | 667 | strbuf_addf(err, "unable to rewrite packed-refs file: " |
3478983b MH |
668 | "error iterating over old contents"); |
669 | goto error; | |
670 | } | |
67be7c5a | 671 | |
198b808e | 672 | if (rename_tempfile(&refs->tempfile, packed_refs_path)) { |
42dfa7ec | 673 | strbuf_addf(err, "error replacing %s: %s", |
3478983b | 674 | refs->path, strerror(errno)); |
198b808e | 675 | goto out; |
67be7c5a | 676 | } |
3478983b | 677 | |
198b808e MH |
678 | ret = 0; |
679 | goto out; | |
3478983b MH |
680 | |
681 | error: | |
42dfa7ec | 682 | delete_tempfile(&refs->tempfile); |
198b808e MH |
683 | |
684 | out: | |
685 | free(packed_refs_path); | |
686 | return ret; | |
67be7c5a MH |
687 | } |
688 | ||
67be7c5a MH |
689 | /* |
690 | * Rewrite the packed-refs file, omitting any refs listed in | |
691 | * 'refnames'. On error, leave packed-refs unchanged, write an error | |
e5cc7d7d MH |
692 | * message to 'err', and return a nonzero value. The packed refs lock |
693 | * must be held when calling this function; it will still be held when | |
694 | * the function returns. | |
67be7c5a MH |
695 | * |
696 | * The refs in 'refnames' needn't be sorted. `err` must not be NULL. | |
697 | */ | |
e0cc8ac8 | 698 | int repack_without_refs(struct ref_store *ref_store, |
67be7c5a MH |
699 | struct string_list *refnames, struct strbuf *err) |
700 | { | |
e0cc8ac8 MH |
701 | struct packed_ref_store *refs = |
702 | packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN, | |
703 | "repack_without_refs"); | |
67be7c5a MH |
704 | struct ref_dir *packed; |
705 | struct string_list_item *refname; | |
3478983b | 706 | int needs_repacking = 0, removed = 0; |
67be7c5a MH |
707 | |
708 | packed_assert_main_repository(refs, "repack_without_refs"); | |
709 | assert(err); | |
710 | ||
e5cc7d7d MH |
711 | if (!is_lock_file_locked(&refs->lock)) |
712 | die("BUG: repack_without_refs called without holding lock"); | |
713 | ||
67be7c5a MH |
714 | /* Look for a packed ref */ |
715 | for_each_string_list_item(refname, refnames) { | |
716 | if (get_packed_ref(refs, refname->string)) { | |
717 | needs_repacking = 1; | |
718 | break; | |
719 | } | |
720 | } | |
721 | ||
722 | /* Avoid locking if we have nothing to do */ | |
723 | if (!needs_repacking) | |
724 | return 0; /* no refname exists in packed refs */ | |
725 | ||
67be7c5a MH |
726 | packed = get_packed_refs(refs); |
727 | ||
728 | /* Remove refnames from the cache */ | |
729 | for_each_string_list_item(refname, refnames) | |
730 | if (remove_entry_from_dir(packed, refname->string) != -1) | |
731 | removed = 1; | |
732 | if (!removed) { | |
733 | /* | |
734 | * All packed entries disappeared while we were | |
735 | * acquiring the lock. | |
736 | */ | |
e5cc7d7d | 737 | clear_packed_ref_cache(refs); |
67be7c5a MH |
738 | return 0; |
739 | } | |
740 | ||
741 | /* Write what remains */ | |
e5cc7d7d | 742 | return commit_packed_refs(&refs->base, err); |
67be7c5a | 743 | } |
e0cc8ac8 MH |
744 | |
745 | static int packed_init_db(struct ref_store *ref_store, struct strbuf *err) | |
746 | { | |
747 | /* Nothing to do. */ | |
748 | return 0; | |
749 | } | |
750 | ||
751 | static int packed_transaction_prepare(struct ref_store *ref_store, | |
752 | struct ref_transaction *transaction, | |
753 | struct strbuf *err) | |
754 | { | |
755 | die("BUG: not implemented yet"); | |
756 | } | |
757 | ||
758 | static int packed_transaction_abort(struct ref_store *ref_store, | |
759 | struct ref_transaction *transaction, | |
760 | struct strbuf *err) | |
761 | { | |
762 | die("BUG: not implemented yet"); | |
763 | } | |
764 | ||
765 | static int packed_transaction_finish(struct ref_store *ref_store, | |
766 | struct ref_transaction *transaction, | |
767 | struct strbuf *err) | |
768 | { | |
769 | die("BUG: not implemented yet"); | |
770 | } | |
771 | ||
772 | static int packed_initial_transaction_commit(struct ref_store *ref_store, | |
773 | struct ref_transaction *transaction, | |
774 | struct strbuf *err) | |
775 | { | |
776 | return ref_transaction_commit(transaction, err); | |
777 | } | |
778 | ||
779 | static int packed_delete_refs(struct ref_store *ref_store, const char *msg, | |
780 | struct string_list *refnames, unsigned int flags) | |
781 | { | |
782 | die("BUG: not implemented yet"); | |
783 | } | |
784 | ||
785 | static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags) | |
786 | { | |
787 | /* | |
788 | * Packed refs are already packed. It might be that loose refs | |
789 | * are packed *into* a packed refs store, but that is done by | |
790 | * updating the packed references via a transaction. | |
791 | */ | |
792 | return 0; | |
793 | } | |
794 | ||
795 | static int packed_create_symref(struct ref_store *ref_store, | |
796 | const char *refname, const char *target, | |
797 | const char *logmsg) | |
798 | { | |
799 | die("BUG: packed reference store does not support symrefs"); | |
800 | } | |
801 | ||
802 | static int packed_rename_ref(struct ref_store *ref_store, | |
803 | const char *oldrefname, const char *newrefname, | |
804 | const char *logmsg) | |
805 | { | |
806 | die("BUG: packed reference store does not support renaming references"); | |
807 | } | |
808 | ||
809 | static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store) | |
810 | { | |
811 | return empty_ref_iterator_begin(); | |
812 | } | |
813 | ||
814 | static int packed_for_each_reflog_ent(struct ref_store *ref_store, | |
815 | const char *refname, | |
816 | each_reflog_ent_fn fn, void *cb_data) | |
817 | { | |
818 | return 0; | |
819 | } | |
820 | ||
821 | static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store, | |
822 | const char *refname, | |
823 | each_reflog_ent_fn fn, | |
824 | void *cb_data) | |
825 | { | |
826 | return 0; | |
827 | } | |
828 | ||
829 | static int packed_reflog_exists(struct ref_store *ref_store, | |
830 | const char *refname) | |
831 | { | |
832 | return 0; | |
833 | } | |
834 | ||
835 | static int packed_create_reflog(struct ref_store *ref_store, | |
836 | const char *refname, int force_create, | |
837 | struct strbuf *err) | |
838 | { | |
839 | die("BUG: packed reference store does not support reflogs"); | |
840 | } | |
841 | ||
842 | static int packed_delete_reflog(struct ref_store *ref_store, | |
843 | const char *refname) | |
844 | { | |
845 | return 0; | |
846 | } | |
847 | ||
848 | static int packed_reflog_expire(struct ref_store *ref_store, | |
849 | const char *refname, const unsigned char *sha1, | |
850 | unsigned int flags, | |
851 | reflog_expiry_prepare_fn prepare_fn, | |
852 | reflog_expiry_should_prune_fn should_prune_fn, | |
853 | reflog_expiry_cleanup_fn cleanup_fn, | |
854 | void *policy_cb_data) | |
855 | { | |
856 | return 0; | |
857 | } | |
858 | ||
859 | struct ref_storage_be refs_be_packed = { | |
860 | NULL, | |
861 | "packed", | |
862 | packed_ref_store_create, | |
863 | packed_init_db, | |
864 | packed_transaction_prepare, | |
865 | packed_transaction_finish, | |
866 | packed_transaction_abort, | |
867 | packed_initial_transaction_commit, | |
868 | ||
869 | packed_pack_refs, | |
870 | packed_peel_ref, | |
871 | packed_create_symref, | |
872 | packed_delete_refs, | |
873 | packed_rename_ref, | |
874 | ||
875 | packed_ref_iterator_begin, | |
876 | packed_read_raw_ref, | |
877 | ||
878 | packed_reflog_iterator_begin, | |
879 | packed_for_each_reflog_ent, | |
880 | packed_for_each_reflog_ent_reverse, | |
881 | packed_reflog_exists, | |
882 | packed_create_reflog, | |
883 | packed_delete_reflog, | |
884 | packed_reflog_expire | |
885 | }; |