int git_repository_index__weakptr(git_index **out, git_repository *repo) { int error = 0; assert(out && repo); if (repo->_index == NULL) { git_buf index_path = GIT_BUF_INIT; git_index *index; if ((error = git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE)) < 0) return error; error = git_index_open(&index, index_path.ptr); if (!error) { GIT_REFCOUNT_OWN(index, repo); index = (git_index*) git__compare_and_swap(&repo->_index, NULL, index); if (index != NULL) { GIT_REFCOUNT_OWN(index, NULL); git_index_free(index); } error = git_index_set_caps(repo->_index, GIT_INDEXCAP_FROM_OWNER); } git_buf_free(&index_path); } *out = repo->_index; return error; }
int git_repository_odb__weakptr(git_odb **out, git_repository *repo) { int error = 0; assert(repo && out); if (repo->_odb == NULL) { git_buf odb_path = GIT_BUF_INIT; git_odb *odb; if ((error = git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR)) < 0) return error; error = git_odb_open(&odb, odb_path.ptr); if (!error) { GIT_REFCOUNT_OWN(odb, repo); odb = (git_odb*) git__compare_and_swap(&repo->_odb, NULL, odb); if (odb != NULL) { GIT_REFCOUNT_OWN(odb, NULL); git_odb_free(odb); } } git_buf_free(&odb_path); } *out = repo->_odb; return error; }
static git_diff_driver_registry *git_repository_driver_registry( git_repository *repo) { if (!repo->diff_drivers) { git_diff_driver_registry *reg = git_diff_driver_registry_new(); reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg); if (reg != NULL) /* if we race, free losing allocation */ git_diff_driver_registry_free(reg); } if (!repo->diff_drivers) giterr_set(GITERR_REPOSITORY, "Unable to create diff driver registry"); return repo->diff_drivers; }
int git_repository_config__weakptr(git_config **out, git_repository *repo) { int error = 0; if (repo->_config == NULL) { git_buf global_buf = GIT_BUF_INIT; git_buf xdg_buf = GIT_BUF_INIT; git_buf system_buf = GIT_BUF_INIT; git_config *config; git_config_find_global(&global_buf); git_config_find_xdg(&xdg_buf); git_config_find_system(&system_buf); /* If there is no global file, open a backend for it anyway */ if (git_buf_len(&global_buf) == 0) git_config__global_location(&global_buf); error = load_config( &config, repo, path_unless_empty(&global_buf), path_unless_empty(&xdg_buf), path_unless_empty(&system_buf)); if (!error) { GIT_REFCOUNT_OWN(config, repo); config = (git_config*) git__compare_and_swap(&repo->_config, NULL, config); if (config != NULL) { GIT_REFCOUNT_OWN(config, NULL); git_config_free(config); } } git_buf_free(&global_buf); git_buf_free(&xdg_buf); git_buf_free(&system_buf); } *out = repo->_config; return error; }
static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file) { int error = 0; git_attr_file_entry *entry; if (!file) return 0; if ((error = attr_cache_lock(cache)) < 0) return error; if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL) file = git__compare_and_swap(&entry->file[file->source], file, NULL); attr_cache_unlock(cache); if (file) { GIT_REFCOUNT_OWN(file, NULL); git_attr_file__free(file); } return error; }
/* insert entry or replace existing if we raced with another thread */ static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file) { git_attr_file_entry *entry; git_attr_file *old; if (attr_cache_lock(cache) < 0) return -1; entry = attr_cache_lookup_entry(cache, file->entry->path); GIT_REFCOUNT_OWN(file, entry); GIT_REFCOUNT_INC(file); old = git__compare_and_swap( &entry->file[file->source], entry->file[file->source], file); if (old) { GIT_REFCOUNT_OWN(old, NULL); git_attr_file__free(old); } attr_cache_unlock(cache); return 0; }
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo) { int error = 0; assert(out && repo); if (repo->_refdb == NULL) { git_refdb *refdb; error = git_refdb_open(&refdb, repo); if (!error) { GIT_REFCOUNT_OWN(refdb, repo); refdb = (git_refdb*) git__compare_and_swap(&repo->_refdb, NULL, refdb); if (refdb != NULL) { GIT_REFCOUNT_OWN(refdb, NULL); git_refdb_free(refdb); } } } *out = repo->_refdb; return error; }