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