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