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
;
333 if ((error
= packed_reload(backend
)) < 0 ||
334 (error
= git_buf_joinpath(&ref_path
, backend
->path
, ref_name
)) < 0)
337 *exists
= git_path_isfile(ref_path
.ptr
) ||
338 (git_sortedcache_lookup(backend
->refcache
, ref_name
) != NULL
);
340 git_buf_free(&ref_path
);
344 static const char *loose_parse_symbolic(git_buf
*file_content
)
346 const unsigned int header_len
= (unsigned int)strlen(GIT_SYMREF
);
347 const char *refname_start
;
349 refname_start
= (const char *)file_content
->ptr
;
351 if (git_buf_len(file_content
) < header_len
+ 1) {
352 giterr_set(GITERR_REFERENCE
, "Corrupted loose reference file");
357 * Assume we have already checked for the header
358 * before calling this function
360 refname_start
+= header_len
;
362 return refname_start
;
365 static int loose_lookup(
367 refdb_fs_backend
*backend
,
368 const char *ref_name
)
370 git_buf ref_file
= GIT_BUF_INIT
;
376 if ((error
= loose_readbuffer(&ref_file
, backend
->path
, ref_name
)) < 0)
377 /* cannot read loose ref file - gah */;
378 else if (git__prefixcmp(git_buf_cstr(&ref_file
), GIT_SYMREF
) == 0) {
381 git_buf_rtrim(&ref_file
);
383 if (!(target
= loose_parse_symbolic(&ref_file
)))
385 else if (out
!= NULL
)
386 *out
= git_reference__alloc_symbolic(ref_name
, target
);
390 if (!(error
= loose_parse_oid(&oid
, ref_name
, &ref_file
)) &&
392 *out
= git_reference__alloc(ref_name
, &oid
, NULL
);
395 git_buf_free(&ref_file
);
399 static int ref_error_notfound(const char *name
)
401 giterr_set(GITERR_REFERENCE
, "Reference '%s' not found", name
);
402 return GIT_ENOTFOUND
;
405 static int packed_lookup(
407 refdb_fs_backend
*backend
,
408 const char *ref_name
)
411 struct packref
*entry
;
413 if ((error
= packed_reload(backend
)) < 0)
416 if (git_sortedcache_rlock(backend
->refcache
) < 0)
419 entry
= git_sortedcache_lookup(backend
->refcache
, ref_name
);
421 error
= ref_error_notfound(ref_name
);
423 *out
= git_reference__alloc(ref_name
, &entry
->oid
, &entry
->peel
);
428 git_sortedcache_runlock(backend
->refcache
);
433 static int refdb_fs_backend__lookup(
435 git_refdb_backend
*_backend
,
436 const char *ref_name
)
438 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
443 if (!(error
= loose_lookup(out
, backend
, ref_name
)))
446 /* only try to lookup this reference on the packfile if it
447 * wasn't found on the loose refs; not if there was a critical error */
448 if (error
== GIT_ENOTFOUND
) {
450 error
= packed_lookup(out
, backend
, ref_name
);
457 git_reference_iterator parent
;
464 git_sortedcache
*cache
;
469 static void refdb_fs_backend__iterator_free(git_reference_iterator
*_iter
)
471 refdb_fs_iter
*iter
= (refdb_fs_iter
*) _iter
;
473 git_vector_free(&iter
->loose
);
474 git_pool_clear(&iter
->pool
);
475 git_sortedcache_free(iter
->cache
);
479 static int iter_load_loose_paths(refdb_fs_backend
*backend
, refdb_fs_iter
*iter
)
482 git_buf path
= GIT_BUF_INIT
;
483 git_iterator
*fsit
= NULL
;
484 git_iterator_options fsit_opts
= GIT_ITERATOR_OPTIONS_INIT
;
485 const git_index_entry
*entry
= NULL
;
487 if (!backend
->path
) /* do nothing if no path for loose refs */
490 fsit_opts
.flags
= backend
->iterator_flags
;
492 if ((error
= git_buf_printf(&path
, "%s/refs", backend
->path
)) < 0 ||
493 (error
= git_iterator_for_filesystem(&fsit
, path
.ptr
, &fsit_opts
)) < 0) {
498 error
= git_buf_sets(&path
, GIT_REFS_DIR
);
500 while (!error
&& !git_iterator_advance(&entry
, fsit
)) {
501 const char *ref_name
;
505 git_buf_truncate(&path
, strlen(GIT_REFS_DIR
));
506 git_buf_puts(&path
, entry
->path
);
507 ref_name
= git_buf_cstr(&path
);
509 if (git__suffixcmp(ref_name
, ".lock") == 0 ||
510 (iter
->glob
&& p_fnmatch(iter
->glob
, ref_name
, 0) != 0))
513 git_sortedcache_rlock(backend
->refcache
);
514 ref
= git_sortedcache_lookup(backend
->refcache
, ref_name
);
516 ref
->flags
|= PACKREF_SHADOWED
;
517 git_sortedcache_runlock(backend
->refcache
);
519 ref_dup
= git_pool_strdup(&iter
->pool
, ref_name
);
523 error
= git_vector_insert(&iter
->loose
, ref_dup
);
526 git_iterator_free(fsit
);
532 static int refdb_fs_backend__iterator_next(
533 git_reference
**out
, git_reference_iterator
*_iter
)
535 int error
= GIT_ITEROVER
;
536 refdb_fs_iter
*iter
= (refdb_fs_iter
*)_iter
;
537 refdb_fs_backend
*backend
= (refdb_fs_backend
*)iter
->parent
.db
->backend
;
540 while (iter
->loose_pos
< iter
->loose
.length
) {
541 const char *path
= git_vector_get(&iter
->loose
, iter
->loose_pos
++);
543 if (loose_lookup(out
, backend
, path
) == 0)
550 if ((error
= git_sortedcache_copy(&iter
->cache
, backend
->refcache
, 1, NULL
, NULL
)) < 0)
554 error
= GIT_ITEROVER
;
555 while (iter
->packed_pos
< git_sortedcache_entrycount(iter
->cache
)) {
556 ref
= git_sortedcache_entry(iter
->cache
, iter
->packed_pos
++);
557 if (!ref
) /* stop now if another thread deleted refs and we past end */
560 if (ref
->flags
& PACKREF_SHADOWED
)
562 if (iter
->glob
&& p_fnmatch(iter
->glob
, ref
->name
, 0) != 0)
565 *out
= git_reference__alloc(ref
->name
, &ref
->oid
, &ref
->peel
);
566 error
= (*out
!= NULL
) ? 0 : -1;
573 static int refdb_fs_backend__iterator_next_name(
574 const char **out
, git_reference_iterator
*_iter
)
576 int error
= GIT_ITEROVER
;
577 refdb_fs_iter
*iter
= (refdb_fs_iter
*)_iter
;
578 refdb_fs_backend
*backend
= (refdb_fs_backend
*)iter
->parent
.db
->backend
;
581 while (iter
->loose_pos
< iter
->loose
.length
) {
582 const char *path
= git_vector_get(&iter
->loose
, iter
->loose_pos
++);
584 if (loose_lookup(NULL
, backend
, path
) == 0) {
593 if ((error
= git_sortedcache_copy(&iter
->cache
, backend
->refcache
, 1, NULL
, NULL
)) < 0)
597 error
= GIT_ITEROVER
;
598 while (iter
->packed_pos
< git_sortedcache_entrycount(iter
->cache
)) {
599 ref
= git_sortedcache_entry(iter
->cache
, iter
->packed_pos
++);
600 if (!ref
) /* stop now if another thread deleted refs and we past end */
603 if (ref
->flags
& PACKREF_SHADOWED
)
605 if (iter
->glob
&& p_fnmatch(iter
->glob
, ref
->name
, 0) != 0)
616 static int refdb_fs_backend__iterator(
617 git_reference_iterator
**out
, git_refdb_backend
*_backend
, const char *glob
)
621 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
625 if ((error
= packed_reload(backend
)) < 0)
628 iter
= git__calloc(1, sizeof(refdb_fs_iter
));
629 GITERR_CHECK_ALLOC(iter
);
631 git_pool_init(&iter
->pool
, 1);
633 if (git_vector_init(&iter
->loose
, 8, NULL
) < 0)
637 (iter
->glob
= git_pool_strdup(&iter
->pool
, glob
)) == NULL
)
640 iter
->parent
.next
= refdb_fs_backend__iterator_next
;
641 iter
->parent
.next_name
= refdb_fs_backend__iterator_next_name
;
642 iter
->parent
.free
= refdb_fs_backend__iterator_free
;
644 if (iter_load_loose_paths(backend
, iter
) < 0)
647 *out
= (git_reference_iterator
*)iter
;
651 refdb_fs_backend__iterator_free((git_reference_iterator
*)iter
);
655 static bool ref_is_available(
656 const char *old_ref
, const char *new_ref
, const char *this_ref
)
658 if (old_ref
== NULL
|| strcmp(old_ref
, this_ref
)) {
659 size_t reflen
= strlen(this_ref
);
660 size_t newlen
= strlen(new_ref
);
661 size_t cmplen
= reflen
< newlen
? reflen
: newlen
;
662 const char *lead
= reflen
< newlen
? new_ref
: this_ref
;
664 if (!strncmp(new_ref
, this_ref
, cmplen
) && lead
[cmplen
] == '/') {
672 static int reference_path_available(
673 refdb_fs_backend
*backend
,
681 if ((error
= packed_reload(backend
)) < 0)
687 if ((error
= refdb_fs_backend__exists(
688 &exists
, (git_refdb_backend
*)backend
, new_ref
)) < 0) {
693 giterr_set(GITERR_REFERENCE
,
694 "Failed to write reference '%s': a reference with "
695 "that name already exists.", new_ref
);
700 git_sortedcache_rlock(backend
->refcache
);
702 for (i
= 0; i
< git_sortedcache_entrycount(backend
->refcache
); ++i
) {
703 struct packref
*ref
= git_sortedcache_entry(backend
->refcache
, i
);
705 if (ref
&& !ref_is_available(old_ref
, new_ref
, ref
->name
)) {
706 git_sortedcache_runlock(backend
->refcache
);
707 giterr_set(GITERR_REFERENCE
,
708 "Path to reference '%s' collides with existing one", new_ref
);
713 git_sortedcache_runlock(backend
->refcache
);
717 static int loose_lock(git_filebuf
*file
, refdb_fs_backend
*backend
, const char *name
)
720 git_buf ref_path
= GIT_BUF_INIT
;
722 assert(file
&& backend
&& name
);
724 if (!git_path_isvalid(backend
->repo
, name
, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS
)) {
725 giterr_set(GITERR_INVALID
, "Invalid reference name '%s'.", name
);
726 return GIT_EINVALIDSPEC
;
729 /* Remove a possibly existing empty directory hierarchy
730 * which name would collide with the reference name
732 if ((error
= git_futils_rmdir_r(name
, backend
->path
, GIT_RMDIR_SKIP_NONEMPTY
)) < 0)
735 if (git_buf_joinpath(&ref_path
, backend
->path
, name
) < 0)
738 error
= git_filebuf_open(file
, ref_path
.ptr
, GIT_FILEBUF_FORCE
, GIT_REFS_FILE_MODE
);
740 if (error
== GIT_EDIRECTORY
)
741 giterr_set(GITERR_REFERENCE
, "cannot lock ref '%s', there are refs beneath that folder", name
);
743 git_buf_free(&ref_path
);
747 static int loose_commit(git_filebuf
*file
, const git_reference
*ref
)
751 if (ref
->type
== GIT_REF_OID
) {
752 char oid
[GIT_OID_HEXSZ
+ 1];
753 git_oid_nfmt(oid
, sizeof(oid
), &ref
->target
.oid
);
755 git_filebuf_printf(file
, "%s\n", oid
);
756 } else if (ref
->type
== GIT_REF_SYMBOLIC
) {
757 git_filebuf_printf(file
, GIT_SYMREF
"%s\n", ref
->target
.symbolic
);
759 assert(0); /* don't let this happen */
762 return git_filebuf_commit(file
);
765 static int refdb_fs_backend__lock(void **out
, git_refdb_backend
*_backend
, const char *refname
)
769 refdb_fs_backend
*backend
= (refdb_fs_backend
*) _backend
;
771 lock
= git__calloc(1, sizeof(git_filebuf
));
772 GITERR_CHECK_ALLOC(lock
);
774 if ((error
= loose_lock(lock
, backend
, refname
)) < 0) {
783 static int refdb_fs_backend__write_tail(
784 git_refdb_backend
*_backend
,
785 const git_reference
*ref
,
788 const git_signature
*who
,
790 const git_oid
*old_id
,
791 const char *old_target
);
793 static int refdb_fs_backend__delete_tail(
794 git_refdb_backend
*_backend
,
796 const char *ref_name
,
797 const git_oid
*old_id
, const char *old_target
);
799 static int refdb_fs_backend__unlock(git_refdb_backend
*backend
, void *payload
, int success
, int update_reflog
,
800 const git_reference
*ref
, const git_signature
*sig
, const char *message
)
802 git_filebuf
*lock
= (git_filebuf
*) payload
;
806 error
= refdb_fs_backend__delete_tail(backend
, lock
, ref
->name
, NULL
, NULL
);
808 error
= refdb_fs_backend__write_tail(backend
, ref
, lock
, update_reflog
, sig
, message
, NULL
, NULL
);
810 git_filebuf_cleanup(lock
);
817 * Find out what object this reference resolves to.
819 * For references that point to a 'big' tag (e.g. an
820 * actual tag object on the repository), we need to
821 * cache on the packfile the OID of the object to
822 * which that 'big tag' is pointing to.
824 static int packed_find_peel(refdb_fs_backend
*backend
, struct packref
*ref
)
828 if (ref
->flags
& PACKREF_HAS_PEEL
|| ref
->flags
& PACKREF_CANNOT_PEEL
)
832 * Find the tagged object in the repository
834 if (git_object_lookup(&object
, backend
->repo
, &ref
->oid
, GIT_OBJ_ANY
) < 0)
838 * If the tagged object is a Tag object, we need to resolve it;
839 * if the ref is actually a 'weak' ref, we don't need to resolve
842 if (git_object_type(object
) == GIT_OBJ_TAG
) {
843 git_tag
*tag
= (git_tag
*)object
;
846 * Find the object pointed at by this tag
848 git_oid_cpy(&ref
->peel
, git_tag_target_id(tag
));
849 ref
->flags
|= PACKREF_HAS_PEEL
;
852 * The reference has now cached the resolved OID, and is
853 * marked at such. When written to the packfile, it'll be
854 * accompanied by this resolved oid
858 git_object_free(object
);
863 * Write a single reference into a packfile
865 static int packed_write_ref(struct packref
*ref
, git_filebuf
*file
)
867 char oid
[GIT_OID_HEXSZ
+ 1];
868 git_oid_nfmt(oid
, sizeof(oid
), &ref
->oid
);
871 * For references that peel to an object in the repo, we must
872 * write the resulting peel on a separate line, e.g.
874 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
875 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
877 * This obviously only applies to tags.
878 * The required peels have already been loaded into `ref->peel_target`.
880 if (ref
->flags
& PACKREF_HAS_PEEL
) {
881 char peel
[GIT_OID_HEXSZ
+ 1];
882 git_oid_nfmt(peel
, sizeof(peel
), &ref
->peel
);
884 if (git_filebuf_printf(file
, "%s %s\n^%s\n", oid
, ref
->name
, peel
) < 0)
887 if (git_filebuf_printf(file
, "%s %s\n", oid
, ref
->name
) < 0)
895 * Remove all loose references
897 * Once we have successfully written a packfile,
898 * all the loose references that were packed must be
901 * This is a dangerous method; make sure the packfile
902 * is well-written, because we are destructing references
905 static int packed_remove_loose(refdb_fs_backend
*backend
)
908 git_filebuf lock
= GIT_FILEBUF_INIT
;
909 git_buf ref_content
= GIT_BUF_INIT
;
912 /* backend->refcache is already locked when this is called */
914 for (i
= 0; i
< git_sortedcache_entrycount(backend
->refcache
); ++i
) {
915 struct packref
*ref
= git_sortedcache_entry(backend
->refcache
, i
);
918 if (!ref
|| !(ref
->flags
& PACKREF_WAS_LOOSE
))
921 git_filebuf_cleanup(&lock
);
923 /* We need to stop anybody from updating the ref while we try to do a safe delete */
924 error
= loose_lock(&lock
, backend
, ref
->name
);
925 /* If someone else is updating it, let them do it */
926 if (error
== GIT_EEXISTS
|| error
== GIT_ENOTFOUND
)
930 git_buf_free(&ref_content
);
931 giterr_set(GITERR_REFERENCE
, "failed to lock loose reference '%s'", ref
->name
);
935 error
= git_futils_readbuffer(&ref_content
, lock
.path_original
);
936 /* Someone else beat us to cleaning up the ref, let's simply continue */
937 if (error
== GIT_ENOTFOUND
)
940 /* This became a symref between us packing and trying to delete it, so ignore it */
941 if (!git__prefixcmp(ref_content
.ptr
, GIT_SYMREF
))
944 /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */
945 if (loose_parse_oid(¤t_id
, lock
.path_original
, &ref_content
) < 0)
948 /* If the ref moved since we packed it, we must not delete it */
949 if (!git_oid_equal(¤t_id
, &ref
->oid
))
953 * if we fail to remove a single file, this is *not* good,
954 * but we should keep going and remove as many as possible.
955 * If we fail to remove, the ref is still in the old state, so
956 * we haven't lost information.
958 p_unlink(lock
.path_original
);
961 git_buf_free(&ref_content
);
962 git_filebuf_cleanup(&lock
);
967 * Write all the contents in the in-memory packfile to disk.
969 static int packed_write(refdb_fs_backend
*backend
)
971 git_sortedcache
*refcache
= backend
->refcache
;
972 git_filebuf pack_file
= GIT_FILEBUF_INIT
;
976 /* lock the cache to updates while we do this */
977 if ((error
= git_sortedcache_wlock(refcache
)) < 0)
981 if ((error
= git_filebuf_open(&pack_file
, git_sortedcache_path(refcache
), 0, GIT_PACKEDREFS_FILE_MODE
)) < 0)
984 /* Packfiles have a header... apparently
985 * This is in fact not required, but we might as well print it
987 if ((error
= git_filebuf_printf(&pack_file
, "%s\n", GIT_PACKEDREFS_HEADER
)) < 0)
990 for (i
= 0; i
< git_sortedcache_entrycount(refcache
); ++i
) {
991 struct packref
*ref
= git_sortedcache_entry(refcache
, i
);
994 if ((error
= packed_find_peel(backend
, ref
)) < 0)
997 if ((error
= packed_write_ref(ref
, &pack_file
)) < 0)
1001 /* if we've written all the references properly, we can commit
1002 * the packfile to make the changes effective */
1003 if ((error
= git_filebuf_commit(&pack_file
)) < 0)
1006 /* when and only when the packfile has been properly written,
1007 * we can go ahead and remove the loose refs */
1008 if ((error
= packed_remove_loose(backend
)) < 0)
1011 git_sortedcache_updated(refcache
);
1012 git_sortedcache_wunlock(refcache
);
1014 /* we're good now */
1018 git_filebuf_cleanup(&pack_file
);
1019 git_sortedcache_wunlock(refcache
);
1024 static int reflog_append(refdb_fs_backend
*backend
, const git_reference
*ref
, const git_oid
*old
, const git_oid
*new, const git_signature
*author
, const char *message
);
1025 static int has_reflog(git_repository
*repo
, const char *name
);
1027 /* We only write if it's under heads/, remotes/ or notes/ or if it already has a log */
1028 static int should_write_reflog(int *write
, git_repository
*repo
, const char *name
)
1032 error
= git_repository__cvar(&logall
, repo
, GIT_CVAR_LOGALLREFUPDATES
);
1036 /* Defaults to the opposite of the repo being bare */
1037 if (logall
== GIT_LOGALLREFUPDATES_UNSET
)
1038 logall
= !git_repository_is_bare(repo
);
1042 } else if (has_reflog(repo
, name
)) {
1044 } else if (!git__prefixcmp(name
, GIT_REFS_HEADS_DIR
) ||
1045 !git__strcmp(name
, GIT_HEAD_FILE
) ||
1046 !git__prefixcmp(name
, GIT_REFS_REMOTES_DIR
) ||
1047 !git__prefixcmp(name
, GIT_REFS_NOTES_DIR
)) {
1056 static int cmp_old_ref(int *cmp
, git_refdb_backend
*backend
, const char *name
,
1057 const git_oid
*old_id
, const char *old_target
)
1060 git_reference
*old_ref
= NULL
;
1063 /* It "matches" if there is no old value to compare against */
1064 if (!old_id
&& !old_target
)
1067 if ((error
= refdb_fs_backend__lookup(&old_ref
, backend
, name
)) < 0)
1070 /* If the types don't match, there's no way the values do */
1071 if (old_id
&& old_ref
->type
!= GIT_REF_OID
) {
1075 if (old_target
&& old_ref
->type
!= GIT_REF_SYMBOLIC
) {
1080 if (old_id
&& old_ref
->type
== GIT_REF_OID
)
1081 *cmp
= git_oid_cmp(old_id
, &old_ref
->target
.oid
);
1083 if (old_target
&& old_ref
->type
== GIT_REF_SYMBOLIC
)
1084 *cmp
= git__strcmp(old_target
, old_ref
->target
.symbolic
);
1087 git_reference_free(old_ref
);
1093 * The git.git comment regarding this, for your viewing pleasure:
1095 * Special hack: If a branch is updated directly and HEAD
1096 * points to it (may happen on the remote side of a push
1097 * for example) then logically the HEAD reflog should be
1099 * A generic solution implies reverse symref information,
1100 * but finding all symrefs pointing to the given branch
1101 * would be rather costly for this rare event (the direct
1102 * update of a branch) to be worth it. So let's cheat and
1103 * check with HEAD only which should cover 99% of all usage
1104 * scenarios (even 100% of the default ones).
1106 static int maybe_append_head(refdb_fs_backend
*backend
, const git_reference
*ref
, const git_signature
*who
, const char *message
)
1109 git_oid old_id
= {{0}};
1110 git_reference
*tmp
= NULL
, *head
= NULL
, *peeled
= NULL
;
1113 if (ref
->type
== GIT_REF_SYMBOLIC
)
1116 /* if we can't resolve, we use {0}*40 as old id */
1117 git_reference_name_to_id(&old_id
, backend
->repo
, ref
->name
);
1119 if ((error
= git_reference_lookup(&head
, backend
->repo
, GIT_HEAD_FILE
)) < 0)
1122 if (git_reference_type(head
) == GIT_REF_OID
)
1125 if ((error
= git_reference_lookup(&tmp
, backend
->repo
, GIT_HEAD_FILE
)) < 0)
1128 /* Go down the symref chain until we find the branch */
1129 while (git_reference_type(tmp
) == GIT_REF_SYMBOLIC
) {
1130 error
= git_reference_lookup(&peeled
, backend
->repo
, git_reference_symbolic_target(tmp
));
1134 git_reference_free(tmp
);
1138 if (error
== GIT_ENOTFOUND
) {
1140 name
= git_reference_symbolic_target(tmp
);
1141 } else if (error
< 0) {
1144 name
= git_reference_name(tmp
);
1147 if (strcmp(name
, ref
->name
))
1150 error
= reflog_append(backend
, head
, &old_id
, git_reference_target(ref
), who
, message
);
1153 git_reference_free(tmp
);
1154 git_reference_free(head
);
1158 static int refdb_fs_backend__write(
1159 git_refdb_backend
*_backend
,
1160 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 git_filebuf file
= GIT_FILEBUF_INIT
;
1173 if ((error
= reference_path_available(backend
, ref
->name
, NULL
, force
)) < 0)
1176 /* We need to perform the reflog append and old value check under the ref's lock */
1177 if ((error
= loose_lock(&file
, backend
, ref
->name
)) < 0)
1180 return refdb_fs_backend__write_tail(_backend
, ref
, &file
, true, who
, message
, old_id
, old_target
);
1183 static int refdb_fs_backend__write_tail(
1184 git_refdb_backend
*_backend
,
1185 const git_reference
*ref
,
1188 const git_signature
*who
,
1189 const char *message
,
1190 const git_oid
*old_id
,
1191 const char *old_target
)
1193 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1194 int error
= 0, cmp
= 0, should_write
;
1195 const char *new_target
= NULL
;
1196 const git_oid
*new_id
= NULL
;
1198 if ((error
= cmp_old_ref(&cmp
, _backend
, ref
->name
, old_id
, old_target
)) < 0)
1202 giterr_set(GITERR_REFERENCE
, "old reference value does not match");
1203 error
= GIT_EMODIFIED
;
1207 if (ref
->type
== GIT_REF_SYMBOLIC
)
1208 new_target
= ref
->target
.symbolic
;
1210 new_id
= &ref
->target
.oid
;
1212 error
= cmp_old_ref(&cmp
, _backend
, ref
->name
, new_id
, new_target
);
1213 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1216 /* Don't update if we have the same value */
1217 if (!error
&& !cmp
) {
1219 goto on_error
; /* not really error */
1222 if (update_reflog
) {
1223 if ((error
= should_write_reflog(&should_write
, backend
->repo
, ref
->name
)) < 0)
1227 if ((error
= reflog_append(backend
, ref
, NULL
, NULL
, who
, message
)) < 0)
1229 if ((error
= maybe_append_head(backend
, ref
, who
, message
)) < 0)
1234 return loose_commit(file
, ref
);
1237 git_filebuf_cleanup(file
);
1241 static int refdb_fs_backend__delete(
1242 git_refdb_backend
*_backend
,
1243 const char *ref_name
,
1244 const git_oid
*old_id
, const char *old_target
)
1246 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1247 git_filebuf file
= GIT_FILEBUF_INIT
;
1250 assert(backend
&& ref_name
);
1252 if ((error
= loose_lock(&file
, backend
, ref_name
)) < 0)
1255 if ((error
= refdb_reflog_fs__delete(_backend
, ref_name
)) < 0) {
1256 git_filebuf_cleanup(&file
);
1260 return refdb_fs_backend__delete_tail(_backend
, &file
, ref_name
, old_id
, old_target
);
1263 static int refdb_fs_backend__delete_tail(
1264 git_refdb_backend
*_backend
,
1266 const char *ref_name
,
1267 const git_oid
*old_id
, const char *old_target
)
1269 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1270 git_buf loose_path
= GIT_BUF_INIT
;
1272 int error
= 0, cmp
= 0;
1273 bool loose_deleted
= 0;
1275 error
= cmp_old_ref(&cmp
, _backend
, ref_name
, old_id
, old_target
);
1280 giterr_set(GITERR_REFERENCE
, "old reference value does not match");
1281 error
= GIT_EMODIFIED
;
1285 /* If a loose reference exists, remove it from the filesystem */
1286 if (git_buf_joinpath(&loose_path
, backend
->path
, ref_name
) < 0)
1290 error
= p_unlink(loose_path
.ptr
);
1291 if (error
< 0 && errno
== ENOENT
)
1295 else if (error
== 0)
1298 if ((error
= packed_reload(backend
)) < 0)
1301 /* If a packed reference exists, remove it from the packfile and repack */
1302 if ((error
= git_sortedcache_wlock(backend
->refcache
)) < 0)
1305 if (!(error
= git_sortedcache_lookup_index(
1306 &pack_pos
, backend
->refcache
, ref_name
)))
1307 error
= git_sortedcache_remove(backend
->refcache
, pack_pos
);
1309 git_sortedcache_wunlock(backend
->refcache
);
1311 if (error
== GIT_ENOTFOUND
) {
1312 error
= loose_deleted
? 0 : ref_error_notfound(ref_name
);
1316 error
= packed_write(backend
);
1319 git_buf_free(&loose_path
);
1320 git_filebuf_cleanup(file
);
1325 static int refdb_reflog_fs__rename(git_refdb_backend
*_backend
, const char *old_name
, const char *new_name
);
1327 static int refdb_fs_backend__rename(
1328 git_reference
**out
,
1329 git_refdb_backend
*_backend
,
1330 const char *old_name
,
1331 const char *new_name
,
1333 const git_signature
*who
,
1334 const char *message
)
1336 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1337 git_reference
*old
, *new;
1338 git_filebuf file
= GIT_FILEBUF_INIT
;
1343 if ((error
= reference_path_available(
1344 backend
, new_name
, old_name
, force
)) < 0 ||
1345 (error
= refdb_fs_backend__lookup(&old
, _backend
, old_name
)) < 0)
1348 if ((error
= refdb_fs_backend__delete(_backend
, old_name
, NULL
, NULL
)) < 0) {
1349 git_reference_free(old
);
1353 new = git_reference__set_name(old
, new_name
);
1355 git_reference_free(old
);
1359 if ((error
= loose_lock(&file
, backend
, new->name
)) < 0) {
1360 git_reference_free(new);
1364 /* Try to rename the refog; it's ok if the old doesn't exist */
1365 error
= refdb_reflog_fs__rename(_backend
, old_name
, new_name
);
1366 if (((error
== 0) || (error
== GIT_ENOTFOUND
)) &&
1367 ((error
= reflog_append(backend
, new, git_reference_target(new), NULL
, who
, message
)) < 0)) {
1368 git_reference_free(new);
1369 git_filebuf_cleanup(&file
);
1374 git_reference_free(new);
1375 git_filebuf_cleanup(&file
);
1380 if ((error
= loose_commit(&file
, new)) < 0 || out
== NULL
) {
1381 git_reference_free(new);
1389 static int refdb_fs_backend__compress(git_refdb_backend
*_backend
)
1392 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1396 if ((error
= packed_reload(backend
)) < 0 || /* load the existing packfile */
1397 (error
= packed_loadloose(backend
)) < 0 || /* add all the loose refs */
1398 (error
= packed_write(backend
)) < 0) /* write back to disk */
1404 static void refdb_fs_backend__free(git_refdb_backend
*_backend
)
1406 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1410 git_sortedcache_free(backend
->refcache
);
1411 git__free(backend
->path
);
1415 static int setup_namespace(git_buf
*path
, git_repository
*repo
)
1417 char *parts
, *start
, *end
;
1419 /* Not all repositories have a path */
1420 if (repo
->path_repository
== NULL
)
1423 /* Load the path to the repo first */
1424 git_buf_puts(path
, repo
->path_repository
);
1426 /* if the repo is not namespaced, nothing else to do */
1427 if (repo
->namespace == NULL
)
1430 parts
= end
= git__strdup(repo
->namespace);
1435 * From `man gitnamespaces`:
1436 * namespaces which include a / will expand to a hierarchy
1437 * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
1438 * refs under refs/namespaces/foo/refs/namespaces/bar/
1440 while ((start
= git__strsep(&end
, "/")) != NULL
) {
1441 git_buf_printf(path
, "refs/namespaces/%s/", start
);
1444 git_buf_printf(path
, "refs/namespaces/%s/refs", end
);
1447 /* Make sure that the folder with the namespace exists */
1448 if (git_futils_mkdir_relative(git_buf_cstr(path
), repo
->path_repository
,
1449 0777, GIT_MKDIR_PATH
, NULL
) < 0)
1452 /* Return root of the namespaced path, i.e. without the trailing '/refs' */
1453 git_buf_rtruncate_at_char(path
, '/');
1457 static int reflog_alloc(git_reflog
**reflog
, const char *name
)
1463 log
= git__calloc(1, sizeof(git_reflog
));
1464 GITERR_CHECK_ALLOC(log
);
1466 log
->ref_name
= git__strdup(name
);
1467 GITERR_CHECK_ALLOC(log
->ref_name
);
1469 if (git_vector_init(&log
->entries
, 0, NULL
) < 0) {
1470 git__free(log
->ref_name
);
1480 static int reflog_parse(git_reflog
*log
, const char *buf
, size_t buf_size
)
1483 git_reflog_entry
*entry
;
1485 #define seek_forward(_increase) do { \
1486 if (_increase >= buf_size) { \
1487 giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
1491 buf_size -= _increase; \
1494 while (buf_size
> GIT_REFLOG_SIZE_MIN
) {
1495 entry
= git__calloc(1, sizeof(git_reflog_entry
));
1496 GITERR_CHECK_ALLOC(entry
);
1498 entry
->committer
= git__calloc(1, sizeof(git_signature
));
1499 GITERR_CHECK_ALLOC(entry
->committer
);
1501 if (git_oid_fromstrn(&entry
->oid_old
, buf
, GIT_OID_HEXSZ
) < 0)
1503 seek_forward(GIT_OID_HEXSZ
+ 1);
1505 if (git_oid_fromstrn(&entry
->oid_cur
, buf
, GIT_OID_HEXSZ
) < 0)
1507 seek_forward(GIT_OID_HEXSZ
+ 1);
1511 /* Seek forward to the end of the signature. */
1512 while (*buf
&& *buf
!= '\t' && *buf
!= '\n')
1515 if (git_signature__parse(entry
->committer
, &ptr
, buf
+ 1, NULL
, *buf
) < 0)
1519 /* We got a message. Read everything till we reach LF. */
1523 while (*buf
&& *buf
!= '\n')
1526 entry
->msg
= git__strndup(ptr
, buf
- ptr
);
1527 GITERR_CHECK_ALLOC(entry
->msg
);
1531 while (*buf
&& *buf
== '\n' && buf_size
> 1)
1534 if (git_vector_insert(&log
->entries
, entry
) < 0)
1543 git_reflog_entry__free(entry
);
1548 static int create_new_reflog_file(const char *filepath
)
1552 if ((error
= git_futils_mkpath2file(filepath
, GIT_REFLOG_DIR_MODE
)) < 0)
1555 if ((fd
= p_open(filepath
,
1557 GIT_REFLOG_FILE_MODE
)) < 0)
1563 GIT_INLINE(int) retrieve_reflog_path(git_buf
*path
, git_repository
*repo
, const char *name
)
1565 return git_buf_join3(path
, '/', repo
->path_repository
, GIT_REFLOG_DIR
, name
);
1568 static int refdb_reflog_fs__ensure_log(git_refdb_backend
*_backend
, const char *name
)
1570 refdb_fs_backend
*backend
;
1571 git_repository
*repo
;
1572 git_buf path
= GIT_BUF_INIT
;
1575 assert(_backend
&& name
);
1577 backend
= (refdb_fs_backend
*) _backend
;
1578 repo
= backend
->repo
;
1580 if ((error
= retrieve_reflog_path(&path
, repo
, name
)) < 0)
1583 error
= create_new_reflog_file(git_buf_cstr(&path
));
1584 git_buf_free(&path
);
1589 static int has_reflog(git_repository
*repo
, const char *name
)
1592 git_buf path
= GIT_BUF_INIT
;
1594 if (retrieve_reflog_path(&path
, repo
, name
) < 0)
1597 ret
= git_path_isfile(git_buf_cstr(&path
));
1600 git_buf_free(&path
);
1604 static int refdb_reflog_fs__has_log(git_refdb_backend
*_backend
, const char *name
)
1606 refdb_fs_backend
*backend
;
1608 assert(_backend
&& name
);
1610 backend
= (refdb_fs_backend
*) _backend
;
1612 return has_reflog(backend
->repo
, name
);
1615 static int refdb_reflog_fs__read(git_reflog
**out
, git_refdb_backend
*_backend
, const char *name
)
1618 git_buf log_path
= GIT_BUF_INIT
;
1619 git_buf log_file
= GIT_BUF_INIT
;
1620 git_reflog
*log
= NULL
;
1621 git_repository
*repo
;
1622 refdb_fs_backend
*backend
;
1624 assert(out
&& _backend
&& name
);
1626 backend
= (refdb_fs_backend
*) _backend
;
1627 repo
= backend
->repo
;
1629 if (reflog_alloc(&log
, name
) < 0)
1632 if (retrieve_reflog_path(&log_path
, repo
, name
) < 0)
1635 error
= git_futils_readbuffer(&log_file
, git_buf_cstr(&log_path
));
1636 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1639 if ((error
== GIT_ENOTFOUND
) &&
1640 ((error
= create_new_reflog_file(git_buf_cstr(&log_path
))) < 0))
1643 if ((error
= reflog_parse(log
,
1644 git_buf_cstr(&log_file
), git_buf_len(&log_file
))) < 0)
1651 git_reflog_free(log
);
1654 git_buf_free(&log_file
);
1655 git_buf_free(&log_path
);
1660 static int serialize_reflog_entry(
1662 const git_oid
*oid_old
,
1663 const git_oid
*oid_new
,
1664 const git_signature
*committer
,
1667 char raw_old
[GIT_OID_HEXSZ
+1];
1668 char raw_new
[GIT_OID_HEXSZ
+1];
1670 git_oid_tostr(raw_old
, GIT_OID_HEXSZ
+1, oid_old
);
1671 git_oid_tostr(raw_new
, GIT_OID_HEXSZ
+1, oid_new
);
1675 git_buf_puts(buf
, raw_old
);
1676 git_buf_putc(buf
, ' ');
1677 git_buf_puts(buf
, raw_new
);
1679 git_signature__writebuf(buf
, " ", committer
);
1681 /* drop trailing LF */
1685 git_buf_putc(buf
, '\t');
1686 git_buf_puts(buf
, msg
);
1689 git_buf_putc(buf
, '\n');
1691 return git_buf_oom(buf
);
1694 static int lock_reflog(git_filebuf
*file
, refdb_fs_backend
*backend
, const char *refname
)
1696 git_repository
*repo
;
1697 git_buf log_path
= GIT_BUF_INIT
;
1700 repo
= backend
->repo
;
1702 if (!git_path_isvalid(backend
->repo
, refname
, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS
)) {
1703 giterr_set(GITERR_INVALID
, "Invalid reference name '%s'.", refname
);
1704 return GIT_EINVALIDSPEC
;
1707 if (retrieve_reflog_path(&log_path
, repo
, refname
) < 0)
1710 if (!git_path_isfile(git_buf_cstr(&log_path
))) {
1711 giterr_set(GITERR_INVALID
,
1712 "Log file for reference '%s' doesn't exist.", refname
);
1717 error
= git_filebuf_open(file
, git_buf_cstr(&log_path
), 0, GIT_REFLOG_FILE_MODE
);
1720 git_buf_free(&log_path
);
1725 static int refdb_reflog_fs__write(git_refdb_backend
*_backend
, git_reflog
*reflog
)
1729 git_reflog_entry
*entry
;
1730 refdb_fs_backend
*backend
;
1731 git_buf log
= GIT_BUF_INIT
;
1732 git_filebuf fbuf
= GIT_FILEBUF_INIT
;
1734 assert(_backend
&& reflog
);
1736 backend
= (refdb_fs_backend
*) _backend
;
1738 if ((error
= lock_reflog(&fbuf
, backend
, reflog
->ref_name
)) < 0)
1741 git_vector_foreach(&reflog
->entries
, i
, entry
) {
1742 if (serialize_reflog_entry(&log
, &(entry
->oid_old
), &(entry
->oid_cur
), entry
->committer
, entry
->msg
) < 0)
1745 if ((error
= git_filebuf_write(&fbuf
, log
.ptr
, log
.size
)) < 0)
1749 error
= git_filebuf_commit(&fbuf
);
1753 git_filebuf_cleanup(&fbuf
);
1761 /* Append to the reflog, must be called under reference lock */
1762 static int reflog_append(refdb_fs_backend
*backend
, const git_reference
*ref
, const git_oid
*old
, const git_oid
*new, const git_signature
*who
, const char *message
)
1764 int error
, is_symbolic
;
1765 git_oid old_id
= {{0}}, new_id
= {{0}};
1766 git_buf buf
= GIT_BUF_INIT
, path
= GIT_BUF_INIT
;
1767 git_repository
*repo
= backend
->repo
;
1769 is_symbolic
= ref
->type
== GIT_REF_SYMBOLIC
;
1771 /* "normal" symbolic updates do not write */
1773 strcmp(ref
->name
, GIT_HEAD_FILE
) &&
1777 /* From here on is_symoblic also means that it's HEAD */
1780 git_oid_cpy(&old_id
, old
);
1782 error
= git_reference_name_to_id(&old_id
, repo
, ref
->name
);
1783 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1788 git_oid_cpy(&new_id
, new);
1791 git_oid_cpy(&new_id
, git_reference_target(ref
));
1793 error
= git_reference_name_to_id(&new_id
, repo
, git_reference_symbolic_target(ref
));
1794 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1796 /* detaching HEAD does not create an entry */
1797 if (error
== GIT_ENOTFOUND
)
1804 if ((error
= serialize_reflog_entry(&buf
, &old_id
, &new_id
, who
, message
)) < 0)
1807 if ((error
= retrieve_reflog_path(&path
, repo
, ref
->name
)) < 0)
1810 if (((error
= git_futils_mkpath2file(git_buf_cstr(&path
), 0777)) < 0) &&
1811 (error
!= GIT_EEXISTS
)) {
1815 /* If the new branch matches part of the namespace of a previously deleted branch,
1816 * there maybe an obsolete/unused directory (or directory hierarchy) in the way.
1818 if (git_path_isdir(git_buf_cstr(&path
))) {
1819 if ((error
= git_futils_rmdir_r(git_buf_cstr(&path
), NULL
, GIT_RMDIR_SKIP_NONEMPTY
)) < 0) {
1820 if (error
== GIT_ENOTFOUND
)
1822 } else if (git_path_isdir(git_buf_cstr(&path
))) {
1823 giterr_set(GITERR_REFERENCE
, "cannot create reflog at '%s', there are reflogs beneath that folder",
1825 error
= GIT_EDIRECTORY
;
1832 error
= git_futils_writebuffer(&buf
, git_buf_cstr(&path
), O_WRONLY
|O_CREAT
|O_APPEND
, GIT_REFLOG_FILE_MODE
);
1836 git_buf_free(&path
);
1841 static int refdb_reflog_fs__rename(git_refdb_backend
*_backend
, const char *old_name
, const char *new_name
)
1844 git_buf old_path
= GIT_BUF_INIT
;
1845 git_buf new_path
= GIT_BUF_INIT
;
1846 git_buf temp_path
= GIT_BUF_INIT
;
1847 git_buf normalized
= GIT_BUF_INIT
;
1848 git_repository
*repo
;
1849 refdb_fs_backend
*backend
;
1851 assert(_backend
&& old_name
&& new_name
);
1853 backend
= (refdb_fs_backend
*) _backend
;
1854 repo
= backend
->repo
;
1856 if ((error
= git_reference__normalize_name(
1857 &normalized
, new_name
, GIT_REF_FORMAT_ALLOW_ONELEVEL
)) < 0)
1860 if (git_buf_joinpath(&temp_path
, repo
->path_repository
, GIT_REFLOG_DIR
) < 0)
1863 if (git_buf_joinpath(&old_path
, git_buf_cstr(&temp_path
), old_name
) < 0)
1866 if (git_buf_joinpath(&new_path
, git_buf_cstr(&temp_path
), git_buf_cstr(&normalized
)) < 0)
1869 if (!git_path_exists(git_buf_cstr(&old_path
))) {
1870 error
= GIT_ENOTFOUND
;
1875 * Move the reflog to a temporary place. This two-phase renaming is required
1876 * in order to cope with funny renaming use cases when one tries to move a reference
1877 * to a partially colliding namespace:
1879 * - a/b/c/d -> a/b/c
1881 if (git_buf_joinpath(&temp_path
, git_buf_cstr(&temp_path
), "temp_reflog") < 0)
1884 if ((fd
= git_futils_mktmp(&temp_path
, git_buf_cstr(&temp_path
), GIT_REFLOG_FILE_MODE
)) < 0) {
1891 if (p_rename(git_buf_cstr(&old_path
), git_buf_cstr(&temp_path
)) < 0) {
1892 giterr_set(GITERR_OS
, "Failed to rename reflog for %s", new_name
);
1897 if (git_path_isdir(git_buf_cstr(&new_path
)) &&
1898 (git_futils_rmdir_r(git_buf_cstr(&new_path
), NULL
, GIT_RMDIR_SKIP_NONEMPTY
) < 0)) {
1903 if (git_futils_mkpath2file(git_buf_cstr(&new_path
), GIT_REFLOG_DIR_MODE
) < 0) {
1908 if (p_rename(git_buf_cstr(&temp_path
), git_buf_cstr(&new_path
)) < 0) {
1909 giterr_set(GITERR_OS
, "Failed to rename reflog for %s", new_name
);
1914 git_buf_free(&temp_path
);
1915 git_buf_free(&old_path
);
1916 git_buf_free(&new_path
);
1917 git_buf_free(&normalized
);
1922 static int refdb_reflog_fs__delete(git_refdb_backend
*_backend
, const char *name
)
1925 git_buf path
= GIT_BUF_INIT
;
1927 git_repository
*repo
;
1928 refdb_fs_backend
*backend
;
1930 assert(_backend
&& name
);
1932 backend
= (refdb_fs_backend
*) _backend
;
1933 repo
= backend
->repo
;
1935 error
= retrieve_reflog_path(&path
, repo
, name
);
1937 if (!error
&& git_path_exists(path
.ptr
))
1938 error
= p_unlink(path
.ptr
);
1940 git_buf_free(&path
);
1946 int git_refdb_backend_fs(
1947 git_refdb_backend
**backend_out
,
1948 git_repository
*repository
)
1951 git_buf path
= GIT_BUF_INIT
;
1952 refdb_fs_backend
*backend
;
1954 backend
= git__calloc(1, sizeof(refdb_fs_backend
));
1955 GITERR_CHECK_ALLOC(backend
);
1957 backend
->repo
= repository
;
1959 if (setup_namespace(&path
, repository
) < 0)
1962 backend
->path
= git_buf_detach(&path
);
1964 if (git_buf_joinpath(&path
, backend
->path
, GIT_PACKEDREFS_FILE
) < 0 ||
1965 git_sortedcache_new(
1966 &backend
->refcache
, offsetof(struct packref
, name
),
1967 NULL
, NULL
, packref_cmp
, git_buf_cstr(&path
)) < 0)
1970 git_buf_free(&path
);
1972 if (!git_repository__cvar(&t
, backend
->repo
, GIT_CVAR_IGNORECASE
) && t
) {
1973 backend
->iterator_flags
|= GIT_ITERATOR_IGNORE_CASE
;
1974 backend
->direach_flags
|= GIT_PATH_DIR_IGNORE_CASE
;
1976 if (!git_repository__cvar(&t
, backend
->repo
, GIT_CVAR_PRECOMPOSE
) && t
) {
1977 backend
->iterator_flags
|= GIT_ITERATOR_PRECOMPOSE_UNICODE
;
1978 backend
->direach_flags
|= GIT_PATH_DIR_PRECOMPOSE_UNICODE
;
1981 backend
->parent
.exists
= &refdb_fs_backend__exists
;
1982 backend
->parent
.lookup
= &refdb_fs_backend__lookup
;
1983 backend
->parent
.iterator
= &refdb_fs_backend__iterator
;
1984 backend
->parent
.write
= &refdb_fs_backend__write
;
1985 backend
->parent
.del
= &refdb_fs_backend__delete
;
1986 backend
->parent
.rename
= &refdb_fs_backend__rename
;
1987 backend
->parent
.compress
= &refdb_fs_backend__compress
;
1988 backend
->parent
.lock
= &refdb_fs_backend__lock
;
1989 backend
->parent
.unlock
= &refdb_fs_backend__unlock
;
1990 backend
->parent
.has_log
= &refdb_reflog_fs__has_log
;
1991 backend
->parent
.ensure_log
= &refdb_reflog_fs__ensure_log
;
1992 backend
->parent
.free
= &refdb_fs_backend__free
;
1993 backend
->parent
.reflog_read
= &refdb_reflog_fs__read
;
1994 backend
->parent
.reflog_write
= &refdb_reflog_fs__write
;
1995 backend
->parent
.reflog_rename
= &refdb_reflog_fs__rename
;
1996 backend
->parent
.reflog_delete
= &refdb_reflog_fs__delete
;
1998 *backend_out
= (git_refdb_backend
*)backend
;
2002 git_buf_free(&path
);
2003 git__free(backend
->path
);