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