]> git.proxmox.com Git - libgit2.git/blame - src/refdb_fs.c
Merge pull request #3399 from arthurschreiber/patch-8
[libgit2.git] / src / refdb_fs.c
CommitLineData
d00d5464
ET
1/*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8#include "refs.h"
9#include "hash.h"
10#include "repository.h"
11#include "fileops.h"
114f5a6c 12#include "filebuf.h"
d00d5464
ET
13#include "pack.h"
14#include "reflog.h"
d00d5464
ET
15#include "refdb.h"
16#include "refdb_fs.h"
4def7035 17#include "iterator.h"
fe372740 18#include "sortedcache.h"
b976f3c2 19#include "signature.h"
d00d5464
ET
20
21#include <git2/tag.h>
22#include <git2/object.h>
23#include <git2/refdb.h>
4b7e1b9e 24#include <git2/branch.h>
4dcd8780 25#include <git2/sys/refdb_backend.h>
21ca0451 26#include <git2/sys/refs.h>
b976f3c2 27#include <git2/sys/reflog.h>
d00d5464 28
c8e02b87 29GIT__USE_STRMAP
d00d5464
ET
30
31#define DEFAULT_NESTING_LEVEL 5
32#define MAX_NESTING_LEVEL 10
33
34enum {
f69db390
VM
35 PACKREF_HAS_PEEL = 1,
36 PACKREF_WAS_LOOSE = 2,
2638a03a
VM
37 PACKREF_CANNOT_PEEL = 4,
38 PACKREF_SHADOWED = 8,
f69db390
VM
39};
40
41enum {
42 PEELING_NONE = 0,
43 PEELING_STANDARD,
44 PEELING_FULL
d00d5464
ET
45};
46
47struct packref {
48 git_oid oid;
49 git_oid peel;
50 char flags;
51 char name[GIT_FLEX_ARRAY];
52};
53
54typedef struct refdb_fs_backend {
55 git_refdb_backend parent;
56
57 git_repository *repo;
bade5194 58 char *path;
d00d5464 59
fe372740 60 git_sortedcache *refcache;
f69db390 61 int peeling_mode;
219d3457
RB
62 git_iterator_flag_t iterator_flags;
63 uint32_t direach_flags;
d00d5464
ET
64} refdb_fs_backend;
65
01d0c02d
CMN
66static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name);
67
fe372740 68static int packref_cmp(const void *a_, const void *b_)
d00d5464 69{
fe372740
RB
70 const struct packref *a = a_, *b = b_;
71 return strcmp(a->name, b->name);
d00d5464
ET
72}
73
fe372740 74static int packed_reload(refdb_fs_backend *backend)
d00d5464 75{
fe372740
RB
76 int error;
77 git_buf packedrefs = GIT_BUF_INIT;
78 char *scan, *eof, *eol;
4dcd8780 79
fe372740 80 if (!backend->path)
69a3c766
CMN
81 return 0;
82
fe372740 83 error = git_sortedcache_lockandload(backend->refcache, &packedrefs);
d00d5464
ET
84
85 /*
fe372740
RB
86 * If we can't find the packed-refs, clear table and return.
87 * Any other error just gets passed through.
88 * If no error, and file wasn't changed, just return.
89 * Anything else means we need to refresh the packed refs.
d00d5464 90 */
fe372740
RB
91 if (error <= 0) {
92 if (error == GIT_ENOTFOUND) {
93 git_sortedcache_clear(backend->refcache, true);
94 giterr_clear();
95 error = 0;
96 }
97 return error;
d00d5464
ET
98 }
99
fe372740 100 /* At this point, refresh the packed refs from the loaded buffer. */
d00d5464 101
fe372740 102 git_sortedcache_clear(backend->refcache, false);
d00d5464 103
fe372740
RB
104 scan = (char *)packedrefs.ptr;
105 eof = scan + packedrefs.size;
d00d5464 106
f69db390
VM
107 backend->peeling_mode = PEELING_NONE;
108
fe372740 109 if (*scan == '#') {
1fed6b07 110 static const char *traits_header = "# pack-refs with: ";
f69db390 111
fe372740
RB
112 if (git__prefixcmp(scan, traits_header) == 0) {
113 scan += strlen(traits_header);
114 eol = strchr(scan, '\n');
822645f6 115
fe372740 116 if (!eol)
822645f6 117 goto parse_failed;
fe372740 118 *eol = '\0';
822645f6 119
fe372740 120 if (strstr(scan, " fully-peeled ") != NULL) {
f69db390 121 backend->peeling_mode = PEELING_FULL;
fe372740 122 } else if (strstr(scan, " peeled ") != NULL) {
f69db390
VM
123 backend->peeling_mode = PEELING_STANDARD;
124 }
125
fe372740 126 scan = eol + 1;
f69db390
VM
127 }
128 }
129
fe372740
RB
130 while (scan < eof && *scan == '#') {
131 if (!(eol = strchr(scan, '\n')))
d00d5464 132 goto parse_failed;
fe372740 133 scan = eol + 1;
d00d5464
ET
134 }
135
fe372740
RB
136 while (scan < eof) {
137 struct packref *ref;
138 git_oid oid;
139
140 /* parse "<OID> <refname>\n" */
d00d5464 141
fe372740 142 if (git_oid_fromstr(&oid, scan) < 0)
d00d5464 143 goto parse_failed;
fe372740 144 scan += GIT_OID_HEXSZ;
d00d5464 145
fe372740
RB
146 if (*scan++ != ' ')
147 goto parse_failed;
148 if (!(eol = strchr(scan, '\n')))
149 goto parse_failed;
150 *eol = '\0';
151 if (eol[-1] == '\r')
152 eol[-1] = '\0';
d00d5464 153
fe372740 154 if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0)
d00d5464 155 goto parse_failed;
fe372740
RB
156 scan = eol + 1;
157
158 git_oid_cpy(&ref->oid, &oid);
159
160 /* look for optional "^<OID>\n" */
161
162 if (*scan == '^') {
163 if (git_oid_fromstr(&oid, scan + 1) < 0)
164 goto parse_failed;
165 scan += GIT_OID_HEXSZ + 1;
166
167 if (scan < eof) {
168 if (!(eol = strchr(scan, '\n')))
169 goto parse_failed;
170 scan = eol + 1;
171 }
172
173 git_oid_cpy(&ref->peel, &oid);
174 ref->flags |= PACKREF_HAS_PEEL;
175 }
176 else if (backend->peeling_mode == PEELING_FULL ||
177 (backend->peeling_mode == PEELING_STANDARD &&
178 git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0))
179 ref->flags |= PACKREF_CANNOT_PEEL;
d00d5464
ET
180 }
181
8d9a85d4 182 git_sortedcache_wunlock(backend->refcache);
fe372740
RB
183 git_buf_free(&packedrefs);
184
d00d5464
ET
185 return 0;
186
187parse_failed:
fe372740
RB
188 giterr_set(GITERR_REFERENCE, "Corrupted packed references file");
189
190 git_sortedcache_clear(backend->refcache, false);
8d9a85d4 191 git_sortedcache_wunlock(backend->refcache);
fe372740
RB
192 git_buf_free(&packedrefs);
193
d00d5464
ET
194 return -1;
195}
196
fe372740
RB
197static int loose_parse_oid(
198 git_oid *oid, const char *filename, git_buf *file_content)
d00d5464 199{
fe372740 200 const char *str = git_buf_cstr(file_content);
d00d5464 201
fe372740 202 if (git_buf_len(file_content) < GIT_OID_HEXSZ)
d00d5464
ET
203 goto corrupted;
204
d00d5464 205 /* we need to get 40 OID characters from the file */
fe372740 206 if (git_oid_fromstr(oid, str) < 0)
d00d5464
ET
207 goto corrupted;
208
209 /* If the file is longer than 40 chars, the 41st must be a space */
210 str += GIT_OID_HEXSZ;
211 if (*str == '\0' || git__isspace(*str))
212 return 0;
213
214corrupted:
a5de9044 215 giterr_set(GITERR_REFERENCE, "Corrupted loose reference file: %s", filename);
d00d5464
ET
216 return -1;
217}
218
fe372740
RB
219static int loose_readbuffer(git_buf *buf, const char *base, const char *path)
220{
221 int error;
222
223 /* build full path to file */
224 if ((error = git_buf_joinpath(buf, base, path)) < 0 ||
225 (error = git_futils_readbuffer(buf, buf->ptr)) < 0)
226 git_buf_free(buf);
227
228 return error;
229}
230
231static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name)
d00d5464 232{
fe372740 233 int error = 0;
d00d5464
ET
234 git_buf ref_file = GIT_BUF_INIT;
235 struct packref *ref = NULL;
fe372740 236 git_oid oid;
d00d5464 237
fe372740
RB
238 /* if we fail to load the loose reference, assume someone changed
239 * the filesystem under us and skip it...
240 */
241 if (loose_readbuffer(&ref_file, backend->path, name) < 0) {
242 giterr_clear();
243 goto done;
244 }
d00d5464 245
0f0f5655 246 /* skip symbolic refs */
fe372740
RB
247 if (!git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF))
248 goto done;
0f0f5655 249
fe372740
RB
250 /* parse OID from file */
251 if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0)
252 goto done;
d00d5464 253
8d9a85d4 254 git_sortedcache_wlock(backend->refcache);
d00d5464 255
fe372740
RB
256 if (!(error = git_sortedcache_upsert(
257 (void **)&ref, backend->refcache, name))) {
d00d5464 258
fe372740
RB
259 git_oid_cpy(&ref->oid, &oid);
260 ref->flags = PACKREF_WAS_LOOSE;
d00d5464
ET
261 }
262
8d9a85d4 263 git_sortedcache_wunlock(backend->refcache);
d00d5464 264
fe372740 265done:
d00d5464 266 git_buf_free(&ref_file);
fe372740 267 return error;
d00d5464
ET
268}
269
25e0b157 270static int _dirent_loose_load(void *payload, git_buf *full_path)
d00d5464 271{
25e0b157 272 refdb_fs_backend *backend = payload;
d00d5464 273 const char *file_path;
d00d5464 274
fe372740
RB
275 if (git__suffixcmp(full_path->ptr, ".lock") == 0)
276 return 0;
277
a213a7bf
CMN
278 if (git_path_isdir(full_path->ptr)) {
279 int error = git_path_direach(
25e0b157 280 full_path, backend->direach_flags, _dirent_loose_load, backend);
a213a7bf
CMN
281 /* Race with the filesystem, ignore it */
282 if (error == GIT_ENOTFOUND) {
283 giterr_clear();
284 return 0;
285 }
286
287 return error;
288 }
d00d5464 289
25e0b157 290 file_path = full_path->ptr + strlen(backend->path);
d00d5464 291
25e0b157 292 return loose_lookup_to_packfile(backend, file_path);
d00d5464
ET
293}
294
295/*
296 * Load all the loose references from the repository
297 * into the in-memory Packfile, and build a vector with
298 * all the references so it can be written back to
299 * disk.
300 */
301static int packed_loadloose(refdb_fs_backend *backend)
302{
fe372740 303 int error;
d00d5464 304 git_buf refs_path = GIT_BUF_INIT;
d00d5464
ET
305
306 if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0)
307 return -1;
308
309 /*
310 * Load all the loose files from disk into the Packfile table.
311 * This will overwrite any old packed entries with their
312 * updated loose versions
313 */
219d3457 314 error = git_path_direach(
25e0b157 315 &refs_path, backend->direach_flags, _dirent_loose_load, backend);
fe372740 316
d00d5464
ET
317 git_buf_free(&refs_path);
318
96869a4e 319 return error;
d00d5464
ET
320}
321
322static int refdb_fs_backend__exists(
323 int *exists,
324 git_refdb_backend *_backend,
325 const char *ref_name)
326{
fe372740 327 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
d00d5464
ET
328 git_buf ref_path = GIT_BUF_INIT;
329
fe372740 330 assert(backend);
d00d5464 331
fe372740
RB
332 if (packed_reload(backend) < 0 ||
333 git_buf_joinpath(&ref_path, backend->path, ref_name) < 0)
d00d5464
ET
334 return -1;
335
fe372740 336 *exists = git_path_isfile(ref_path.ptr) ||
8d9a85d4 337 (git_sortedcache_lookup(backend->refcache, ref_name) != NULL);
d00d5464
ET
338
339 git_buf_free(&ref_path);
340 return 0;
341}
342
343static const char *loose_parse_symbolic(git_buf *file_content)
344{
345 const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF);
346 const char *refname_start;
347
348 refname_start = (const char *)file_content->ptr;
349
350 if (git_buf_len(file_content) < header_len + 1) {
351 giterr_set(GITERR_REFERENCE, "Corrupted loose reference file");
352 return NULL;
353 }
354
355 /*
356 * Assume we have already checked for the header
357 * before calling this function
358 */
359 refname_start += header_len;
360
361 return refname_start;
362}
363
364static int loose_lookup(
365 git_reference **out,
366 refdb_fs_backend *backend,
367 const char *ref_name)
368{
d00d5464
ET
369 git_buf ref_file = GIT_BUF_INIT;
370 int error = 0;
371
b7107131
RB
372 if (out)
373 *out = NULL;
374
fe372740
RB
375 if ((error = loose_readbuffer(&ref_file, backend->path, ref_name)) < 0)
376 /* cannot read loose ref file - gah */;
377 else if (git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF) == 0) {
378 const char *target;
d00d5464 379
d00d5464
ET
380 git_buf_rtrim(&ref_file);
381
fe372740 382 if (!(target = loose_parse_symbolic(&ref_file)))
d00d5464 383 error = -1;
fe372740 384 else if (out != NULL)
b7107131 385 *out = git_reference__alloc_symbolic(ref_name, target);
d00d5464 386 } else {
fe372740 387 git_oid oid;
4dcd8780 388
fe372740
RB
389 if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) &&
390 out != NULL)
b7107131 391 *out = git_reference__alloc(ref_name, &oid, NULL);
d00d5464
ET
392 }
393
d00d5464
ET
394 git_buf_free(&ref_file);
395 return error;
396}
397
fe372740 398static int ref_error_notfound(const char *name)
d00d5464 399{
fe372740
RB
400 giterr_set(GITERR_REFERENCE, "Reference '%s' not found", name);
401 return GIT_ENOTFOUND;
d00d5464
ET
402}
403
404static int packed_lookup(
405 git_reference **out,
406 refdb_fs_backend *backend,
407 const char *ref_name)
408{
d00d5464 409 int error = 0;
fe372740 410 struct packref *entry;
4dcd8780 411
fe372740 412 if (packed_reload(backend) < 0)
d00d5464 413 return -1;
4dcd8780 414
8d9a85d4
RB
415 if (git_sortedcache_rlock(backend->refcache) < 0)
416 return -1;
fe372740
RB
417
418 entry = git_sortedcache_lookup(backend->refcache, ref_name);
419 if (!entry) {
420 error = ref_error_notfound(ref_name);
421 } else {
422 *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel);
423 if (!*out)
424 error = -1;
425 }
426
8d9a85d4
RB
427 git_sortedcache_runlock(backend->refcache);
428
fe372740 429 return error;
d00d5464
ET
430}
431
432static int refdb_fs_backend__lookup(
433 git_reference **out,
434 git_refdb_backend *_backend,
435 const char *ref_name)
436{
fe372740
RB
437 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
438 int error;
d00d5464 439
fe372740 440 assert(backend);
d00d5464 441
fe372740 442 if (!(error = loose_lookup(out, backend, ref_name)))
d00d5464
ET
443 return 0;
444
445 /* only try to lookup this reference on the packfile if it
446 * wasn't found on the loose refs; not if there was a critical error */
fe372740 447 if (error == GIT_ENOTFOUND) {
d00d5464 448 giterr_clear();
fe372740 449 error = packed_lookup(out, backend, ref_name);
d00d5464
ET
450 }
451
fe372740 452 return error;
d00d5464
ET
453}
454
4def7035
CMN
455typedef struct {
456 git_reference_iterator parent;
2638a03a 457
ec24e542 458 char *glob;
c77342ef
RB
459
460 git_pool pool;
2638a03a 461 git_vector loose;
c77342ef 462
2d945f82 463 git_sortedcache *cache;
fe372740
RB
464 size_t loose_pos;
465 size_t packed_pos;
4def7035
CMN
466} refdb_fs_iter;
467
4def7035 468static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
d00d5464 469{
4def7035 470 refdb_fs_iter *iter = (refdb_fs_iter *) _iter;
2638a03a
VM
471
472 git_vector_free(&iter->loose);
c77342ef 473 git_pool_clear(&iter->pool);
2d945f82 474 git_sortedcache_free(iter->cache);
4def7035
CMN
475 git__free(iter);
476}
d00d5464 477
ec24e542 478static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
4def7035 479{
219d3457 480 int error = 0;
2638a03a 481 git_buf path = GIT_BUF_INIT;
c77342ef 482 git_iterator *fsit = NULL;
2638a03a 483 const git_index_entry *entry = NULL;
d00d5464 484
cee695ae
RB
485 if (!backend->path) /* do nothing if no path for loose refs */
486 return 0;
487
fe372740
RB
488 if ((error = git_buf_printf(&path, "%s/refs", backend->path)) < 0 ||
489 (error = git_iterator_for_filesystem(
219d3457 490 &fsit, path.ptr, backend->iterator_flags, NULL, NULL)) < 0) {
fe372740
RB
491 git_buf_free(&path);
492 return error;
493 }
d00d5464 494
fe372740 495 error = git_buf_sets(&path, GIT_REFS_DIR);
d00d5464 496
fe372740 497 while (!error && !git_iterator_advance(&entry, fsit)) {
2638a03a 498 const char *ref_name;
fe372740 499 struct packref *ref;
c77342ef 500 char *ref_dup;
d00d5464 501
2638a03a
VM
502 git_buf_truncate(&path, strlen(GIT_REFS_DIR));
503 git_buf_puts(&path, entry->path);
504 ref_name = git_buf_cstr(&path);
4dcd8780 505
ec24e542 506 if (git__suffixcmp(ref_name, ".lock") == 0 ||
cee695ae 507 (iter->glob && p_fnmatch(iter->glob, ref_name, 0) != 0))
2638a03a 508 continue;
d00d5464 509
8d9a85d4 510 git_sortedcache_rlock(backend->refcache);
fe372740
RB
511 ref = git_sortedcache_lookup(backend->refcache, ref_name);
512 if (ref)
2638a03a 513 ref->flags |= PACKREF_SHADOWED;
8d9a85d4 514 git_sortedcache_runlock(backend->refcache);
d00d5464 515
fe372740
RB
516 ref_dup = git_pool_strdup(&iter->pool, ref_name);
517 if (!ref_dup)
c77342ef 518 error = -1;
fe372740
RB
519 else
520 error = git_vector_insert(&iter->loose, ref_dup);
2638a03a 521 }
d00d5464 522
2638a03a
VM
523 git_iterator_free(fsit);
524 git_buf_free(&path);
4def7035 525
fe372740 526 return error;
4def7035
CMN
527}
528
ec24e542
VM
529static int refdb_fs_backend__iterator_next(
530 git_reference **out, git_reference_iterator *_iter)
4def7035 531{
8d9a85d4 532 int error = GIT_ITEROVER;
2638a03a 533 refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
ec24e542 534 refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend;
fe372740 535 struct packref *ref;
4def7035 536
56960b83 537 while (iter->loose_pos < iter->loose.length) {
2638a03a 538 const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
56960b83
VM
539
540 if (loose_lookup(out, backend, path) == 0)
541 return 0;
542
543 giterr_clear();
2638a03a 544 }
d00d5464 545
2d945f82
CMN
546 if (!iter->cache) {
547 if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
548 return error;
549 }
d00d5464 550
2d945f82
CMN
551 error = GIT_ITEROVER;
552 while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
553 ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
fe372740
RB
554 if (!ref) /* stop now if another thread deleted refs and we past end */
555 break;
ec24e542
VM
556
557 if (ref->flags & PACKREF_SHADOWED)
558 continue;
ec24e542
VM
559 if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
560 continue;
99d32707 561
56960b83 562 *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
8d9a85d4
RB
563 error = (*out != NULL) ? 0 : -1;
564 break;
4def7035
CMN
565 }
566
8d9a85d4 567 return error;
d00d5464
ET
568}
569
ec24e542
VM
570static int refdb_fs_backend__iterator_next_name(
571 const char **out, git_reference_iterator *_iter)
572{
8d9a85d4 573 int error = GIT_ITEROVER;
ec24e542
VM
574 refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
575 refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend;
8d9a85d4 576 struct packref *ref;
ec24e542
VM
577
578 while (iter->loose_pos < iter->loose.length) {
579 const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
580
8d9a85d4
RB
581 if (loose_lookup(NULL, backend, path) == 0) {
582 *out = path;
583 return 0;
b7107131
RB
584 }
585
8d9a85d4 586 giterr_clear();
ec24e542
VM
587 }
588
2d945f82
CMN
589 if (!iter->cache) {
590 if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
591 return error;
592 }
fe372740 593
2d945f82
CMN
594 error = GIT_ITEROVER;
595 while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
596 ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
8d9a85d4
RB
597 if (!ref) /* stop now if another thread deleted refs and we past end */
598 break;
fe372740
RB
599
600 if (ref->flags & PACKREF_SHADOWED)
601 continue;
8d9a85d4 602 if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
ec24e542
VM
603 continue;
604
8d9a85d4
RB
605 *out = ref->name;
606 error = 0;
607 break;
ec24e542
VM
608 }
609
8d9a85d4 610 return error;
ec24e542
VM
611}
612
613static int refdb_fs_backend__iterator(
614 git_reference_iterator **out, git_refdb_backend *_backend, const char *glob)
615{
616 refdb_fs_iter *iter;
fe372740 617 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
ec24e542 618
fe372740 619 assert(backend);
ec24e542 620
fe372740 621 if (packed_reload(backend) < 0)
ec24e542
VM
622 return -1;
623
624 iter = git__calloc(1, sizeof(refdb_fs_iter));
625 GITERR_CHECK_ALLOC(iter);
626
fe372740
RB
627 if (git_pool_init(&iter->pool, 1, 0) < 0 ||
628 git_vector_init(&iter->loose, 8, NULL) < 0)
c77342ef
RB
629 goto fail;
630
631 if (glob != NULL &&
632 (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL)
633 goto fail;
ec24e542
VM
634
635 iter->parent.next = refdb_fs_backend__iterator_next;
636 iter->parent.next_name = refdb_fs_backend__iterator_next_name;
637 iter->parent.free = refdb_fs_backend__iterator_free;
638
c77342ef
RB
639 if (iter_load_loose_paths(backend, iter) < 0)
640 goto fail;
ec24e542
VM
641
642 *out = (git_reference_iterator *)iter;
643 return 0;
c77342ef
RB
644
645fail:
646 refdb_fs_backend__iterator_free((git_reference_iterator *)iter);
647 return -1;
ec24e542
VM
648}
649
4e6e2ff2
VM
650static bool ref_is_available(
651 const char *old_ref, const char *new_ref, const char *this_ref)
652{
653 if (old_ref == NULL || strcmp(old_ref, this_ref)) {
654 size_t reflen = strlen(this_ref);
655 size_t newlen = strlen(new_ref);
656 size_t cmplen = reflen < newlen ? reflen : newlen;
657 const char *lead = reflen < newlen ? new_ref : this_ref;
658
659 if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') {
660 return false;
661 }
662 }
663
664 return true;
665}
666
667static int reference_path_available(
668 refdb_fs_backend *backend,
669 const char *new_ref,
670 const char* old_ref,
671 int force)
672{
fe372740 673 size_t i;
4e6e2ff2 674
fe372740 675 if (packed_reload(backend) < 0)
4e6e2ff2
VM
676 return -1;
677
678 if (!force) {
679 int exists;
680
fe372740
RB
681 if (refdb_fs_backend__exists(
682 &exists, (git_refdb_backend *)backend, new_ref) < 0)
4e6e2ff2
VM
683 return -1;
684
685 if (exists) {
686 giterr_set(GITERR_REFERENCE,
687 "Failed to write reference '%s': a reference with "
8d9a85d4 688 "that name already exists.", new_ref);
4e6e2ff2
VM
689 return GIT_EEXISTS;
690 }
691 }
692
8d9a85d4 693 git_sortedcache_rlock(backend->refcache);
fe372740
RB
694
695 for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
8d9a85d4 696 struct packref *ref = git_sortedcache_entry(backend->refcache, i);
fe372740 697
8d9a85d4
RB
698 if (ref && !ref_is_available(old_ref, new_ref, ref->name)) {
699 git_sortedcache_runlock(backend->refcache);
4e6e2ff2 700 giterr_set(GITERR_REFERENCE,
8d9a85d4 701 "Path to reference '%s' collides with existing one", new_ref);
4e6e2ff2
VM
702 return -1;
703 }
fe372740 704 }
c77342ef 705
8d9a85d4 706 git_sortedcache_runlock(backend->refcache);
4e6e2ff2
VM
707 return 0;
708}
ec24e542 709
7ee8c7e6 710static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
d00d5464 711{
ee5da720 712 int error;
d00d5464
ET
713 git_buf ref_path = GIT_BUF_INIT;
714
7ee8c7e6 715 assert(file && backend && name);
9b148098 716
a64119e3
ET
717 if (!git_path_isvalid(backend->repo, name, GIT_PATH_REJECT_DEFAULTS)) {
718 giterr_set(GITERR_INVALID, "Invalid reference name '%s'.", name);
719 return GIT_EINVALIDSPEC;
720 }
721
d00d5464
ET
722 /* Remove a possibly existing empty directory hierarchy
723 * which name would collide with the reference name
724 */
7ee8c7e6 725 if (git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0)
d00d5464
ET
726 return -1;
727
7ee8c7e6 728 if (git_buf_joinpath(&ref_path, backend->path, name) < 0)
d00d5464
ET
729 return -1;
730
9b148098 731 error = git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE);
d00d5464
ET
732
733 git_buf_free(&ref_path);
9b148098 734 return error;
a57dd3b7 735}
d00d5464 736
a57dd3b7
CMN
737static int loose_commit(git_filebuf *file, const git_reference *ref)
738{
9b148098
CMN
739 assert(file && ref);
740
d00d5464
ET
741 if (ref->type == GIT_REF_OID) {
742 char oid[GIT_OID_HEXSZ + 1];
fe372740 743 git_oid_nfmt(oid, sizeof(oid), &ref->target.oid);
d00d5464 744
a57dd3b7 745 git_filebuf_printf(file, "%s\n", oid);
d00d5464 746 } else if (ref->type == GIT_REF_SYMBOLIC) {
a57dd3b7 747 git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic);
d00d5464
ET
748 } else {
749 assert(0); /* don't let this happen */
750 }
751
a57dd3b7 752 return git_filebuf_commit(file);
d00d5464
ET
753}
754
ab8d9242
CMN
755static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const char *refname)
756{
757 int error;
758 git_filebuf *lock;
759 refdb_fs_backend *backend = (refdb_fs_backend *) _backend;
760
761 lock = git__calloc(1, sizeof(git_filebuf));
762 GITERR_CHECK_ALLOC(lock);
763
764 if ((error = loose_lock(lock, backend, refname)) < 0) {
765 git__free(lock);
766 return error;
767 }
768
769 *out = lock;
770 return 0;
771}
772
773static int refdb_fs_backend__write_tail(
774 git_refdb_backend *_backend,
775 const git_reference *ref,
776 git_filebuf *file,
777 int update_reflog,
778 const git_signature *who,
779 const char *message,
780 const git_oid *old_id,
781 const char *old_target);
782
783static int refdb_fs_backend__delete_tail(
784 git_refdb_backend *_backend,
785 git_filebuf *file,
786 const char *ref_name,
787 const git_oid *old_id, const char *old_target);
788
789static int refdb_fs_backend__unlock(git_refdb_backend *backend, void *payload, int success, int update_reflog,
790 const git_reference *ref, const git_signature *sig, const char *message)
791{
792 git_filebuf *lock = (git_filebuf *) payload;
793 int error = 0;
794
795 if (success == 2)
796 error = refdb_fs_backend__delete_tail(backend, lock, ref->name, NULL, NULL);
797 else if (success)
798 error = refdb_fs_backend__write_tail(backend, ref, lock, update_reflog, sig, message, NULL, NULL);
799 else
800 git_filebuf_cleanup(lock);
801
802 git__free(lock);
803 return error;
804}
805
d00d5464
ET
806/*
807 * Find out what object this reference resolves to.
808 *
809 * For references that point to a 'big' tag (e.g. an
810 * actual tag object on the repository), we need to
811 * cache on the packfile the OID of the object to
812 * which that 'big tag' is pointing to.
813 */
814static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
815{
816 git_object *object;
817
f69db390 818 if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL)
d00d5464
ET
819 return 0;
820
d00d5464
ET
821 /*
822 * Find the tagged object in the repository
823 */
824 if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJ_ANY) < 0)
825 return -1;
826
827 /*
828 * If the tagged object is a Tag object, we need to resolve it;
829 * if the ref is actually a 'weak' ref, we don't need to resolve
830 * anything.
831 */
832 if (git_object_type(object) == GIT_OBJ_TAG) {
833 git_tag *tag = (git_tag *)object;
834
835 /*
836 * Find the object pointed at by this tag
837 */
838 git_oid_cpy(&ref->peel, git_tag_target_id(tag));
f69db390 839 ref->flags |= PACKREF_HAS_PEEL;
d00d5464
ET
840
841 /*
842 * The reference has now cached the resolved OID, and is
843 * marked at such. When written to the packfile, it'll be
844 * accompanied by this resolved oid
845 */
846 }
847
848 git_object_free(object);
849 return 0;
850}
851
852/*
853 * Write a single reference into a packfile
854 */
855static int packed_write_ref(struct packref *ref, git_filebuf *file)
856{
857 char oid[GIT_OID_HEXSZ + 1];
fe372740 858 git_oid_nfmt(oid, sizeof(oid), &ref->oid);
d00d5464
ET
859
860 /*
861 * For references that peel to an object in the repo, we must
862 * write the resulting peel on a separate line, e.g.
863 *
864 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
865 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
866 *
867 * This obviously only applies to tags.
868 * The required peels have already been loaded into `ref->peel_target`.
869 */
f69db390 870 if (ref->flags & PACKREF_HAS_PEEL) {
d00d5464 871 char peel[GIT_OID_HEXSZ + 1];
fe372740 872 git_oid_nfmt(peel, sizeof(peel), &ref->peel);
d00d5464
ET
873
874 if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0)
875 return -1;
876 } else {
877 if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0)
878 return -1;
879 }
880
881 return 0;
882}
883
884/*
885 * Remove all loose references
886 *
887 * Once we have successfully written a packfile,
888 * all the loose references that were packed must be
889 * removed from disk.
890 *
891 * This is a dangerous method; make sure the packfile
892 * is well-written, because we are destructing references
893 * here otherwise.
894 */
fe372740 895static int packed_remove_loose(refdb_fs_backend *backend)
d00d5464 896{
10c06114 897 size_t i;
d00d5464
ET
898 git_buf full_path = GIT_BUF_INIT;
899 int failed = 0;
900
fe372740
RB
901 /* backend->refcache is already locked when this is called */
902
903 for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
904 struct packref *ref = git_sortedcache_entry(backend->refcache, i);
d00d5464 905
8d9a85d4 906 if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
d00d5464
ET
907 continue;
908
909 if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0)
910 return -1; /* critical; do not try to recover on oom */
911
fe372740 912 if (git_path_exists(full_path.ptr) && p_unlink(full_path.ptr) < 0) {
d00d5464
ET
913 if (failed)
914 continue;
915
916 giterr_set(GITERR_REFERENCE,
917 "Failed to remove loose reference '%s' after packing: %s",
918 full_path.ptr, strerror(errno));
d00d5464
ET
919 failed = 1;
920 }
921
922 /*
923 * if we fail to remove a single file, this is *not* good,
924 * but we should keep going and remove as many as possible.
925 * After we've removed as many files as possible, we return
926 * the error code anyway.
927 */
928 }
929
930 git_buf_free(&full_path);
931 return failed ? -1 : 0;
932}
933
934/*
935 * Write all the contents in the in-memory packfile to disk.
936 */
937static int packed_write(refdb_fs_backend *backend)
938{
8d9a85d4 939 git_sortedcache *refcache = backend->refcache;
d00d5464 940 git_filebuf pack_file = GIT_FILEBUF_INIT;
10c06114 941 size_t i;
d00d5464 942
fe372740 943 /* lock the cache to updates while we do this */
8d9a85d4 944 if (git_sortedcache_wlock(refcache) < 0)
d00d5464
ET
945 return -1;
946
fe372740 947 /* Open the file! */
1d3a8aeb 948 if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE) < 0)
8d9a85d4 949 goto fail;
d00d5464
ET
950
951 /* Packfiles have a header... apparently
952 * This is in fact not required, but we might as well print it
953 * just for kicks */
954 if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0)
8d9a85d4 955 goto fail;
d00d5464 956
8d9a85d4
RB
957 for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
958 struct packref *ref = git_sortedcache_entry(refcache, i);
d00d5464
ET
959
960 if (packed_find_peel(backend, ref) < 0)
8d9a85d4 961 goto fail;
d00d5464
ET
962
963 if (packed_write_ref(ref, &pack_file) < 0)
8d9a85d4 964 goto fail;
d00d5464
ET
965 }
966
967 /* if we've written all the references properly, we can commit
968 * the packfile to make the changes effective */
1d3a8aeb 969 if (git_filebuf_commit(&pack_file) < 0)
8d9a85d4 970 goto fail;
d00d5464
ET
971
972 /* when and only when the packfile has been properly written,
973 * we can go ahead and remove the loose refs */
fe372740 974 if (packed_remove_loose(backend) < 0)
8d9a85d4 975 goto fail;
d00d5464 976
8d9a85d4
RB
977 git_sortedcache_updated(refcache);
978 git_sortedcache_wunlock(refcache);
d00d5464
ET
979
980 /* we're good now */
981 return 0;
982
8d9a85d4 983fail:
d00d5464 984 git_filebuf_cleanup(&pack_file);
8d9a85d4 985 git_sortedcache_wunlock(refcache);
d00d5464
ET
986
987 return -1;
988}
989
4b7e1b9e 990static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message);
6f13a305
CMN
991static int has_reflog(git_repository *repo, const char *name);
992
993/* We only write if it's under heads/, remotes/ or notes/ or if it already has a log */
83504371 994static int should_write_reflog(int *write, git_repository *repo, const char *name)
6f13a305 995{
2b52a0bf 996 int error, logall;
6f13a305 997
2b52a0bf
RB
998 error = git_repository__cvar(&logall, repo, GIT_CVAR_LOGALLREFUPDATES);
999 if (error < 0)
83504371
CMN
1000 return error;
1001
2b52a0bf
RB
1002 /* Defaults to the opposite of the repo being bare */
1003 if (logall == GIT_LOGALLREFUPDATES_UNSET)
1004 logall = !git_repository_is_bare(repo);
83504371
CMN
1005
1006 if (!logall) {
1007 *write = 0;
1008 } else if (has_reflog(repo, name)) {
1009 *write = 1;
1010 } else if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR) ||
1011 !git__strcmp(name, GIT_HEAD_FILE) ||
1012 !git__prefixcmp(name, GIT_REFS_REMOTES_DIR) ||
1013 !git__prefixcmp(name, GIT_REFS_NOTES_DIR)) {
1014 *write = 1;
1015 } else {
1016 *write = 0;
1017 }
6f13a305
CMN
1018
1019 return 0;
1020}
a57dd3b7 1021
7ee8c7e6 1022static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name,
f8621dde
CMN
1023 const git_oid *old_id, const char *old_target)
1024{
1025 int error = 0;
1026 git_reference *old_ref = NULL;
1027
1028 *cmp = 0;
b7ae71ec
CMN
1029 /* It "matches" if there is no old value to compare against */
1030 if (!old_id && !old_target)
1031 return 0;
1032
1033 if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0)
1034 goto out;
1035
1036 /* If the types don't match, there's no way the values do */
1037 if (old_id && old_ref->type != GIT_REF_OID) {
1038 *cmp = -1;
1039 goto out;
1040 }
1041 if (old_target && old_ref->type != GIT_REF_SYMBOLIC) {
1042 *cmp = 1;
1043 goto out;
f8621dde
CMN
1044 }
1045
1046 if (old_id && old_ref->type == GIT_REF_OID)
1047 *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
1048
1049 if (old_target && old_ref->type == GIT_REF_SYMBOLIC)
1050 *cmp = git__strcmp(old_target, old_ref->target.symbolic);
1051
1052out:
1053 git_reference_free(old_ref);
1054
1055 return error;
1056}
1057
4b7e1b9e
CMN
1058/*
1059 * The git.git comment regarding this, for your viewing pleasure:
1060 *
1061 * Special hack: If a branch is updated directly and HEAD
1062 * points to it (may happen on the remote side of a push
1063 * for example) then logically the HEAD reflog should be
1064 * updated too.
1065 * A generic solution implies reverse symref information,
1066 * but finding all symrefs pointing to the given branch
1067 * would be rather costly for this rare event (the direct
1068 * update of a branch) to be worth it. So let's cheat and
1069 * check with HEAD only which should cover 99% of all usage
1070 * scenarios (even 100% of the default ones).
1071 */
1072static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
1073{
1074 int error;
6aaae94a 1075 git_oid old_id = {{0}};
99797c96
CMN
1076 git_reference *tmp = NULL, *head = NULL, *peeled = NULL;
1077 const char *name;
1078
1079 if (ref->type == GIT_REF_SYMBOLIC)
1080 return 0;
4b7e1b9e 1081
6aaae94a
CMN
1082 /* if we can't resolve, we use {0}*40 as old id */
1083 git_reference_name_to_id(&old_id, backend->repo, ref->name);
4b7e1b9e 1084
99797c96 1085 if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0)
4b7e1b9e
CMN
1086 return error;
1087
6aaae94a
CMN
1088 if (git_reference_type(head) == GIT_REF_OID)
1089 goto cleanup;
1090
99797c96
CMN
1091 if ((error = git_reference_lookup(&tmp, backend->repo, GIT_HEAD_FILE)) < 0)
1092 goto cleanup;
1093
1094 /* Go down the symref chain until we find the branch */
1095 while (git_reference_type(tmp) == GIT_REF_SYMBOLIC) {
1096 error = git_reference_lookup(&peeled, backend->repo, git_reference_symbolic_target(tmp));
1097 if (error < 0)
1098 break;
1099
1100 git_reference_free(tmp);
1101 tmp = peeled;
1102 }
1103
1104 if (error == GIT_ENOTFOUND) {
1105 error = 0;
1106 name = git_reference_symbolic_target(tmp);
1107 } else if (error < 0) {
1108 goto cleanup;
1109 } else {
1110 name = git_reference_name(tmp);
1111 }
1112
99797c96 1113 if (strcmp(name, ref->name))
6aaae94a
CMN
1114 goto cleanup;
1115
4b7e1b9e
CMN
1116 error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message);
1117
6aaae94a 1118cleanup:
99797c96 1119 git_reference_free(tmp);
4b7e1b9e
CMN
1120 git_reference_free(head);
1121 return error;
1122}
1123
d00d5464
ET
1124static int refdb_fs_backend__write(
1125 git_refdb_backend *_backend,
4e6e2ff2 1126 const git_reference *ref,
110df893 1127 int force,
a57dd3b7 1128 const git_signature *who,
9b148098 1129 const char *message,
91123661
CMN
1130 const git_oid *old_id,
1131 const char *old_target)
d00d5464 1132{
fe372740 1133 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
a57dd3b7 1134 git_filebuf file = GIT_FILEBUF_INIT;
ab8d9242 1135 int error = 0;
d00d5464 1136
fe372740 1137 assert(backend);
d00d5464 1138
4e6e2ff2
VM
1139 error = reference_path_available(backend, ref->name, NULL, force);
1140 if (error < 0)
1141 return error;
1142
9b148098 1143 /* We need to perform the reflog append and old value check under the ref's lock */
7ee8c7e6 1144 if ((error = loose_lock(&file, backend, ref->name)) < 0)
a57dd3b7
CMN
1145 return error;
1146
ab8d9242
CMN
1147 return refdb_fs_backend__write_tail(_backend, ref, &file, true, who, message, old_id, old_target);
1148}
1149
1150static int refdb_fs_backend__write_tail(
1151 git_refdb_backend *_backend,
1152 const git_reference *ref,
1153 git_filebuf *file,
1154 int update_reflog,
1155 const git_signature *who,
1156 const char *message,
1157 const git_oid *old_id,
1158 const char *old_target)
1159{
1160 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1161 int error = 0, cmp = 0, should_write;
1162 const char *new_target = NULL;
1163 const git_oid *new_id = NULL;
1164
7ee8c7e6 1165 if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0)
f8621dde 1166 goto on_error;
91123661
CMN
1167
1168 if (cmp) {
1169 giterr_set(GITERR_REFERENCE, "old reference value does not match");
1170 error = GIT_EMODIFIED;
1171 goto on_error;
9b148098
CMN
1172 }
1173
1afe1400
CMN
1174 if (ref->type == GIT_REF_SYMBOLIC)
1175 new_target = ref->target.symbolic;
1176 else
1177 new_id = &ref->target.oid;
1178
1179 error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target);
1180 if (error < 0 && error != GIT_ENOTFOUND)
1181 goto on_error;
1182
1183 /* Don't update if we have the same value */
1184 if (!error && !cmp) {
1185 error = 0;
1186 goto on_error; /* not really error */
1187 }
1188
ab8d9242
CMN
1189 if (update_reflog) {
1190 if ((error = should_write_reflog(&should_write, backend->repo, ref->name)) < 0)
4b7e1b9e 1191 goto on_error;
ab8d9242
CMN
1192
1193 if (should_write) {
1194 if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
1195 goto on_error;
1196 if ((error = maybe_append_head(backend, ref, who, message)) < 0)
1197 goto on_error;
1198 }
a57dd3b7
CMN
1199 }
1200
ab8d9242 1201 return loose_commit(file, ref);
9b148098
CMN
1202
1203on_error:
ab8d9242 1204 git_filebuf_cleanup(file);
fc4728e3 1205 return error;
d00d5464
ET
1206}
1207
1208static int refdb_fs_backend__delete(
1209 git_refdb_backend *_backend,
7ee8c7e6
CMN
1210 const char *ref_name,
1211 const git_oid *old_id, const char *old_target)
d00d5464 1212{
fe372740 1213 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
77ad6754 1214 git_filebuf file = GIT_FILEBUF_INIT;
ab8d9242 1215 int error = 0;
d00d5464 1216
fe372740 1217 assert(backend && ref_name);
d00d5464 1218
7ee8c7e6
CMN
1219 if ((error = loose_lock(&file, backend, ref_name)) < 0)
1220 return error;
1221
01d0c02d
CMN
1222 if ((error = refdb_reflog_fs__delete(_backend, ref_name)) < 0) {
1223 git_filebuf_cleanup(&file);
1224 return error;
1225 }
1226
ab8d9242
CMN
1227 return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target);
1228}
1229
1230static int refdb_fs_backend__delete_tail(
1231 git_refdb_backend *_backend,
1232 git_filebuf *file,
1233 const char *ref_name,
1234 const git_oid *old_id, const char *old_target)
1235{
1236 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1237 git_buf loose_path = GIT_BUF_INIT;
1238 size_t pack_pos;
1239 int error = 0, cmp = 0;
1240 bool loose_deleted = 0;
1241
7ee8c7e6 1242 error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target);
7ee8c7e6
CMN
1243 if (error < 0)
1244 goto cleanup;
1245
1246 if (cmp) {
1247 giterr_set(GITERR_REFERENCE, "old reference value does not match");
1248 error = GIT_EMODIFIED;
1249 goto cleanup;
1250 }
1251
d00d5464 1252 /* If a loose reference exists, remove it from the filesystem */
4e6e2ff2 1253 if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
d00d5464
ET
1254 return -1;
1255
1256 if (git_path_isfile(loose_path.ptr)) {
1257 error = p_unlink(loose_path.ptr);
1258 loose_deleted = 1;
1259 }
4dcd8780 1260
d00d5464
ET
1261 git_buf_free(&loose_path);
1262
1263 if (error != 0)
7ee8c7e6 1264 goto cleanup;
d00d5464 1265
7ee8c7e6
CMN
1266 if ((error = packed_reload(backend)) < 0)
1267 goto cleanup;
fe372740 1268
d00d5464 1269 /* If a packed reference exists, remove it from the packfile and repack */
7ee8c7e6
CMN
1270 if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
1271 goto cleanup;
4e6e2ff2 1272
fe372740
RB
1273 if (!(error = git_sortedcache_lookup_index(
1274 &pack_pos, backend->refcache, ref_name)))
8d9a85d4 1275 error = git_sortedcache_remove(backend->refcache, pack_pos);
d00d5464 1276
8d9a85d4 1277 git_sortedcache_wunlock(backend->refcache);
4dcd8780 1278
7ee8c7e6
CMN
1279 if (error == GIT_ENOTFOUND) {
1280 error = loose_deleted ? 0 : ref_error_notfound(ref_name);
1281 goto cleanup;
1282 }
1283
1284 error = packed_write(backend);
fe372740 1285
7ee8c7e6 1286cleanup:
ab8d9242 1287 git_filebuf_cleanup(file);
7ee8c7e6
CMN
1288
1289 return error;
d00d5464
ET
1290}
1291
a57dd3b7
CMN
1292static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name);
1293
4e6e2ff2
VM
1294static int refdb_fs_backend__rename(
1295 git_reference **out,
1296 git_refdb_backend *_backend,
1297 const char *old_name,
1298 const char *new_name,
110df893 1299 int force,
a57dd3b7 1300 const git_signature *who,
110df893 1301 const char *message)
4e6e2ff2 1302{
fe372740 1303 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
4e6e2ff2 1304 git_reference *old, *new;
a57dd3b7 1305 git_filebuf file = GIT_FILEBUF_INIT;
4e6e2ff2
VM
1306 int error;
1307
fe372740 1308 assert(backend);
4e6e2ff2 1309
fe372740
RB
1310 if ((error = reference_path_available(
1311 backend, new_name, old_name, force)) < 0 ||
1312 (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0)
4e6e2ff2
VM
1313 return error;
1314
7ee8c7e6 1315 if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
4e6e2ff2
VM
1316 git_reference_free(old);
1317 return error;
1318 }
1319
fe372740
RB
1320 new = git_reference__set_name(old, new_name);
1321 if (!new) {
1322 git_reference_free(old);
1323 return -1;
4e6e2ff2
VM
1324 }
1325
7ee8c7e6 1326 if ((error = loose_lock(&file, backend, new->name)) < 0) {
a57dd3b7
CMN
1327 git_reference_free(new);
1328 return error;
1329 }
1330
1331 /* Try to rename the refog; it's ok if the old doesn't exist */
1332 error = refdb_reflog_fs__rename(_backend, old_name, new_name);
1333 if (((error == 0) || (error == GIT_ENOTFOUND)) &&
d578b45f 1334 ((error = reflog_append(backend, new, git_reference_target(new), NULL, who, message)) < 0)) {
a57dd3b7
CMN
1335 git_reference_free(new);
1336 git_filebuf_cleanup(&file);
1337 return error;
1338 }
1339
1340 if (error < 0) {
1341 git_reference_free(new);
1342 git_filebuf_cleanup(&file);
1343 return error;
1344 }
1345
1346
1347 if ((error = loose_commit(&file, new)) < 0 || out == NULL) {
4e6e2ff2 1348 git_reference_free(new);
fe372740 1349 return error;
4e6e2ff2
VM
1350 }
1351
fe372740 1352 *out = new;
4e6e2ff2
VM
1353 return 0;
1354}
1355
d00d5464
ET
1356static int refdb_fs_backend__compress(git_refdb_backend *_backend)
1357{
fe372740 1358 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
d00d5464 1359
fe372740 1360 assert(backend);
d00d5464 1361
fe372740 1362 if (packed_reload(backend) < 0 || /* load the existing packfile */
d00d5464
ET
1363 packed_loadloose(backend) < 0 || /* add all the loose refs */
1364 packed_write(backend) < 0) /* write back to disk */
1365 return -1;
1366
1367 return 0;
1368}
1369
d00d5464
ET
1370static void refdb_fs_backend__free(git_refdb_backend *_backend)
1371{
fe372740 1372 refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
d00d5464 1373
fe372740 1374 assert(backend);
d00d5464 1375
fe372740 1376 git_sortedcache_free(backend->refcache);
bade5194 1377 git__free(backend->path);
d00d5464
ET
1378 git__free(backend);
1379}
1380
8cddf9b8
VM
1381static int setup_namespace(git_buf *path, git_repository *repo)
1382{
1fed6b07 1383 char *parts, *start, *end;
8cddf9b8 1384
69a3c766
CMN
1385 /* Not all repositories have a path */
1386 if (repo->path_repository == NULL)
1387 return 0;
1388
8cddf9b8
VM
1389 /* Load the path to the repo first */
1390 git_buf_puts(path, repo->path_repository);
1391
1392 /* if the repo is not namespaced, nothing else to do */
1393 if (repo->namespace == NULL)
1394 return 0;
1395
1396 parts = end = git__strdup(repo->namespace);
1397 if (parts == NULL)
1398 return -1;
1399
fe372740 1400 /*
8cddf9b8
VM
1401 * From `man gitnamespaces`:
1402 * namespaces which include a / will expand to a hierarchy
1403 * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
1404 * refs under refs/namespaces/foo/refs/namespaces/bar/
1405 */
1406 while ((start = git__strsep(&end, "/")) != NULL) {
1407 git_buf_printf(path, "refs/namespaces/%s/", start);
1408 }
1409
1410 git_buf_printf(path, "refs/namespaces/%s/refs", end);
1ed356dc 1411 git__free(parts);
8cddf9b8
VM
1412
1413 /* Make sure that the folder with the namespace exists */
1fed6b07 1414 if (git_futils_mkdir_r(git_buf_cstr(path), repo->path_repository, 0777) < 0)
8cddf9b8
VM
1415 return -1;
1416
fe372740 1417 /* Return root of the namespaced path, i.e. without the trailing '/refs' */
8cddf9b8
VM
1418 git_buf_rtruncate_at_char(path, '/');
1419 return 0;
1420}
1421
b976f3c2
CMN
1422static int reflog_alloc(git_reflog **reflog, const char *name)
1423{
1424 git_reflog *log;
1425
1426 *reflog = NULL;
1427
1428 log = git__calloc(1, sizeof(git_reflog));
1429 GITERR_CHECK_ALLOC(log);
1430
1431 log->ref_name = git__strdup(name);
1432 GITERR_CHECK_ALLOC(log->ref_name);
1433
1434 if (git_vector_init(&log->entries, 0, NULL) < 0) {
1435 git__free(log->ref_name);
1436 git__free(log);
1437 return -1;
1438 }
1439
1440 *reflog = log;
1441
1442 return 0;
1443}
1444
1445static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
1446{
1447 const char *ptr;
1448 git_reflog_entry *entry;
1449
1450#define seek_forward(_increase) do { \
1451 if (_increase >= buf_size) { \
1452 giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
1453 goto fail; \
1454 } \
1455 buf += _increase; \
1456 buf_size -= _increase; \
1457 } while (0)
1458
1459 while (buf_size > GIT_REFLOG_SIZE_MIN) {
1460 entry = git__calloc(1, sizeof(git_reflog_entry));
1461 GITERR_CHECK_ALLOC(entry);
1462
1463 entry->committer = git__malloc(sizeof(git_signature));
1464 GITERR_CHECK_ALLOC(entry->committer);
1465
1466 if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0)
1467 goto fail;
1468 seek_forward(GIT_OID_HEXSZ + 1);
1469
1470 if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0)
1471 goto fail;
1472 seek_forward(GIT_OID_HEXSZ + 1);
1473
1474 ptr = buf;
1475
1476 /* Seek forward to the end of the signature. */
1477 while (*buf && *buf != '\t' && *buf != '\n')
1478 seek_forward(1);
1479
1480 if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0)
1481 goto fail;
1482
1483 if (*buf == '\t') {
1484 /* We got a message. Read everything till we reach LF. */
1485 seek_forward(1);
1486 ptr = buf;
1487
1488 while (*buf && *buf != '\n')
1489 seek_forward(1);
1490
1491 entry->msg = git__strndup(ptr, buf - ptr);
1492 GITERR_CHECK_ALLOC(entry->msg);
1493 } else
1494 entry->msg = NULL;
1495
1496 while (*buf && *buf == '\n' && buf_size > 1)
1497 seek_forward(1);
1498
1499 if (git_vector_insert(&log->entries, entry) < 0)
1500 goto fail;
1501 }
1502
1503 return 0;
1504
1505#undef seek_forward
1506
1507fail:
1508 if (entry)
1509 git_reflog_entry__free(entry);
1510
1511 return -1;
1512}
1513
1514static int create_new_reflog_file(const char *filepath)
1515{
1516 int fd, error;
1517
1518 if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
1519 return error;
1520
1521 if ((fd = p_open(filepath,
8d5ec910 1522 O_WRONLY | O_CREAT,
b976f3c2
CMN
1523 GIT_REFLOG_FILE_MODE)) < 0)
1524 return -1;
1525
1526 return p_close(fd);
1527}
1528
1529GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name)
1530{
18234b14 1531 return git_buf_join3(path, '/', repo->path_repository, GIT_REFLOG_DIR, name);
b976f3c2
CMN
1532}
1533
8d5ec910
CMN
1534static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name)
1535{
1536 refdb_fs_backend *backend;
1537 git_repository *repo;
1538 git_buf path = GIT_BUF_INIT;
1539 int error;
1540
1541 assert(_backend && name);
1542
1543 backend = (refdb_fs_backend *) _backend;
1544 repo = backend->repo;
1545
1546 if ((error = retrieve_reflog_path(&path, repo, name)) < 0)
1547 return error;
1548
ae32c54e
CMN
1549 error = create_new_reflog_file(git_buf_cstr(&path));
1550 git_buf_free(&path);
1551
1552 return error;
8d5ec910
CMN
1553}
1554
6f13a305
CMN
1555static int has_reflog(git_repository *repo, const char *name)
1556{
1557 int ret = 0;
1558 git_buf path = GIT_BUF_INIT;
1559
1560 if (retrieve_reflog_path(&path, repo, name) < 0)
1561 goto cleanup;
1562
1563 ret = git_path_isfile(git_buf_cstr(&path));
1564
1565cleanup:
1566 git_buf_free(&path);
1567 return ret;
1568}
1569
f2105129
CMN
1570static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name)
1571{
1572 refdb_fs_backend *backend;
1573
1574 assert(_backend && name);
1575
1576 backend = (refdb_fs_backend *) _backend;
1577
1578 return has_reflog(backend->repo, name);
1579}
1580
b976f3c2
CMN
1581static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name)
1582{
1583 int error = -1;
1584 git_buf log_path = GIT_BUF_INIT;
1585 git_buf log_file = GIT_BUF_INIT;
1586 git_reflog *log = NULL;
1587 git_repository *repo;
1588 refdb_fs_backend *backend;
1589
1590 assert(out && _backend && name);
1591
1592 backend = (refdb_fs_backend *) _backend;
1593 repo = backend->repo;
1594
1595 if (reflog_alloc(&log, name) < 0)
1596 return -1;
1597
1598 if (retrieve_reflog_path(&log_path, repo, name) < 0)
1599 goto cleanup;
1600
1601 error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path));
1602 if (error < 0 && error != GIT_ENOTFOUND)
1603 goto cleanup;
1604
1605 if ((error == GIT_ENOTFOUND) &&
1606 ((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0))
1607 goto cleanup;
1608
1609 if ((error = reflog_parse(log,
1610 git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
1611 goto cleanup;
1612
1613 *out = log;
1614 goto success;
1615
1616cleanup:
1617 git_reflog_free(log);
1618
1619success:
1620 git_buf_free(&log_file);
1621 git_buf_free(&log_path);
1622
1623 return error;
1624}
1625
1626static int serialize_reflog_entry(
1627 git_buf *buf,
1628 const git_oid *oid_old,
1629 const git_oid *oid_new,
1630 const git_signature *committer,
1631 const char *msg)
1632{
1633 char raw_old[GIT_OID_HEXSZ+1];
1634 char raw_new[GIT_OID_HEXSZ+1];
1635
1636 git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
1637 git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
1638
1639 git_buf_clear(buf);
1640
1641 git_buf_puts(buf, raw_old);
1642 git_buf_putc(buf, ' ');
1643 git_buf_puts(buf, raw_new);
1644
1645 git_signature__writebuf(buf, " ", committer);
1646
1647 /* drop trailing LF */
1648 git_buf_rtrim(buf);
1649
1650 if (msg) {
1651 git_buf_putc(buf, '\t');
1652 git_buf_puts(buf, msg);
1653 }
1654
1655 git_buf_putc(buf, '\n');
1656
1657 return git_buf_oom(buf);
1658}
1659
110df893
CMN
1660static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname)
1661{
1662 git_repository *repo;
1663 git_buf log_path = GIT_BUF_INIT;
1664 int error;
1665
1666 repo = backend->repo;
1667
a64119e3
ET
1668 if (!git_path_isvalid(backend->repo, refname, GIT_PATH_REJECT_DEFAULTS)) {
1669 giterr_set(GITERR_INVALID, "Invalid reference name '%s'.", refname);
1670 return GIT_EINVALIDSPEC;
1671 }
1672
110df893
CMN
1673 if (retrieve_reflog_path(&log_path, repo, refname) < 0)
1674 return -1;
1675
1676 if (!git_path_isfile(git_buf_cstr(&log_path))) {
1677 giterr_set(GITERR_INVALID,
1678 "Log file for reference '%s' doesn't exist.", refname);
1679 error = -1;
1680 goto cleanup;
1681 }
1682
1683 error = git_filebuf_open(file, git_buf_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE);
1684
1685cleanup:
1686 git_buf_free(&log_path);
1687
1688 return error;
1689}
1690
b976f3c2
CMN
1691static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog)
1692{
1693 int error = -1;
1694 unsigned int i;
1695 git_reflog_entry *entry;
b976f3c2 1696 refdb_fs_backend *backend;
b976f3c2
CMN
1697 git_buf log = GIT_BUF_INIT;
1698 git_filebuf fbuf = GIT_FILEBUF_INIT;
1699
1700 assert(_backend && reflog);
1701
1702 backend = (refdb_fs_backend *) _backend;
b976f3c2 1703
110df893 1704 if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0)
b976f3c2
CMN
1705 return -1;
1706
b976f3c2
CMN
1707 git_vector_foreach(&reflog->entries, i, entry) {
1708 if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
1709 goto cleanup;
1710
1711 if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
1712 goto cleanup;
1713 }
1714
1d3a8aeb 1715 error = git_filebuf_commit(&fbuf);
b976f3c2
CMN
1716 goto success;
1717
1718cleanup:
1719 git_filebuf_cleanup(&fbuf);
1720
1721success:
1722 git_buf_free(&log);
110df893 1723
b976f3c2
CMN
1724 return error;
1725}
1726
a57dd3b7 1727/* Append to the reflog, must be called under reference lock */
4b7e1b9e 1728static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *who, const char *message)
a57dd3b7 1729{
afc57eb4 1730 int error, is_symbolic;
4b7e1b9e 1731 git_oid old_id = {{0}}, new_id = {{0}};
a57dd3b7
CMN
1732 git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
1733 git_repository *repo = backend->repo;
1734
4b7e1b9e
CMN
1735 is_symbolic = ref->type == GIT_REF_SYMBOLIC;
1736
1737 /* "normal" symbolic updates do not write */
1738 if (is_symbolic &&
1739 strcmp(ref->name, GIT_HEAD_FILE) &&
1740 !(old && new))
a57dd3b7
CMN
1741 return 0;
1742
4b7e1b9e
CMN
1743 /* From here on is_symoblic also means that it's HEAD */
1744
1745 if (old) {
1746 git_oid_cpy(&old_id, old);
afc57eb4 1747 } else {
4b7e1b9e 1748 error = git_reference_name_to_id(&old_id, repo, ref->name);
afc57eb4 1749 if (error < 0 && error != GIT_ENOTFOUND)
4b7e1b9e
CMN
1750 return error;
1751 }
1752
6aaae94a
CMN
1753 if (new) {
1754 git_oid_cpy(&new_id, new);
1755 } else {
1756 if (!is_symbolic) {
1757 git_oid_cpy(&new_id, git_reference_target(ref));
1758 } else {
1759 error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
1760 if (error < 0 && error != GIT_ENOTFOUND)
1761 return error;
1762 /* detaching HEAD does not create an entry */
1763 if (error == GIT_ENOTFOUND)
1764 return 0;
4b7e1b9e 1765
6aaae94a
CMN
1766 giterr_clear();
1767 }
7f058b86 1768 }
4b7e1b9e 1769
a57dd3b7
CMN
1770 if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
1771 goto cleanup;
1772
1773 if ((error = retrieve_reflog_path(&path, repo, ref->name)) < 0)
1774 goto cleanup;
1775
1776 if (((error = git_futils_mkpath2file(git_buf_cstr(&path), 0777)) < 0) &&
1777 (error != GIT_EEXISTS)) {
1778 goto cleanup;
1779 }
1780
1589a93a
JH
1781 /* If the new branch matches part of the namespace of a previously deleted branch,
1782 * there maybe an obsolete/unused directory (or directory hierarchy) in the way.
1783 */
1784 if (git_path_isdir(git_buf_cstr(&path)) &&
1785 (git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
1786 error = -1;
1787 goto cleanup;
1788 }
1789
a57dd3b7
CMN
1790 error = git_futils_writebuffer(&buf, git_buf_cstr(&path), O_WRONLY|O_CREAT|O_APPEND, GIT_REFLOG_FILE_MODE);
1791
1792cleanup:
1793 git_buf_free(&buf);
1794 git_buf_free(&path);
1795
b976f3c2
CMN
1796 return error;
1797}
1798
b976f3c2
CMN
1799static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
1800{
1801 int error = 0, fd;
1802 git_buf old_path = GIT_BUF_INIT;
1803 git_buf new_path = GIT_BUF_INIT;
1804 git_buf temp_path = GIT_BUF_INIT;
1805 git_buf normalized = GIT_BUF_INIT;
1806 git_repository *repo;
1807 refdb_fs_backend *backend;
1808
1809 assert(_backend && old_name && new_name);
1810
1811 backend = (refdb_fs_backend *) _backend;
1812 repo = backend->repo;
1813
1814 if ((error = git_reference__normalize_name(
1815 &normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0)
1816 return error;
1817
1818 if (git_buf_joinpath(&temp_path, repo->path_repository, GIT_REFLOG_DIR) < 0)
1819 return -1;
1820
1821 if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0)
1822 return -1;
1823
1824 if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0)
1825 return -1;
1826
a57dd3b7
CMN
1827 if (!git_path_exists(git_buf_cstr(&old_path))) {
1828 error = GIT_ENOTFOUND;
1829 goto cleanup;
1830 }
1831
b976f3c2
CMN
1832 /*
1833 * Move the reflog to a temporary place. This two-phase renaming is required
1834 * in order to cope with funny renaming use cases when one tries to move a reference
1835 * to a partially colliding namespace:
1836 * - a/b -> a/b/c
1837 * - a/b/c/d -> a/b/c
1838 */
1839 if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
1840 return -1;
1841
1d3a8aeb 1842 if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) {
b976f3c2
CMN
1843 error = -1;
1844 goto cleanup;
1845 }
1846
1847 p_close(fd);
1848
1849 if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) {
1850 giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
1851 error = -1;
1852 goto cleanup;
1853 }
1854
1855 if (git_path_isdir(git_buf_cstr(&new_path)) &&
1856 (git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
1857 error = -1;
1858 goto cleanup;
1859 }
1860
1861 if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
1862 error = -1;
1863 goto cleanup;
1864 }
1865
1866 if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) {
1867 giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
1868 error = -1;
1869 }
1870
1871cleanup:
1872 git_buf_free(&temp_path);
1873 git_buf_free(&old_path);
1874 git_buf_free(&new_path);
1875 git_buf_free(&normalized);
1876
1877 return error;
1878}
1879
b976f3c2
CMN
1880static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
1881{
1882 int error;
1883 git_buf path = GIT_BUF_INIT;
1884
1885 git_repository *repo;
1886 refdb_fs_backend *backend;
1887
1888 assert(_backend && name);
1889
1890 backend = (refdb_fs_backend *) _backend;
1891 repo = backend->repo;
1892
1893 error = retrieve_reflog_path(&path, repo, name);
1894
1895 if (!error && git_path_exists(path.ptr))
1896 error = p_unlink(path.ptr);
1897
1898 git_buf_free(&path);
1899
1900 return error;
1901
1902}
1903
d00d5464
ET
1904int git_refdb_backend_fs(
1905 git_refdb_backend **backend_out,
4e4eab52 1906 git_repository *repository)
d00d5464 1907{
219d3457 1908 int t = 0;
bade5194 1909 git_buf path = GIT_BUF_INIT;
d00d5464
ET
1910 refdb_fs_backend *backend;
1911
1912 backend = git__calloc(1, sizeof(refdb_fs_backend));
1913 GITERR_CHECK_ALLOC(backend);
1914
1915 backend->repo = repository;
bade5194 1916
fe372740
RB
1917 if (setup_namespace(&path, repository) < 0)
1918 goto fail;
bade5194
VM
1919
1920 backend->path = git_buf_detach(&path);
d00d5464 1921
fe372740
RB
1922 if (git_buf_joinpath(&path, backend->path, GIT_PACKEDREFS_FILE) < 0 ||
1923 git_sortedcache_new(
1924 &backend->refcache, offsetof(struct packref, name),
1925 NULL, NULL, packref_cmp, git_buf_cstr(&path)) < 0)
1926 goto fail;
1927
1928 git_buf_free(&path);
1929
219d3457
RB
1930 if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_IGNORECASE) && t) {
1931 backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
1932 backend->direach_flags |= GIT_PATH_DIR_IGNORE_CASE;
1933 }
1934 if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_PRECOMPOSE) && t) {
1935 backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
1936 backend->direach_flags |= GIT_PATH_DIR_PRECOMPOSE_UNICODE;
1937 }
1938
d00d5464
ET
1939 backend->parent.exists = &refdb_fs_backend__exists;
1940 backend->parent.lookup = &refdb_fs_backend__lookup;
4def7035 1941 backend->parent.iterator = &refdb_fs_backend__iterator;
d00d5464 1942 backend->parent.write = &refdb_fs_backend__write;
e3f3868a 1943 backend->parent.del = &refdb_fs_backend__delete;
4e6e2ff2 1944 backend->parent.rename = &refdb_fs_backend__rename;
d00d5464 1945 backend->parent.compress = &refdb_fs_backend__compress;
ab8d9242
CMN
1946 backend->parent.lock = &refdb_fs_backend__lock;
1947 backend->parent.unlock = &refdb_fs_backend__unlock;
f2105129 1948 backend->parent.has_log = &refdb_reflog_fs__has_log;
8d5ec910 1949 backend->parent.ensure_log = &refdb_reflog_fs__ensure_log;
d00d5464 1950 backend->parent.free = &refdb_fs_backend__free;
b976f3c2
CMN
1951 backend->parent.reflog_read = &refdb_reflog_fs__read;
1952 backend->parent.reflog_write = &refdb_reflog_fs__write;
b976f3c2 1953 backend->parent.reflog_rename = &refdb_reflog_fs__rename;
b976f3c2 1954 backend->parent.reflog_delete = &refdb_reflog_fs__delete;
d00d5464
ET
1955
1956 *backend_out = (git_refdb_backend *)backend;
1957 return 0;
fe372740
RB
1958
1959fail:
1960 git_buf_free(&path);
1961 git__free(backend->path);
1962 git__free(backend);
1963 return -1;
d00d5464 1964}