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