]> git.ipfire.org Git - thirdparty/git.git/blame - refs/packed-backend.c
t3210: add some tests of bogus packed-refs file contents
[thirdparty/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
67be7c5a
MH
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 */
149static 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 */
197static 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 */
283static 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 */
298static 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
309static 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
314static 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
b7de57d8 322 * (see packed_refs_lock()). To actually write the packed-refs file,
67be7c5a
MH
323 * call commit_packed_refs().
324 */
e0cc8ac8 325void add_packed_ref(struct ref_store *ref_store,
67be7c5a
MH
326 const char *refname, const struct object_id *oid)
327{
e0cc8ac8
MH
328 struct packed_ref_store *refs =
329 packed_downcast(ref_store, REF_STORE_WRITE,
330 "add_packed_ref");
67be7c5a
MH
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 */
357static 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
e0cc8ac8
MH
363static int packed_read_raw_ref(struct ref_store *ref_store,
364 const char *refname, unsigned char *sha1,
365 struct strbuf *referent, unsigned int *type)
67be7c5a 366{
e0cc8ac8
MH
367 struct packed_ref_store *refs =
368 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
369
67be7c5a
MH
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
e0cc8ac8
MH
385static int packed_peel_ref(struct ref_store *ref_store,
386 const char *refname, unsigned char *sha1)
67be7c5a 387{
e0cc8ac8
MH
388 struct packed_ref_store *refs =
389 packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
390 "peel_ref");
67be7c5a
MH
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
400struct 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
408static 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
438static 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
447static 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
461static 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
e0cc8ac8
MH
467static struct ref_iterator *packed_ref_iterator_begin(
468 struct ref_store *ref_store,
67be7c5a
MH
469 const char *prefix, unsigned int flags)
470{
e0cc8ac8 471 struct packed_ref_store *refs;
67be7c5a
MH
472 struct packed_ref_iterator *iter;
473 struct ref_iterator *ref_iterator;
e0cc8ac8
MH
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");
67be7c5a
MH
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.
3478983b
MH
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()`.
67be7c5a 504 */
3478983b
MH
505static int write_packed_entry(FILE *fh, const char *refname,
506 const unsigned char *sha1,
507 const unsigned char *peeled)
67be7c5a 508{
3478983b
MH
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;
67be7c5a
MH
514}
515
c8bed835 516int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
67be7c5a 517{
e0cc8ac8
MH
518 struct packed_ref_store *refs =
519 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
b7de57d8 520 "packed_refs_lock");
67be7c5a
MH
521 static int timeout_configured = 0;
522 static int timeout_value = 1000;
523 struct packed_ref_cache *packed_ref_cache;
524
67be7c5a
MH
525 if (!timeout_configured) {
526 git_config_get_int("core.packedrefstimeout", &timeout_value);
527 timeout_configured = 1;
528 }
529
42dfa7ec
MH
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 */
67be7c5a
MH
535 if (hold_lock_file_for_update_timeout(
536 &refs->lock,
537 refs->path,
c8bed835
MH
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));
67be7c5a 545 return -1;
c8bed835 546 }
67be7c5a
MH
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
49aebcf4
MH
564void 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
577int 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
67be7c5a
MH
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 */
591static 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
b7de57d8 597 * packed_refs_lock()). Return zero on success. On errors, rollback
3478983b
MH
598 * the lockfile, write an error message to `err`, and return a nonzero
599 * value.
67be7c5a 600 */
3478983b 601int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
67be7c5a 602{
e0cc8ac8
MH
603 struct packed_ref_store *refs =
604 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
605 "commit_packed_refs");
67be7c5a
MH
606 struct packed_ref_cache *packed_ref_cache =
607 get_packed_ref_cache(refs);
3478983b 608 int ok;
42dfa7ec 609 struct strbuf sb = STRBUF_INIT;
67be7c5a
MH
610 FILE *out;
611 struct ref_iterator *iter;
612
67be7c5a 613 if (!is_lock_file_locked(&refs->lock))
3478983b 614 die("BUG: commit_packed_refs() called when unlocked");
67be7c5a 615
42dfa7ec
MH
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);
42c7f7ff 621 return -1;
42dfa7ec
MH
622 }
623 strbuf_release(&sb);
624
625 out = fdopen_tempfile(&refs->tempfile, "w");
3478983b
MH
626 if (!out) {
627 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
628 strerror(errno));
629 goto error;
630 }
67be7c5a 631
3478983b
MH
632 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) {
633 strbuf_addf(err, "error writing to %s: %s",
42dfa7ec 634 get_tempfile_path(&refs->tempfile), strerror(errno));
3478983b
MH
635 goto error;
636 }
67be7c5a
MH
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
3478983b
MH
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",
42dfa7ec 646 get_tempfile_path(&refs->tempfile),
3478983b
MH
647 strerror(errno));
648 ref_iterator_abort(iter);
649 goto error;
650 }
67be7c5a
MH
651 }
652
3478983b 653 if (ok != ITER_DONE) {
42dfa7ec 654 strbuf_addf(err, "unable to rewrite packed-refs file: "
3478983b
MH
655 "error iterating over old contents");
656 goto error;
657 }
67be7c5a 658
42dfa7ec
MH
659 if (rename_tempfile(&refs->tempfile, refs->path)) {
660 strbuf_addf(err, "error replacing %s: %s",
3478983b 661 refs->path, strerror(errno));
42c7f7ff 662 return -1;
67be7c5a 663 }
3478983b 664
42c7f7ff 665 return 0;
3478983b
MH
666
667error:
42dfa7ec 668 delete_tempfile(&refs->tempfile);
42c7f7ff 669 return -1;
67be7c5a
MH
670}
671
67be7c5a
MH
672/*
673 * Rewrite the packed-refs file, omitting any refs listed in
674 * 'refnames'. On error, leave packed-refs unchanged, write an error
e5cc7d7d
MH
675 * message to 'err', and return a nonzero value. The packed refs lock
676 * must be held when calling this function; it will still be held when
677 * the function returns.
67be7c5a
MH
678 *
679 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
680 */
e0cc8ac8 681int repack_without_refs(struct ref_store *ref_store,
67be7c5a
MH
682 struct string_list *refnames, struct strbuf *err)
683{
e0cc8ac8
MH
684 struct packed_ref_store *refs =
685 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
686 "repack_without_refs");
67be7c5a
MH
687 struct ref_dir *packed;
688 struct string_list_item *refname;
3478983b 689 int needs_repacking = 0, removed = 0;
67be7c5a
MH
690
691 packed_assert_main_repository(refs, "repack_without_refs");
692 assert(err);
693
e5cc7d7d
MH
694 if (!is_lock_file_locked(&refs->lock))
695 die("BUG: repack_without_refs called without holding lock");
696
67be7c5a
MH
697 /* Look for a packed ref */
698 for_each_string_list_item(refname, refnames) {
699 if (get_packed_ref(refs, refname->string)) {
700 needs_repacking = 1;
701 break;
702 }
703 }
704
705 /* Avoid locking if we have nothing to do */
706 if (!needs_repacking)
707 return 0; /* no refname exists in packed refs */
708
67be7c5a
MH
709 packed = get_packed_refs(refs);
710
711 /* Remove refnames from the cache */
712 for_each_string_list_item(refname, refnames)
713 if (remove_entry_from_dir(packed, refname->string) != -1)
714 removed = 1;
715 if (!removed) {
716 /*
717 * All packed entries disappeared while we were
718 * acquiring the lock.
719 */
e5cc7d7d 720 clear_packed_ref_cache(refs);
67be7c5a
MH
721 return 0;
722 }
723
724 /* Write what remains */
e5cc7d7d 725 return commit_packed_refs(&refs->base, err);
67be7c5a 726}
e0cc8ac8
MH
727
728static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
729{
730 /* Nothing to do. */
731 return 0;
732}
733
734static int packed_transaction_prepare(struct ref_store *ref_store,
735 struct ref_transaction *transaction,
736 struct strbuf *err)
737{
738 die("BUG: not implemented yet");
739}
740
741static int packed_transaction_abort(struct ref_store *ref_store,
742 struct ref_transaction *transaction,
743 struct strbuf *err)
744{
745 die("BUG: not implemented yet");
746}
747
748static int packed_transaction_finish(struct ref_store *ref_store,
749 struct ref_transaction *transaction,
750 struct strbuf *err)
751{
752 die("BUG: not implemented yet");
753}
754
755static int packed_initial_transaction_commit(struct ref_store *ref_store,
756 struct ref_transaction *transaction,
757 struct strbuf *err)
758{
759 return ref_transaction_commit(transaction, err);
760}
761
762static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
763 struct string_list *refnames, unsigned int flags)
764{
765 die("BUG: not implemented yet");
766}
767
768static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
769{
770 /*
771 * Packed refs are already packed. It might be that loose refs
772 * are packed *into* a packed refs store, but that is done by
773 * updating the packed references via a transaction.
774 */
775 return 0;
776}
777
778static int packed_create_symref(struct ref_store *ref_store,
779 const char *refname, const char *target,
780 const char *logmsg)
781{
782 die("BUG: packed reference store does not support symrefs");
783}
784
785static int packed_rename_ref(struct ref_store *ref_store,
786 const char *oldrefname, const char *newrefname,
787 const char *logmsg)
788{
789 die("BUG: packed reference store does not support renaming references");
790}
791
792static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
793{
794 return empty_ref_iterator_begin();
795}
796
797static int packed_for_each_reflog_ent(struct ref_store *ref_store,
798 const char *refname,
799 each_reflog_ent_fn fn, void *cb_data)
800{
801 return 0;
802}
803
804static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
805 const char *refname,
806 each_reflog_ent_fn fn,
807 void *cb_data)
808{
809 return 0;
810}
811
812static int packed_reflog_exists(struct ref_store *ref_store,
813 const char *refname)
814{
815 return 0;
816}
817
818static int packed_create_reflog(struct ref_store *ref_store,
819 const char *refname, int force_create,
820 struct strbuf *err)
821{
822 die("BUG: packed reference store does not support reflogs");
823}
824
825static int packed_delete_reflog(struct ref_store *ref_store,
826 const char *refname)
827{
828 return 0;
829}
830
831static int packed_reflog_expire(struct ref_store *ref_store,
832 const char *refname, const unsigned char *sha1,
833 unsigned int flags,
834 reflog_expiry_prepare_fn prepare_fn,
835 reflog_expiry_should_prune_fn should_prune_fn,
836 reflog_expiry_cleanup_fn cleanup_fn,
837 void *policy_cb_data)
838{
839 return 0;
840}
841
842struct ref_storage_be refs_be_packed = {
843 NULL,
844 "packed",
845 packed_ref_store_create,
846 packed_init_db,
847 packed_transaction_prepare,
848 packed_transaction_finish,
849 packed_transaction_abort,
850 packed_initial_transaction_commit,
851
852 packed_pack_refs,
853 packed_peel_ref,
854 packed_create_symref,
855 packed_delete_refs,
856 packed_rename_ref,
857
858 packed_ref_iterator_begin,
859 packed_read_raw_ref,
860
861 packed_reflog_iterator_begin,
862 packed_for_each_reflog_ent,
863 packed_for_each_reflog_ent_reverse,
864 packed_reflog_exists,
865 packed_create_reflog,
866 packed_delete_reflog,
867 packed_reflog_expire
868};