read_packed_refs(): only check for a header at the top of the file
[git/git.git] / refs / packed-backend.c
CommitLineData
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
f0a7dc86
MH
10struct packed_ref_store;
11
67be7c5a 12struct packed_ref_cache {
f0a7dc86
MH
13 /*
14 * A back-pointer to the packed_ref_store with which this
15 * cache is associated:
16 */
17 struct packed_ref_store *refs;
18
67be7c5a
MH
19 struct ref_cache *cache;
20
21 /*
22 * Count of references to the data structure in this instance,
23 * including the pointer from files_ref_store::packed if any.
24 * The data will not be freed as long as the reference count
25 * is nonzero.
26 */
27 unsigned int referrers;
28
29 /* The metadata from when this packed-refs cache was read */
30 struct stat_validity validity;
31};
32
33/*
34 * Increment the reference count of *packed_refs.
35 */
36static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
37{
38 packed_refs->referrers++;
39}
40
41/*
42 * Decrease the reference count of *packed_refs. If it goes to zero,
43 * free *packed_refs and return true; otherwise return false.
44 */
45static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
46{
47 if (!--packed_refs->referrers) {
48 free_ref_cache(packed_refs->cache);
49 stat_validity_clear(&packed_refs->validity);
50 free(packed_refs);
51 return 1;
52 } else {
53 return 0;
54 }
55}
56
57/*
58 * A container for `packed-refs`-related data. It is not (yet) a
59 * `ref_store`.
60 */
61struct packed_ref_store {
e0cc8ac8
MH
62 struct ref_store base;
63
67be7c5a
MH
64 unsigned int store_flags;
65
66 /* The path of the "packed-refs" file: */
67 char *path;
68
69 /*
70 * A cache of the values read from the `packed-refs` file, if
71 * it might still be current; otherwise, NULL.
72 */
73 struct packed_ref_cache *cache;
74
75 /*
76 * Lock used for the "packed-refs" file. Note that this (and
77 * thus the enclosing `packed_ref_store`) must not be freed.
78 */
79 struct lock_file lock;
42dfa7ec
MH
80
81 /*
82 * Temporary file used when rewriting new contents to the
83 * "packed-refs" file. Note that this (and thus the enclosing
84 * `packed_ref_store`) must not be freed.
85 */
86 struct tempfile tempfile;
67be7c5a
MH
87};
88
e0cc8ac8
MH
89struct ref_store *packed_ref_store_create(const char *path,
90 unsigned int store_flags)
67be7c5a
MH
91{
92 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
e0cc8ac8 93 struct ref_store *ref_store = (struct ref_store *)refs;
67be7c5a 94
e0cc8ac8 95 base_ref_store_init(ref_store, &refs_be_packed);
67be7c5a 96 refs->store_flags = store_flags;
e0cc8ac8 97
67be7c5a 98 refs->path = xstrdup(path);
e0cc8ac8 99 return ref_store;
67be7c5a
MH
100}
101
e0cc8ac8
MH
102/*
103 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
104 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
105 * support at least the flags specified in `required_flags`. `caller`
106 * is used in any necessary error messages.
107 */
108static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
109 unsigned int required_flags,
110 const char *caller)
111{
112 struct packed_ref_store *refs;
113
114 if (ref_store->be != &refs_be_packed)
115 die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
116 ref_store->be->name, caller);
117
118 refs = (struct packed_ref_store *)ref_store;
119
120 if ((refs->store_flags & required_flags) != required_flags)
121 die("BUG: unallowed operation (%s), requires %x, has %x\n",
122 caller, required_flags, refs->store_flags);
123
124 return refs;
125}
126
67be7c5a
MH
127static void clear_packed_ref_cache(struct packed_ref_store *refs)
128{
129 if (refs->cache) {
130 struct packed_ref_cache *cache = refs->cache;
131
67be7c5a
MH
132 refs->cache = NULL;
133 release_packed_ref_cache(cache);
134 }
135}
136
137/* The length of a peeled reference line in packed-refs, including EOL: */
138#define PEELED_LINE_LENGTH 42
139
140/*
141 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
142 * Return a pointer to the refname within the line (null-terminated),
143 * or NULL if there was a problem.
144 */
145static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
146{
147 const char *ref;
148
149 if (parse_oid_hex(line->buf, oid, &ref) < 0)
150 return NULL;
151 if (!isspace(*ref++))
152 return NULL;
153
154 if (isspace(*ref))
155 return NULL;
156
157 if (line->buf[line->len - 1] != '\n')
158 return NULL;
159 line->buf[--line->len] = 0;
160
161 return ref;
162}
163
735267aa
MH
164static NORETURN void die_unterminated_line(const char *path,
165 const char *p, size_t len)
166{
167 if (len < 80)
168 die("unterminated line in %s: %.*s", path, (int)len, p);
169 else
170 die("unterminated line in %s: %.75s...", path, p);
171}
172
173static NORETURN void die_invalid_line(const char *path,
174 const char *p, size_t len)
175{
176 const char *eol = memchr(p, '\n', len);
177
178 if (!eol)
179 die_unterminated_line(path, p, len);
180 else if (eol - p < 80)
181 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
182 else
183 die("unexpected line in %s: %.75s...", path, p);
184
185}
186
67be7c5a 187/*
f0a7dc86 188 * Read from the `packed-refs` file into a newly-allocated
67be7c5a
MH
189 * `packed_ref_cache` and return it. The return value will already
190 * have its reference count incremented.
191 *
192 * A comment line of the form "# pack-refs with: " may contain zero or
193 * more traits. We interpret the traits as follows:
194 *
195 * No traits:
196 *
197 * Probably no references are peeled. But if the file contains a
198 * peeled value for a reference, we will use it.
199 *
200 * peeled:
201 *
202 * References under "refs/tags/", if they *can* be peeled, *are*
203 * peeled in this file. References outside of "refs/tags/" are
204 * probably not peeled even if they could have been, but if we find
205 * a peeled value for such a reference we will use it.
206 *
207 * fully-peeled:
208 *
209 * All references in the file that can be peeled are peeled.
210 * Inversely (and this is more important), any references in the
211 * file for which no peeled value is recorded is not peelable. This
212 * trait should typically be written alongside "peeled" for
213 * compatibility with older clients, but we do not require it
214 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
215 */
f0a7dc86 216static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
67be7c5a 217{
67be7c5a 218 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
49a03ef4
MH
219 int fd;
220 struct stat st;
221 size_t size;
222 char *buf;
223 const char *pos, *eol, *eof;
67be7c5a
MH
224 struct ref_entry *last = NULL;
225 struct strbuf line = STRBUF_INIT;
226 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
227 struct ref_dir *dir;
228
f0a7dc86 229 packed_refs->refs = refs;
67be7c5a
MH
230 acquire_packed_ref_cache(packed_refs);
231 packed_refs->cache = create_ref_cache(NULL, NULL);
232 packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
233
49a03ef4
MH
234 fd = open(refs->path, O_RDONLY);
235 if (fd < 0) {
67be7c5a
MH
236 if (errno == ENOENT) {
237 /*
238 * This is OK; it just means that no
239 * "packed-refs" file has been written yet,
240 * which is equivalent to it being empty.
241 */
242 return packed_refs;
243 } else {
f0a7dc86 244 die_errno("couldn't read %s", refs->path);
67be7c5a
MH
245 }
246 }
247
49a03ef4
MH
248 stat_validity_update(&packed_refs->validity, fd);
249
250 if (fstat(fd, &st) < 0)
251 die_errno("couldn't stat %s", refs->path);
252
253 size = xsize_t(st.st_size);
254 buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
255 pos = buf;
256 eof = buf + size;
67be7c5a 257
36f23534
MH
258 /* If the file has a header line, process it: */
259 if (pos < eof && *pos == '#') {
260 const char *traits;
261
262 eol = memchr(pos, '\n', eof - pos);
263 if (!eol)
264 die_unterminated_line(refs->path, pos, eof - pos);
265
266 strbuf_add(&line, pos, eol + 1 - pos);
267
268 if (!skip_prefix(line.buf, "# pack-refs with:", &traits))
269 die_invalid_line(refs->path, pos, eof - pos);
270
271 if (strstr(traits, " fully-peeled "))
272 peeled = PEELED_FULLY;
273 else if (strstr(traits, " peeled "))
274 peeled = PEELED_TAGS;
275 /* perhaps other traits later as well */
276
277 /* The "+ 1" is for the LF character. */
278 pos = eol + 1;
279 strbuf_reset(&line);
280 }
281
67be7c5a 282 dir = get_ref_dir(packed_refs->cache->root);
49a03ef4 283 while (pos < eof) {
67be7c5a
MH
284 struct object_id oid;
285 const char *refname;
67be7c5a 286
49a03ef4
MH
287 eol = memchr(pos, '\n', eof - pos);
288 if (!eol)
289 die_unterminated_line(refs->path, pos, eof - pos);
290
291 strbuf_add(&line, pos, eol + 1 - pos);
9308b7f3 292
67be7c5a
MH
293 refname = parse_ref_line(&line, &oid);
294 if (refname) {
295 int flag = REF_ISPACKED;
296
297 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
298 if (!refname_is_safe(refname))
299 die("packed refname is dangerous: %s", refname);
300 oidclr(&oid);
301 flag |= REF_BAD_NAME | REF_ISBROKEN;
302 }
303 last = create_ref_entry(refname, &oid, flag);
304 if (peeled == PEELED_FULLY ||
305 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
306 last->flag |= REF_KNOWS_PEELED;
307 add_ref_entry(dir, last);
9308b7f3 308 } else if (last &&
67be7c5a
MH
309 line.buf[0] == '^' &&
310 line.len == PEELED_LINE_LENGTH &&
311 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
312 !get_oid_hex(line.buf + 1, &oid)) {
313 oidcpy(&last->u.value.peeled, &oid);
314 /*
315 * Regardless of what the file header said,
316 * we definitely know the value of *this*
317 * reference:
318 */
319 last->flag |= REF_KNOWS_PEELED;
9308b7f3 320 } else {
735267aa 321 die_invalid_line(refs->path, line.buf, line.len);
67be7c5a 322 }
49a03ef4 323
49a03ef4
MH
324 /* The "+ 1" is for the LF character. */
325 pos = eol + 1;
326 strbuf_reset(&line);
67be7c5a
MH
327 }
328
49a03ef4
MH
329 if (munmap(buf, size))
330 die_errno("error ummapping packed-refs file");
331 close(fd);
67be7c5a 332
49a03ef4 333 strbuf_release(&line);
67be7c5a
MH
334 return packed_refs;
335}
336
337/*
338 * Check that the packed refs cache (if any) still reflects the
339 * contents of the file. If not, clear the cache.
340 */
341static void validate_packed_ref_cache(struct packed_ref_store *refs)
342{
343 if (refs->cache &&
344 !stat_validity_check(&refs->cache->validity, refs->path))
345 clear_packed_ref_cache(refs);
346}
347
348/*
349 * Get the packed_ref_cache for the specified packed_ref_store,
350 * creating and populating it if it hasn't been read before or if the
351 * file has been changed (according to its `validity` field) since it
352 * was last read. On the other hand, if we hold the lock, then assume
353 * that the file hasn't been changed out from under us, so skip the
354 * extra `stat()` call in `stat_validity_check()`.
355 */
356static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
357{
358 if (!is_lock_file_locked(&refs->lock))
359 validate_packed_ref_cache(refs);
360
361 if (!refs->cache)
f0a7dc86 362 refs->cache = read_packed_refs(refs);
67be7c5a
MH
363
364 return refs->cache;
365}
366
367static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
368{
369 return get_ref_dir(packed_ref_cache->cache->root);
370}
371
372static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
373{
374 return get_packed_ref_dir(get_packed_ref_cache(refs));
375}
376
67be7c5a
MH
377/*
378 * Return the ref_entry for the given refname from the packed
379 * references. If it does not exist, return NULL.
380 */
381static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
382 const char *refname)
383{
384 return find_ref_entry(get_packed_refs(refs), refname);
385}
386
e0cc8ac8
MH
387static int packed_read_raw_ref(struct ref_store *ref_store,
388 const char *refname, unsigned char *sha1,
389 struct strbuf *referent, unsigned int *type)
67be7c5a 390{
e0cc8ac8
MH
391 struct packed_ref_store *refs =
392 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
393
67be7c5a
MH
394 struct ref_entry *entry;
395
396 *type = 0;
397
398 entry = get_packed_ref(refs, refname);
399 if (!entry) {
400 errno = ENOENT;
401 return -1;
402 }
403
404 hashcpy(sha1, entry->u.value.oid.hash);
405 *type = REF_ISPACKED;
406 return 0;
407}
408
e0cc8ac8
MH
409static int packed_peel_ref(struct ref_store *ref_store,
410 const char *refname, unsigned char *sha1)
67be7c5a 411{
e0cc8ac8
MH
412 struct packed_ref_store *refs =
413 packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
414 "peel_ref");
67be7c5a
MH
415 struct ref_entry *r = get_packed_ref(refs, refname);
416
417 if (!r || peel_entry(r, 0))
418 return -1;
419
420 hashcpy(sha1, r->u.value.peeled.hash);
421 return 0;
422}
423
424struct packed_ref_iterator {
425 struct ref_iterator base;
426
427 struct packed_ref_cache *cache;
428 struct ref_iterator *iter0;
429 unsigned int flags;
430};
431
432static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
433{
434 struct packed_ref_iterator *iter =
435 (struct packed_ref_iterator *)ref_iterator;
436 int ok;
437
438 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
439 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
440 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
441 continue;
442
443 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
444 !ref_resolves_to_object(iter->iter0->refname,
445 iter->iter0->oid,
446 iter->iter0->flags))
447 continue;
448
449 iter->base.refname = iter->iter0->refname;
450 iter->base.oid = iter->iter0->oid;
451 iter->base.flags = iter->iter0->flags;
452 return ITER_OK;
453 }
454
455 iter->iter0 = NULL;
456 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
457 ok = ITER_ERROR;
458
459 return ok;
460}
461
462static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
463 struct object_id *peeled)
464{
465 struct packed_ref_iterator *iter =
466 (struct packed_ref_iterator *)ref_iterator;
467
468 return ref_iterator_peel(iter->iter0, peeled);
469}
470
471static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
472{
473 struct packed_ref_iterator *iter =
474 (struct packed_ref_iterator *)ref_iterator;
475 int ok = ITER_DONE;
476
477 if (iter->iter0)
478 ok = ref_iterator_abort(iter->iter0);
479
480 release_packed_ref_cache(iter->cache);
481 base_ref_iterator_free(ref_iterator);
482 return ok;
483}
484
485static struct ref_iterator_vtable packed_ref_iterator_vtable = {
486 packed_ref_iterator_advance,
487 packed_ref_iterator_peel,
488 packed_ref_iterator_abort
489};
490
e0cc8ac8
MH
491static struct ref_iterator *packed_ref_iterator_begin(
492 struct ref_store *ref_store,
67be7c5a
MH
493 const char *prefix, unsigned int flags)
494{
e0cc8ac8 495 struct packed_ref_store *refs;
67be7c5a
MH
496 struct packed_ref_iterator *iter;
497 struct ref_iterator *ref_iterator;
e0cc8ac8
MH
498 unsigned int required_flags = REF_STORE_READ;
499
500 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
501 required_flags |= REF_STORE_ODB;
502 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
67be7c5a
MH
503
504 iter = xcalloc(1, sizeof(*iter));
505 ref_iterator = &iter->base;
8738a8a4 506 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
67be7c5a
MH
507
508 /*
509 * Note that get_packed_ref_cache() internally checks whether
510 * the packed-ref cache is up to date with what is on disk,
511 * and re-reads it if not.
512 */
513
514 iter->cache = get_packed_ref_cache(refs);
515 acquire_packed_ref_cache(iter->cache);
516 iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
517
518 iter->flags = flags;
519
520 return ref_iterator;
521}
522
523/*
524 * Write an entry to the packed-refs file for the specified refname.
3478983b
MH
525 * If peeled is non-NULL, write it as the entry's peeled value. On
526 * error, return a nonzero value and leave errno set at the value left
527 * by the failing call to `fprintf()`.
67be7c5a 528 */
3478983b
MH
529static int write_packed_entry(FILE *fh, const char *refname,
530 const unsigned char *sha1,
531 const unsigned char *peeled)
67be7c5a 532{
3478983b
MH
533 if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
534 (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
535 return -1;
536
537 return 0;
67be7c5a
MH
538}
539
c8bed835 540int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
67be7c5a 541{
e0cc8ac8
MH
542 struct packed_ref_store *refs =
543 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
b7de57d8 544 "packed_refs_lock");
67be7c5a
MH
545 static int timeout_configured = 0;
546 static int timeout_value = 1000;
67be7c5a 547
67be7c5a
MH
548 if (!timeout_configured) {
549 git_config_get_int("core.packedrefstimeout", &timeout_value);
550 timeout_configured = 1;
551 }
552
42dfa7ec
MH
553 /*
554 * Note that we close the lockfile immediately because we
555 * don't write new content to it, but rather to a separate
556 * tempfile.
557 */
67be7c5a
MH
558 if (hold_lock_file_for_update_timeout(
559 &refs->lock,
560 refs->path,
c8bed835
MH
561 flags, timeout_value) < 0) {
562 unable_to_lock_message(refs->path, errno, err);
563 return -1;
564 }
565
566 if (close_lock_file(&refs->lock)) {
567 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
67be7c5a 568 return -1;
c8bed835 569 }
67be7c5a
MH
570
571 /*
572 * Now that we hold the `packed-refs` lock, make sure that our
573 * cache matches the current version of the file. Normally
574 * `get_packed_ref_cache()` does that for us, but that
575 * function assumes that when the file is locked, any existing
576 * cache is still valid. We've just locked the file, but it
577 * might have changed the moment *before* we locked it.
578 */
579 validate_packed_ref_cache(refs);
580
39c8df0c
MH
581 /*
582 * Now make sure that the packed-refs file as it exists in the
583 * locked state is loaded into the cache:
584 */
585 get_packed_ref_cache(refs);
67be7c5a
MH
586 return 0;
587}
588
49aebcf4
MH
589void packed_refs_unlock(struct ref_store *ref_store)
590{
591 struct packed_ref_store *refs = packed_downcast(
592 ref_store,
593 REF_STORE_READ | REF_STORE_WRITE,
594 "packed_refs_unlock");
595
596 if (!is_lock_file_locked(&refs->lock))
597 die("BUG: packed_refs_unlock() called when not locked");
598 rollback_lock_file(&refs->lock);
49aebcf4
MH
599}
600
601int packed_refs_is_locked(struct ref_store *ref_store)
602{
603 struct packed_ref_store *refs = packed_downcast(
604 ref_store,
605 REF_STORE_READ | REF_STORE_WRITE,
606 "packed_refs_is_locked");
607
608 return is_lock_file_locked(&refs->lock);
609}
610
67be7c5a
MH
611/*
612 * The packed-refs header line that we write out. Perhaps other
613 * traits will be added later. The trailing space is required.
614 */
615static const char PACKED_REFS_HEADER[] =
616 "# pack-refs with: peeled fully-peeled \n";
617
e0cc8ac8
MH
618static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
619{
620 /* Nothing to do. */
621 return 0;
622}
623
2775d872
MH
624/*
625 * Write the packed-refs from the cache to the packed-refs tempfile,
626 * incorporating any changes from `updates`. `updates` must be a
627 * sorted string list whose keys are the refnames and whose util
628 * values are `struct ref_update *`. On error, rollback the tempfile,
629 * write an error message to `err`, and return a nonzero value.
630 *
631 * The packfile must be locked before calling this function and will
632 * remain locked when it is done.
633 */
634static int write_with_updates(struct packed_ref_store *refs,
635 struct string_list *updates,
636 struct strbuf *err)
637{
638 struct ref_iterator *iter = NULL;
639 size_t i;
640 int ok;
641 FILE *out;
642 struct strbuf sb = STRBUF_INIT;
643 char *packed_refs_path;
644
645 if (!is_lock_file_locked(&refs->lock))
646 die("BUG: write_with_updates() called while unlocked");
647
648 /*
649 * If packed-refs is a symlink, we want to overwrite the
650 * symlinked-to file, not the symlink itself. Also, put the
651 * staging file next to it:
652 */
653 packed_refs_path = get_locked_file_path(&refs->lock);
654 strbuf_addf(&sb, "%s.new", packed_refs_path);
655 free(packed_refs_path);
656 if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
657 strbuf_addf(err, "unable to create file %s: %s",
658 sb.buf, strerror(errno));
659 strbuf_release(&sb);
660 return -1;
661 }
662 strbuf_release(&sb);
663
664 out = fdopen_tempfile(&refs->tempfile, "w");
665 if (!out) {
666 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
667 strerror(errno));
668 goto error;
669 }
670
671 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
672 goto write_error;
673
674 /*
675 * We iterate in parallel through the current list of refs and
676 * the list of updates, processing an entry from at least one
677 * of the lists each time through the loop. When the current
678 * list of refs is exhausted, set iter to NULL. When the list
679 * of updates is exhausted, leave i set to updates->nr.
680 */
681 iter = packed_ref_iterator_begin(&refs->base, "",
682 DO_FOR_EACH_INCLUDE_BROKEN);
683 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
684 iter = NULL;
685
686 i = 0;
687
688 while (iter || i < updates->nr) {
689 struct ref_update *update = NULL;
690 int cmp;
691
692 if (i >= updates->nr) {
693 cmp = -1;
694 } else {
695 update = updates->items[i].util;
696
697 if (!iter)
698 cmp = +1;
699 else
700 cmp = strcmp(iter->refname, update->refname);
701 }
702
703 if (!cmp) {
704 /*
705 * There is both an old value and an update
706 * for this reference. Check the old value if
707 * necessary:
708 */
709 if ((update->flags & REF_HAVE_OLD)) {
710 if (is_null_oid(&update->old_oid)) {
711 strbuf_addf(err, "cannot update ref '%s': "
712 "reference already exists",
713 update->refname);
714 goto error;
715 } else if (oidcmp(&update->old_oid, iter->oid)) {
716 strbuf_addf(err, "cannot update ref '%s': "
717 "is at %s but expected %s",
718 update->refname,
719 oid_to_hex(iter->oid),
720 oid_to_hex(&update->old_oid));
721 goto error;
722 }
723 }
724
725 /* Now figure out what to use for the new value: */
726 if ((update->flags & REF_HAVE_NEW)) {
727 /*
728 * The update takes precedence. Skip
729 * the iterator over the unneeded
730 * value.
731 */
732 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
733 iter = NULL;
734 cmp = +1;
735 } else {
736 /*
737 * The update doesn't actually want to
738 * change anything. We're done with it.
739 */
740 i++;
741 cmp = -1;
742 }
743 } else if (cmp > 0) {
744 /*
745 * There is no old value but there is an
746 * update for this reference. Make sure that
747 * the update didn't expect an existing value:
748 */
749 if ((update->flags & REF_HAVE_OLD) &&
750 !is_null_oid(&update->old_oid)) {
751 strbuf_addf(err, "cannot update ref '%s': "
752 "reference is missing but expected %s",
753 update->refname,
754 oid_to_hex(&update->old_oid));
755 goto error;
756 }
757 }
758
759 if (cmp < 0) {
760 /* Pass the old reference through. */
761
762 struct object_id peeled;
763 int peel_error = ref_iterator_peel(iter, &peeled);
764
765 if (write_packed_entry(out, iter->refname,
766 iter->oid->hash,
767 peel_error ? NULL : peeled.hash))
768 goto write_error;
769
770 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
771 iter = NULL;
772 } else if (is_null_oid(&update->new_oid)) {
773 /*
774 * The update wants to delete the reference,
775 * and the reference either didn't exist or we
776 * have already skipped it. So we're done with
777 * the update (and don't have to write
778 * anything).
779 */
780 i++;
781 } else {
782 struct object_id peeled;
783 int peel_error = peel_object(update->new_oid.hash,
784 peeled.hash);
785
786 if (write_packed_entry(out, update->refname,
787 update->new_oid.hash,
788 peel_error ? NULL : peeled.hash))
789 goto write_error;
790
791 i++;
792 }
793 }
794
795 if (ok != ITER_DONE) {
796 strbuf_addf(err, "unable to write packed-refs file: "
797 "error iterating over old contents");
798 goto error;
799 }
800
801 if (close_tempfile(&refs->tempfile)) {
802 strbuf_addf(err, "error closing file %s: %s",
803 get_tempfile_path(&refs->tempfile),
804 strerror(errno));
805 strbuf_release(&sb);
806 return -1;
807 }
808
809 return 0;
810
811write_error:
812 strbuf_addf(err, "error writing to %s: %s",
813 get_tempfile_path(&refs->tempfile), strerror(errno));
814
815error:
816 if (iter)
817 ref_iterator_abort(iter);
818
819 delete_tempfile(&refs->tempfile);
820 return -1;
821}
822
823struct packed_transaction_backend_data {
824 /* True iff the transaction owns the packed-refs lock. */
825 int own_lock;
826
827 struct string_list updates;
828};
829
830static void packed_transaction_cleanup(struct packed_ref_store *refs,
831 struct ref_transaction *transaction)
832{
833 struct packed_transaction_backend_data *data = transaction->backend_data;
834
835 if (data) {
836 string_list_clear(&data->updates, 0);
837
838 if (is_tempfile_active(&refs->tempfile))
839 delete_tempfile(&refs->tempfile);
840
841 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
842 packed_refs_unlock(&refs->base);
843 data->own_lock = 0;
844 }
845
846 free(data);
847 transaction->backend_data = NULL;
848 }
849
850 transaction->state = REF_TRANSACTION_CLOSED;
851}
852
e0cc8ac8
MH
853static int packed_transaction_prepare(struct ref_store *ref_store,
854 struct ref_transaction *transaction,
855 struct strbuf *err)
856{
2775d872
MH
857 struct packed_ref_store *refs = packed_downcast(
858 ref_store,
859 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
860 "ref_transaction_prepare");
861 struct packed_transaction_backend_data *data;
862 size_t i;
863 int ret = TRANSACTION_GENERIC_ERROR;
864
865 /*
866 * Note that we *don't* skip transactions with zero updates,
867 * because such a transaction might be executed for the side
868 * effect of ensuring that all of the references are peeled.
869 * If the caller wants to optimize away empty transactions, it
870 * should do so itself.
871 */
872
873 data = xcalloc(1, sizeof(*data));
874 string_list_init(&data->updates, 0);
875
876 transaction->backend_data = data;
877
878 /*
879 * Stick the updates in a string list by refname so that we
880 * can sort them:
881 */
882 for (i = 0; i < transaction->nr; i++) {
883 struct ref_update *update = transaction->updates[i];
884 struct string_list_item *item =
885 string_list_append(&data->updates, update->refname);
886
887 /* Store a pointer to update in item->util: */
888 item->util = update;
889 }
890 string_list_sort(&data->updates);
891
892 if (ref_update_reject_duplicates(&data->updates, err))
893 goto failure;
894
895 if (!is_lock_file_locked(&refs->lock)) {
896 if (packed_refs_lock(ref_store, 0, err))
897 goto failure;
898 data->own_lock = 1;
899 }
900
901 if (write_with_updates(refs, &data->updates, err))
902 goto failure;
903
904 transaction->state = REF_TRANSACTION_PREPARED;
905 return 0;
906
907failure:
908 packed_transaction_cleanup(refs, transaction);
909 return ret;
e0cc8ac8
MH
910}
911
912static int packed_transaction_abort(struct ref_store *ref_store,
913 struct ref_transaction *transaction,
914 struct strbuf *err)
915{
2775d872
MH
916 struct packed_ref_store *refs = packed_downcast(
917 ref_store,
918 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
919 "ref_transaction_abort");
920
921 packed_transaction_cleanup(refs, transaction);
922 return 0;
e0cc8ac8
MH
923}
924
925static int packed_transaction_finish(struct ref_store *ref_store,
926 struct ref_transaction *transaction,
927 struct strbuf *err)
928{
2775d872
MH
929 struct packed_ref_store *refs = packed_downcast(
930 ref_store,
931 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
932 "ref_transaction_finish");
933 int ret = TRANSACTION_GENERIC_ERROR;
934 char *packed_refs_path;
935
936 packed_refs_path = get_locked_file_path(&refs->lock);
937 if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
938 strbuf_addf(err, "error replacing %s: %s",
939 refs->path, strerror(errno));
940 goto cleanup;
941 }
942
943 clear_packed_ref_cache(refs);
944 ret = 0;
945
946cleanup:
947 free(packed_refs_path);
948 packed_transaction_cleanup(refs, transaction);
949 return ret;
e0cc8ac8
MH
950}
951
952static int packed_initial_transaction_commit(struct ref_store *ref_store,
953 struct ref_transaction *transaction,
954 struct strbuf *err)
955{
956 return ref_transaction_commit(transaction, err);
957}
958
959static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
960 struct string_list *refnames, unsigned int flags)
961{
2fb330ca
MH
962 struct packed_ref_store *refs =
963 packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
964 struct strbuf err = STRBUF_INIT;
965 struct ref_transaction *transaction;
966 struct string_list_item *item;
967 int ret;
968
969 (void)refs; /* We need the check above, but don't use the variable */
970
971 if (!refnames->nr)
972 return 0;
973
974 /*
975 * Since we don't check the references' old_oids, the
976 * individual updates can't fail, so we can pack all of the
977 * updates into a single transaction.
978 */
979
980 transaction = ref_store_transaction_begin(ref_store, &err);
981 if (!transaction)
982 return -1;
983
984 for_each_string_list_item(item, refnames) {
985 if (ref_transaction_delete(transaction, item->string, NULL,
986 flags, msg, &err)) {
987 warning(_("could not delete reference %s: %s"),
988 item->string, err.buf);
989 strbuf_reset(&err);
990 }
991 }
992
993 ret = ref_transaction_commit(transaction, &err);
994
995 if (ret) {
996 if (refnames->nr == 1)
997 error(_("could not delete reference %s: %s"),
998 refnames->items[0].string, err.buf);
999 else
1000 error(_("could not delete references: %s"), err.buf);
1001 }
1002
1003 ref_transaction_free(transaction);
1004 strbuf_release(&err);
1005 return ret;
e0cc8ac8
MH
1006}
1007
1008static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
1009{
1010 /*
1011 * Packed refs are already packed. It might be that loose refs
1012 * are packed *into* a packed refs store, but that is done by
1013 * updating the packed references via a transaction.
1014 */
1015 return 0;
1016}
1017
1018static int packed_create_symref(struct ref_store *ref_store,
1019 const char *refname, const char *target,
1020 const char *logmsg)
1021{
1022 die("BUG: packed reference store does not support symrefs");
1023}
1024
1025static int packed_rename_ref(struct ref_store *ref_store,
1026 const char *oldrefname, const char *newrefname,
1027 const char *logmsg)
1028{
1029 die("BUG: packed reference store does not support renaming references");
1030}
1031
1032static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
1033{
1034 return empty_ref_iterator_begin();
1035}
1036
1037static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1038 const char *refname,
1039 each_reflog_ent_fn fn, void *cb_data)
1040{
1041 return 0;
1042}
1043
1044static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1045 const char *refname,
1046 each_reflog_ent_fn fn,
1047 void *cb_data)
1048{
1049 return 0;
1050}
1051
1052static int packed_reflog_exists(struct ref_store *ref_store,
1053 const char *refname)
1054{
1055 return 0;
1056}
1057
1058static int packed_create_reflog(struct ref_store *ref_store,
1059 const char *refname, int force_create,
1060 struct strbuf *err)
1061{
1062 die("BUG: packed reference store does not support reflogs");
1063}
1064
1065static int packed_delete_reflog(struct ref_store *ref_store,
1066 const char *refname)
1067{
1068 return 0;
1069}
1070
1071static int packed_reflog_expire(struct ref_store *ref_store,
1072 const char *refname, const unsigned char *sha1,
1073 unsigned int flags,
1074 reflog_expiry_prepare_fn prepare_fn,
1075 reflog_expiry_should_prune_fn should_prune_fn,
1076 reflog_expiry_cleanup_fn cleanup_fn,
1077 void *policy_cb_data)
1078{
1079 return 0;
1080}
1081
1082struct ref_storage_be refs_be_packed = {
1083 NULL,
1084 "packed",
1085 packed_ref_store_create,
1086 packed_init_db,
1087 packed_transaction_prepare,
1088 packed_transaction_finish,
1089 packed_transaction_abort,
1090 packed_initial_transaction_commit,
1091
1092 packed_pack_refs,
1093 packed_peel_ref,
1094 packed_create_symref,
1095 packed_delete_refs,
1096 packed_rename_ref,
1097
1098 packed_ref_iterator_begin,
1099 packed_read_raw_ref,
1100
1101 packed_reflog_iterator_begin,
1102 packed_for_each_reflog_ent,
1103 packed_for_each_reflog_ent_reverse,
1104 packed_reflog_exists,
1105 packed_create_reflog,
1106 packed_delete_reflog,
1107 packed_reflog_expire
1108};