]> git.proxmox.com Git - libgit2.git/blob - src/libgit2/refdb_fs.c
New upstream version 1.5.0+ds
[libgit2.git] / src / libgit2 / 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 "futils.h"
12 #include "filebuf.h"
13 #include "pack.h"
14 #include "parse.h"
15 #include "reflog.h"
16 #include "refdb.h"
17 #include "iterator.h"
18 #include "sortedcache.h"
19 #include "signature.h"
20 #include "wildmatch.h"
21 #include "path.h"
22
23 #include <git2/tag.h>
24 #include <git2/object.h>
25 #include <git2/refdb.h>
26 #include <git2/branch.h>
27 #include <git2/sys/refdb_backend.h>
28 #include <git2/sys/refs.h>
29 #include <git2/sys/reflog.h>
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 /* path to git directory */
59 char *gitpath;
60 /* path to common objects' directory */
61 char *commonpath;
62
63 git_sortedcache *refcache;
64 int peeling_mode;
65 git_iterator_flag_t iterator_flags;
66 uint32_t direach_flags;
67 int fsync;
68 git_map packed_refs_map;
69 git_mutex prlock; /* protect packed_refs_map */
70 git_futils_filestamp packed_refs_stamp;
71 bool sorted;
72 } refdb_fs_backend;
73
74 static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name);
75 static char *packed_set_peeling_mode(char *data, size_t data_sz, refdb_fs_backend *backend);
76
77 GIT_INLINE(int) loose_path(
78 git_str *out,
79 const char *base,
80 const char *refname)
81 {
82 if (git_str_joinpath(out, base, refname) < 0)
83 return -1;
84
85 return git_fs_path_validate_str_length_with_suffix(out,
86 CONST_STRLEN(".lock"));
87 }
88
89 GIT_INLINE(int) reflog_path(
90 git_str *out,
91 git_repository *repo,
92 const char *refname)
93 {
94 const char *base;
95 int error;
96
97 base = (strcmp(refname, GIT_HEAD_FILE) == 0) ? repo->gitdir :
98 repo->commondir;
99
100 if ((error = git_str_joinpath(out, base, GIT_REFLOG_DIR)) < 0)
101 return error;
102
103 return loose_path(out, out->ptr, refname);
104 }
105
106 static int packref_cmp(const void *a_, const void *b_)
107 {
108 const struct packref *a = a_, *b = b_;
109 return strcmp(a->name, b->name);
110 }
111
112 static int packed_reload(refdb_fs_backend *backend)
113 {
114 int error;
115 git_str packedrefs = GIT_STR_INIT;
116 char *scan, *eof, *eol;
117
118 if (!backend->gitpath)
119 return 0;
120
121 error = git_sortedcache_lockandload(backend->refcache, &packedrefs);
122
123 /*
124 * If we can't find the packed-refs, clear table and return.
125 * Any other error just gets passed through.
126 * If no error, and file wasn't changed, just return.
127 * Anything else means we need to refresh the packed refs.
128 */
129 if (error <= 0) {
130 if (error == GIT_ENOTFOUND) {
131 GIT_UNUSED(git_sortedcache_clear(backend->refcache, true));
132 git_error_clear();
133 error = 0;
134 }
135 return error;
136 }
137
138 /* At this point, refresh the packed refs from the loaded buffer. */
139
140 GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
141
142 scan = packedrefs.ptr;
143 eof = scan + packedrefs.size;
144
145 scan = packed_set_peeling_mode(scan, packedrefs.size, backend);
146 if (!scan)
147 goto parse_failed;
148
149 while (scan < eof && *scan == '#') {
150 if (!(eol = strchr(scan, '\n')))
151 goto parse_failed;
152 scan = eol + 1;
153 }
154
155 while (scan < eof) {
156 struct packref *ref;
157 git_oid oid;
158
159 /* parse "<OID> <refname>\n" */
160
161 if (git_oid_fromstr(&oid, scan) < 0)
162 goto parse_failed;
163 scan += GIT_OID_HEXSZ;
164
165 if (*scan++ != ' ')
166 goto parse_failed;
167 if (!(eol = strchr(scan, '\n')))
168 goto parse_failed;
169 *eol = '\0';
170 if (eol[-1] == '\r')
171 eol[-1] = '\0';
172
173 if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0)
174 goto parse_failed;
175 scan = eol + 1;
176
177 git_oid_cpy(&ref->oid, &oid);
178
179 /* look for optional "^<OID>\n" */
180
181 if (*scan == '^') {
182 if (git_oid_fromstr(&oid, scan + 1) < 0)
183 goto parse_failed;
184 scan += GIT_OID_HEXSZ + 1;
185
186 if (scan < eof) {
187 if (!(eol = strchr(scan, '\n')))
188 goto parse_failed;
189 scan = eol + 1;
190 }
191
192 git_oid_cpy(&ref->peel, &oid);
193 ref->flags |= PACKREF_HAS_PEEL;
194 }
195 else if (backend->peeling_mode == PEELING_FULL ||
196 (backend->peeling_mode == PEELING_STANDARD &&
197 git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0))
198 ref->flags |= PACKREF_CANNOT_PEEL;
199 }
200
201 git_sortedcache_wunlock(backend->refcache);
202 git_str_dispose(&packedrefs);
203
204 return 0;
205
206 parse_failed:
207 git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
208
209 GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
210 git_sortedcache_wunlock(backend->refcache);
211 git_str_dispose(&packedrefs);
212
213 return -1;
214 }
215
216 static int loose_parse_oid(
217 git_oid *oid, const char *filename, git_str *file_content)
218 {
219 const char *str = git_str_cstr(file_content);
220
221 if (git_str_len(file_content) < GIT_OID_HEXSZ)
222 goto corrupted;
223
224 /* we need to get 40 OID characters from the file */
225 if (git_oid_fromstr(oid, str) < 0)
226 goto corrupted;
227
228 /* If the file is longer than 40 chars, the 41st must be a space */
229 str += GIT_OID_HEXSZ;
230 if (*str == '\0' || git__isspace(*str))
231 return 0;
232
233 corrupted:
234 git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file: %s", filename);
235 return -1;
236 }
237
238 static int loose_readbuffer(git_str *buf, const char *base, const char *path)
239 {
240 int error;
241
242 if ((error = loose_path(buf, base, path)) < 0 ||
243 (error = git_futils_readbuffer(buf, buf->ptr)) < 0)
244 git_str_dispose(buf);
245
246 return error;
247 }
248
249 static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name)
250 {
251 int error = 0;
252 git_str ref_file = GIT_STR_INIT;
253 struct packref *ref = NULL;
254 git_oid oid;
255
256 /* if we fail to load the loose reference, assume someone changed
257 * the filesystem under us and skip it...
258 */
259 if (loose_readbuffer(&ref_file, backend->gitpath, name) < 0) {
260 git_error_clear();
261 goto done;
262 }
263
264 /* skip symbolic refs */
265 if (!git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF))
266 goto done;
267
268 /* parse OID from file */
269 if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0)
270 goto done;
271
272 if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
273 goto done;
274
275 if (!(error = git_sortedcache_upsert(
276 (void **)&ref, backend->refcache, name))) {
277
278 git_oid_cpy(&ref->oid, &oid);
279 ref->flags = PACKREF_WAS_LOOSE;
280 }
281
282 git_sortedcache_wunlock(backend->refcache);
283
284 done:
285 git_str_dispose(&ref_file);
286 return error;
287 }
288
289 static int _dirent_loose_load(void *payload, git_str *full_path)
290 {
291 refdb_fs_backend *backend = payload;
292 const char *file_path;
293
294 if (git__suffixcmp(full_path->ptr, ".lock") == 0)
295 return 0;
296
297 if (git_fs_path_isdir(full_path->ptr)) {
298 int error = git_fs_path_direach(
299 full_path, backend->direach_flags, _dirent_loose_load, backend);
300 /* Race with the filesystem, ignore it */
301 if (error == GIT_ENOTFOUND) {
302 git_error_clear();
303 return 0;
304 }
305
306 return error;
307 }
308
309 file_path = full_path->ptr + strlen(backend->gitpath);
310
311 return loose_lookup_to_packfile(backend, file_path);
312 }
313
314 /*
315 * Load all the loose references from the repository
316 * into the in-memory Packfile, and build a vector with
317 * all the references so it can be written back to
318 * disk.
319 */
320 static int packed_loadloose(refdb_fs_backend *backend)
321 {
322 int error;
323 git_str refs_path = GIT_STR_INIT;
324
325 if (git_str_joinpath(&refs_path, backend->gitpath, GIT_REFS_DIR) < 0)
326 return -1;
327
328 /*
329 * Load all the loose files from disk into the Packfile table.
330 * This will overwrite any old packed entries with their
331 * updated loose versions
332 */
333 error = git_fs_path_direach(
334 &refs_path, backend->direach_flags, _dirent_loose_load, backend);
335
336 git_str_dispose(&refs_path);
337
338 return error;
339 }
340
341 static int refdb_fs_backend__exists(
342 int *exists,
343 git_refdb_backend *_backend,
344 const char *ref_name)
345 {
346 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
347 git_str ref_path = GIT_STR_INIT;
348 int error;
349
350 GIT_ASSERT_ARG(backend);
351
352 *exists = 0;
353
354 if ((error = loose_path(&ref_path, backend->gitpath, ref_name)) < 0)
355 goto out;
356
357 if (git_fs_path_isfile(ref_path.ptr)) {
358 *exists = 1;
359 goto out;
360 }
361
362 if ((error = packed_reload(backend)) < 0)
363 goto out;
364
365 if (git_sortedcache_lookup(backend->refcache, ref_name) != NULL) {
366 *exists = 1;
367 goto out;
368 }
369
370 out:
371 git_str_dispose(&ref_path);
372 return error;
373 }
374
375 static const char *loose_parse_symbolic(git_str *file_content)
376 {
377 const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF);
378 const char *refname_start;
379
380 refname_start = (const char *)file_content->ptr;
381
382 if (git_str_len(file_content) < header_len + 1) {
383 git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file");
384 return NULL;
385 }
386
387 /*
388 * Assume we have already checked for the header
389 * before calling this function
390 */
391 refname_start += header_len;
392
393 return refname_start;
394 }
395
396 /*
397 * Returns whether a reference is stored per worktree or not.
398 * Per-worktree references are:
399 *
400 * - all pseudorefs, e.g. HEAD and MERGE_HEAD
401 * - all references stored inside of "refs/bisect/"
402 */
403 static bool is_per_worktree_ref(const char *ref_name)
404 {
405 return git__prefixcmp(ref_name, "refs/") != 0 ||
406 git__prefixcmp(ref_name, "refs/bisect/") == 0;
407 }
408
409 static int loose_lookup(
410 git_reference **out,
411 refdb_fs_backend *backend,
412 const char *ref_name)
413 {
414 git_str ref_file = GIT_STR_INIT;
415 int error = 0;
416 const char *ref_dir;
417
418 if (out)
419 *out = NULL;
420
421 if (is_per_worktree_ref(ref_name))
422 ref_dir = backend->gitpath;
423 else
424 ref_dir = backend->commonpath;
425
426 if ((error = loose_readbuffer(&ref_file, ref_dir, ref_name)) < 0)
427 /* cannot read loose ref file - gah */;
428 else if (git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF) == 0) {
429 const char *target;
430
431 git_str_rtrim(&ref_file);
432
433 if (!(target = loose_parse_symbolic(&ref_file)))
434 error = -1;
435 else if (out != NULL)
436 *out = git_reference__alloc_symbolic(ref_name, target);
437 } else {
438 git_oid oid;
439
440 if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) &&
441 out != NULL)
442 *out = git_reference__alloc(ref_name, &oid, NULL);
443 }
444
445 git_str_dispose(&ref_file);
446 return error;
447 }
448
449 static int ref_error_notfound(const char *name)
450 {
451 git_error_set(GIT_ERROR_REFERENCE, "reference '%s' not found", name);
452 return GIT_ENOTFOUND;
453 }
454
455 static char *packed_set_peeling_mode(
456 char *data,
457 size_t data_sz,
458 refdb_fs_backend *backend)
459 {
460 static const char *traits_header = "# pack-refs with:";
461 char *eol;
462 backend->peeling_mode = PEELING_NONE;
463
464 if (git__prefixncmp(data, data_sz, traits_header) == 0) {
465 size_t hdr_sz = strlen(traits_header);
466 const char *sorted = " sorted ";
467 const char *peeled = " peeled ";
468 const char *fully_peeled = " fully-peeled ";
469 data += hdr_sz;
470 data_sz -= hdr_sz;
471
472 eol = memchr(data, '\n', data_sz);
473
474 if (!eol)
475 return NULL;
476
477 if (git__memmem(data, eol - data, fully_peeled, strlen(fully_peeled)))
478 backend->peeling_mode = PEELING_FULL;
479 else if (git__memmem(data, eol - data, peeled, strlen(peeled)))
480 backend->peeling_mode = PEELING_STANDARD;
481
482 backend->sorted = NULL != git__memmem(data, eol - data, sorted, strlen(sorted));
483
484 return eol + 1;
485 }
486 return data;
487 }
488
489 static void packed_map_free(refdb_fs_backend *backend)
490 {
491 if (backend->packed_refs_map.data) {
492 #ifdef GIT_WIN32
493 git__free(backend->packed_refs_map.data);
494 #else
495 git_futils_mmap_free(&backend->packed_refs_map);
496 #endif
497 backend->packed_refs_map.data = NULL;
498 backend->packed_refs_map.len = 0;
499 git_futils_filestamp_set(&backend->packed_refs_stamp, NULL);
500 }
501 }
502
503 static int packed_map_check(refdb_fs_backend *backend)
504 {
505 int error = 0;
506 git_file fd = -1;
507 struct stat st;
508
509 if ((error = git_mutex_lock(&backend->prlock)) < 0)
510 return error;
511
512 if (backend->packed_refs_map.data &&
513 !git_futils_filestamp_check(
514 &backend->packed_refs_stamp, backend->refcache->path)) {
515 git_mutex_unlock(&backend->prlock);
516 return error;
517 }
518 packed_map_free(backend);
519
520 fd = git_futils_open_ro(backend->refcache->path);
521 if (fd < 0) {
522 git_mutex_unlock(&backend->prlock);
523 if (fd == GIT_ENOTFOUND) {
524 git_error_clear();
525 return 0;
526 }
527 return fd;
528 }
529
530 if (p_fstat(fd, &st) < 0) {
531 p_close(fd);
532 git_mutex_unlock(&backend->prlock);
533 git_error_set(GIT_ERROR_OS, "unable to stat packed-refs '%s'", backend->refcache->path);
534 return -1;
535 }
536
537 if (st.st_size == 0) {
538 p_close(fd);
539 git_mutex_unlock(&backend->prlock);
540 return 0;
541 }
542
543 git_futils_filestamp_set_from_stat(&backend->packed_refs_stamp, &st);
544
545 #ifdef GIT_WIN32
546 /* on windows, we copy the entire file into memory rather than using
547 * mmap() because using mmap() on windows also locks the file and this
548 * map is long-lived. */
549 backend->packed_refs_map.len = (size_t)st.st_size;
550 backend->packed_refs_map.data =
551 git__malloc(backend->packed_refs_map.len);
552 GIT_ERROR_CHECK_ALLOC(backend->packed_refs_map.data);
553 {
554 ssize_t bytesread =
555 p_read(fd, backend->packed_refs_map.data,
556 backend->packed_refs_map.len);
557 error = (bytesread == (ssize_t)backend->packed_refs_map.len) ? 0 : -1;
558 }
559 #else
560 error = git_futils_mmap_ro(&backend->packed_refs_map, fd, 0, (size_t)st.st_size);
561 #endif
562 p_close(fd);
563 if (error < 0) {
564 git_mutex_unlock(&backend->prlock);
565 return error;
566 }
567
568 packed_set_peeling_mode(
569 backend->packed_refs_map.data, backend->packed_refs_map.len,
570 backend);
571
572 git_mutex_unlock(&backend->prlock);
573 return error;
574 }
575
576 /*
577 * Find beginning of packed-ref record pointed to by p.
578 * buf - a lower-bound pointer to some memory buffer
579 * p - an upper-bound pointer to the same memory buffer
580 */
581 static const char *start_of_record(const char *buf, const char *p)
582 {
583 const char *nl = p;
584 while (true) {
585 nl = git__memrchr(buf, '\n', nl - buf);
586 if (!nl)
587 return buf;
588
589 if (nl[1] == '^' && nl > buf)
590 --nl;
591 else
592 break;
593 };
594 return nl + 1;
595 }
596
597 /*
598 * Find end of packed-ref record pointed to by p.
599 * end - an upper-bound pointer to some memory buffer
600 * p - a lower-bound pointer to the same memory buffer
601 */
602 static const char *end_of_record(const char *p, const char *end)
603 {
604 while (1) {
605 size_t sz = end - p;
606 p = memchr(p, '\n', sz);
607 if (!p)
608 return end;
609 ++p;
610 if (p < end && p[0] == '^')
611 ++p;
612 else
613 break;
614 }
615 return p;
616 }
617
618 static int
619 cmp_record_to_refname(const char *rec, size_t data_end, const char *ref_name)
620 {
621 const size_t ref_len = strlen(ref_name);
622 int cmp_val;
623 const char *end;
624
625 rec += GIT_OID_HEXSZ + 1; /* <oid> + space */
626 if (data_end < GIT_OID_HEXSZ + 3) {
627 /* an incomplete (corrupt) record is treated as less than ref_name */
628 return -1;
629 }
630 data_end -= GIT_OID_HEXSZ + 1;
631
632 end = memchr(rec, '\n', data_end);
633 if (end)
634 data_end = end - rec;
635
636 cmp_val = memcmp(rec, ref_name, min(ref_len, data_end));
637
638 if (cmp_val == 0 && data_end != ref_len)
639 return (data_end > ref_len) ? 1 : -1;
640 return cmp_val;
641 }
642
643 static int packed_unsorted_lookup(
644 git_reference **out,
645 refdb_fs_backend *backend,
646 const char *ref_name)
647 {
648 int error = 0;
649 struct packref *entry;
650
651 if ((error = packed_reload(backend)) < 0)
652 return error;
653
654 if (git_sortedcache_rlock(backend->refcache) < 0)
655 return -1;
656
657 entry = git_sortedcache_lookup(backend->refcache, ref_name);
658 if (!entry) {
659 error = ref_error_notfound(ref_name);
660 } else {
661 *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel);
662 if (!*out)
663 error = -1;
664 }
665
666 git_sortedcache_runlock(backend->refcache);
667
668 return error;
669 }
670
671 static int packed_lookup(
672 git_reference **out,
673 refdb_fs_backend *backend,
674 const char *ref_name)
675 {
676 int error = 0;
677 const char *left, *right, *data_end;
678
679 if ((error = packed_map_check(backend)) < 0)
680 return error;
681
682 if (!backend->sorted)
683 return packed_unsorted_lookup(out, backend, ref_name);
684
685 left = backend->packed_refs_map.data;
686 right = data_end = (const char *) backend->packed_refs_map.data +
687 backend->packed_refs_map.len;
688
689 while (left < right && *left == '#') {
690 if (!(left = memchr(left, '\n', data_end - left)))
691 goto parse_failed;
692 left++;
693 }
694
695 while (left < right) {
696 const char *mid, *rec;
697 int compare;
698
699 mid = left + (right - left) / 2;
700 rec = start_of_record(left, mid);
701 compare = cmp_record_to_refname(rec, data_end - rec, ref_name);
702
703 if (compare < 0) {
704 left = end_of_record(mid, right);
705 } else if (compare > 0) {
706 right = rec;
707 } else {
708 const char *eol;
709 git_oid oid, peel, *peel_ptr = NULL;
710
711 if (data_end - rec < GIT_OID_HEXSZ ||
712 git_oid_fromstr(&oid, rec) < 0) {
713 goto parse_failed;
714 }
715 rec += GIT_OID_HEXSZ + 1;
716 if (!(eol = memchr(rec, '\n', data_end - rec))) {
717 goto parse_failed;
718 }
719
720 /* look for optional "^<OID>\n" */
721
722 if (eol + 1 < data_end) {
723 rec = eol + 1;
724
725 if (*rec == '^') {
726 rec++;
727 if (data_end - rec < GIT_OID_HEXSZ ||
728 git_oid_fromstr(&peel, rec) < 0) {
729 goto parse_failed;
730 }
731 peel_ptr = &peel;
732 }
733 }
734
735 *out = git_reference__alloc(ref_name, &oid, peel_ptr);
736 if (!*out) {
737 return -1;
738 }
739
740 return 0;
741 }
742 }
743 return ref_error_notfound(ref_name);
744
745 parse_failed:
746 git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
747 return -1;
748 }
749
750 static int refdb_fs_backend__lookup(
751 git_reference **out,
752 git_refdb_backend *_backend,
753 const char *ref_name)
754 {
755 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
756 int error;
757
758 GIT_ASSERT_ARG(backend);
759
760 if (!(error = loose_lookup(out, backend, ref_name)))
761 return 0;
762
763 /* only try to lookup this reference on the packfile if it
764 * wasn't found on the loose refs; not if there was a critical error */
765 if (error == GIT_ENOTFOUND) {
766 git_error_clear();
767 error = packed_lookup(out, backend, ref_name);
768 }
769 return error;
770 }
771
772 typedef struct {
773 git_reference_iterator parent;
774
775 char *glob;
776
777 git_pool pool;
778 git_vector loose;
779
780 git_sortedcache *cache;
781 size_t loose_pos;
782 size_t packed_pos;
783 } refdb_fs_iter;
784
785 static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
786 {
787 refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
788
789 git_vector_free(&iter->loose);
790 git_pool_clear(&iter->pool);
791 git_sortedcache_free(iter->cache);
792 git__free(iter);
793 }
794
795 static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
796 {
797 int error = 0;
798 git_str path = GIT_STR_INIT;
799 git_iterator *fsit = NULL;
800 git_iterator_options fsit_opts = GIT_ITERATOR_OPTIONS_INIT;
801 const git_index_entry *entry = NULL;
802 const char *ref_prefix = GIT_REFS_DIR;
803 size_t ref_prefix_len = strlen(ref_prefix);
804
805 if (!backend->commonpath) /* do nothing if no commonpath for loose refs */
806 return 0;
807
808 fsit_opts.flags = backend->iterator_flags;
809
810 if (iter->glob) {
811 const char *last_sep = NULL;
812 const char *pos;
813 for (pos = iter->glob; *pos; ++pos) {
814 switch (*pos) {
815 case '?':
816 case '*':
817 case '[':
818 case '\\':
819 break;
820 case '/':
821 last_sep = pos;
822 /* FALLTHROUGH */
823 default:
824 continue;
825 }
826 break;
827 }
828 if (last_sep) {
829 ref_prefix = iter->glob;
830 ref_prefix_len = (last_sep - ref_prefix) + 1;
831 }
832 }
833
834 if ((error = git_str_puts(&path, backend->commonpath)) < 0 ||
835 (error = git_str_put(&path, ref_prefix, ref_prefix_len)) < 0) {
836 git_str_dispose(&path);
837 return error;
838 }
839
840 if ((error = git_iterator_for_filesystem(&fsit, path.ptr, &fsit_opts)) < 0) {
841 git_str_dispose(&path);
842 return (iter->glob && error == GIT_ENOTFOUND)? 0 : error;
843 }
844
845 error = git_str_sets(&path, ref_prefix);
846
847 while (!error && !git_iterator_advance(&entry, fsit)) {
848 const char *ref_name;
849 char *ref_dup;
850
851 git_str_truncate(&path, ref_prefix_len);
852 git_str_puts(&path, entry->path);
853 ref_name = git_str_cstr(&path);
854
855 if (git__suffixcmp(ref_name, ".lock") == 0 ||
856 (iter->glob && wildmatch(iter->glob, ref_name, 0) != 0))
857 continue;
858
859 ref_dup = git_pool_strdup(&iter->pool, ref_name);
860 if (!ref_dup)
861 error = -1;
862 else
863 error = git_vector_insert(&iter->loose, ref_dup);
864 }
865
866 git_iterator_free(fsit);
867 git_str_dispose(&path);
868
869 return error;
870 }
871
872 static int refdb_fs_backend__iterator_next(
873 git_reference **out, git_reference_iterator *_iter)
874 {
875 int error = GIT_ITEROVER;
876 refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
877 refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
878 struct packref *ref;
879
880 while (iter->loose_pos < iter->loose.length) {
881 const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
882
883 if (loose_lookup(out, backend, path) == 0) {
884 ref = git_sortedcache_lookup(iter->cache, path);
885 if (ref)
886 ref->flags |= PACKREF_SHADOWED;
887
888 return 0;
889 }
890
891 git_error_clear();
892 }
893
894 error = GIT_ITEROVER;
895 while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
896 ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
897 if (!ref) /* stop now if another thread deleted refs and we past end */
898 break;
899
900 if (ref->flags & PACKREF_SHADOWED)
901 continue;
902 if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
903 continue;
904
905 *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
906 error = (*out != NULL) ? 0 : -1;
907 break;
908 }
909
910 return error;
911 }
912
913 static int refdb_fs_backend__iterator_next_name(
914 const char **out, git_reference_iterator *_iter)
915 {
916 int error = GIT_ITEROVER;
917 refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
918 refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
919 struct packref *ref;
920
921 while (iter->loose_pos < iter->loose.length) {
922 const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
923 struct packref *ref;
924
925 if (loose_lookup(NULL, backend, path) == 0) {
926 ref = git_sortedcache_lookup(iter->cache, path);
927 if (ref)
928 ref->flags |= PACKREF_SHADOWED;
929
930 *out = path;
931 return 0;
932 }
933
934 git_error_clear();
935 }
936
937 error = GIT_ITEROVER;
938 while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
939 ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
940 if (!ref) /* stop now if another thread deleted refs and we past end */
941 break;
942
943 if (ref->flags & PACKREF_SHADOWED)
944 continue;
945 if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
946 continue;
947
948 *out = ref->name;
949 error = 0;
950 break;
951 }
952
953 return error;
954 }
955
956 static int refdb_fs_backend__iterator(
957 git_reference_iterator **out, git_refdb_backend *_backend, const char *glob)
958 {
959 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
960 refdb_fs_iter *iter = NULL;
961 int error;
962
963 GIT_ASSERT_ARG(backend);
964
965 iter = git__calloc(1, sizeof(refdb_fs_iter));
966 GIT_ERROR_CHECK_ALLOC(iter);
967
968 if ((error = git_pool_init(&iter->pool, 1)) < 0)
969 goto out;
970
971 if ((error = git_vector_init(&iter->loose, 8, NULL)) < 0)
972 goto out;
973
974 if (glob != NULL &&
975 (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) {
976 error = GIT_ERROR_NOMEMORY;
977 goto out;
978 }
979
980 if ((error = iter_load_loose_paths(backend, iter)) < 0)
981 goto out;
982
983 if ((error = packed_reload(backend)) < 0)
984 goto out;
985
986 if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
987 goto out;
988
989 iter->parent.next = refdb_fs_backend__iterator_next;
990 iter->parent.next_name = refdb_fs_backend__iterator_next_name;
991 iter->parent.free = refdb_fs_backend__iterator_free;
992
993 *out = (git_reference_iterator *)iter;
994 out:
995 if (error)
996 refdb_fs_backend__iterator_free((git_reference_iterator *)iter);
997 return error;
998 }
999
1000 static bool ref_is_available(
1001 const char *old_ref, const char *new_ref, const char *this_ref)
1002 {
1003 if (old_ref == NULL || strcmp(old_ref, this_ref)) {
1004 size_t reflen = strlen(this_ref);
1005 size_t newlen = strlen(new_ref);
1006 size_t cmplen = reflen < newlen ? reflen : newlen;
1007 const char *lead = reflen < newlen ? new_ref : this_ref;
1008
1009 if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') {
1010 return false;
1011 }
1012 }
1013
1014 return true;
1015 }
1016
1017 static int reference_path_available(
1018 refdb_fs_backend *backend,
1019 const char *new_ref,
1020 const char *old_ref,
1021 int force)
1022 {
1023 size_t i;
1024 int error;
1025
1026 if ((error = packed_reload(backend)) < 0)
1027 return error;
1028
1029 if (!force) {
1030 int exists;
1031
1032 if ((error = refdb_fs_backend__exists(
1033 &exists, (git_refdb_backend *)backend, new_ref)) < 0) {
1034 return error;
1035 }
1036
1037 if (exists) {
1038 git_error_set(GIT_ERROR_REFERENCE,
1039 "failed to write reference '%s': a reference with "
1040 "that name already exists.", new_ref);
1041 return GIT_EEXISTS;
1042 }
1043 }
1044
1045 if ((error = git_sortedcache_rlock(backend->refcache)) < 0)
1046 return error;
1047
1048 for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
1049 struct packref *ref = git_sortedcache_entry(backend->refcache, i);
1050
1051 if (ref && !ref_is_available(old_ref, new_ref, ref->name)) {
1052 git_sortedcache_runlock(backend->refcache);
1053 git_error_set(GIT_ERROR_REFERENCE,
1054 "path to reference '%s' collides with existing one", new_ref);
1055 return -1;
1056 }
1057 }
1058
1059 git_sortedcache_runlock(backend->refcache);
1060 return 0;
1061 }
1062
1063 static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
1064 {
1065 int error, filebuf_flags;
1066 git_str ref_path = GIT_STR_INIT;
1067 const char *basedir;
1068
1069 GIT_ASSERT_ARG(file);
1070 GIT_ASSERT_ARG(backend);
1071 GIT_ASSERT_ARG(name);
1072
1073 if (!git_path_is_valid(backend->repo, name, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
1074 git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", name);
1075 return GIT_EINVALIDSPEC;
1076 }
1077
1078 if (is_per_worktree_ref(name))
1079 basedir = backend->gitpath;
1080 else
1081 basedir = backend->commonpath;
1082
1083 /* Remove a possibly existing empty directory hierarchy
1084 * which name would collide with the reference name
1085 */
1086 if ((error = git_futils_rmdir_r(name, basedir, GIT_RMDIR_SKIP_NONEMPTY)) < 0)
1087 return error;
1088
1089 if ((error = loose_path(&ref_path, basedir, name)) < 0)
1090 return error;
1091
1092 filebuf_flags = GIT_FILEBUF_CREATE_LEADING_DIRS;
1093 if (backend->fsync)
1094 filebuf_flags |= GIT_FILEBUF_FSYNC;
1095
1096 error = git_filebuf_open(file, ref_path.ptr, filebuf_flags, GIT_REFS_FILE_MODE);
1097
1098 if (error == GIT_EDIRECTORY)
1099 git_error_set(GIT_ERROR_REFERENCE, "cannot lock ref '%s', there are refs beneath that folder", name);
1100
1101 git_str_dispose(&ref_path);
1102 return error;
1103 }
1104
1105 static int loose_commit(git_filebuf *file, const git_reference *ref)
1106 {
1107 GIT_ASSERT_ARG(file);
1108 GIT_ASSERT_ARG(ref);
1109
1110 if (ref->type == GIT_REFERENCE_DIRECT) {
1111 char oid[GIT_OID_HEXSZ + 1];
1112 git_oid_nfmt(oid, sizeof(oid), &ref->target.oid);
1113
1114 git_filebuf_printf(file, "%s\n", oid);
1115 } else if (ref->type == GIT_REFERENCE_SYMBOLIC) {
1116 git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic);
1117 } else {
1118 GIT_ASSERT(0);
1119 }
1120
1121 return git_filebuf_commit(file);
1122 }
1123
1124 static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const char *refname)
1125 {
1126 int error;
1127 git_filebuf *lock;
1128 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1129
1130 lock = git__calloc(1, sizeof(git_filebuf));
1131 GIT_ERROR_CHECK_ALLOC(lock);
1132
1133 if ((error = loose_lock(lock, backend, refname)) < 0) {
1134 git__free(lock);
1135 return error;
1136 }
1137
1138 *out = lock;
1139 return 0;
1140 }
1141
1142 static int refdb_fs_backend__write_tail(
1143 git_refdb_backend *_backend,
1144 const git_reference *ref,
1145 git_filebuf *file,
1146 int update_reflog,
1147 const git_oid *old_id,
1148 const char *old_target,
1149 const git_signature *who,
1150 const char *message);
1151
1152 static int refdb_fs_backend__delete_tail(
1153 git_refdb_backend *_backend,
1154 git_filebuf *file,
1155 const char *ref_name,
1156 const git_oid *old_id,
1157 const char *old_target);
1158
1159 static int refdb_fs_backend__unlock(git_refdb_backend *backend, void *payload, int success, int update_reflog,
1160 const git_reference *ref, const git_signature *sig, const char *message)
1161 {
1162 git_filebuf *lock = (git_filebuf *) payload;
1163 int error = 0;
1164
1165 if (success == 2)
1166 error = refdb_fs_backend__delete_tail(backend, lock, ref->name, NULL, NULL);
1167 else if (success)
1168 error = refdb_fs_backend__write_tail(backend, ref, lock, update_reflog, NULL, NULL, sig, message);
1169 else
1170 git_filebuf_cleanup(lock);
1171
1172 git__free(lock);
1173 return error;
1174 }
1175
1176 /*
1177 * Find out what object this reference resolves to.
1178 *
1179 * For references that point to a 'big' tag (e.g. an
1180 * actual tag object on the repository), we need to
1181 * cache on the packfile the OID of the object to
1182 * which that 'big tag' is pointing to.
1183 */
1184 static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
1185 {
1186 git_object *object;
1187
1188 if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL)
1189 return 0;
1190
1191 /*
1192 * Find the tagged object in the repository
1193 */
1194 if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJECT_ANY) < 0)
1195 return -1;
1196
1197 /*
1198 * If the tagged object is a Tag object, we need to resolve it;
1199 * if the ref is actually a 'weak' ref, we don't need to resolve
1200 * anything.
1201 */
1202 if (git_object_type(object) == GIT_OBJECT_TAG) {
1203 git_tag *tag = (git_tag *)object;
1204
1205 /*
1206 * Find the object pointed at by this tag
1207 */
1208 git_oid_cpy(&ref->peel, git_tag_target_id(tag));
1209 ref->flags |= PACKREF_HAS_PEEL;
1210
1211 /*
1212 * The reference has now cached the resolved OID, and is
1213 * marked at such. When written to the packfile, it'll be
1214 * accompanied by this resolved oid
1215 */
1216 }
1217
1218 git_object_free(object);
1219 return 0;
1220 }
1221
1222 /*
1223 * Write a single reference into a packfile
1224 */
1225 static int packed_write_ref(struct packref *ref, git_filebuf *file)
1226 {
1227 char oid[GIT_OID_HEXSZ + 1];
1228 git_oid_nfmt(oid, sizeof(oid), &ref->oid);
1229
1230 /*
1231 * For references that peel to an object in the repo, we must
1232 * write the resulting peel on a separate line, e.g.
1233 *
1234 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
1235 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
1236 *
1237 * This obviously only applies to tags.
1238 * The required peels have already been loaded into `ref->peel_target`.
1239 */
1240 if (ref->flags & PACKREF_HAS_PEEL) {
1241 char peel[GIT_OID_HEXSZ + 1];
1242 git_oid_nfmt(peel, sizeof(peel), &ref->peel);
1243
1244 if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0)
1245 return -1;
1246 } else {
1247 if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0)
1248 return -1;
1249 }
1250
1251 return 0;
1252 }
1253
1254 /*
1255 * Remove all loose references
1256 *
1257 * Once we have successfully written a packfile,
1258 * all the loose references that were packed must be
1259 * removed from disk.
1260 *
1261 * This is a dangerous method; make sure the packfile
1262 * is well-written, because we are destructing references
1263 * here otherwise.
1264 */
1265 static int packed_remove_loose(refdb_fs_backend *backend)
1266 {
1267 size_t i;
1268 git_filebuf lock = GIT_FILEBUF_INIT;
1269 git_str ref_content = GIT_STR_INIT;
1270 int error = 0;
1271
1272 /* backend->refcache is already locked when this is called */
1273
1274 for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
1275 struct packref *ref = git_sortedcache_entry(backend->refcache, i);
1276 git_oid current_id;
1277
1278 if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
1279 continue;
1280
1281 git_filebuf_cleanup(&lock);
1282
1283 /* We need to stop anybody from updating the ref while we try to do a safe delete */
1284 error = loose_lock(&lock, backend, ref->name);
1285 /* If someone else is updating it, let them do it */
1286 if (error == GIT_EEXISTS || error == GIT_ENOTFOUND)
1287 continue;
1288
1289 if (error < 0) {
1290 git_str_dispose(&ref_content);
1291 git_error_set(GIT_ERROR_REFERENCE, "failed to lock loose reference '%s'", ref->name);
1292 return error;
1293 }
1294
1295 error = git_futils_readbuffer(&ref_content, lock.path_original);
1296 /* Someone else beat us to cleaning up the ref, let's simply continue */
1297 if (error == GIT_ENOTFOUND)
1298 continue;
1299
1300 /* This became a symref between us packing and trying to delete it, so ignore it */
1301 if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF))
1302 continue;
1303
1304 /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */
1305 if (loose_parse_oid(&current_id, lock.path_original, &ref_content) < 0)
1306 continue;
1307
1308 /* If the ref moved since we packed it, we must not delete it */
1309 if (!git_oid_equal(&current_id, &ref->oid))
1310 continue;
1311
1312 /*
1313 * if we fail to remove a single file, this is *not* good,
1314 * but we should keep going and remove as many as possible.
1315 * If we fail to remove, the ref is still in the old state, so
1316 * we haven't lost information.
1317 */
1318 p_unlink(lock.path_original);
1319 }
1320
1321 git_str_dispose(&ref_content);
1322 git_filebuf_cleanup(&lock);
1323 return 0;
1324 }
1325
1326 /*
1327 * Write all the contents in the in-memory packfile to disk.
1328 */
1329 static int packed_write(refdb_fs_backend *backend)
1330 {
1331 git_sortedcache *refcache = backend->refcache;
1332 git_filebuf pack_file = GIT_FILEBUF_INIT;
1333 int error, open_flags = 0;
1334 size_t i;
1335
1336 /* take lock and close up packed-refs mmap if open */
1337 if ((error = git_mutex_lock(&backend->prlock)) < 0) {
1338 return error;
1339 }
1340
1341 packed_map_free(backend);
1342
1343 git_mutex_unlock(&backend->prlock);
1344
1345 /* lock the cache to updates while we do this */
1346 if ((error = git_sortedcache_wlock(refcache)) < 0)
1347 return error;
1348
1349 if (backend->fsync)
1350 open_flags = GIT_FILEBUF_FSYNC;
1351
1352 /* Open the file! */
1353 if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), open_flags, GIT_PACKEDREFS_FILE_MODE)) < 0)
1354 goto fail;
1355
1356 /* Packfiles have a header... apparently
1357 * This is in fact not required, but we might as well print it
1358 * just for kicks */
1359 if ((error = git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER)) < 0)
1360 goto fail;
1361
1362 for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
1363 struct packref *ref = git_sortedcache_entry(refcache, i);
1364
1365 GIT_ASSERT_WITH_CLEANUP(ref, {
1366 error = -1;
1367 goto fail;
1368 });
1369
1370 if ((error = packed_find_peel(backend, ref)) < 0)
1371 goto fail;
1372
1373 if ((error = packed_write_ref(ref, &pack_file)) < 0)
1374 goto fail;
1375 }
1376
1377 /* if we've written all the references properly, we can commit
1378 * the packfile to make the changes effective */
1379 if ((error = git_filebuf_commit(&pack_file)) < 0)
1380 goto fail;
1381
1382 /* when and only when the packfile has been properly written,
1383 * we can go ahead and remove the loose refs */
1384 if ((error = packed_remove_loose(backend)) < 0)
1385 goto fail;
1386
1387 git_sortedcache_updated(refcache);
1388 git_sortedcache_wunlock(refcache);
1389
1390 /* we're good now */
1391 return 0;
1392
1393 fail:
1394 git_filebuf_cleanup(&pack_file);
1395 git_sortedcache_wunlock(refcache);
1396
1397 return error;
1398 }
1399
1400 static int packed_delete(refdb_fs_backend *backend, const char *ref_name)
1401 {
1402 size_t pack_pos;
1403 int error, found = 0;
1404
1405 if ((error = packed_reload(backend)) < 0)
1406 goto cleanup;
1407
1408 if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
1409 goto cleanup;
1410
1411 /* If a packed reference exists, remove it from the packfile and repack if necessary */
1412 error = git_sortedcache_lookup_index(&pack_pos, backend->refcache, ref_name);
1413 if (error == 0) {
1414 error = git_sortedcache_remove(backend->refcache, pack_pos);
1415 found = 1;
1416 }
1417 if (error == GIT_ENOTFOUND)
1418 error = 0;
1419
1420 git_sortedcache_wunlock(backend->refcache);
1421
1422 if (found)
1423 error = packed_write(backend);
1424
1425 cleanup:
1426 return error;
1427 }
1428
1429 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);
1430
1431 static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name,
1432 const git_oid *old_id, const char *old_target)
1433 {
1434 int error = 0;
1435 git_reference *old_ref = NULL;
1436
1437 *cmp = 0;
1438 /* It "matches" if there is no old value to compare against */
1439 if (!old_id && !old_target)
1440 return 0;
1441
1442 if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0) {
1443 if (error == GIT_ENOTFOUND && old_id && git_oid_is_zero(old_id))
1444 return 0;
1445 goto out;
1446 }
1447
1448 /* If the types don't match, there's no way the values do */
1449 if (old_id && old_ref->type != GIT_REFERENCE_DIRECT) {
1450 *cmp = -1;
1451 goto out;
1452 }
1453 if (old_target && old_ref->type != GIT_REFERENCE_SYMBOLIC) {
1454 *cmp = 1;
1455 goto out;
1456 }
1457
1458 if (old_id && old_ref->type == GIT_REFERENCE_DIRECT)
1459 *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
1460
1461 if (old_target && old_ref->type == GIT_REFERENCE_SYMBOLIC)
1462 *cmp = git__strcmp(old_target, old_ref->target.symbolic);
1463
1464 out:
1465 git_reference_free(old_ref);
1466
1467 return error;
1468 }
1469
1470 /*
1471 * The git.git comment regarding this, for your viewing pleasure:
1472 *
1473 * Special hack: If a branch is updated directly and HEAD
1474 * points to it (may happen on the remote side of a push
1475 * for example) then logically the HEAD reflog should be
1476 * updated too.
1477 * A generic solution implies reverse symref information,
1478 * but finding all symrefs pointing to the given branch
1479 * would be rather costly for this rare event (the direct
1480 * update of a branch) to be worth it. So let's cheat and
1481 * check with HEAD only which should cover 99% of all usage
1482 * scenarios (even 100% of the default ones).
1483 */
1484 static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
1485 {
1486 git_reference *head = NULL;
1487 git_refdb *refdb = NULL;
1488 int error, write_reflog;
1489 git_oid old_id;
1490
1491 if ((error = git_repository_refdb(&refdb, backend->repo)) < 0 ||
1492 (error = git_refdb_should_write_head_reflog(&write_reflog, refdb, ref)) < 0)
1493 goto out;
1494 if (!write_reflog)
1495 goto out;
1496
1497 /* if we can't resolve, we use {0}*40 as old id */
1498 if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0)
1499 memset(&old_id, 0, sizeof(old_id));
1500
1501 if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0 ||
1502 (error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message)) < 0)
1503 goto out;
1504
1505 out:
1506 git_reference_free(head);
1507 git_refdb_free(refdb);
1508 return error;
1509 }
1510
1511 static int refdb_fs_backend__write(
1512 git_refdb_backend *_backend,
1513 const git_reference *ref,
1514 int force,
1515 const git_signature *who,
1516 const char *message,
1517 const git_oid *old_id,
1518 const char *old_target)
1519 {
1520 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1521 git_filebuf file = GIT_FILEBUF_INIT;
1522 int error = 0;
1523
1524 GIT_ASSERT_ARG(backend);
1525
1526 if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0)
1527 return error;
1528
1529 /* We need to perform the reflog append and old value check under the ref's lock */
1530 if ((error = loose_lock(&file, backend, ref->name)) < 0)
1531 return error;
1532
1533 return refdb_fs_backend__write_tail(_backend, ref, &file, true, old_id, old_target, who, message);
1534 }
1535
1536 static int refdb_fs_backend__write_tail(
1537 git_refdb_backend *_backend,
1538 const git_reference *ref,
1539 git_filebuf *file,
1540 int update_reflog,
1541 const git_oid *old_id,
1542 const char *old_target,
1543 const git_signature *who,
1544 const char *message)
1545 {
1546 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1547 int error = 0, cmp = 0, should_write;
1548 const char *new_target = NULL;
1549 const git_oid *new_id = NULL;
1550
1551 if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0)
1552 goto on_error;
1553
1554 if (cmp) {
1555 git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match");
1556 error = GIT_EMODIFIED;
1557 goto on_error;
1558 }
1559
1560 if (ref->type == GIT_REFERENCE_SYMBOLIC)
1561 new_target = ref->target.symbolic;
1562 else
1563 new_id = &ref->target.oid;
1564
1565 error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target);
1566 if (error < 0 && error != GIT_ENOTFOUND)
1567 goto on_error;
1568
1569 /* Don't update if we have the same value */
1570 if (!error && !cmp) {
1571 error = 0;
1572 goto on_error; /* not really error */
1573 }
1574
1575 if (update_reflog) {
1576 git_refdb *refdb;
1577
1578 if ((error = git_repository_refdb__weakptr(&refdb, backend->repo)) < 0 ||
1579 (error = git_refdb_should_write_reflog(&should_write, refdb, ref)) < 0)
1580 goto on_error;
1581
1582 if (should_write) {
1583 if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
1584 goto on_error;
1585 if ((error = maybe_append_head(backend, ref, who, message)) < 0)
1586 goto on_error;
1587 }
1588 }
1589
1590 return loose_commit(file, ref);
1591
1592 on_error:
1593 git_filebuf_cleanup(file);
1594 return error;
1595 }
1596
1597 static int refdb_fs_backend__prune_refs(
1598 refdb_fs_backend *backend,
1599 const char *ref_name,
1600 const char *prefix)
1601 {
1602 git_str relative_path = GIT_STR_INIT;
1603 git_str base_path = GIT_STR_INIT;
1604 size_t commonlen;
1605 int error;
1606
1607 GIT_ASSERT_ARG(backend);
1608 GIT_ASSERT_ARG(ref_name);
1609
1610 if ((error = git_str_sets(&relative_path, ref_name)) < 0)
1611 goto cleanup;
1612
1613 git_fs_path_squash_slashes(&relative_path);
1614 if ((commonlen = git_fs_path_common_dirlen("refs/heads/", git_str_cstr(&relative_path))) == strlen("refs/heads/") ||
1615 (commonlen = git_fs_path_common_dirlen("refs/tags/", git_str_cstr(&relative_path))) == strlen("refs/tags/") ||
1616 (commonlen = git_fs_path_common_dirlen("refs/remotes/", git_str_cstr(&relative_path))) == strlen("refs/remotes/")) {
1617
1618 git_str_truncate(&relative_path, commonlen);
1619
1620 if (prefix)
1621 error = git_str_join3(&base_path, '/',
1622 backend->commonpath, prefix,
1623 git_str_cstr(&relative_path));
1624 else
1625 error = git_str_joinpath(&base_path,
1626 backend->commonpath,
1627 git_str_cstr(&relative_path));
1628
1629 if (!error)
1630 error = git_path_validate_str_length(NULL, &base_path);
1631
1632 if (error < 0)
1633 goto cleanup;
1634
1635 error = git_futils_rmdir_r(ref_name + commonlen,
1636 git_str_cstr(&base_path),
1637 GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT);
1638
1639 if (error == GIT_ENOTFOUND)
1640 error = 0;
1641 }
1642
1643 cleanup:
1644 git_str_dispose(&relative_path);
1645 git_str_dispose(&base_path);
1646 return error;
1647 }
1648
1649 static int refdb_fs_backend__delete(
1650 git_refdb_backend *_backend,
1651 const char *ref_name,
1652 const git_oid *old_id, const char *old_target)
1653 {
1654 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1655 git_filebuf file = GIT_FILEBUF_INIT;
1656 int error = 0;
1657
1658 GIT_ASSERT_ARG(backend);
1659 GIT_ASSERT_ARG(ref_name);
1660
1661 if ((error = loose_lock(&file, backend, ref_name)) < 0)
1662 return error;
1663
1664 if ((error = refdb_reflog_fs__delete(_backend, ref_name)) < 0) {
1665 git_filebuf_cleanup(&file);
1666 return error;
1667 }
1668
1669 return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target);
1670 }
1671
1672 static int loose_delete(refdb_fs_backend *backend, const char *ref_name)
1673 {
1674 git_str path = GIT_STR_INIT;
1675 int error = 0;
1676
1677 if ((error = loose_path(&path, backend->commonpath, ref_name)) < 0)
1678 return error;
1679
1680 error = p_unlink(path.ptr);
1681 if (error < 0 && errno == ENOENT)
1682 error = GIT_ENOTFOUND;
1683 else if (error != 0)
1684 error = -1;
1685
1686 git_str_dispose(&path);
1687
1688 return error;
1689 }
1690
1691 static int refdb_fs_backend__delete_tail(
1692 git_refdb_backend *_backend,
1693 git_filebuf *file,
1694 const char *ref_name,
1695 const git_oid *old_id, const char *old_target)
1696 {
1697 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1698 int error = 0, cmp = 0;
1699 bool packed_deleted = 0;
1700
1701 error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target);
1702 if (error < 0)
1703 goto cleanup;
1704
1705 if (cmp) {
1706 git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match");
1707 error = GIT_EMODIFIED;
1708 goto cleanup;
1709 }
1710
1711 /*
1712 * To ensure that an external observer will see either the current ref value
1713 * (because the loose ref still exists), or a missing ref (after the packed-file is
1714 * unlocked, there will be nothing left), we must ensure things happen in the
1715 * following order:
1716 *
1717 * - the packed-ref file is locked and loaded, as well as a loose one, if it exists
1718 * - we optimistically delete a packed ref, keeping track of whether it existed
1719 * - we delete the loose ref, note that we have its .lock
1720 * - the loose ref is "unlocked", then the packed-ref file is rewritten and unlocked
1721 * - we should prune the path components if a loose ref was deleted
1722 *
1723 * Note that, because our packed backend doesn't expose its filesystem lock,
1724 * we might not be able to guarantee that this is what actually happens (ie.
1725 * as our current code never write packed-refs.lock, nothing stops observers
1726 * from grabbing a "stale" value from there).
1727 */
1728 if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
1729 goto cleanup;
1730
1731 if (error == 0)
1732 packed_deleted = 1;
1733
1734 if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
1735 goto cleanup;
1736
1737 if (error == GIT_ENOTFOUND) {
1738 error = packed_deleted ? 0 : ref_error_notfound(ref_name);
1739 goto cleanup;
1740 }
1741
1742 cleanup:
1743 git_filebuf_cleanup(file);
1744 if (error == 0)
1745 error = refdb_fs_backend__prune_refs(backend, ref_name, "");
1746 return error;
1747 }
1748
1749 static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name);
1750
1751 static int refdb_fs_backend__rename(
1752 git_reference **out,
1753 git_refdb_backend *_backend,
1754 const char *old_name,
1755 const char *new_name,
1756 int force,
1757 const git_signature *who,
1758 const char *message)
1759 {
1760 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1761 git_reference *old, *new = NULL;
1762 git_filebuf file = GIT_FILEBUF_INIT;
1763 int error;
1764
1765 GIT_ASSERT_ARG(backend);
1766
1767 if ((error = reference_path_available(
1768 backend, new_name, old_name, force)) < 0 ||
1769 (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0)
1770 return error;
1771
1772 if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
1773 git_reference_free(old);
1774 return error;
1775 }
1776
1777 new = git_reference__realloc(&old, new_name);
1778 if (!new) {
1779 git_reference_free(old);
1780 return -1;
1781 }
1782
1783 if ((error = loose_lock(&file, backend, new->name)) < 0) {
1784 git_reference_free(new);
1785 return error;
1786 }
1787
1788 /* Try to rename the refog; it's ok if the old doesn't exist */
1789 error = refdb_reflog_fs__rename(_backend, old_name, new_name);
1790 if (((error == 0) || (error == GIT_ENOTFOUND)) &&
1791 ((error = reflog_append(backend, new, git_reference_target(new), NULL, who, message)) < 0)) {
1792 git_reference_free(new);
1793 git_filebuf_cleanup(&file);
1794 return error;
1795 }
1796
1797 if (error < 0) {
1798 git_reference_free(new);
1799 git_filebuf_cleanup(&file);
1800 return error;
1801 }
1802
1803
1804 if ((error = loose_commit(&file, new)) < 0 || out == NULL) {
1805 git_reference_free(new);
1806 return error;
1807 }
1808
1809 *out = new;
1810 return 0;
1811 }
1812
1813 static int refdb_fs_backend__compress(git_refdb_backend *_backend)
1814 {
1815 int error;
1816 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1817
1818 GIT_ASSERT_ARG(backend);
1819
1820 if ((error = packed_reload(backend)) < 0 || /* load the existing packfile */
1821 (error = packed_loadloose(backend)) < 0 || /* add all the loose refs */
1822 (error = packed_write(backend)) < 0) /* write back to disk */
1823 return error;
1824
1825 return 0;
1826 }
1827
1828 static void refdb_fs_backend__free(git_refdb_backend *_backend)
1829 {
1830 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1831
1832 if (!backend)
1833 return;
1834
1835 git_sortedcache_free(backend->refcache);
1836
1837 git_mutex_lock(&backend->prlock);
1838 packed_map_free(backend);
1839 git_mutex_unlock(&backend->prlock);
1840 git_mutex_free(&backend->prlock);
1841
1842 git__free(backend->gitpath);
1843 git__free(backend->commonpath);
1844 git__free(backend);
1845 }
1846
1847 static char *setup_namespace(git_repository *repo, const char *in)
1848 {
1849 git_str path = GIT_STR_INIT;
1850 char *parts, *start, *end, *out = NULL;
1851
1852 if (!in)
1853 goto done;
1854
1855 git_str_puts(&path, in);
1856
1857 /* if the repo is not namespaced, nothing else to do */
1858 if (repo->namespace == NULL) {
1859 out = git_str_detach(&path);
1860 goto done;
1861 }
1862
1863 parts = end = git__strdup(repo->namespace);
1864 if (parts == NULL)
1865 goto done;
1866
1867 /*
1868 * From `man gitnamespaces`:
1869 * namespaces which include a / will expand to a hierarchy
1870 * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
1871 * refs under refs/namespaces/foo/refs/namespaces/bar/
1872 */
1873 while ((start = git__strsep(&end, "/")) != NULL)
1874 git_str_printf(&path, "refs/namespaces/%s/", start);
1875
1876 git_str_printf(&path, "refs/namespaces/%s/refs", end);
1877 git__free(parts);
1878
1879 /* Make sure that the folder with the namespace exists */
1880 if (git_futils_mkdir_relative(git_str_cstr(&path), in, 0777,
1881 GIT_MKDIR_PATH, NULL) < 0)
1882 goto done;
1883
1884 /* Return root of the namespaced gitpath, i.e. without the trailing 'refs' */
1885 git_str_rtruncate_at_char(&path, '/');
1886 git_str_putc(&path, '/');
1887 out = git_str_detach(&path);
1888
1889 done:
1890 git_str_dispose(&path);
1891 return out;
1892 }
1893
1894 static int reflog_alloc(git_reflog **reflog, const char *name)
1895 {
1896 git_reflog *log;
1897
1898 *reflog = NULL;
1899
1900 log = git__calloc(1, sizeof(git_reflog));
1901 GIT_ERROR_CHECK_ALLOC(log);
1902
1903 log->ref_name = git__strdup(name);
1904 GIT_ERROR_CHECK_ALLOC(log->ref_name);
1905
1906 if (git_vector_init(&log->entries, 0, NULL) < 0) {
1907 git__free(log->ref_name);
1908 git__free(log);
1909 return -1;
1910 }
1911
1912 *reflog = log;
1913
1914 return 0;
1915 }
1916
1917 static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
1918 {
1919 git_parse_ctx parser = GIT_PARSE_CTX_INIT;
1920
1921 if ((git_parse_ctx_init(&parser, buf, buf_size)) < 0)
1922 return -1;
1923
1924 for (; parser.remain_len; git_parse_advance_line(&parser)) {
1925 git_reflog_entry *entry;
1926 const char *sig;
1927 char c;
1928
1929 entry = git__calloc(1, sizeof(*entry));
1930 GIT_ERROR_CHECK_ALLOC(entry);
1931 entry->committer = git__calloc(1, sizeof(*entry->committer));
1932 GIT_ERROR_CHECK_ALLOC(entry->committer);
1933
1934 if (git_parse_advance_oid(&entry->oid_old, &parser) < 0 ||
1935 git_parse_advance_expected(&parser, " ", 1) < 0 ||
1936 git_parse_advance_oid(&entry->oid_cur, &parser) < 0)
1937 goto next;
1938
1939 sig = parser.line;
1940 while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n')
1941 git_parse_advance_chars(&parser, 1);
1942
1943 if (git_signature__parse(entry->committer, &sig, parser.line, NULL, 0) < 0)
1944 goto next;
1945
1946 if (c == '\t') {
1947 size_t len;
1948 git_parse_advance_chars(&parser, 1);
1949
1950 len = parser.line_len;
1951 if (parser.line[len - 1] == '\n')
1952 len--;
1953
1954 entry->msg = git__strndup(parser.line, len);
1955 GIT_ERROR_CHECK_ALLOC(entry->msg);
1956 }
1957
1958 if ((git_vector_insert(&log->entries, entry)) < 0) {
1959 git_reflog_entry__free(entry);
1960 return -1;
1961 }
1962
1963 continue;
1964
1965 next:
1966 git_reflog_entry__free(entry);
1967 }
1968
1969 return 0;
1970 }
1971
1972 static int create_new_reflog_file(const char *filepath)
1973 {
1974 int fd, error;
1975
1976 if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
1977 return error;
1978
1979 if ((fd = p_open(filepath,
1980 O_WRONLY | O_CREAT,
1981 GIT_REFLOG_FILE_MODE)) < 0)
1982 return -1;
1983
1984 return p_close(fd);
1985 }
1986
1987 static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name)
1988 {
1989 refdb_fs_backend *backend;
1990 git_repository *repo;
1991 git_str path = GIT_STR_INIT;
1992 int error;
1993
1994 GIT_ASSERT_ARG(_backend && name);
1995
1996 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1997 repo = backend->repo;
1998
1999 if ((error = reflog_path(&path, repo, name)) < 0)
2000 return error;
2001
2002 error = create_new_reflog_file(git_str_cstr(&path));
2003 git_str_dispose(&path);
2004
2005 return error;
2006 }
2007
2008 static int has_reflog(git_repository *repo, const char *name)
2009 {
2010 int ret = 0;
2011 git_str path = GIT_STR_INIT;
2012
2013 if (reflog_path(&path, repo, name) < 0)
2014 goto cleanup;
2015
2016 ret = git_fs_path_isfile(git_str_cstr(&path));
2017
2018 cleanup:
2019 git_str_dispose(&path);
2020 return ret;
2021 }
2022
2023 static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name)
2024 {
2025 refdb_fs_backend *backend;
2026
2027 GIT_ASSERT_ARG(_backend);
2028 GIT_ASSERT_ARG(name);
2029
2030 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2031
2032 return has_reflog(backend->repo, name);
2033 }
2034
2035 static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name)
2036 {
2037 int error = -1;
2038 git_str log_path = GIT_STR_INIT;
2039 git_str log_file = GIT_STR_INIT;
2040 git_reflog *log = NULL;
2041 git_repository *repo;
2042 refdb_fs_backend *backend;
2043
2044 GIT_ASSERT_ARG(out);
2045 GIT_ASSERT_ARG(_backend);
2046 GIT_ASSERT_ARG(name);
2047
2048 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2049 repo = backend->repo;
2050
2051 if (reflog_alloc(&log, name) < 0)
2052 return -1;
2053
2054 if (reflog_path(&log_path, repo, name) < 0)
2055 goto cleanup;
2056
2057 error = git_futils_readbuffer(&log_file, git_str_cstr(&log_path));
2058 if (error < 0 && error != GIT_ENOTFOUND)
2059 goto cleanup;
2060
2061 if ((error == GIT_ENOTFOUND) &&
2062 ((error = create_new_reflog_file(git_str_cstr(&log_path))) < 0))
2063 goto cleanup;
2064
2065 if ((error = reflog_parse(log,
2066 git_str_cstr(&log_file), git_str_len(&log_file))) < 0)
2067 goto cleanup;
2068
2069 *out = log;
2070 goto success;
2071
2072 cleanup:
2073 git_reflog_free(log);
2074
2075 success:
2076 git_str_dispose(&log_file);
2077 git_str_dispose(&log_path);
2078
2079 return error;
2080 }
2081
2082 static int serialize_reflog_entry(
2083 git_str *buf,
2084 const git_oid *oid_old,
2085 const git_oid *oid_new,
2086 const git_signature *committer,
2087 const char *msg)
2088 {
2089 char raw_old[GIT_OID_HEXSZ+1];
2090 char raw_new[GIT_OID_HEXSZ+1];
2091
2092 git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
2093 git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
2094
2095 git_str_clear(buf);
2096
2097 git_str_puts(buf, raw_old);
2098 git_str_putc(buf, ' ');
2099 git_str_puts(buf, raw_new);
2100
2101 git_signature__writebuf(buf, " ", committer);
2102
2103 /* drop trailing LF */
2104 git_str_rtrim(buf);
2105
2106 if (msg) {
2107 size_t i;
2108
2109 git_str_putc(buf, '\t');
2110 git_str_puts(buf, msg);
2111
2112 for (i = 0; i < buf->size - 2; i++)
2113 if (buf->ptr[i] == '\n')
2114 buf->ptr[i] = ' ';
2115 git_str_rtrim(buf);
2116 }
2117
2118 git_str_putc(buf, '\n');
2119
2120 return git_str_oom(buf);
2121 }
2122
2123 static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname)
2124 {
2125 git_repository *repo;
2126 git_str log_path = GIT_STR_INIT;
2127 int error;
2128
2129 repo = backend->repo;
2130
2131 if (!git_path_is_valid(backend->repo, refname, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
2132 git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", refname);
2133 return GIT_EINVALIDSPEC;
2134 }
2135
2136 if (reflog_path(&log_path, repo, refname) < 0)
2137 return -1;
2138
2139 if (!git_fs_path_isfile(git_str_cstr(&log_path))) {
2140 git_error_set(GIT_ERROR_INVALID,
2141 "log file for reference '%s' doesn't exist", refname);
2142 error = -1;
2143 goto cleanup;
2144 }
2145
2146 error = git_filebuf_open(file, git_str_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE);
2147
2148 cleanup:
2149 git_str_dispose(&log_path);
2150
2151 return error;
2152 }
2153
2154 static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog)
2155 {
2156 int error = -1;
2157 unsigned int i;
2158 git_reflog_entry *entry;
2159 refdb_fs_backend *backend;
2160 git_str log = GIT_STR_INIT;
2161 git_filebuf fbuf = GIT_FILEBUF_INIT;
2162
2163 GIT_ASSERT_ARG(_backend);
2164 GIT_ASSERT_ARG(reflog);
2165
2166 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2167
2168 if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0)
2169 return -1;
2170
2171 git_vector_foreach(&reflog->entries, i, entry) {
2172 if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
2173 goto cleanup;
2174
2175 if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
2176 goto cleanup;
2177 }
2178
2179 error = git_filebuf_commit(&fbuf);
2180 goto success;
2181
2182 cleanup:
2183 git_filebuf_cleanup(&fbuf);
2184
2185 success:
2186 git_str_dispose(&log);
2187
2188 return error;
2189 }
2190
2191 /* Append to the reflog, must be called under reference lock */
2192 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)
2193 {
2194 int error, is_symbolic, open_flags;
2195 git_oid old_id = {{0}}, new_id = {{0}};
2196 git_str buf = GIT_STR_INIT, path = GIT_STR_INIT;
2197 git_repository *repo = backend->repo;
2198
2199 is_symbolic = ref->type == GIT_REFERENCE_SYMBOLIC;
2200
2201 /* "normal" symbolic updates do not write */
2202 if (is_symbolic &&
2203 strcmp(ref->name, GIT_HEAD_FILE) &&
2204 !(old && new))
2205 return 0;
2206
2207 /* From here on is_symbolic also means that it's HEAD */
2208
2209 if (old) {
2210 git_oid_cpy(&old_id, old);
2211 } else {
2212 error = git_reference_name_to_id(&old_id, repo, ref->name);
2213 if (error < 0 && error != GIT_ENOTFOUND)
2214 return error;
2215 }
2216
2217 if (new) {
2218 git_oid_cpy(&new_id, new);
2219 } else {
2220 if (!is_symbolic) {
2221 git_oid_cpy(&new_id, git_reference_target(ref));
2222 } else {
2223 error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
2224 if (error < 0 && error != GIT_ENOTFOUND)
2225 return error;
2226 /* detaching HEAD does not create an entry */
2227 if (error == GIT_ENOTFOUND)
2228 return 0;
2229
2230 git_error_clear();
2231 }
2232 }
2233
2234 if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
2235 goto cleanup;
2236
2237 if ((error = reflog_path(&path, repo, ref->name)) < 0)
2238 goto cleanup;
2239
2240 if (((error = git_futils_mkpath2file(git_str_cstr(&path), 0777)) < 0) &&
2241 (error != GIT_EEXISTS)) {
2242 goto cleanup;
2243 }
2244
2245 /* If the new branch matches part of the namespace of a previously deleted branch,
2246 * there maybe an obsolete/unused directory (or directory hierarchy) in the way.
2247 */
2248 if (git_fs_path_isdir(git_str_cstr(&path))) {
2249 if ((error = git_futils_rmdir_r(git_str_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY)) < 0) {
2250 if (error == GIT_ENOTFOUND)
2251 error = 0;
2252 } else if (git_fs_path_isdir(git_str_cstr(&path))) {
2253 git_error_set(GIT_ERROR_REFERENCE, "cannot create reflog at '%s', there are reflogs beneath that folder",
2254 ref->name);
2255 error = GIT_EDIRECTORY;
2256 }
2257
2258 if (error != 0)
2259 goto cleanup;
2260 }
2261
2262 open_flags = O_WRONLY | O_CREAT | O_APPEND;
2263
2264 if (backend->fsync)
2265 open_flags |= O_FSYNC;
2266
2267 error = git_futils_writebuffer(&buf, git_str_cstr(&path), open_flags, GIT_REFLOG_FILE_MODE);
2268
2269 cleanup:
2270 git_str_dispose(&buf);
2271 git_str_dispose(&path);
2272
2273 return error;
2274 }
2275
2276 static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
2277 {
2278 int error = 0, fd;
2279 git_str old_path = GIT_STR_INIT;
2280 git_str new_path = GIT_STR_INIT;
2281 git_str temp_path = GIT_STR_INIT;
2282 git_str normalized = GIT_STR_INIT;
2283 git_repository *repo;
2284 refdb_fs_backend *backend;
2285
2286 GIT_ASSERT_ARG(_backend);
2287 GIT_ASSERT_ARG(old_name);
2288 GIT_ASSERT_ARG(new_name);
2289
2290 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2291 repo = backend->repo;
2292
2293 if ((error = git_reference__normalize_name(
2294 &normalized, new_name, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL)) < 0)
2295 return error;
2296
2297 if (git_str_joinpath(&temp_path, repo->gitdir, GIT_REFLOG_DIR) < 0)
2298 return -1;
2299
2300 if ((error = loose_path(&old_path, git_str_cstr(&temp_path), old_name)) < 0)
2301 return error;
2302
2303 if ((error = loose_path(&new_path, git_str_cstr(&temp_path), git_str_cstr(&normalized))) < 0)
2304 return error;
2305
2306 if (!git_fs_path_exists(git_str_cstr(&old_path))) {
2307 error = GIT_ENOTFOUND;
2308 goto cleanup;
2309 }
2310
2311 /*
2312 * Move the reflog to a temporary place. This two-phase renaming is required
2313 * in order to cope with funny renaming use cases when one tries to move a reference
2314 * to a partially colliding namespace:
2315 * - a/b -> a/b/c
2316 * - a/b/c/d -> a/b/c
2317 */
2318 if ((error = loose_path(&temp_path, git_str_cstr(&temp_path), "temp_reflog")) < 0)
2319 return error;
2320
2321 if ((fd = git_futils_mktmp(&temp_path, git_str_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) {
2322 error = -1;
2323 goto cleanup;
2324 }
2325
2326 p_close(fd);
2327
2328 if (p_rename(git_str_cstr(&old_path), git_str_cstr(&temp_path)) < 0) {
2329 git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name);
2330 error = -1;
2331 goto cleanup;
2332 }
2333
2334 if (git_fs_path_isdir(git_str_cstr(&new_path)) &&
2335 (git_futils_rmdir_r(git_str_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
2336 error = -1;
2337 goto cleanup;
2338 }
2339
2340 if (git_futils_mkpath2file(git_str_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
2341 error = -1;
2342 goto cleanup;
2343 }
2344
2345 if (p_rename(git_str_cstr(&temp_path), git_str_cstr(&new_path)) < 0) {
2346 git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name);
2347 error = -1;
2348 }
2349
2350 cleanup:
2351 git_str_dispose(&temp_path);
2352 git_str_dispose(&old_path);
2353 git_str_dispose(&new_path);
2354 git_str_dispose(&normalized);
2355
2356 return error;
2357 }
2358
2359 static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
2360 {
2361 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2362 git_str path = GIT_STR_INIT;
2363 int error;
2364
2365 GIT_ASSERT_ARG(_backend);
2366 GIT_ASSERT_ARG(name);
2367
2368 if ((error = reflog_path(&path, backend->repo, name)) < 0)
2369 goto out;
2370
2371 if (!git_fs_path_exists(path.ptr))
2372 goto out;
2373
2374 if ((error = p_unlink(path.ptr)) < 0)
2375 goto out;
2376
2377 error = refdb_fs_backend__prune_refs(backend, name, GIT_REFLOG_DIR);
2378
2379 out:
2380 git_str_dispose(&path);
2381
2382 return error;
2383 }
2384
2385 int git_refdb_backend_fs(
2386 git_refdb_backend **backend_out,
2387 git_repository *repository)
2388 {
2389 int t = 0;
2390 git_str gitpath = GIT_STR_INIT;
2391 refdb_fs_backend *backend;
2392
2393 backend = git__calloc(1, sizeof(refdb_fs_backend));
2394 GIT_ERROR_CHECK_ALLOC(backend);
2395 if (git_mutex_init(&backend->prlock) < 0) {
2396 git__free(backend);
2397 return -1;
2398 }
2399
2400
2401 if (git_refdb_init_backend(&backend->parent, GIT_REFDB_BACKEND_VERSION) < 0)
2402 goto fail;
2403
2404 backend->repo = repository;
2405
2406 if (repository->gitdir) {
2407 backend->gitpath = setup_namespace(repository, repository->gitdir);
2408
2409 if (backend->gitpath == NULL)
2410 goto fail;
2411 }
2412
2413 if (repository->commondir) {
2414 backend->commonpath = setup_namespace(repository, repository->commondir);
2415
2416 if (backend->commonpath == NULL)
2417 goto fail;
2418 }
2419
2420 if (git_str_joinpath(&gitpath, backend->commonpath, GIT_PACKEDREFS_FILE) < 0 ||
2421 git_sortedcache_new(
2422 &backend->refcache, offsetof(struct packref, name),
2423 NULL, NULL, packref_cmp, git_str_cstr(&gitpath)) < 0)
2424 goto fail;
2425
2426 git_str_dispose(&gitpath);
2427
2428 if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_IGNORECASE) && t) {
2429 backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
2430 backend->direach_flags |= GIT_FS_PATH_DIR_IGNORE_CASE;
2431 }
2432 if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_PRECOMPOSE) && t) {
2433 backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
2434 backend->direach_flags |= GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE;
2435 }
2436 if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) ||
2437 git_repository__fsync_gitdir)
2438 backend->fsync = 1;
2439 backend->iterator_flags |= GIT_ITERATOR_DESCEND_SYMLINKS;
2440
2441 backend->parent.exists = &refdb_fs_backend__exists;
2442 backend->parent.lookup = &refdb_fs_backend__lookup;
2443 backend->parent.iterator = &refdb_fs_backend__iterator;
2444 backend->parent.write = &refdb_fs_backend__write;
2445 backend->parent.del = &refdb_fs_backend__delete;
2446 backend->parent.rename = &refdb_fs_backend__rename;
2447 backend->parent.compress = &refdb_fs_backend__compress;
2448 backend->parent.lock = &refdb_fs_backend__lock;
2449 backend->parent.unlock = &refdb_fs_backend__unlock;
2450 backend->parent.has_log = &refdb_reflog_fs__has_log;
2451 backend->parent.ensure_log = &refdb_reflog_fs__ensure_log;
2452 backend->parent.free = &refdb_fs_backend__free;
2453 backend->parent.reflog_read = &refdb_reflog_fs__read;
2454 backend->parent.reflog_write = &refdb_reflog_fs__write;
2455 backend->parent.reflog_rename = &refdb_reflog_fs__rename;
2456 backend->parent.reflog_delete = &refdb_reflog_fs__delete;
2457
2458 *backend_out = (git_refdb_backend *)backend;
2459 return 0;
2460
2461 fail:
2462 git_mutex_free(&backend->prlock);
2463 git_str_dispose(&gitpath);
2464 git__free(backend->gitpath);
2465 git__free(backend->commonpath);
2466 git__free(backend);
2467 return -1;
2468 }