2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "repository.h"
18 #include "sortedcache.h"
19 #include "signature.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>
31 #define DEFAULT_NESTING_LEVEL 5
32 #define MAX_NESTING_LEVEL 10
36 PACKREF_WAS_LOOSE
= 2,
37 PACKREF_CANNOT_PEEL
= 4,
51 char name
[GIT_FLEX_ARRAY
];
54 typedef struct refdb_fs_backend
{
55 git_refdb_backend parent
;
60 git_sortedcache
*refcache
;
62 git_iterator_flag_t iterator_flags
;
63 uint32_t direach_flags
;
66 static int refdb_reflog_fs__delete(git_refdb_backend
*_backend
, const char *name
);
68 static int packref_cmp(const void *a_
, const void *b_
)
70 const struct packref
*a
= a_
, *b
= b_
;
71 return strcmp(a
->name
, b
->name
);
74 static int packed_reload(refdb_fs_backend
*backend
)
77 git_buf packedrefs
= GIT_BUF_INIT
;
78 char *scan
, *eof
, *eol
;
83 error
= git_sortedcache_lockandload(backend
->refcache
, &packedrefs
);
86 * If we can't find the packed-refs, clear table and return.
87 * Any other error just gets passed through.
88 * If no error, and file wasn't changed, just return.
89 * Anything else means we need to refresh the packed refs.
92 if (error
== GIT_ENOTFOUND
) {
93 git_sortedcache_clear(backend
->refcache
, true);
100 /* At this point, refresh the packed refs from the loaded buffer. */
102 git_sortedcache_clear(backend
->refcache
, false);
104 scan
= (char *)packedrefs
.ptr
;
105 eof
= scan
+ packedrefs
.size
;
107 backend
->peeling_mode
= PEELING_NONE
;
110 static const char *traits_header
= "# pack-refs with: ";
112 if (git__prefixcmp(scan
, traits_header
) == 0) {
113 scan
+= strlen(traits_header
);
114 eol
= strchr(scan
, '\n');
120 if (strstr(scan
, " fully-peeled ") != NULL
) {
121 backend
->peeling_mode
= PEELING_FULL
;
122 } else if (strstr(scan
, " peeled ") != NULL
) {
123 backend
->peeling_mode
= PEELING_STANDARD
;
130 while (scan
< eof
&& *scan
== '#') {
131 if (!(eol
= strchr(scan
, '\n')))
140 /* parse "<OID> <refname>\n" */
142 if (git_oid_fromstr(&oid
, scan
) < 0)
144 scan
+= GIT_OID_HEXSZ
;
148 if (!(eol
= strchr(scan
, '\n')))
154 if (git_sortedcache_upsert((void **)&ref
, backend
->refcache
, scan
) < 0)
158 git_oid_cpy(&ref
->oid
, &oid
);
160 /* look for optional "^<OID>\n" */
163 if (git_oid_fromstr(&oid
, scan
+ 1) < 0)
165 scan
+= GIT_OID_HEXSZ
+ 1;
168 if (!(eol
= strchr(scan
, '\n')))
173 git_oid_cpy(&ref
->peel
, &oid
);
174 ref
->flags
|= PACKREF_HAS_PEEL
;
176 else if (backend
->peeling_mode
== PEELING_FULL
||
177 (backend
->peeling_mode
== PEELING_STANDARD
&&
178 git__prefixcmp(ref
->name
, GIT_REFS_TAGS_DIR
) == 0))
179 ref
->flags
|= PACKREF_CANNOT_PEEL
;
182 git_sortedcache_wunlock(backend
->refcache
);
183 git_buf_free(&packedrefs
);
188 giterr_set(GITERR_REFERENCE
, "Corrupted packed references file");
190 git_sortedcache_clear(backend
->refcache
, false);
191 git_sortedcache_wunlock(backend
->refcache
);
192 git_buf_free(&packedrefs
);
197 static int loose_parse_oid(
198 git_oid
*oid
, const char *filename
, git_buf
*file_content
)
200 const char *str
= git_buf_cstr(file_content
);
202 if (git_buf_len(file_content
) < GIT_OID_HEXSZ
)
205 /* we need to get 40 OID characters from the file */
206 if (git_oid_fromstr(oid
, str
) < 0)
209 /* If the file is longer than 40 chars, the 41st must be a space */
210 str
+= GIT_OID_HEXSZ
;
211 if (*str
== '\0' || git__isspace(*str
))
215 giterr_set(GITERR_REFERENCE
, "Corrupted loose reference file: %s", filename
);
219 static int loose_readbuffer(git_buf
*buf
, const char *base
, const char *path
)
223 /* build full path to file */
224 if ((error
= git_buf_joinpath(buf
, base
, path
)) < 0 ||
225 (error
= git_futils_readbuffer(buf
, buf
->ptr
)) < 0)
231 static int loose_lookup_to_packfile(refdb_fs_backend
*backend
, const char *name
)
234 git_buf ref_file
= GIT_BUF_INIT
;
235 struct packref
*ref
= NULL
;
238 /* if we fail to load the loose reference, assume someone changed
239 * the filesystem under us and skip it...
241 if (loose_readbuffer(&ref_file
, backend
->path
, name
) < 0) {
246 /* skip symbolic refs */
247 if (!git__prefixcmp(git_buf_cstr(&ref_file
), GIT_SYMREF
))
250 /* parse OID from file */
251 if ((error
= loose_parse_oid(&oid
, name
, &ref_file
)) < 0)
254 git_sortedcache_wlock(backend
->refcache
);
256 if (!(error
= git_sortedcache_upsert(
257 (void **)&ref
, backend
->refcache
, name
))) {
259 git_oid_cpy(&ref
->oid
, &oid
);
260 ref
->flags
= PACKREF_WAS_LOOSE
;
263 git_sortedcache_wunlock(backend
->refcache
);
266 git_buf_free(&ref_file
);
270 static int _dirent_loose_load(void *payload
, git_buf
*full_path
)
272 refdb_fs_backend
*backend
= payload
;
273 const char *file_path
;
275 if (git__suffixcmp(full_path
->ptr
, ".lock") == 0)
278 if (git_path_isdir(full_path
->ptr
)) {
279 int error
= git_path_direach(
280 full_path
, backend
->direach_flags
, _dirent_loose_load
, backend
);
281 /* Race with the filesystem, ignore it */
282 if (error
== GIT_ENOTFOUND
) {
290 file_path
= full_path
->ptr
+ strlen(backend
->path
);
292 return loose_lookup_to_packfile(backend
, file_path
);
296 * Load all the loose references from the repository
297 * into the in-memory Packfile, and build a vector with
298 * all the references so it can be written back to
301 static int packed_loadloose(refdb_fs_backend
*backend
)
304 git_buf refs_path
= GIT_BUF_INIT
;
306 if (git_buf_joinpath(&refs_path
, backend
->path
, GIT_REFS_DIR
) < 0)
310 * Load all the loose files from disk into the Packfile table.
311 * This will overwrite any old packed entries with their
312 * updated loose versions
314 error
= git_path_direach(
315 &refs_path
, backend
->direach_flags
, _dirent_loose_load
, backend
);
317 git_buf_free(&refs_path
);
322 static int refdb_fs_backend__exists(
324 git_refdb_backend
*_backend
,
325 const char *ref_name
)
327 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
328 git_buf ref_path
= GIT_BUF_INIT
;
332 if (packed_reload(backend
) < 0 ||
333 git_buf_joinpath(&ref_path
, backend
->path
, ref_name
) < 0)
336 *exists
= git_path_isfile(ref_path
.ptr
) ||
337 (git_sortedcache_lookup(backend
->refcache
, ref_name
) != NULL
);
339 git_buf_free(&ref_path
);
343 static const char *loose_parse_symbolic(git_buf
*file_content
)
345 const unsigned int header_len
= (unsigned int)strlen(GIT_SYMREF
);
346 const char *refname_start
;
348 refname_start
= (const char *)file_content
->ptr
;
350 if (git_buf_len(file_content
) < header_len
+ 1) {
351 giterr_set(GITERR_REFERENCE
, "Corrupted loose reference file");
356 * Assume we have already checked for the header
357 * before calling this function
359 refname_start
+= header_len
;
361 return refname_start
;
364 static int loose_lookup(
366 refdb_fs_backend
*backend
,
367 const char *ref_name
)
369 git_buf ref_file
= GIT_BUF_INIT
;
375 if ((error
= loose_readbuffer(&ref_file
, backend
->path
, ref_name
)) < 0)
376 /* cannot read loose ref file - gah */;
377 else if (git__prefixcmp(git_buf_cstr(&ref_file
), GIT_SYMREF
) == 0) {
380 git_buf_rtrim(&ref_file
);
382 if (!(target
= loose_parse_symbolic(&ref_file
)))
384 else if (out
!= NULL
)
385 *out
= git_reference__alloc_symbolic(ref_name
, target
);
389 if (!(error
= loose_parse_oid(&oid
, ref_name
, &ref_file
)) &&
391 *out
= git_reference__alloc(ref_name
, &oid
, NULL
);
394 git_buf_free(&ref_file
);
398 static int ref_error_notfound(const char *name
)
400 giterr_set(GITERR_REFERENCE
, "Reference '%s' not found", name
);
401 return GIT_ENOTFOUND
;
404 static int packed_lookup(
406 refdb_fs_backend
*backend
,
407 const char *ref_name
)
410 struct packref
*entry
;
412 if (packed_reload(backend
) < 0)
415 if (git_sortedcache_rlock(backend
->refcache
) < 0)
418 entry
= git_sortedcache_lookup(backend
->refcache
, ref_name
);
420 error
= ref_error_notfound(ref_name
);
422 *out
= git_reference__alloc(ref_name
, &entry
->oid
, &entry
->peel
);
427 git_sortedcache_runlock(backend
->refcache
);
432 static int refdb_fs_backend__lookup(
434 git_refdb_backend
*_backend
,
435 const char *ref_name
)
437 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
442 if (!(error
= loose_lookup(out
, backend
, ref_name
)))
445 /* only try to lookup this reference on the packfile if it
446 * wasn't found on the loose refs; not if there was a critical error */
447 if (error
== GIT_ENOTFOUND
) {
449 error
= packed_lookup(out
, backend
, ref_name
);
456 git_reference_iterator parent
;
463 git_sortedcache
*cache
;
468 static void refdb_fs_backend__iterator_free(git_reference_iterator
*_iter
)
470 refdb_fs_iter
*iter
= (refdb_fs_iter
*) _iter
;
472 git_vector_free(&iter
->loose
);
473 git_pool_clear(&iter
->pool
);
474 git_sortedcache_free(iter
->cache
);
478 static int iter_load_loose_paths(refdb_fs_backend
*backend
, refdb_fs_iter
*iter
)
481 git_buf path
= GIT_BUF_INIT
;
482 git_iterator
*fsit
= NULL
;
483 git_iterator_options fsit_opts
= GIT_ITERATOR_OPTIONS_INIT
;
484 const git_index_entry
*entry
= NULL
;
486 if (!backend
->path
) /* do nothing if no path for loose refs */
489 fsit_opts
.flags
= backend
->iterator_flags
;
491 if ((error
= git_buf_printf(&path
, "%s/refs", backend
->path
)) < 0 ||
492 (error
= git_iterator_for_filesystem(&fsit
, path
.ptr
, &fsit_opts
)) < 0) {
497 error
= git_buf_sets(&path
, GIT_REFS_DIR
);
499 while (!error
&& !git_iterator_advance(&entry
, fsit
)) {
500 const char *ref_name
;
504 git_buf_truncate(&path
, strlen(GIT_REFS_DIR
));
505 git_buf_puts(&path
, entry
->path
);
506 ref_name
= git_buf_cstr(&path
);
508 if (git__suffixcmp(ref_name
, ".lock") == 0 ||
509 (iter
->glob
&& p_fnmatch(iter
->glob
, ref_name
, 0) != 0))
512 git_sortedcache_rlock(backend
->refcache
);
513 ref
= git_sortedcache_lookup(backend
->refcache
, ref_name
);
515 ref
->flags
|= PACKREF_SHADOWED
;
516 git_sortedcache_runlock(backend
->refcache
);
518 ref_dup
= git_pool_strdup(&iter
->pool
, ref_name
);
522 error
= git_vector_insert(&iter
->loose
, ref_dup
);
525 git_iterator_free(fsit
);
531 static int refdb_fs_backend__iterator_next(
532 git_reference
**out
, git_reference_iterator
*_iter
)
534 int error
= GIT_ITEROVER
;
535 refdb_fs_iter
*iter
= (refdb_fs_iter
*)_iter
;
536 refdb_fs_backend
*backend
= (refdb_fs_backend
*)iter
->parent
.db
->backend
;
539 while (iter
->loose_pos
< iter
->loose
.length
) {
540 const char *path
= git_vector_get(&iter
->loose
, iter
->loose_pos
++);
542 if (loose_lookup(out
, backend
, path
) == 0)
549 if ((error
= git_sortedcache_copy(&iter
->cache
, backend
->refcache
, 1, NULL
, NULL
)) < 0)
553 error
= GIT_ITEROVER
;
554 while (iter
->packed_pos
< git_sortedcache_entrycount(iter
->cache
)) {
555 ref
= git_sortedcache_entry(iter
->cache
, iter
->packed_pos
++);
556 if (!ref
) /* stop now if another thread deleted refs and we past end */
559 if (ref
->flags
& PACKREF_SHADOWED
)
561 if (iter
->glob
&& p_fnmatch(iter
->glob
, ref
->name
, 0) != 0)
564 *out
= git_reference__alloc(ref
->name
, &ref
->oid
, &ref
->peel
);
565 error
= (*out
!= NULL
) ? 0 : -1;
572 static int refdb_fs_backend__iterator_next_name(
573 const char **out
, git_reference_iterator
*_iter
)
575 int error
= GIT_ITEROVER
;
576 refdb_fs_iter
*iter
= (refdb_fs_iter
*)_iter
;
577 refdb_fs_backend
*backend
= (refdb_fs_backend
*)iter
->parent
.db
->backend
;
580 while (iter
->loose_pos
< iter
->loose
.length
) {
581 const char *path
= git_vector_get(&iter
->loose
, iter
->loose_pos
++);
583 if (loose_lookup(NULL
, backend
, path
) == 0) {
592 if ((error
= git_sortedcache_copy(&iter
->cache
, backend
->refcache
, 1, NULL
, NULL
)) < 0)
596 error
= GIT_ITEROVER
;
597 while (iter
->packed_pos
< git_sortedcache_entrycount(iter
->cache
)) {
598 ref
= git_sortedcache_entry(iter
->cache
, iter
->packed_pos
++);
599 if (!ref
) /* stop now if another thread deleted refs and we past end */
602 if (ref
->flags
& PACKREF_SHADOWED
)
604 if (iter
->glob
&& p_fnmatch(iter
->glob
, ref
->name
, 0) != 0)
615 static int refdb_fs_backend__iterator(
616 git_reference_iterator
**out
, git_refdb_backend
*_backend
, const char *glob
)
619 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
623 if (packed_reload(backend
) < 0)
626 iter
= git__calloc(1, sizeof(refdb_fs_iter
));
627 GITERR_CHECK_ALLOC(iter
);
629 git_pool_init(&iter
->pool
, 1);
631 if (git_vector_init(&iter
->loose
, 8, NULL
) < 0)
635 (iter
->glob
= git_pool_strdup(&iter
->pool
, glob
)) == NULL
)
638 iter
->parent
.next
= refdb_fs_backend__iterator_next
;
639 iter
->parent
.next_name
= refdb_fs_backend__iterator_next_name
;
640 iter
->parent
.free
= refdb_fs_backend__iterator_free
;
642 if (iter_load_loose_paths(backend
, iter
) < 0)
645 *out
= (git_reference_iterator
*)iter
;
649 refdb_fs_backend__iterator_free((git_reference_iterator
*)iter
);
653 static bool ref_is_available(
654 const char *old_ref
, const char *new_ref
, const char *this_ref
)
656 if (old_ref
== NULL
|| strcmp(old_ref
, this_ref
)) {
657 size_t reflen
= strlen(this_ref
);
658 size_t newlen
= strlen(new_ref
);
659 size_t cmplen
= reflen
< newlen
? reflen
: newlen
;
660 const char *lead
= reflen
< newlen
? new_ref
: this_ref
;
662 if (!strncmp(new_ref
, this_ref
, cmplen
) && lead
[cmplen
] == '/') {
670 static int reference_path_available(
671 refdb_fs_backend
*backend
,
678 if (packed_reload(backend
) < 0)
684 if (refdb_fs_backend__exists(
685 &exists
, (git_refdb_backend
*)backend
, new_ref
) < 0)
689 giterr_set(GITERR_REFERENCE
,
690 "Failed to write reference '%s': a reference with "
691 "that name already exists.", new_ref
);
696 git_sortedcache_rlock(backend
->refcache
);
698 for (i
= 0; i
< git_sortedcache_entrycount(backend
->refcache
); ++i
) {
699 struct packref
*ref
= git_sortedcache_entry(backend
->refcache
, i
);
701 if (ref
&& !ref_is_available(old_ref
, new_ref
, ref
->name
)) {
702 git_sortedcache_runlock(backend
->refcache
);
703 giterr_set(GITERR_REFERENCE
,
704 "Path to reference '%s' collides with existing one", new_ref
);
709 git_sortedcache_runlock(backend
->refcache
);
713 static int loose_lock(git_filebuf
*file
, refdb_fs_backend
*backend
, const char *name
)
716 git_buf ref_path
= GIT_BUF_INIT
;
718 assert(file
&& backend
&& name
);
720 if (!git_path_isvalid(backend
->repo
, name
, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS
)) {
721 giterr_set(GITERR_INVALID
, "Invalid reference name '%s'.", name
);
722 return GIT_EINVALIDSPEC
;
725 /* Remove a possibly existing empty directory hierarchy
726 * which name would collide with the reference name
728 if (git_futils_rmdir_r(name
, backend
->path
, GIT_RMDIR_SKIP_NONEMPTY
) < 0)
731 if (git_buf_joinpath(&ref_path
, backend
->path
, name
) < 0)
734 error
= git_filebuf_open(file
, ref_path
.ptr
, GIT_FILEBUF_FORCE
, GIT_REFS_FILE_MODE
);
736 if (error
== GIT_EDIRECTORY
)
737 giterr_set(GITERR_REFERENCE
, "cannot lock ref '%s', there are refs beneath that folder", name
);
739 git_buf_free(&ref_path
);
743 static int loose_commit(git_filebuf
*file
, const git_reference
*ref
)
747 if (ref
->type
== GIT_REF_OID
) {
748 char oid
[GIT_OID_HEXSZ
+ 1];
749 git_oid_nfmt(oid
, sizeof(oid
), &ref
->target
.oid
);
751 git_filebuf_printf(file
, "%s\n", oid
);
752 } else if (ref
->type
== GIT_REF_SYMBOLIC
) {
753 git_filebuf_printf(file
, GIT_SYMREF
"%s\n", ref
->target
.symbolic
);
755 assert(0); /* don't let this happen */
758 return git_filebuf_commit(file
);
761 static int refdb_fs_backend__lock(void **out
, git_refdb_backend
*_backend
, const char *refname
)
765 refdb_fs_backend
*backend
= (refdb_fs_backend
*) _backend
;
767 lock
= git__calloc(1, sizeof(git_filebuf
));
768 GITERR_CHECK_ALLOC(lock
);
770 if ((error
= loose_lock(lock
, backend
, refname
)) < 0) {
779 static int refdb_fs_backend__write_tail(
780 git_refdb_backend
*_backend
,
781 const git_reference
*ref
,
784 const git_signature
*who
,
786 const git_oid
*old_id
,
787 const char *old_target
);
789 static int refdb_fs_backend__delete_tail(
790 git_refdb_backend
*_backend
,
792 const char *ref_name
,
793 const git_oid
*old_id
, const char *old_target
);
795 static int refdb_fs_backend__unlock(git_refdb_backend
*backend
, void *payload
, int success
, int update_reflog
,
796 const git_reference
*ref
, const git_signature
*sig
, const char *message
)
798 git_filebuf
*lock
= (git_filebuf
*) payload
;
802 error
= refdb_fs_backend__delete_tail(backend
, lock
, ref
->name
, NULL
, NULL
);
804 error
= refdb_fs_backend__write_tail(backend
, ref
, lock
, update_reflog
, sig
, message
, NULL
, NULL
);
806 git_filebuf_cleanup(lock
);
813 * Find out what object this reference resolves to.
815 * For references that point to a 'big' tag (e.g. an
816 * actual tag object on the repository), we need to
817 * cache on the packfile the OID of the object to
818 * which that 'big tag' is pointing to.
820 static int packed_find_peel(refdb_fs_backend
*backend
, struct packref
*ref
)
824 if (ref
->flags
& PACKREF_HAS_PEEL
|| ref
->flags
& PACKREF_CANNOT_PEEL
)
828 * Find the tagged object in the repository
830 if (git_object_lookup(&object
, backend
->repo
, &ref
->oid
, GIT_OBJ_ANY
) < 0)
834 * If the tagged object is a Tag object, we need to resolve it;
835 * if the ref is actually a 'weak' ref, we don't need to resolve
838 if (git_object_type(object
) == GIT_OBJ_TAG
) {
839 git_tag
*tag
= (git_tag
*)object
;
842 * Find the object pointed at by this tag
844 git_oid_cpy(&ref
->peel
, git_tag_target_id(tag
));
845 ref
->flags
|= PACKREF_HAS_PEEL
;
848 * The reference has now cached the resolved OID, and is
849 * marked at such. When written to the packfile, it'll be
850 * accompanied by this resolved oid
854 git_object_free(object
);
859 * Write a single reference into a packfile
861 static int packed_write_ref(struct packref
*ref
, git_filebuf
*file
)
863 char oid
[GIT_OID_HEXSZ
+ 1];
864 git_oid_nfmt(oid
, sizeof(oid
), &ref
->oid
);
867 * For references that peel to an object in the repo, we must
868 * write the resulting peel on a separate line, e.g.
870 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
871 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
873 * This obviously only applies to tags.
874 * The required peels have already been loaded into `ref->peel_target`.
876 if (ref
->flags
& PACKREF_HAS_PEEL
) {
877 char peel
[GIT_OID_HEXSZ
+ 1];
878 git_oid_nfmt(peel
, sizeof(peel
), &ref
->peel
);
880 if (git_filebuf_printf(file
, "%s %s\n^%s\n", oid
, ref
->name
, peel
) < 0)
883 if (git_filebuf_printf(file
, "%s %s\n", oid
, ref
->name
) < 0)
891 * Remove all loose references
893 * Once we have successfully written a packfile,
894 * all the loose references that were packed must be
897 * This is a dangerous method; make sure the packfile
898 * is well-written, because we are destructing references
901 static int packed_remove_loose(refdb_fs_backend
*backend
)
904 git_buf full_path
= GIT_BUF_INIT
;
907 /* backend->refcache is already locked when this is called */
909 for (i
= 0; i
< git_sortedcache_entrycount(backend
->refcache
); ++i
) {
910 struct packref
*ref
= git_sortedcache_entry(backend
->refcache
, i
);
912 if (!ref
|| !(ref
->flags
& PACKREF_WAS_LOOSE
))
915 if (git_buf_joinpath(&full_path
, backend
->path
, ref
->name
) < 0)
916 return -1; /* critical; do not try to recover on oom */
918 if (git_path_exists(full_path
.ptr
) && p_unlink(full_path
.ptr
) < 0) {
922 giterr_set(GITERR_REFERENCE
,
923 "Failed to remove loose reference '%s' after packing: %s",
924 full_path
.ptr
, strerror(errno
));
929 * if we fail to remove a single file, this is *not* good,
930 * but we should keep going and remove as many as possible.
931 * After we've removed as many files as possible, we return
932 * the error code anyway.
936 git_buf_free(&full_path
);
937 return failed
? -1 : 0;
941 * Write all the contents in the in-memory packfile to disk.
943 static int packed_write(refdb_fs_backend
*backend
)
945 git_sortedcache
*refcache
= backend
->refcache
;
946 git_filebuf pack_file
= GIT_FILEBUF_INIT
;
949 /* lock the cache to updates while we do this */
950 if (git_sortedcache_wlock(refcache
) < 0)
954 if (git_filebuf_open(&pack_file
, git_sortedcache_path(refcache
), 0, GIT_PACKEDREFS_FILE_MODE
) < 0)
957 /* Packfiles have a header... apparently
958 * This is in fact not required, but we might as well print it
960 if (git_filebuf_printf(&pack_file
, "%s\n", GIT_PACKEDREFS_HEADER
) < 0)
963 for (i
= 0; i
< git_sortedcache_entrycount(refcache
); ++i
) {
964 struct packref
*ref
= git_sortedcache_entry(refcache
, i
);
967 if (packed_find_peel(backend
, ref
) < 0)
970 if (packed_write_ref(ref
, &pack_file
) < 0)
974 /* if we've written all the references properly, we can commit
975 * the packfile to make the changes effective */
976 if (git_filebuf_commit(&pack_file
) < 0)
979 /* when and only when the packfile has been properly written,
980 * we can go ahead and remove the loose refs */
981 if (packed_remove_loose(backend
) < 0)
984 git_sortedcache_updated(refcache
);
985 git_sortedcache_wunlock(refcache
);
991 git_filebuf_cleanup(&pack_file
);
992 git_sortedcache_wunlock(refcache
);
997 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
);
998 static int has_reflog(git_repository
*repo
, const char *name
);
1000 /* We only write if it's under heads/, remotes/ or notes/ or if it already has a log */
1001 static int should_write_reflog(int *write
, git_repository
*repo
, const char *name
)
1005 error
= git_repository__cvar(&logall
, repo
, GIT_CVAR_LOGALLREFUPDATES
);
1009 /* Defaults to the opposite of the repo being bare */
1010 if (logall
== GIT_LOGALLREFUPDATES_UNSET
)
1011 logall
= !git_repository_is_bare(repo
);
1015 } else if (has_reflog(repo
, name
)) {
1017 } else if (!git__prefixcmp(name
, GIT_REFS_HEADS_DIR
) ||
1018 !git__strcmp(name
, GIT_HEAD_FILE
) ||
1019 !git__prefixcmp(name
, GIT_REFS_REMOTES_DIR
) ||
1020 !git__prefixcmp(name
, GIT_REFS_NOTES_DIR
)) {
1029 static int cmp_old_ref(int *cmp
, git_refdb_backend
*backend
, const char *name
,
1030 const git_oid
*old_id
, const char *old_target
)
1033 git_reference
*old_ref
= NULL
;
1036 /* It "matches" if there is no old value to compare against */
1037 if (!old_id
&& !old_target
)
1040 if ((error
= refdb_fs_backend__lookup(&old_ref
, backend
, name
)) < 0)
1043 /* If the types don't match, there's no way the values do */
1044 if (old_id
&& old_ref
->type
!= GIT_REF_OID
) {
1048 if (old_target
&& old_ref
->type
!= GIT_REF_SYMBOLIC
) {
1053 if (old_id
&& old_ref
->type
== GIT_REF_OID
)
1054 *cmp
= git_oid_cmp(old_id
, &old_ref
->target
.oid
);
1056 if (old_target
&& old_ref
->type
== GIT_REF_SYMBOLIC
)
1057 *cmp
= git__strcmp(old_target
, old_ref
->target
.symbolic
);
1060 git_reference_free(old_ref
);
1066 * The git.git comment regarding this, for your viewing pleasure:
1068 * Special hack: If a branch is updated directly and HEAD
1069 * points to it (may happen on the remote side of a push
1070 * for example) then logically the HEAD reflog should be
1072 * A generic solution implies reverse symref information,
1073 * but finding all symrefs pointing to the given branch
1074 * would be rather costly for this rare event (the direct
1075 * update of a branch) to be worth it. So let's cheat and
1076 * check with HEAD only which should cover 99% of all usage
1077 * scenarios (even 100% of the default ones).
1079 static int maybe_append_head(refdb_fs_backend
*backend
, const git_reference
*ref
, const git_signature
*who
, const char *message
)
1082 git_oid old_id
= {{0}};
1083 git_reference
*tmp
= NULL
, *head
= NULL
, *peeled
= NULL
;
1086 if (ref
->type
== GIT_REF_SYMBOLIC
)
1089 /* if we can't resolve, we use {0}*40 as old id */
1090 git_reference_name_to_id(&old_id
, backend
->repo
, ref
->name
);
1092 if ((error
= git_reference_lookup(&head
, backend
->repo
, GIT_HEAD_FILE
)) < 0)
1095 if (git_reference_type(head
) == GIT_REF_OID
)
1098 if ((error
= git_reference_lookup(&tmp
, backend
->repo
, GIT_HEAD_FILE
)) < 0)
1101 /* Go down the symref chain until we find the branch */
1102 while (git_reference_type(tmp
) == GIT_REF_SYMBOLIC
) {
1103 error
= git_reference_lookup(&peeled
, backend
->repo
, git_reference_symbolic_target(tmp
));
1107 git_reference_free(tmp
);
1111 if (error
== GIT_ENOTFOUND
) {
1113 name
= git_reference_symbolic_target(tmp
);
1114 } else if (error
< 0) {
1117 name
= git_reference_name(tmp
);
1120 if (strcmp(name
, ref
->name
))
1123 error
= reflog_append(backend
, head
, &old_id
, git_reference_target(ref
), who
, message
);
1126 git_reference_free(tmp
);
1127 git_reference_free(head
);
1131 static int refdb_fs_backend__write(
1132 git_refdb_backend
*_backend
,
1133 const git_reference
*ref
,
1135 const git_signature
*who
,
1136 const char *message
,
1137 const git_oid
*old_id
,
1138 const char *old_target
)
1140 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1141 git_filebuf file
= GIT_FILEBUF_INIT
;
1146 error
= reference_path_available(backend
, ref
->name
, NULL
, force
);
1150 /* We need to perform the reflog append and old value check under the ref's lock */
1151 if ((error
= loose_lock(&file
, backend
, ref
->name
)) < 0)
1154 return refdb_fs_backend__write_tail(_backend
, ref
, &file
, true, who
, message
, old_id
, old_target
);
1157 static int refdb_fs_backend__write_tail(
1158 git_refdb_backend
*_backend
,
1159 const git_reference
*ref
,
1162 const git_signature
*who
,
1163 const char *message
,
1164 const git_oid
*old_id
,
1165 const char *old_target
)
1167 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1168 int error
= 0, cmp
= 0, should_write
;
1169 const char *new_target
= NULL
;
1170 const git_oid
*new_id
= NULL
;
1172 if ((error
= cmp_old_ref(&cmp
, _backend
, ref
->name
, old_id
, old_target
)) < 0)
1176 giterr_set(GITERR_REFERENCE
, "old reference value does not match");
1177 error
= GIT_EMODIFIED
;
1181 if (ref
->type
== GIT_REF_SYMBOLIC
)
1182 new_target
= ref
->target
.symbolic
;
1184 new_id
= &ref
->target
.oid
;
1186 error
= cmp_old_ref(&cmp
, _backend
, ref
->name
, new_id
, new_target
);
1187 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1190 /* Don't update if we have the same value */
1191 if (!error
&& !cmp
) {
1193 goto on_error
; /* not really error */
1196 if (update_reflog
) {
1197 if ((error
= should_write_reflog(&should_write
, backend
->repo
, ref
->name
)) < 0)
1201 if ((error
= reflog_append(backend
, ref
, NULL
, NULL
, who
, message
)) < 0)
1203 if ((error
= maybe_append_head(backend
, ref
, who
, message
)) < 0)
1208 return loose_commit(file
, ref
);
1211 git_filebuf_cleanup(file
);
1215 static int refdb_fs_backend__delete(
1216 git_refdb_backend
*_backend
,
1217 const char *ref_name
,
1218 const git_oid
*old_id
, const char *old_target
)
1220 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1221 git_filebuf file
= GIT_FILEBUF_INIT
;
1224 assert(backend
&& ref_name
);
1226 if ((error
= loose_lock(&file
, backend
, ref_name
)) < 0)
1229 if ((error
= refdb_reflog_fs__delete(_backend
, ref_name
)) < 0) {
1230 git_filebuf_cleanup(&file
);
1234 return refdb_fs_backend__delete_tail(_backend
, &file
, ref_name
, old_id
, old_target
);
1237 static int refdb_fs_backend__delete_tail(
1238 git_refdb_backend
*_backend
,
1240 const char *ref_name
,
1241 const git_oid
*old_id
, const char *old_target
)
1243 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1244 git_buf loose_path
= GIT_BUF_INIT
;
1246 int error
= 0, cmp
= 0;
1247 bool loose_deleted
= 0;
1249 error
= cmp_old_ref(&cmp
, _backend
, ref_name
, old_id
, old_target
);
1254 giterr_set(GITERR_REFERENCE
, "old reference value does not match");
1255 error
= GIT_EMODIFIED
;
1259 /* If a loose reference exists, remove it from the filesystem */
1260 if (git_buf_joinpath(&loose_path
, backend
->path
, ref_name
) < 0)
1263 if (git_path_isfile(loose_path
.ptr
)) {
1264 error
= p_unlink(loose_path
.ptr
);
1268 git_buf_free(&loose_path
);
1273 if ((error
= packed_reload(backend
)) < 0)
1276 /* If a packed reference exists, remove it from the packfile and repack */
1277 if ((error
= git_sortedcache_wlock(backend
->refcache
)) < 0)
1280 if (!(error
= git_sortedcache_lookup_index(
1281 &pack_pos
, backend
->refcache
, ref_name
)))
1282 error
= git_sortedcache_remove(backend
->refcache
, pack_pos
);
1284 git_sortedcache_wunlock(backend
->refcache
);
1286 if (error
== GIT_ENOTFOUND
) {
1287 error
= loose_deleted
? 0 : ref_error_notfound(ref_name
);
1291 error
= packed_write(backend
);
1294 git_filebuf_cleanup(file
);
1299 static int refdb_reflog_fs__rename(git_refdb_backend
*_backend
, const char *old_name
, const char *new_name
);
1301 static int refdb_fs_backend__rename(
1302 git_reference
**out
,
1303 git_refdb_backend
*_backend
,
1304 const char *old_name
,
1305 const char *new_name
,
1307 const git_signature
*who
,
1308 const char *message
)
1310 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1311 git_reference
*old
, *new;
1312 git_filebuf file
= GIT_FILEBUF_INIT
;
1317 if ((error
= reference_path_available(
1318 backend
, new_name
, old_name
, force
)) < 0 ||
1319 (error
= refdb_fs_backend__lookup(&old
, _backend
, old_name
)) < 0)
1322 if ((error
= refdb_fs_backend__delete(_backend
, old_name
, NULL
, NULL
)) < 0) {
1323 git_reference_free(old
);
1327 new = git_reference__set_name(old
, new_name
);
1329 git_reference_free(old
);
1333 if ((error
= loose_lock(&file
, backend
, new->name
)) < 0) {
1334 git_reference_free(new);
1338 /* Try to rename the refog; it's ok if the old doesn't exist */
1339 error
= refdb_reflog_fs__rename(_backend
, old_name
, new_name
);
1340 if (((error
== 0) || (error
== GIT_ENOTFOUND
)) &&
1341 ((error
= reflog_append(backend
, new, git_reference_target(new), NULL
, who
, message
)) < 0)) {
1342 git_reference_free(new);
1343 git_filebuf_cleanup(&file
);
1348 git_reference_free(new);
1349 git_filebuf_cleanup(&file
);
1354 if ((error
= loose_commit(&file
, new)) < 0 || out
== NULL
) {
1355 git_reference_free(new);
1363 static int refdb_fs_backend__compress(git_refdb_backend
*_backend
)
1365 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1369 if (packed_reload(backend
) < 0 || /* load the existing packfile */
1370 packed_loadloose(backend
) < 0 || /* add all the loose refs */
1371 packed_write(backend
) < 0) /* write back to disk */
1377 static void refdb_fs_backend__free(git_refdb_backend
*_backend
)
1379 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1383 git_sortedcache_free(backend
->refcache
);
1384 git__free(backend
->path
);
1388 static int setup_namespace(git_buf
*path
, git_repository
*repo
)
1390 char *parts
, *start
, *end
;
1392 /* Not all repositories have a path */
1393 if (repo
->path_repository
== NULL
)
1396 /* Load the path to the repo first */
1397 git_buf_puts(path
, repo
->path_repository
);
1399 /* if the repo is not namespaced, nothing else to do */
1400 if (repo
->namespace == NULL
)
1403 parts
= end
= git__strdup(repo
->namespace);
1408 * From `man gitnamespaces`:
1409 * namespaces which include a / will expand to a hierarchy
1410 * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
1411 * refs under refs/namespaces/foo/refs/namespaces/bar/
1413 while ((start
= git__strsep(&end
, "/")) != NULL
) {
1414 git_buf_printf(path
, "refs/namespaces/%s/", start
);
1417 git_buf_printf(path
, "refs/namespaces/%s/refs", end
);
1420 /* Make sure that the folder with the namespace exists */
1421 if (git_futils_mkdir_relative(git_buf_cstr(path
), repo
->path_repository
,
1422 0777, GIT_MKDIR_PATH
, NULL
) < 0)
1425 /* Return root of the namespaced path, i.e. without the trailing '/refs' */
1426 git_buf_rtruncate_at_char(path
, '/');
1430 static int reflog_alloc(git_reflog
**reflog
, const char *name
)
1436 log
= git__calloc(1, sizeof(git_reflog
));
1437 GITERR_CHECK_ALLOC(log
);
1439 log
->ref_name
= git__strdup(name
);
1440 GITERR_CHECK_ALLOC(log
->ref_name
);
1442 if (git_vector_init(&log
->entries
, 0, NULL
) < 0) {
1443 git__free(log
->ref_name
);
1453 static int reflog_parse(git_reflog
*log
, const char *buf
, size_t buf_size
)
1456 git_reflog_entry
*entry
;
1458 #define seek_forward(_increase) do { \
1459 if (_increase >= buf_size) { \
1460 giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
1464 buf_size -= _increase; \
1467 while (buf_size
> GIT_REFLOG_SIZE_MIN
) {
1468 entry
= git__calloc(1, sizeof(git_reflog_entry
));
1469 GITERR_CHECK_ALLOC(entry
);
1471 entry
->committer
= git__calloc(1, sizeof(git_signature
));
1472 GITERR_CHECK_ALLOC(entry
->committer
);
1474 if (git_oid_fromstrn(&entry
->oid_old
, buf
, GIT_OID_HEXSZ
) < 0)
1476 seek_forward(GIT_OID_HEXSZ
+ 1);
1478 if (git_oid_fromstrn(&entry
->oid_cur
, buf
, GIT_OID_HEXSZ
) < 0)
1480 seek_forward(GIT_OID_HEXSZ
+ 1);
1484 /* Seek forward to the end of the signature. */
1485 while (*buf
&& *buf
!= '\t' && *buf
!= '\n')
1488 if (git_signature__parse(entry
->committer
, &ptr
, buf
+ 1, NULL
, *buf
) < 0)
1492 /* We got a message. Read everything till we reach LF. */
1496 while (*buf
&& *buf
!= '\n')
1499 entry
->msg
= git__strndup(ptr
, buf
- ptr
);
1500 GITERR_CHECK_ALLOC(entry
->msg
);
1504 while (*buf
&& *buf
== '\n' && buf_size
> 1)
1507 if (git_vector_insert(&log
->entries
, entry
) < 0)
1516 git_reflog_entry__free(entry
);
1521 static int create_new_reflog_file(const char *filepath
)
1525 if ((error
= git_futils_mkpath2file(filepath
, GIT_REFLOG_DIR_MODE
)) < 0)
1528 if ((fd
= p_open(filepath
,
1530 GIT_REFLOG_FILE_MODE
)) < 0)
1536 GIT_INLINE(int) retrieve_reflog_path(git_buf
*path
, git_repository
*repo
, const char *name
)
1538 return git_buf_join3(path
, '/', repo
->path_repository
, GIT_REFLOG_DIR
, name
);
1541 static int refdb_reflog_fs__ensure_log(git_refdb_backend
*_backend
, const char *name
)
1543 refdb_fs_backend
*backend
;
1544 git_repository
*repo
;
1545 git_buf path
= GIT_BUF_INIT
;
1548 assert(_backend
&& name
);
1550 backend
= (refdb_fs_backend
*) _backend
;
1551 repo
= backend
->repo
;
1553 if ((error
= retrieve_reflog_path(&path
, repo
, name
)) < 0)
1556 error
= create_new_reflog_file(git_buf_cstr(&path
));
1557 git_buf_free(&path
);
1562 static int has_reflog(git_repository
*repo
, const char *name
)
1565 git_buf path
= GIT_BUF_INIT
;
1567 if (retrieve_reflog_path(&path
, repo
, name
) < 0)
1570 ret
= git_path_isfile(git_buf_cstr(&path
));
1573 git_buf_free(&path
);
1577 static int refdb_reflog_fs__has_log(git_refdb_backend
*_backend
, const char *name
)
1579 refdb_fs_backend
*backend
;
1581 assert(_backend
&& name
);
1583 backend
= (refdb_fs_backend
*) _backend
;
1585 return has_reflog(backend
->repo
, name
);
1588 static int refdb_reflog_fs__read(git_reflog
**out
, git_refdb_backend
*_backend
, const char *name
)
1591 git_buf log_path
= GIT_BUF_INIT
;
1592 git_buf log_file
= GIT_BUF_INIT
;
1593 git_reflog
*log
= NULL
;
1594 git_repository
*repo
;
1595 refdb_fs_backend
*backend
;
1597 assert(out
&& _backend
&& name
);
1599 backend
= (refdb_fs_backend
*) _backend
;
1600 repo
= backend
->repo
;
1602 if (reflog_alloc(&log
, name
) < 0)
1605 if (retrieve_reflog_path(&log_path
, repo
, name
) < 0)
1608 error
= git_futils_readbuffer(&log_file
, git_buf_cstr(&log_path
));
1609 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1612 if ((error
== GIT_ENOTFOUND
) &&
1613 ((error
= create_new_reflog_file(git_buf_cstr(&log_path
))) < 0))
1616 if ((error
= reflog_parse(log
,
1617 git_buf_cstr(&log_file
), git_buf_len(&log_file
))) < 0)
1624 git_reflog_free(log
);
1627 git_buf_free(&log_file
);
1628 git_buf_free(&log_path
);
1633 static int serialize_reflog_entry(
1635 const git_oid
*oid_old
,
1636 const git_oid
*oid_new
,
1637 const git_signature
*committer
,
1640 char raw_old
[GIT_OID_HEXSZ
+1];
1641 char raw_new
[GIT_OID_HEXSZ
+1];
1643 git_oid_tostr(raw_old
, GIT_OID_HEXSZ
+1, oid_old
);
1644 git_oid_tostr(raw_new
, GIT_OID_HEXSZ
+1, oid_new
);
1648 git_buf_puts(buf
, raw_old
);
1649 git_buf_putc(buf
, ' ');
1650 git_buf_puts(buf
, raw_new
);
1652 git_signature__writebuf(buf
, " ", committer
);
1654 /* drop trailing LF */
1658 git_buf_putc(buf
, '\t');
1659 git_buf_puts(buf
, msg
);
1662 git_buf_putc(buf
, '\n');
1664 return git_buf_oom(buf
);
1667 static int lock_reflog(git_filebuf
*file
, refdb_fs_backend
*backend
, const char *refname
)
1669 git_repository
*repo
;
1670 git_buf log_path
= GIT_BUF_INIT
;
1673 repo
= backend
->repo
;
1675 if (!git_path_isvalid(backend
->repo
, refname
, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS
)) {
1676 giterr_set(GITERR_INVALID
, "Invalid reference name '%s'.", refname
);
1677 return GIT_EINVALIDSPEC
;
1680 if (retrieve_reflog_path(&log_path
, repo
, refname
) < 0)
1683 if (!git_path_isfile(git_buf_cstr(&log_path
))) {
1684 giterr_set(GITERR_INVALID
,
1685 "Log file for reference '%s' doesn't exist.", refname
);
1690 error
= git_filebuf_open(file
, git_buf_cstr(&log_path
), 0, GIT_REFLOG_FILE_MODE
);
1693 git_buf_free(&log_path
);
1698 static int refdb_reflog_fs__write(git_refdb_backend
*_backend
, git_reflog
*reflog
)
1702 git_reflog_entry
*entry
;
1703 refdb_fs_backend
*backend
;
1704 git_buf log
= GIT_BUF_INIT
;
1705 git_filebuf fbuf
= GIT_FILEBUF_INIT
;
1707 assert(_backend
&& reflog
);
1709 backend
= (refdb_fs_backend
*) _backend
;
1711 if ((error
= lock_reflog(&fbuf
, backend
, reflog
->ref_name
)) < 0)
1714 git_vector_foreach(&reflog
->entries
, i
, entry
) {
1715 if (serialize_reflog_entry(&log
, &(entry
->oid_old
), &(entry
->oid_cur
), entry
->committer
, entry
->msg
) < 0)
1718 if ((error
= git_filebuf_write(&fbuf
, log
.ptr
, log
.size
)) < 0)
1722 error
= git_filebuf_commit(&fbuf
);
1726 git_filebuf_cleanup(&fbuf
);
1734 /* Append to the reflog, must be called under reference lock */
1735 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
)
1737 int error
, is_symbolic
;
1738 git_oid old_id
= {{0}}, new_id
= {{0}};
1739 git_buf buf
= GIT_BUF_INIT
, path
= GIT_BUF_INIT
;
1740 git_repository
*repo
= backend
->repo
;
1742 is_symbolic
= ref
->type
== GIT_REF_SYMBOLIC
;
1744 /* "normal" symbolic updates do not write */
1746 strcmp(ref
->name
, GIT_HEAD_FILE
) &&
1750 /* From here on is_symoblic also means that it's HEAD */
1753 git_oid_cpy(&old_id
, old
);
1755 error
= git_reference_name_to_id(&old_id
, repo
, ref
->name
);
1756 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1761 git_oid_cpy(&new_id
, new);
1764 git_oid_cpy(&new_id
, git_reference_target(ref
));
1766 error
= git_reference_name_to_id(&new_id
, repo
, git_reference_symbolic_target(ref
));
1767 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1769 /* detaching HEAD does not create an entry */
1770 if (error
== GIT_ENOTFOUND
)
1777 if ((error
= serialize_reflog_entry(&buf
, &old_id
, &new_id
, who
, message
)) < 0)
1780 if ((error
= retrieve_reflog_path(&path
, repo
, ref
->name
)) < 0)
1783 if (((error
= git_futils_mkpath2file(git_buf_cstr(&path
), 0777)) < 0) &&
1784 (error
!= GIT_EEXISTS
)) {
1788 /* If the new branch matches part of the namespace of a previously deleted branch,
1789 * there maybe an obsolete/unused directory (or directory hierarchy) in the way.
1791 if (git_path_isdir(git_buf_cstr(&path
))) {
1792 if ((git_futils_rmdir_r(git_buf_cstr(&path
), NULL
, GIT_RMDIR_SKIP_NONEMPTY
) < 0))
1794 else if (git_path_isdir(git_buf_cstr(&path
))) {
1795 giterr_set(GITERR_REFERENCE
, "cannot create reflog at '%s', there are reflogs beneath that folder",
1797 error
= GIT_EDIRECTORY
;
1804 error
= git_futils_writebuffer(&buf
, git_buf_cstr(&path
), O_WRONLY
|O_CREAT
|O_APPEND
, GIT_REFLOG_FILE_MODE
);
1808 git_buf_free(&path
);
1813 static int refdb_reflog_fs__rename(git_refdb_backend
*_backend
, const char *old_name
, const char *new_name
)
1816 git_buf old_path
= GIT_BUF_INIT
;
1817 git_buf new_path
= GIT_BUF_INIT
;
1818 git_buf temp_path
= GIT_BUF_INIT
;
1819 git_buf normalized
= GIT_BUF_INIT
;
1820 git_repository
*repo
;
1821 refdb_fs_backend
*backend
;
1823 assert(_backend
&& old_name
&& new_name
);
1825 backend
= (refdb_fs_backend
*) _backend
;
1826 repo
= backend
->repo
;
1828 if ((error
= git_reference__normalize_name(
1829 &normalized
, new_name
, GIT_REF_FORMAT_ALLOW_ONELEVEL
)) < 0)
1832 if (git_buf_joinpath(&temp_path
, repo
->path_repository
, GIT_REFLOG_DIR
) < 0)
1835 if (git_buf_joinpath(&old_path
, git_buf_cstr(&temp_path
), old_name
) < 0)
1838 if (git_buf_joinpath(&new_path
, git_buf_cstr(&temp_path
), git_buf_cstr(&normalized
)) < 0)
1841 if (!git_path_exists(git_buf_cstr(&old_path
))) {
1842 error
= GIT_ENOTFOUND
;
1847 * Move the reflog to a temporary place. This two-phase renaming is required
1848 * in order to cope with funny renaming use cases when one tries to move a reference
1849 * to a partially colliding namespace:
1851 * - a/b/c/d -> a/b/c
1853 if (git_buf_joinpath(&temp_path
, git_buf_cstr(&temp_path
), "temp_reflog") < 0)
1856 if ((fd
= git_futils_mktmp(&temp_path
, git_buf_cstr(&temp_path
), GIT_REFLOG_FILE_MODE
)) < 0) {
1863 if (p_rename(git_buf_cstr(&old_path
), git_buf_cstr(&temp_path
)) < 0) {
1864 giterr_set(GITERR_OS
, "Failed to rename reflog for %s", new_name
);
1869 if (git_path_isdir(git_buf_cstr(&new_path
)) &&
1870 (git_futils_rmdir_r(git_buf_cstr(&new_path
), NULL
, GIT_RMDIR_SKIP_NONEMPTY
) < 0)) {
1875 if (git_futils_mkpath2file(git_buf_cstr(&new_path
), GIT_REFLOG_DIR_MODE
) < 0) {
1880 if (p_rename(git_buf_cstr(&temp_path
), git_buf_cstr(&new_path
)) < 0) {
1881 giterr_set(GITERR_OS
, "Failed to rename reflog for %s", new_name
);
1886 git_buf_free(&temp_path
);
1887 git_buf_free(&old_path
);
1888 git_buf_free(&new_path
);
1889 git_buf_free(&normalized
);
1894 static int refdb_reflog_fs__delete(git_refdb_backend
*_backend
, const char *name
)
1897 git_buf path
= GIT_BUF_INIT
;
1899 git_repository
*repo
;
1900 refdb_fs_backend
*backend
;
1902 assert(_backend
&& name
);
1904 backend
= (refdb_fs_backend
*) _backend
;
1905 repo
= backend
->repo
;
1907 error
= retrieve_reflog_path(&path
, repo
, name
);
1909 if (!error
&& git_path_exists(path
.ptr
))
1910 error
= p_unlink(path
.ptr
);
1912 git_buf_free(&path
);
1918 int git_refdb_backend_fs(
1919 git_refdb_backend
**backend_out
,
1920 git_repository
*repository
)
1923 git_buf path
= GIT_BUF_INIT
;
1924 refdb_fs_backend
*backend
;
1926 backend
= git__calloc(1, sizeof(refdb_fs_backend
));
1927 GITERR_CHECK_ALLOC(backend
);
1929 backend
->repo
= repository
;
1931 if (setup_namespace(&path
, repository
) < 0)
1934 backend
->path
= git_buf_detach(&path
);
1936 if (git_buf_joinpath(&path
, backend
->path
, GIT_PACKEDREFS_FILE
) < 0 ||
1937 git_sortedcache_new(
1938 &backend
->refcache
, offsetof(struct packref
, name
),
1939 NULL
, NULL
, packref_cmp
, git_buf_cstr(&path
)) < 0)
1942 git_buf_free(&path
);
1944 if (!git_repository__cvar(&t
, backend
->repo
, GIT_CVAR_IGNORECASE
) && t
) {
1945 backend
->iterator_flags
|= GIT_ITERATOR_IGNORE_CASE
;
1946 backend
->direach_flags
|= GIT_PATH_DIR_IGNORE_CASE
;
1948 if (!git_repository__cvar(&t
, backend
->repo
, GIT_CVAR_PRECOMPOSE
) && t
) {
1949 backend
->iterator_flags
|= GIT_ITERATOR_PRECOMPOSE_UNICODE
;
1950 backend
->direach_flags
|= GIT_PATH_DIR_PRECOMPOSE_UNICODE
;
1953 backend
->parent
.exists
= &refdb_fs_backend__exists
;
1954 backend
->parent
.lookup
= &refdb_fs_backend__lookup
;
1955 backend
->parent
.iterator
= &refdb_fs_backend__iterator
;
1956 backend
->parent
.write
= &refdb_fs_backend__write
;
1957 backend
->parent
.del
= &refdb_fs_backend__delete
;
1958 backend
->parent
.rename
= &refdb_fs_backend__rename
;
1959 backend
->parent
.compress
= &refdb_fs_backend__compress
;
1960 backend
->parent
.lock
= &refdb_fs_backend__lock
;
1961 backend
->parent
.unlock
= &refdb_fs_backend__unlock
;
1962 backend
->parent
.has_log
= &refdb_reflog_fs__has_log
;
1963 backend
->parent
.ensure_log
= &refdb_reflog_fs__ensure_log
;
1964 backend
->parent
.free
= &refdb_fs_backend__free
;
1965 backend
->parent
.reflog_read
= &refdb_reflog_fs__read
;
1966 backend
->parent
.reflog_write
= &refdb_reflog_fs__write
;
1967 backend
->parent
.reflog_rename
= &refdb_reflog_fs__rename
;
1968 backend
->parent
.reflog_delete
= &refdb_reflog_fs__delete
;
1970 *backend_out
= (git_refdb_backend
*)backend
;
1974 git_buf_free(&path
);
1975 git__free(backend
->path
);