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>
29 #define DEFAULT_NESTING_LEVEL 5
30 #define MAX_NESTING_LEVEL 10
34 PACKREF_WAS_LOOSE
= 2,
35 PACKREF_CANNOT_PEEL
= 4,
49 char name
[GIT_FLEX_ARRAY
];
52 typedef struct refdb_fs_backend
{
53 git_refdb_backend parent
;
56 /* path to git directory */
58 /* path to common objects' directory */
61 git_sortedcache
*refcache
;
63 git_iterator_flag_t iterator_flags
;
64 uint32_t direach_flags
;
67 static int refdb_reflog_fs__delete(git_refdb_backend
*_backend
, const char *name
);
69 static int packref_cmp(const void *a_
, const void *b_
)
71 const struct packref
*a
= a_
, *b
= b_
;
72 return strcmp(a
->name
, b
->name
);
75 static int packed_reload(refdb_fs_backend
*backend
)
78 git_buf packedrefs
= GIT_BUF_INIT
;
79 char *scan
, *eof
, *eol
;
81 if (!backend
->gitpath
)
84 error
= git_sortedcache_lockandload(backend
->refcache
, &packedrefs
);
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.
93 if (error
== GIT_ENOTFOUND
) {
94 git_sortedcache_clear(backend
->refcache
, true);
101 /* At this point, refresh the packed refs from the loaded buffer. */
103 git_sortedcache_clear(backend
->refcache
, false);
105 scan
= (char *)packedrefs
.ptr
;
106 eof
= scan
+ packedrefs
.size
;
108 backend
->peeling_mode
= PEELING_NONE
;
111 static const char *traits_header
= "# pack-refs with: ";
113 if (git__prefixcmp(scan
, traits_header
) == 0) {
114 scan
+= strlen(traits_header
);
115 eol
= strchr(scan
, '\n');
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
;
131 while (scan
< eof
&& *scan
== '#') {
132 if (!(eol
= strchr(scan
, '\n')))
141 /* parse "<OID> <refname>\n" */
143 if (git_oid_fromstr(&oid
, scan
) < 0)
145 scan
+= GIT_OID_HEXSZ
;
149 if (!(eol
= strchr(scan
, '\n')))
155 if (git_sortedcache_upsert((void **)&ref
, backend
->refcache
, scan
) < 0)
159 git_oid_cpy(&ref
->oid
, &oid
);
161 /* look for optional "^<OID>\n" */
164 if (git_oid_fromstr(&oid
, scan
+ 1) < 0)
166 scan
+= GIT_OID_HEXSZ
+ 1;
169 if (!(eol
= strchr(scan
, '\n')))
174 git_oid_cpy(&ref
->peel
, &oid
);
175 ref
->flags
|= PACKREF_HAS_PEEL
;
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
;
183 git_sortedcache_wunlock(backend
->refcache
);
184 git_buf_free(&packedrefs
);
189 giterr_set(GITERR_REFERENCE
, "corrupted packed references file");
191 git_sortedcache_clear(backend
->refcache
, false);
192 git_sortedcache_wunlock(backend
->refcache
);
193 git_buf_free(&packedrefs
);
198 static int loose_parse_oid(
199 git_oid
*oid
, const char *filename
, git_buf
*file_content
)
201 const char *str
= git_buf_cstr(file_content
);
203 if (git_buf_len(file_content
) < GIT_OID_HEXSZ
)
206 /* we need to get 40 OID characters from the file */
207 if (git_oid_fromstr(oid
, str
) < 0)
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
))
216 giterr_set(GITERR_REFERENCE
, "corrupted loose reference file: %s", filename
);
220 static int loose_readbuffer(git_buf
*buf
, const char *base
, const char *path
)
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)
232 static int loose_lookup_to_packfile(refdb_fs_backend
*backend
, const char *name
)
235 git_buf ref_file
= GIT_BUF_INIT
;
236 struct packref
*ref
= NULL
;
239 /* if we fail to load the loose reference, assume someone changed
240 * the filesystem under us and skip it...
242 if (loose_readbuffer(&ref_file
, backend
->gitpath
, name
) < 0) {
247 /* skip symbolic refs */
248 if (!git__prefixcmp(git_buf_cstr(&ref_file
), GIT_SYMREF
))
251 /* parse OID from file */
252 if ((error
= loose_parse_oid(&oid
, name
, &ref_file
)) < 0)
255 git_sortedcache_wlock(backend
->refcache
);
257 if (!(error
= git_sortedcache_upsert(
258 (void **)&ref
, backend
->refcache
, name
))) {
260 git_oid_cpy(&ref
->oid
, &oid
);
261 ref
->flags
= PACKREF_WAS_LOOSE
;
264 git_sortedcache_wunlock(backend
->refcache
);
267 git_buf_free(&ref_file
);
271 static int _dirent_loose_load(void *payload
, git_buf
*full_path
)
273 refdb_fs_backend
*backend
= payload
;
274 const char *file_path
;
276 if (git__suffixcmp(full_path
->ptr
, ".lock") == 0)
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
) {
291 file_path
= full_path
->ptr
+ strlen(backend
->gitpath
);
293 return loose_lookup_to_packfile(backend
, file_path
);
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
302 static int packed_loadloose(refdb_fs_backend
*backend
)
305 git_buf refs_path
= GIT_BUF_INIT
;
307 if (git_buf_joinpath(&refs_path
, backend
->gitpath
, GIT_REFS_DIR
) < 0)
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
315 error
= git_path_direach(
316 &refs_path
, backend
->direach_flags
, _dirent_loose_load
, backend
);
318 git_buf_free(&refs_path
);
323 static int refdb_fs_backend__exists(
325 git_refdb_backend
*_backend
,
326 const char *ref_name
)
328 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
329 git_buf ref_path
= GIT_BUF_INIT
;
334 if ((error
= packed_reload(backend
)) < 0 ||
335 (error
= git_buf_joinpath(&ref_path
, backend
->gitpath
, ref_name
)) < 0)
338 *exists
= git_path_isfile(ref_path
.ptr
) ||
339 (git_sortedcache_lookup(backend
->refcache
, ref_name
) != NULL
);
341 git_buf_free(&ref_path
);
345 static const char *loose_parse_symbolic(git_buf
*file_content
)
347 const unsigned int header_len
= (unsigned int)strlen(GIT_SYMREF
);
348 const char *refname_start
;
350 refname_start
= (const char *)file_content
->ptr
;
352 if (git_buf_len(file_content
) < header_len
+ 1) {
353 giterr_set(GITERR_REFERENCE
, "corrupted loose reference file");
358 * Assume we have already checked for the header
359 * before calling this function
361 refname_start
+= header_len
;
363 return refname_start
;
367 * Returns whether a reference is stored per worktree or not.
368 * Per-worktree references are:
370 * - all pseudorefs, e.g. HEAD and MERGE_HEAD
371 * - all references stored inside of "refs/bisect/"
373 static bool is_per_worktree_ref(const char *ref_name
)
375 return git__prefixcmp(ref_name
, "refs/") != 0 ||
376 git__prefixcmp(ref_name
, "refs/bisect/") == 0;
379 static int loose_lookup(
381 refdb_fs_backend
*backend
,
382 const char *ref_name
)
384 git_buf ref_file
= GIT_BUF_INIT
;
391 if (is_per_worktree_ref(ref_name
))
392 ref_dir
= backend
->gitpath
;
394 ref_dir
= backend
->commonpath
;
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) {
401 git_buf_rtrim(&ref_file
);
403 if (!(target
= loose_parse_symbolic(&ref_file
)))
405 else if (out
!= NULL
)
406 *out
= git_reference__alloc_symbolic(ref_name
, target
);
410 if (!(error
= loose_parse_oid(&oid
, ref_name
, &ref_file
)) &&
412 *out
= git_reference__alloc(ref_name
, &oid
, NULL
);
415 git_buf_free(&ref_file
);
419 static int ref_error_notfound(const char *name
)
421 giterr_set(GITERR_REFERENCE
, "reference '%s' not found", name
);
422 return GIT_ENOTFOUND
;
425 static int packed_lookup(
427 refdb_fs_backend
*backend
,
428 const char *ref_name
)
431 struct packref
*entry
;
433 if ((error
= packed_reload(backend
)) < 0)
436 if (git_sortedcache_rlock(backend
->refcache
) < 0)
439 entry
= git_sortedcache_lookup(backend
->refcache
, ref_name
);
441 error
= ref_error_notfound(ref_name
);
443 *out
= git_reference__alloc(ref_name
, &entry
->oid
, &entry
->peel
);
448 git_sortedcache_runlock(backend
->refcache
);
453 static int refdb_fs_backend__lookup(
455 git_refdb_backend
*_backend
,
456 const char *ref_name
)
458 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
463 if (!(error
= loose_lookup(out
, backend
, ref_name
)))
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
) {
470 error
= packed_lookup(out
, backend
, ref_name
);
477 git_reference_iterator parent
;
484 git_sortedcache
*cache
;
489 static void refdb_fs_backend__iterator_free(git_reference_iterator
*_iter
)
491 refdb_fs_iter
*iter
= (refdb_fs_iter
*) _iter
;
493 git_vector_free(&iter
->loose
);
494 git_pool_clear(&iter
->pool
);
495 git_sortedcache_free(iter
->cache
);
499 static int iter_load_loose_paths(refdb_fs_backend
*backend
, refdb_fs_iter
*iter
)
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
;
507 if (!backend
->commonpath
) /* do nothing if no commonpath for loose refs */
510 fsit_opts
.flags
= backend
->iterator_flags
;
512 if ((error
= git_buf_printf(&path
, "%s/refs", backend
->commonpath
)) < 0 ||
513 (error
= git_iterator_for_filesystem(&fsit
, path
.ptr
, &fsit_opts
)) < 0) {
518 error
= git_buf_sets(&path
, GIT_REFS_DIR
);
520 while (!error
&& !git_iterator_advance(&entry
, fsit
)) {
521 const char *ref_name
;
525 git_buf_truncate(&path
, strlen(GIT_REFS_DIR
));
526 git_buf_puts(&path
, entry
->path
);
527 ref_name
= git_buf_cstr(&path
);
529 if (git__suffixcmp(ref_name
, ".lock") == 0 ||
530 (iter
->glob
&& p_fnmatch(iter
->glob
, ref_name
, 0) != 0))
533 git_sortedcache_rlock(backend
->refcache
);
534 ref
= git_sortedcache_lookup(backend
->refcache
, ref_name
);
536 ref
->flags
|= PACKREF_SHADOWED
;
537 git_sortedcache_runlock(backend
->refcache
);
539 ref_dup
= git_pool_strdup(&iter
->pool
, ref_name
);
543 error
= git_vector_insert(&iter
->loose
, ref_dup
);
546 git_iterator_free(fsit
);
552 static int refdb_fs_backend__iterator_next(
553 git_reference
**out
, git_reference_iterator
*_iter
)
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
;
560 while (iter
->loose_pos
< iter
->loose
.length
) {
561 const char *path
= git_vector_get(&iter
->loose
, iter
->loose_pos
++);
563 if (loose_lookup(out
, backend
, path
) == 0)
570 if ((error
= git_sortedcache_copy(&iter
->cache
, backend
->refcache
, 1, NULL
, NULL
)) < 0)
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 */
580 if (ref
->flags
& PACKREF_SHADOWED
)
582 if (iter
->glob
&& p_fnmatch(iter
->glob
, ref
->name
, 0) != 0)
585 *out
= git_reference__alloc(ref
->name
, &ref
->oid
, &ref
->peel
);
586 error
= (*out
!= NULL
) ? 0 : -1;
593 static int refdb_fs_backend__iterator_next_name(
594 const char **out
, git_reference_iterator
*_iter
)
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
;
601 while (iter
->loose_pos
< iter
->loose
.length
) {
602 const char *path
= git_vector_get(&iter
->loose
, iter
->loose_pos
++);
604 if (loose_lookup(NULL
, backend
, path
) == 0) {
613 if ((error
= git_sortedcache_copy(&iter
->cache
, backend
->refcache
, 1, NULL
, NULL
)) < 0)
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 */
623 if (ref
->flags
& PACKREF_SHADOWED
)
625 if (iter
->glob
&& p_fnmatch(iter
->glob
, ref
->name
, 0) != 0)
636 static int refdb_fs_backend__iterator(
637 git_reference_iterator
**out
, git_refdb_backend
*_backend
, const char *glob
)
641 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
645 if ((error
= packed_reload(backend
)) < 0)
648 iter
= git__calloc(1, sizeof(refdb_fs_iter
));
649 GITERR_CHECK_ALLOC(iter
);
651 git_pool_init(&iter
->pool
, 1);
653 if (git_vector_init(&iter
->loose
, 8, NULL
) < 0)
657 (iter
->glob
= git_pool_strdup(&iter
->pool
, glob
)) == NULL
)
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
;
664 if (iter_load_loose_paths(backend
, iter
) < 0)
667 *out
= (git_reference_iterator
*)iter
;
671 refdb_fs_backend__iterator_free((git_reference_iterator
*)iter
);
675 static bool ref_is_available(
676 const char *old_ref
, const char *new_ref
, const char *this_ref
)
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
;
684 if (!strncmp(new_ref
, this_ref
, cmplen
) && lead
[cmplen
] == '/') {
692 static int reference_path_available(
693 refdb_fs_backend
*backend
,
701 if ((error
= packed_reload(backend
)) < 0)
707 if ((error
= refdb_fs_backend__exists(
708 &exists
, (git_refdb_backend
*)backend
, new_ref
)) < 0) {
713 giterr_set(GITERR_REFERENCE
,
714 "failed to write reference '%s': a reference with "
715 "that name already exists.", new_ref
);
720 git_sortedcache_rlock(backend
->refcache
);
722 for (i
= 0; i
< git_sortedcache_entrycount(backend
->refcache
); ++i
) {
723 struct packref
*ref
= git_sortedcache_entry(backend
->refcache
, i
);
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
);
733 git_sortedcache_runlock(backend
->refcache
);
737 static int loose_lock(git_filebuf
*file
, refdb_fs_backend
*backend
, const char *name
)
740 git_buf ref_path
= GIT_BUF_INIT
;
742 assert(file
&& backend
&& name
);
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
;
749 /* Remove a possibly existing empty directory hierarchy
750 * which name would collide with the reference name
752 if ((error
= git_futils_rmdir_r(name
, backend
->gitpath
, GIT_RMDIR_SKIP_NONEMPTY
)) < 0)
755 if (git_buf_joinpath(&ref_path
, backend
->gitpath
, name
) < 0)
758 error
= git_filebuf_open(file
, ref_path
.ptr
, GIT_FILEBUF_FORCE
, GIT_REFS_FILE_MODE
);
760 if (error
== GIT_EDIRECTORY
)
761 giterr_set(GITERR_REFERENCE
, "cannot lock ref '%s', there are refs beneath that folder", name
);
763 git_buf_free(&ref_path
);
767 static int loose_commit(git_filebuf
*file
, const git_reference
*ref
)
771 if (ref
->type
== GIT_REF_OID
) {
772 char oid
[GIT_OID_HEXSZ
+ 1];
773 git_oid_nfmt(oid
, sizeof(oid
), &ref
->target
.oid
);
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
);
779 assert(0); /* don't let this happen */
782 return git_filebuf_commit(file
);
785 static int refdb_fs_backend__lock(void **out
, git_refdb_backend
*_backend
, const char *refname
)
789 refdb_fs_backend
*backend
= (refdb_fs_backend
*) _backend
;
791 lock
= git__calloc(1, sizeof(git_filebuf
));
792 GITERR_CHECK_ALLOC(lock
);
794 if ((error
= loose_lock(lock
, backend
, refname
)) < 0) {
803 static int refdb_fs_backend__write_tail(
804 git_refdb_backend
*_backend
,
805 const git_reference
*ref
,
808 const git_signature
*who
,
810 const git_oid
*old_id
,
811 const char *old_target
);
813 static int refdb_fs_backend__delete_tail(
814 git_refdb_backend
*_backend
,
816 const char *ref_name
,
817 const git_oid
*old_id
, const char *old_target
);
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
)
822 git_filebuf
*lock
= (git_filebuf
*) payload
;
826 error
= refdb_fs_backend__delete_tail(backend
, lock
, ref
->name
, NULL
, NULL
);
828 error
= refdb_fs_backend__write_tail(backend
, ref
, lock
, update_reflog
, sig
, message
, NULL
, NULL
);
830 git_filebuf_cleanup(lock
);
837 * Find out what object this reference resolves to.
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.
844 static int packed_find_peel(refdb_fs_backend
*backend
, struct packref
*ref
)
848 if (ref
->flags
& PACKREF_HAS_PEEL
|| ref
->flags
& PACKREF_CANNOT_PEEL
)
852 * Find the tagged object in the repository
854 if (git_object_lookup(&object
, backend
->repo
, &ref
->oid
, GIT_OBJ_ANY
) < 0)
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
862 if (git_object_type(object
) == GIT_OBJ_TAG
) {
863 git_tag
*tag
= (git_tag
*)object
;
866 * Find the object pointed at by this tag
868 git_oid_cpy(&ref
->peel
, git_tag_target_id(tag
));
869 ref
->flags
|= PACKREF_HAS_PEEL
;
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
878 git_object_free(object
);
883 * Write a single reference into a packfile
885 static int packed_write_ref(struct packref
*ref
, git_filebuf
*file
)
887 char oid
[GIT_OID_HEXSZ
+ 1];
888 git_oid_nfmt(oid
, sizeof(oid
), &ref
->oid
);
891 * For references that peel to an object in the repo, we must
892 * write the resulting peel on a separate line, e.g.
894 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
895 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
897 * This obviously only applies to tags.
898 * The required peels have already been loaded into `ref->peel_target`.
900 if (ref
->flags
& PACKREF_HAS_PEEL
) {
901 char peel
[GIT_OID_HEXSZ
+ 1];
902 git_oid_nfmt(peel
, sizeof(peel
), &ref
->peel
);
904 if (git_filebuf_printf(file
, "%s %s\n^%s\n", oid
, ref
->name
, peel
) < 0)
907 if (git_filebuf_printf(file
, "%s %s\n", oid
, ref
->name
) < 0)
915 * Remove all loose references
917 * Once we have successfully written a packfile,
918 * all the loose references that were packed must be
921 * This is a dangerous method; make sure the packfile
922 * is well-written, because we are destructing references
925 static int packed_remove_loose(refdb_fs_backend
*backend
)
928 git_filebuf lock
= GIT_FILEBUF_INIT
;
929 git_buf ref_content
= GIT_BUF_INIT
;
932 /* backend->refcache is already locked when this is called */
934 for (i
= 0; i
< git_sortedcache_entrycount(backend
->refcache
); ++i
) {
935 struct packref
*ref
= git_sortedcache_entry(backend
->refcache
, i
);
938 if (!ref
|| !(ref
->flags
& PACKREF_WAS_LOOSE
))
941 git_filebuf_cleanup(&lock
);
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
)
950 git_buf_free(&ref_content
);
951 giterr_set(GITERR_REFERENCE
, "failed to lock loose reference '%s'", ref
->name
);
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
)
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
))
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(¤t_id
, lock
.path_original
, &ref_content
) < 0)
968 /* If the ref moved since we packed it, we must not delete it */
969 if (!git_oid_equal(¤t_id
, &ref
->oid
))
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.
978 p_unlink(lock
.path_original
);
981 git_buf_free(&ref_content
);
982 git_filebuf_cleanup(&lock
);
987 * Write all the contents in the in-memory packfile to disk.
989 static int packed_write(refdb_fs_backend
*backend
)
991 git_sortedcache
*refcache
= backend
->refcache
;
992 git_filebuf pack_file
= GIT_FILEBUF_INIT
;
996 /* lock the cache to updates while we do this */
997 if ((error
= git_sortedcache_wlock(refcache
)) < 0)
1000 /* Open the file! */
1001 if ((error
= git_filebuf_open(&pack_file
, git_sortedcache_path(refcache
), 0, GIT_PACKEDREFS_FILE_MODE
)) < 0)
1004 /* Packfiles have a header... apparently
1005 * This is in fact not required, but we might as well print it
1007 if ((error
= git_filebuf_printf(&pack_file
, "%s\n", GIT_PACKEDREFS_HEADER
)) < 0)
1010 for (i
= 0; i
< git_sortedcache_entrycount(refcache
); ++i
) {
1011 struct packref
*ref
= git_sortedcache_entry(refcache
, i
);
1014 if ((error
= packed_find_peel(backend
, ref
)) < 0)
1017 if ((error
= packed_write_ref(ref
, &pack_file
)) < 0)
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)
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)
1031 git_sortedcache_updated(refcache
);
1032 git_sortedcache_wunlock(refcache
);
1034 /* we're good now */
1038 git_filebuf_cleanup(&pack_file
);
1039 git_sortedcache_wunlock(refcache
);
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
);
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
)
1052 error
= git_repository__cvar(&logall
, repo
, GIT_CVAR_LOGALLREFUPDATES
);
1056 /* Defaults to the opposite of the repo being bare */
1057 if (logall
== GIT_LOGALLREFUPDATES_UNSET
)
1058 logall
= !git_repository_is_bare(repo
);
1062 } else if (has_reflog(repo
, name
)) {
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
)) {
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
)
1080 git_reference
*old_ref
= NULL
;
1083 /* It "matches" if there is no old value to compare against */
1084 if (!old_id
&& !old_target
)
1087 if ((error
= refdb_fs_backend__lookup(&old_ref
, backend
, name
)) < 0)
1090 /* If the types don't match, there's no way the values do */
1091 if (old_id
&& old_ref
->type
!= GIT_REF_OID
) {
1095 if (old_target
&& old_ref
->type
!= GIT_REF_SYMBOLIC
) {
1100 if (old_id
&& old_ref
->type
== GIT_REF_OID
)
1101 *cmp
= git_oid_cmp(old_id
, &old_ref
->target
.oid
);
1103 if (old_target
&& old_ref
->type
== GIT_REF_SYMBOLIC
)
1104 *cmp
= git__strcmp(old_target
, old_ref
->target
.symbolic
);
1107 git_reference_free(old_ref
);
1113 * The git.git comment regarding this, for your viewing pleasure:
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
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).
1126 static int maybe_append_head(refdb_fs_backend
*backend
, const git_reference
*ref
, const git_signature
*who
, const char *message
)
1129 git_oid old_id
= {{0}};
1130 git_reference
*tmp
= NULL
, *head
= NULL
, *peeled
= NULL
;
1133 if (ref
->type
== GIT_REF_SYMBOLIC
)
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
);
1139 if ((error
= git_reference_lookup(&head
, backend
->repo
, GIT_HEAD_FILE
)) < 0)
1142 if (git_reference_type(head
) == GIT_REF_OID
)
1145 if ((error
= git_reference_lookup(&tmp
, backend
->repo
, GIT_HEAD_FILE
)) < 0)
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
));
1154 git_reference_free(tmp
);
1158 if (error
== GIT_ENOTFOUND
) {
1160 name
= git_reference_symbolic_target(tmp
);
1161 } else if (error
< 0) {
1164 name
= git_reference_name(tmp
);
1167 if (strcmp(name
, ref
->name
))
1170 error
= reflog_append(backend
, head
, &old_id
, git_reference_target(ref
), who
, message
);
1173 git_reference_free(tmp
);
1174 git_reference_free(head
);
1178 static int refdb_fs_backend__write(
1179 git_refdb_backend
*_backend
,
1180 const git_reference
*ref
,
1182 const git_signature
*who
,
1183 const char *message
,
1184 const git_oid
*old_id
,
1185 const char *old_target
)
1187 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1188 git_filebuf file
= GIT_FILEBUF_INIT
;
1193 if ((error
= reference_path_available(backend
, ref
->name
, NULL
, force
)) < 0)
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)
1200 return refdb_fs_backend__write_tail(_backend
, ref
, &file
, true, who
, message
, old_id
, old_target
);
1203 static int refdb_fs_backend__write_tail(
1204 git_refdb_backend
*_backend
,
1205 const git_reference
*ref
,
1208 const git_signature
*who
,
1209 const char *message
,
1210 const git_oid
*old_id
,
1211 const char *old_target
)
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
;
1218 if ((error
= cmp_old_ref(&cmp
, _backend
, ref
->name
, old_id
, old_target
)) < 0)
1222 giterr_set(GITERR_REFERENCE
, "old reference value does not match");
1223 error
= GIT_EMODIFIED
;
1227 if (ref
->type
== GIT_REF_SYMBOLIC
)
1228 new_target
= ref
->target
.symbolic
;
1230 new_id
= &ref
->target
.oid
;
1232 error
= cmp_old_ref(&cmp
, _backend
, ref
->name
, new_id
, new_target
);
1233 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1236 /* Don't update if we have the same value */
1237 if (!error
&& !cmp
) {
1239 goto on_error
; /* not really error */
1242 if (update_reflog
) {
1243 if ((error
= should_write_reflog(&should_write
, backend
->repo
, ref
->name
)) < 0)
1247 if ((error
= reflog_append(backend
, ref
, NULL
, NULL
, who
, message
)) < 0)
1249 if ((error
= maybe_append_head(backend
, ref
, who
, message
)) < 0)
1254 return loose_commit(file
, ref
);
1257 git_filebuf_cleanup(file
);
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
)
1266 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1267 git_filebuf file
= GIT_FILEBUF_INIT
;
1270 assert(backend
&& ref_name
);
1272 if ((error
= loose_lock(&file
, backend
, ref_name
)) < 0)
1275 if ((error
= refdb_reflog_fs__delete(_backend
, ref_name
)) < 0) {
1276 git_filebuf_cleanup(&file
);
1280 return refdb_fs_backend__delete_tail(_backend
, &file
, ref_name
, old_id
, old_target
);
1283 static int refdb_fs_backend__delete_tail(
1284 git_refdb_backend
*_backend
,
1286 const char *ref_name
,
1287 const git_oid
*old_id
, const char *old_target
)
1289 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1290 git_buf loose_path
= GIT_BUF_INIT
;
1292 int error
= 0, cmp
= 0;
1293 bool loose_deleted
= 0;
1295 error
= cmp_old_ref(&cmp
, _backend
, ref_name
, old_id
, old_target
);
1300 giterr_set(GITERR_REFERENCE
, "old reference value does not match");
1301 error
= GIT_EMODIFIED
;
1305 /* If a loose reference exists, remove it from the filesystem */
1306 if (git_buf_joinpath(&loose_path
, backend
->gitpath
, ref_name
) < 0)
1310 error
= p_unlink(loose_path
.ptr
);
1311 if (error
< 0 && errno
== ENOENT
)
1315 else if (error
== 0)
1318 if ((error
= packed_reload(backend
)) < 0)
1321 /* If a packed reference exists, remove it from the packfile and repack */
1322 if ((error
= git_sortedcache_wlock(backend
->refcache
)) < 0)
1325 if (!(error
= git_sortedcache_lookup_index(
1326 &pack_pos
, backend
->refcache
, ref_name
)))
1327 error
= git_sortedcache_remove(backend
->refcache
, pack_pos
);
1329 git_sortedcache_wunlock(backend
->refcache
);
1331 if (error
== GIT_ENOTFOUND
) {
1332 error
= loose_deleted
? 0 : ref_error_notfound(ref_name
);
1336 error
= packed_write(backend
);
1339 git_buf_free(&loose_path
);
1340 git_filebuf_cleanup(file
);
1345 static int refdb_reflog_fs__rename(git_refdb_backend
*_backend
, const char *old_name
, const char *new_name
);
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
,
1353 const git_signature
*who
,
1354 const char *message
)
1356 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1357 git_reference
*old
, *new;
1358 git_filebuf file
= GIT_FILEBUF_INIT
;
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)
1368 if ((error
= refdb_fs_backend__delete(_backend
, old_name
, NULL
, NULL
)) < 0) {
1369 git_reference_free(old
);
1373 new = git_reference__set_name(old
, new_name
);
1375 git_reference_free(old
);
1379 if ((error
= loose_lock(&file
, backend
, new->name
)) < 0) {
1380 git_reference_free(new);
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
);
1394 git_reference_free(new);
1395 git_filebuf_cleanup(&file
);
1400 if ((error
= loose_commit(&file
, new)) < 0 || out
== NULL
) {
1401 git_reference_free(new);
1409 static int refdb_fs_backend__compress(git_refdb_backend
*_backend
)
1412 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
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 */
1424 static void refdb_fs_backend__free(git_refdb_backend
*_backend
)
1426 refdb_fs_backend
*backend
= (refdb_fs_backend
*)_backend
;
1430 git_sortedcache_free(backend
->refcache
);
1431 git__free(backend
->gitpath
);
1432 git__free(backend
->commonpath
);
1436 static char *setup_namespace(git_repository
*repo
, const char *in
)
1438 git_buf path
= GIT_BUF_INIT
;
1439 char *parts
, *start
, *end
, *out
= NULL
;
1444 git_buf_puts(&path
, in
);
1446 /* if the repo is not namespaced, nothing else to do */
1447 if (repo
->namespace == NULL
) {
1448 out
= git_buf_detach(&path
);
1452 parts
= end
= git__strdup(repo
->namespace);
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/
1462 while ((start
= git__strsep(&end
, "/")) != NULL
)
1463 git_buf_printf(&path
, "refs/namespaces/%s/", start
);
1465 git_buf_printf(&path
, "refs/namespaces/%s/refs", end
);
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)
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
);
1478 git_buf_free(&path
);
1482 static int reflog_alloc(git_reflog
**reflog
, const char *name
)
1488 log
= git__calloc(1, sizeof(git_reflog
));
1489 GITERR_CHECK_ALLOC(log
);
1491 log
->ref_name
= git__strdup(name
);
1492 GITERR_CHECK_ALLOC(log
->ref_name
);
1494 if (git_vector_init(&log
->entries
, 0, NULL
) < 0) {
1495 git__free(log
->ref_name
);
1505 static int reflog_parse(git_reflog
*log
, const char *buf
, size_t buf_size
)
1508 git_reflog_entry
*entry
;
1510 #define seek_forward(_increase) do { \
1511 if (_increase >= buf_size) { \
1512 giterr_set(GITERR_INVALID, "ran out of data while parsing reflog"); \
1516 buf_size -= _increase; \
1519 while (buf_size
> GIT_REFLOG_SIZE_MIN
) {
1520 entry
= git__calloc(1, sizeof(git_reflog_entry
));
1521 GITERR_CHECK_ALLOC(entry
);
1523 entry
->committer
= git__calloc(1, sizeof(git_signature
));
1524 GITERR_CHECK_ALLOC(entry
->committer
);
1526 if (git_oid_fromstrn(&entry
->oid_old
, buf
, GIT_OID_HEXSZ
) < 0)
1528 seek_forward(GIT_OID_HEXSZ
+ 1);
1530 if (git_oid_fromstrn(&entry
->oid_cur
, buf
, GIT_OID_HEXSZ
) < 0)
1532 seek_forward(GIT_OID_HEXSZ
+ 1);
1536 /* Seek forward to the end of the signature. */
1537 while (*buf
&& *buf
!= '\t' && *buf
!= '\n')
1540 if (git_signature__parse(entry
->committer
, &ptr
, buf
+ 1, NULL
, *buf
) < 0)
1544 /* We got a message. Read everything till we reach LF. */
1548 while (*buf
&& *buf
!= '\n')
1551 entry
->msg
= git__strndup(ptr
, buf
- ptr
);
1552 GITERR_CHECK_ALLOC(entry
->msg
);
1556 while (*buf
&& *buf
== '\n' && buf_size
> 1)
1559 if (git_vector_insert(&log
->entries
, entry
) < 0)
1568 git_reflog_entry__free(entry
);
1573 static int create_new_reflog_file(const char *filepath
)
1577 if ((error
= git_futils_mkpath2file(filepath
, GIT_REFLOG_DIR_MODE
)) < 0)
1580 if ((fd
= p_open(filepath
,
1582 GIT_REFLOG_FILE_MODE
)) < 0)
1588 GIT_INLINE(int) retrieve_reflog_path(git_buf
*path
, git_repository
*repo
, const char *name
)
1590 return git_buf_join3(path
, '/', repo
->commondir
, GIT_REFLOG_DIR
, name
);
1593 static int refdb_reflog_fs__ensure_log(git_refdb_backend
*_backend
, const char *name
)
1595 refdb_fs_backend
*backend
;
1596 git_repository
*repo
;
1597 git_buf path
= GIT_BUF_INIT
;
1600 assert(_backend
&& name
);
1602 backend
= (refdb_fs_backend
*) _backend
;
1603 repo
= backend
->repo
;
1605 if ((error
= retrieve_reflog_path(&path
, repo
, name
)) < 0)
1608 error
= create_new_reflog_file(git_buf_cstr(&path
));
1609 git_buf_free(&path
);
1614 static int has_reflog(git_repository
*repo
, const char *name
)
1617 git_buf path
= GIT_BUF_INIT
;
1619 if (retrieve_reflog_path(&path
, repo
, name
) < 0)
1622 ret
= git_path_isfile(git_buf_cstr(&path
));
1625 git_buf_free(&path
);
1629 static int refdb_reflog_fs__has_log(git_refdb_backend
*_backend
, const char *name
)
1631 refdb_fs_backend
*backend
;
1633 assert(_backend
&& name
);
1635 backend
= (refdb_fs_backend
*) _backend
;
1637 return has_reflog(backend
->repo
, name
);
1640 static int refdb_reflog_fs__read(git_reflog
**out
, git_refdb_backend
*_backend
, const char *name
)
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
;
1649 assert(out
&& _backend
&& name
);
1651 backend
= (refdb_fs_backend
*) _backend
;
1652 repo
= backend
->repo
;
1654 if (reflog_alloc(&log
, name
) < 0)
1657 if (retrieve_reflog_path(&log_path
, repo
, name
) < 0)
1660 error
= git_futils_readbuffer(&log_file
, git_buf_cstr(&log_path
));
1661 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1664 if ((error
== GIT_ENOTFOUND
) &&
1665 ((error
= create_new_reflog_file(git_buf_cstr(&log_path
))) < 0))
1668 if ((error
= reflog_parse(log
,
1669 git_buf_cstr(&log_file
), git_buf_len(&log_file
))) < 0)
1676 git_reflog_free(log
);
1679 git_buf_free(&log_file
);
1680 git_buf_free(&log_path
);
1685 static int serialize_reflog_entry(
1687 const git_oid
*oid_old
,
1688 const git_oid
*oid_new
,
1689 const git_signature
*committer
,
1692 char raw_old
[GIT_OID_HEXSZ
+1];
1693 char raw_new
[GIT_OID_HEXSZ
+1];
1695 git_oid_tostr(raw_old
, GIT_OID_HEXSZ
+1, oid_old
);
1696 git_oid_tostr(raw_new
, GIT_OID_HEXSZ
+1, oid_new
);
1700 git_buf_puts(buf
, raw_old
);
1701 git_buf_putc(buf
, ' ');
1702 git_buf_puts(buf
, raw_new
);
1704 git_signature__writebuf(buf
, " ", committer
);
1706 /* drop trailing LF */
1710 git_buf_putc(buf
, '\t');
1711 git_buf_puts(buf
, msg
);
1714 git_buf_putc(buf
, '\n');
1716 return git_buf_oom(buf
);
1719 static int lock_reflog(git_filebuf
*file
, refdb_fs_backend
*backend
, const char *refname
)
1721 git_repository
*repo
;
1722 git_buf log_path
= GIT_BUF_INIT
;
1725 repo
= backend
->repo
;
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
;
1732 if (retrieve_reflog_path(&log_path
, repo
, refname
) < 0)
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
);
1742 error
= git_filebuf_open(file
, git_buf_cstr(&log_path
), 0, GIT_REFLOG_FILE_MODE
);
1745 git_buf_free(&log_path
);
1750 static int refdb_reflog_fs__write(git_refdb_backend
*_backend
, git_reflog
*reflog
)
1754 git_reflog_entry
*entry
;
1755 refdb_fs_backend
*backend
;
1756 git_buf log
= GIT_BUF_INIT
;
1757 git_filebuf fbuf
= GIT_FILEBUF_INIT
;
1759 assert(_backend
&& reflog
);
1761 backend
= (refdb_fs_backend
*) _backend
;
1763 if ((error
= lock_reflog(&fbuf
, backend
, reflog
->ref_name
)) < 0)
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)
1770 if ((error
= git_filebuf_write(&fbuf
, log
.ptr
, log
.size
)) < 0)
1774 error
= git_filebuf_commit(&fbuf
);
1778 git_filebuf_cleanup(&fbuf
);
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
)
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
;
1794 is_symbolic
= ref
->type
== GIT_REF_SYMBOLIC
;
1796 /* "normal" symbolic updates do not write */
1798 strcmp(ref
->name
, GIT_HEAD_FILE
) &&
1802 /* From here on is_symoblic also means that it's HEAD */
1805 git_oid_cpy(&old_id
, old
);
1807 error
= git_reference_name_to_id(&old_id
, repo
, ref
->name
);
1808 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1813 git_oid_cpy(&new_id
, new);
1816 git_oid_cpy(&new_id
, git_reference_target(ref
));
1818 error
= git_reference_name_to_id(&new_id
, repo
, git_reference_symbolic_target(ref
));
1819 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1821 /* detaching HEAD does not create an entry */
1822 if (error
== GIT_ENOTFOUND
)
1829 if ((error
= serialize_reflog_entry(&buf
, &old_id
, &new_id
, who
, message
)) < 0)
1832 if ((error
= retrieve_reflog_path(&path
, repo
, ref
->name
)) < 0)
1835 if (((error
= git_futils_mkpath2file(git_buf_cstr(&path
), 0777)) < 0) &&
1836 (error
!= GIT_EEXISTS
)) {
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.
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
)
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",
1850 error
= GIT_EDIRECTORY
;
1857 error
= git_futils_writebuffer(&buf
, git_buf_cstr(&path
), O_WRONLY
|O_CREAT
|O_APPEND
, GIT_REFLOG_FILE_MODE
);
1861 git_buf_free(&path
);
1866 static int refdb_reflog_fs__rename(git_refdb_backend
*_backend
, const char *old_name
, const char *new_name
)
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
;
1876 assert(_backend
&& old_name
&& new_name
);
1878 backend
= (refdb_fs_backend
*) _backend
;
1879 repo
= backend
->repo
;
1881 if ((error
= git_reference__normalize_name(
1882 &normalized
, new_name
, GIT_REF_FORMAT_ALLOW_ONELEVEL
)) < 0)
1885 if (git_buf_joinpath(&temp_path
, repo
->gitdir
, GIT_REFLOG_DIR
) < 0)
1888 if (git_buf_joinpath(&old_path
, git_buf_cstr(&temp_path
), old_name
) < 0)
1891 if (git_buf_joinpath(&new_path
, git_buf_cstr(&temp_path
), git_buf_cstr(&normalized
)) < 0)
1894 if (!git_path_exists(git_buf_cstr(&old_path
))) {
1895 error
= GIT_ENOTFOUND
;
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:
1904 * - a/b/c/d -> a/b/c
1906 if (git_buf_joinpath(&temp_path
, git_buf_cstr(&temp_path
), "temp_reflog") < 0)
1909 if ((fd
= git_futils_mktmp(&temp_path
, git_buf_cstr(&temp_path
), GIT_REFLOG_FILE_MODE
)) < 0) {
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
);
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)) {
1928 if (git_futils_mkpath2file(git_buf_cstr(&new_path
), GIT_REFLOG_DIR_MODE
) < 0) {
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
);
1939 git_buf_free(&temp_path
);
1940 git_buf_free(&old_path
);
1941 git_buf_free(&new_path
);
1942 git_buf_free(&normalized
);
1947 static int refdb_reflog_fs__delete(git_refdb_backend
*_backend
, const char *name
)
1950 git_buf path
= GIT_BUF_INIT
;
1952 git_repository
*repo
;
1953 refdb_fs_backend
*backend
;
1955 assert(_backend
&& name
);
1957 backend
= (refdb_fs_backend
*) _backend
;
1958 repo
= backend
->repo
;
1960 error
= retrieve_reflog_path(&path
, repo
, name
);
1962 if (!error
&& git_path_exists(path
.ptr
))
1963 error
= p_unlink(path
.ptr
);
1965 git_buf_free(&path
);
1971 int git_refdb_backend_fs(
1972 git_refdb_backend
**backend_out
,
1973 git_repository
*repository
)
1976 git_buf gitpath
= GIT_BUF_INIT
;
1977 refdb_fs_backend
*backend
;
1979 backend
= git__calloc(1, sizeof(refdb_fs_backend
));
1980 GITERR_CHECK_ALLOC(backend
);
1982 backend
->repo
= repository
;
1984 if (repository
->gitdir
) {
1985 backend
->gitpath
= setup_namespace(repository
, repository
->gitdir
);
1987 if (backend
->gitpath
== NULL
)
1991 if (repository
->commondir
) {
1992 backend
->commonpath
= setup_namespace(repository
, repository
->commondir
);
1994 if (backend
->commonpath
== NULL
)
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)
2004 git_buf_free(&gitpath
);
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
;
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
;
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
;
2032 *backend_out
= (git_refdb_backend
*)backend
;
2036 git_buf_free(&gitpath
);
2037 git__free(backend
->gitpath
);
2038 git__free(backend
->commonpath
);