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