static git_diff_parsed *diff_parsed_alloc(void) { git_diff_parsed *diff; if ((diff = git__calloc(1, sizeof(git_diff_parsed))) == NULL) return NULL; GIT_REFCOUNT_INC(diff); diff->base.type = GIT_DIFF_TYPE_PARSED; diff->base.strcomp = git__strcmp; diff->base.strncomp = git__strncmp; diff->base.pfxcomp = git__prefixcmp; diff->base.entrycomp = git_diff__entry_cmp; diff->base.patch_fn = git_patch_parsed_from_diff; diff->base.free_fn = diff_parsed_free; if (git_diff_init_options(&diff->base.opts, GIT_DIFF_OPTIONS_VERSION) < 0) { git__free(diff); return NULL; } diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE; git_pool_init(&diff->base.pool, 1); if (git_vector_init(&diff->patches, 0, NULL) < 0 || git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) { git_diff_free(&diff->base); return NULL; } git_vector_set_cmp(&diff->base.deltas, git_diff_delta__cmp); return diff; }
int git_remote_dup(git_remote **dest, git_remote *source) { int error = 0; git_strarray refspecs = { 0 }; git_remote *remote = git__calloc(1, sizeof(git_remote)); GITERR_CHECK_ALLOC(remote); if (source->name != NULL) { remote->name = git__strdup(source->name); GITERR_CHECK_ALLOC(remote->name); } if (source->url != NULL) { remote->url = git__strdup(source->url); GITERR_CHECK_ALLOC(remote->url); } if (source->pushurl != NULL) { remote->pushurl = git__strdup(source->pushurl); GITERR_CHECK_ALLOC(remote->pushurl); } remote->transport_cb = source->transport_cb; remote->transport_cb_payload = source->transport_cb_payload; remote->repo = source->repo; remote->download_tags = source->download_tags; remote->check_cert = source->check_cert; remote->update_fetchhead = source->update_fetchhead; if (git_vector_init(&remote->refs, 32, NULL) < 0 || git_vector_init(&remote->refspecs, 2, NULL) < 0 || git_vector_init(&remote->active_refspecs, 2, NULL) < 0) { error = -1; goto cleanup; } if ((error = git_remote_get_fetch_refspecs(&refspecs, source)) < 0 || (error = git_remote_set_fetch_refspecs(remote, &refspecs)) < 0) goto cleanup; git_strarray_free(&refspecs); if ((error = git_remote_get_push_refspecs(&refspecs, source)) < 0 || (error = git_remote_set_push_refspecs(remote, &refspecs)) < 0) goto cleanup; *dest = remote; cleanup: git_strarray_free(&refspecs); if (error < 0) git__free(remote); return error; }
void test_iterator_workdir__pathlist_with_directory_include_trees(void) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_vector filelist; const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt", "subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt", }; size_t expected_len = 6; cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "subdir/")); g_repo = cl_git_sandbox_init("testrepo2"); i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); expect_iterator_items(i, expected_len, expected, expected_len, expected); git_iterator_free(i); git_vector_free(&filelist); }
static int index_initialize(git_index **index_out, git_repository *owner, const char *index_path) { git_index *index; assert(index_out && index_path); index = git__malloc(sizeof(git_index)); if (index == NULL) return GIT_ENOMEM; memset(index, 0x0, sizeof(git_index)); index->index_file_path = git__strdup(index_path); if (index->index_file_path == NULL) { free(index); return GIT_ENOMEM; } index->repository = owner; git_vector_init(&index->entries, 32, index_cmp); /* Check if index file is stored on disk already */ if (git_futils_exists(index->index_file_path) == 0) index->on_disk = 1; *index_out = index; return git_index_read(index); }
/* insert_sorted with duplicates */ void test_core_vector__4(void) { git_vector x; intptr_t i; git_vector_init(&x, 1, &compare_them); for (i = 0; i < 10; i += 2) { git_vector_insert_sorted(&x, (void*)(i + 1), NULL); } for (i = 9; i > 0; i -= 2) { git_vector_insert_sorted(&x, (void*)(i + 1), NULL); } for (i = 0; i < 10; i += 2) { git_vector_insert_sorted(&x, (void*)(i + 1), NULL); } for (i = 9; i > 0; i -= 2) { git_vector_insert_sorted(&x, (void*)(i + 1), NULL); } cl_assert(x.length == 20); for (i = 0; i < 20; ++i) { cl_assert(git_vector_get(&x, i) == (void*)(i / 2 + 1)); } git_vector_free(&x); }
/* * Since this is a network connection, we need to parse and store the * pkt-lines at this stage and keep them there. */ static int git_connect(git_transport *transport, int direction) { transport_git *t = (transport_git *) transport; if (direction == GIT_DIR_PUSH) { giterr_set(GITERR_NET, "Pushing over git:// is not supported"); return -1; } t->parent.direction = direction; if (git_vector_init(&t->refs, 16, NULL) < 0) return -1; /* Connect and ask for the refs */ if (do_connect(t, transport->url) < 0) goto cleanup; gitno_buffer_setup(&t->buf, t->buff, sizeof(t->buff), t->socket); t->parent.connected = 1; if (store_refs(t) < 0) goto cleanup; if (detect_caps(t) < 0) goto cleanup; return 0; cleanup: git_vector_free(&t->refs); return -1; }
static int store_refs(transport_local *t) { unsigned int i; git_strarray ref_names = {0}; git_transport *transport = (git_transport *) t; assert(t); if (git_reference_list(&ref_names, t->repo, GIT_REF_LISTALL) < 0 || git_vector_init(&transport->refs, (unsigned int)ref_names.count, NULL) < 0) goto on_error; /* Sort the references first */ git__tsort((void **)ref_names.strings, ref_names.count, &git__strcmp_cb); /* Add HEAD */ if (add_ref(t, GIT_HEAD_FILE) < 0) goto on_error; for (i = 0; i < ref_names.count; ++i) { if (add_ref(t, ref_names.strings[i]) < 0) goto on_error; } git_strarray_free(&ref_names); return 0; on_error: git_vector_free(&transport->refs); git_strarray_free(&ref_names); return -1; }
static int pack_backend__alloc(struct pack_backend **out, size_t initial_size) { struct pack_backend *backend = (pack_backend*) git__calloc(1, sizeof(struct pack_backend)); GITERR_CHECK_ALLOC(backend); if (git_vector_init(&backend->packs, initial_size, packfile_sort__cb) < 0) { git__free(backend); return -1; } backend->parent.version = GIT_ODB_BACKEND_VERSION; backend->parent.read = &pack_backend__read; backend->parent.read_prefix = &pack_backend__read_prefix; backend->parent.read_header = &pack_backend__read_header; backend->parent.exists = &pack_backend__exists; backend->parent.exists_prefix = &pack_backend__exists_prefix; backend->parent.refresh = &pack_backend__refresh; backend->parent.foreach = &pack_backend__foreach; backend->parent.writepack = &pack_backend__writepack; backend->parent.free = &pack_backend__free; *out = backend; return 0; }
int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source) { git_treebuilder *bld; unsigned int i, source_entries = DEFAULT_TREE_SIZE; assert(builder_p); bld = git__calloc(1, sizeof(git_treebuilder)); if (bld == NULL) return GIT_ENOMEM; if (source != NULL) source_entries = source->entries.length; if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < GIT_SUCCESS) { free(bld); return GIT_ENOMEM; } if (source != NULL) { for (i = 0; i < source->entries.length; ++i) { git_tree_entry *entry_src = source->entries.contents[i]; if (append_entry(bld, entry_src->filename, &entry_src->oid, entry_src->attr) < 0) { git_treebuilder_free(bld); return GIT_ENOMEM; } } } *builder_p = bld; return GIT_SUCCESS; }
int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir) { struct pack_backend *backend; char path[GIT_PATH_MAX]; backend = git__calloc(1, sizeof(struct pack_backend)); if (backend == NULL) return GIT_ENOMEM; if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < GIT_SUCCESS) { free(backend); return GIT_ENOMEM; } git_path_join(path, objects_dir, "pack"); if (git_futils_isdir(path) == GIT_SUCCESS) { backend->pack_folder = git__strdup(path); backend->pack_folder_mtime = 0; if (backend->pack_folder == NULL) { free(backend); return GIT_ENOMEM; } } backend->parent.read = &pack_backend__read; backend->parent.read_prefix = &pack_backend__read_prefix; backend->parent.read_header = NULL; backend->parent.exists = &pack_backend__exists; backend->parent.free = &pack_backend__free; *backend_out = (git_odb_backend *)backend; return GIT_SUCCESS; }
/* remove duplicates */ void test_core_vector__2(void) { git_vector x; int *ptrs[2]; ptrs[0] = git__malloc(sizeof(int)); ptrs[1] = git__malloc(sizeof(int)); *ptrs[0] = 2; *ptrs[1] = 1; cl_git_pass(git_vector_init(&x, 5, test_cmp)); cl_git_pass(git_vector_insert(&x, ptrs[0])); cl_git_pass(git_vector_insert(&x, ptrs[1])); cl_git_pass(git_vector_insert(&x, ptrs[1])); cl_git_pass(git_vector_insert(&x, ptrs[0])); cl_git_pass(git_vector_insert(&x, ptrs[1])); cl_assert(x.length == 5); git_vector_uniq(&x, NULL); cl_assert(x.length == 2); git_vector_free(&x); git__free(ptrs[0]); git__free(ptrs[1]); }
int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) { git_revwalk *walk; walk = git__malloc(sizeof(git_revwalk)); if (walk == NULL) return GIT_ENOMEM; memset(walk, 0x0, sizeof(git_revwalk)); walk->commits = git_hashtable_alloc(64, object_table_hash, (git_hash_keyeq_ptr)git_oid_cmp); if (walk->commits == NULL) { free(walk); return GIT_ENOMEM; } git_pqueue_init(&walk->iterator_time, 8, commit_time_cmp); git_vector_init(&walk->memory_alloc, 8, NULL); alloc_chunk(walk); walk->get_next = &revwalk_next_unsorted; walk->enqueue = &revwalk_enqueue_unsorted; walk->repo = repo; *revwalk_out = walk; return GIT_SUCCESS; }
/* build a vector of fnmatch patterns to evaluate efficiently */ int git_pathspec_init( git_vector *vspec, const git_strarray *strspec, git_pool *strpool) { size_t i; memset(vspec, 0, sizeof(*vspec)); if (!git_pathspec_is_interesting(strspec)) return 0; if (git_vector_init(vspec, strspec->count, NULL) < 0) return -1; for (i = 0; i < strspec->count; ++i) { int ret; const char *pattern = strspec->strings[i]; git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch)); if (!match) return -1; match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE; ret = git_attr_fnmatch__parse(match, strpool, NULL, &pattern); if (ret == GIT_ENOTFOUND) { git__free(match); continue; } else if (ret < 0) return ret; if (git_vector_insert(vspec, match) < 0) return -1; } return 0; }
static int read_unmerged(git_index *index, const char *buffer, size_t size) { const char *endptr; size_t len; int i; git_vector_init(&index->unmerged, 16, unmerged_cmp); while (size) { git_index_entry_unmerged *lost; len = strlen(buffer) + 1; if (size <= len) return git__throw(GIT_ERROR, "Failed to read unmerged entries"); if ((lost = git__malloc(sizeof(git_index_entry_unmerged))) == NULL) return GIT_ENOMEM; if (git_vector_insert(&index->unmerged, lost) < GIT_SUCCESS) return git__throw(GIT_ERROR, "Failed to read unmerged entries"); lost->path = git__strdup(buffer); if (!lost->path) return GIT_ENOMEM; size -= len; buffer += len; for (i = 0; i < 3; i++) { long tmp; if (git__strtol32(&tmp, buffer, &endptr, 8) < GIT_SUCCESS || !endptr || endptr == buffer || *endptr || tmp > UINT_MAX) return GIT_ERROR; lost->mode[i] = tmp; len = (endptr + 1) - buffer; if (size <= len) return git__throw(GIT_ERROR, "Failed to read unmerged entries"); size -= len; buffer += len; } for (i = 0; i < 3; i++) { if (!lost->mode[i]) continue; if (size < 20) return git__throw(GIT_ERROR, "Failed to read unmerged entries"); git_oid_fromraw(&lost->oid[i], (unsigned char *) buffer); size -= 20; buffer += 20; } } return GIT_SUCCESS; }
static int filter_wants(git_remote *remote) { git_vector list; git_headarray refs; git_transport *t = remote->transport; git_repository *repo = remote->repo; const git_refspec *spec; int error; unsigned int i; error = git_vector_init(&list, 16, NULL); if (error < GIT_SUCCESS) return error; error = t->ls(t, &refs); if (error < GIT_SUCCESS) { error = git__rethrow(error, "Failed to get remote ref list"); goto cleanup; } spec = git_remote_fetchspec(remote); if (spec == NULL) { error = git__throw(GIT_ERROR, "The remote has no fetchspec"); goto cleanup; } for (i = 0; i < refs.len; ++i) { git_remote_head *head = refs.heads[i]; /* If it doesn't match the refpec, we don't want it */ error = git_refspec_src_match(spec, head->name); if (error == GIT_ENOMATCH) continue; if (error < GIT_SUCCESS) { error = git__rethrow(error, "Error matching remote ref name"); goto cleanup; } /* If we have the object, mark it so we don't ask for it */ if (git_odb_exists(repo->db, &head->oid)) head->local = 1; else remote->need_pack = 1; error = git_vector_insert(&list, head); if (error < GIT_SUCCESS) goto cleanup; } remote->refs.len = list.length; remote->refs.heads = (git_remote_head **) list.contents; return GIT_SUCCESS; cleanup: git_vector_free(&list); return error; }
static int http_connect(git_transport *transport, int direction) { transport_http *t = (transport_http *) transport; int ret; git_buf request = GIT_BUF_INIT; const char *service = "upload-pack"; const char *url = t->parent.url, *prefix_http = "http://", *prefix_https = "https://"; const char *default_port; if (direction == GIT_DIR_PUSH) { giterr_set(GITERR_NET, "Pushing over HTTP is not implemented"); return -1; } t->parent.direction = direction; if (git_vector_init(&t->refs, 16, NULL) < 0) return -1; if (!git__prefixcmp(url, prefix_http)) { url = t->parent.url + strlen(prefix_http); default_port = "80"; } if (!git__prefixcmp(url, prefix_https)) { url += strlen(prefix_https); default_port = "443"; } t->path = strchr(url, '/'); if ((ret = gitno_extract_host_and_port(&t->host, &t->port, url, default_port)) < 0) goto cleanup; t->service = git__strdup(service); GITERR_CHECK_ALLOC(t->service); if ((ret = do_connect(t, t->host, t->port)) < 0) goto cleanup; /* Generate and send the HTTP request */ if ((ret = gen_request(&request, t->path, t->host, "GET", service, 0, 1)) < 0) { giterr_set(GITERR_NET, "Failed to generate request"); goto cleanup; } if (gitno_send(transport, request.ptr, request.size, 0) < 0) goto cleanup; ret = store_refs(t); cleanup: git_buf_free(&request); git_buf_clear(&t->buf); return ret; }
void test_repo_iterator__indexfilelist(void) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_index *index; git_vector filelist; int default_icase; int expect; cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "a")); cl_git_pass(git_vector_insert(&filelist, "B")); cl_git_pass(git_vector_insert(&filelist, "c")); cl_git_pass(git_vector_insert(&filelist, "D")); cl_git_pass(git_vector_insert(&filelist, "e")); cl_git_pass(git_vector_insert(&filelist, "k/1")); cl_git_pass(git_vector_insert(&filelist, "k/a")); cl_git_pass(git_vector_insert(&filelist, "L/1")); g_repo = cl_git_sandbox_init("icase"); cl_git_pass(git_repository_index(&index, g_repo)); /* In this test we DO NOT force a case setting on the index. */ default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ cl_git_pass(git_iterator_for_index(&i, index, &i_opts)); expect_iterator_items(i, 8, NULL, 8, NULL); git_iterator_free(i); i_opts.start = "c"; i_opts.end = NULL; cl_git_pass(git_iterator_for_index(&i, index, &i_opts)); /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ expect = ((default_icase) ? 6 : 4); expect_iterator_items(i, expect, NULL, expect, NULL); git_iterator_free(i); i_opts.start = NULL; i_opts.end = "e"; cl_git_pass(git_iterator_for_index(&i, index, &i_opts)); /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ expect = ((default_icase) ? 5 : 6); expect_iterator_items(i, expect, NULL, expect, NULL); git_iterator_free(i); git_index_free(index); git_vector_free(&filelist); }
void test_repo_iterator__treefilelist_icase(void) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_vector filelist; git_tree *tree; cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "a")); cl_git_pass(git_vector_insert(&filelist, "B")); cl_git_pass(git_vector_insert(&filelist, "c")); cl_git_pass(git_vector_insert(&filelist, "D")); cl_git_pass(git_vector_insert(&filelist, "e")); cl_git_pass(git_vector_insert(&filelist, "k.a")); cl_git_pass(git_vector_insert(&filelist, "k.b")); cl_git_pass(git_vector_insert(&filelist, "k/1")); cl_git_pass(git_vector_insert(&filelist, "k/a")); cl_git_pass(git_vector_insert(&filelist, "kZZZZ")); cl_git_pass(git_vector_insert(&filelist, "L/1")); g_repo = cl_git_sandbox_init("icase"); git_repository_head_tree(&tree, g_repo); i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; i_opts.start = "c"; i_opts.end = "k/D"; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); expect_iterator_items(i, 3, NULL, 3, NULL); git_iterator_free(i); i_opts.start = "k"; i_opts.end = "k/Z"; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); expect_iterator_items(i, 1, NULL, 1, NULL); git_iterator_free(i); i_opts.flags = GIT_ITERATOR_IGNORE_CASE; i_opts.start = "c"; i_opts.end = "k/D"; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); expect_iterator_items(i, 5, NULL, 5, NULL); git_iterator_free(i); i_opts.start = "k"; i_opts.end = "k/Z"; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); expect_iterator_items(i, 2, NULL, 2, NULL); git_iterator_free(i); git_vector_free(&filelist); git_tree_free(tree); }
void test_repo_iterator__treefilelist(void) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_vector filelist; git_tree *tree; bool default_icase; int expect; cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "a")); cl_git_pass(git_vector_insert(&filelist, "B")); cl_git_pass(git_vector_insert(&filelist, "c")); cl_git_pass(git_vector_insert(&filelist, "D")); cl_git_pass(git_vector_insert(&filelist, "e")); cl_git_pass(git_vector_insert(&filelist, "k.a")); cl_git_pass(git_vector_insert(&filelist, "k.b")); cl_git_pass(git_vector_insert(&filelist, "k/1")); cl_git_pass(git_vector_insert(&filelist, "k/a")); cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); cl_git_pass(git_vector_insert(&filelist, "L/1")); g_repo = cl_git_sandbox_init("icase"); git_repository_head_tree(&tree, g_repo); /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ /* In this test we DO NOT force a case on the iteratords and verify default behavior. */ i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); expect_iterator_items(i, 8, NULL, 8, NULL); git_iterator_free(i); i_opts.start = "c"; i_opts.end = NULL; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); default_icase = git_iterator_ignore_case(i); /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ expect = ((default_icase) ? 6 : 4); expect_iterator_items(i, expect, NULL, expect, NULL); git_iterator_free(i); i_opts.start = NULL; i_opts.end = "e"; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); default_icase = git_iterator_ignore_case(i); /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ expect = ((default_icase) ? 5 : 6); expect_iterator_items(i, expect, NULL, expect, NULL); git_iterator_free(i); git_vector_free(&filelist); git_tree_free(tree); }
/* initial size of 1 would cause writing past array bounds */ void test_core_vector__0(void) { git_vector x; int i; git_vector_init(&x, 1, NULL); for (i = 0; i < 10; ++i) { git_vector_insert(&x, (void*) 0xabc); } git_vector_free(&x); }
int git_mwindow_file_register(git_mwindow_file *mwf) { int error; if (ctl.windowfiles.length == 0 && (error = git_vector_init(&ctl.windowfiles, 8, NULL)) < GIT_SUCCESS) return error; return git_vector_insert(&ctl.windowfiles, mwf); }
/* don't read past array bounds on remove() */ void test_core_vector__1(void) { git_vector x; /* make initial capacity exact for our insertions. */ git_vector_init(&x, 3, NULL); git_vector_insert(&x, (void*) 0xabc); git_vector_insert(&x, (void*) 0xdef); git_vector_insert(&x, (void*) 0x123); git_vector_remove(&x, 0); /* used to read past array bounds. */ git_vector_free(&x); }
static int local_ls(git_transport *transport, git_headarray *array) { int error; unsigned int i; git_repository *repo; git_vector *vec; git_strarray refs; transport_local *t = (transport_local *) transport; assert(transport && transport->connected); repo = t->repo; error = git_reference_listall(&refs, repo, GIT_REF_LISTALL); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to list remote heads"); vec = git__malloc(sizeof(git_vector)); if (vec == NULL) { error = GIT_ENOMEM; goto out; } error = git_vector_init(vec, refs.count, NULL); if (error < GIT_SUCCESS) return error; /* Sort the references first */ qsort(refs.strings, refs.count, sizeof(char *), cmp_refs); /* Add HEAD */ error = add_ref(GIT_HEAD_FILE, repo, vec); if (error < GIT_SUCCESS) goto out; for (i = 0; i < refs.count; ++i) { error = add_ref(refs.strings[i], repo, vec); if (error < GIT_SUCCESS) goto out; } array->len = vec->length; array->heads = (git_remote_head **)vec->contents; t->refs = vec; out: git_strarray_free(&refs); return error; }
int git_transport_git(git_transport **out) { transport_git *t; #ifdef GIT_WIN32 int ret; #endif t = git__malloc(sizeof(transport_git)); GITERR_CHECK_ALLOC(t); memset(t, 0x0, sizeof(transport_git)); if (git_vector_init(&t->parent.common, 8, NULL)) goto on_error; if (git_vector_init(&t->parent.refs, 16, NULL) < 0) goto on_error; t->parent.connect = git_connect; t->parent.negotiation_step = git_negotiation_step; t->parent.close = git_close; t->parent.free = git_free; *out = (git_transport *) t; #ifdef GIT_WIN32 ret = WSAStartup(MAKEWORD(2,2), &t->wsd); if (ret != 0) { git_free(*out); giterr_set(GITERR_NET, "Winsock init failed"); return -1; } #endif return 0; on_error: git__free(t); return -1; }
int git_odb_new(git_odb **out) { git_odb *db = git__calloc(1, sizeof(*db)); if (!db) return GIT_ENOMEM; if (git_vector_init(&db->backends, 4, backend_sort_cmp, NULL) < 0) { free(db); return GIT_ENOMEM; } *out = db; return GIT_SUCCESS; }
static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end) { int error = GIT_SUCCESS; if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < GIT_SUCCESS) return GIT_ENOMEM; while (buffer < buffer_end) { git_tree_entry *entry; int tmp; entry = git__calloc(1, sizeof(git_tree_entry)); if (entry == NULL) { error = GIT_ENOMEM; break; } if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS) return GIT_ENOMEM; if (git__strtol32(&tmp, buffer, &buffer, 8) < GIT_SUCCESS || !buffer || !valid_attributes(tmp)) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tree. Can't parse attributes"); entry->attr = tmp; if (*buffer++ != ' ') { error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse tree. Object it corrupted"); break; } if (memchr(buffer, 0, buffer_end - buffer) == NULL) { error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse tree. Object it corrupted"); break; } entry->filename = git__strdup(buffer); entry->filename_len = strlen(buffer); while (buffer < buffer_end && *buffer != 0) buffer++; buffer++; git_oid_fromraw(&entry->oid, (const unsigned char *)buffer); buffer += GIT_OID_RAWSZ; } return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse buffer"); }
int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source) { git_treebuilder *bld; size_t i, source_entries = DEFAULT_TREE_SIZE; assert(builder_p); bld = git__calloc(1, sizeof(git_treebuilder)); if (bld == NULL) return GIT_ENOMEM; if (source != NULL) source_entries = source->entries.length; if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < GIT_SUCCESS) { free(bld); return GIT_ENOMEM; } if (source != NULL) { bld->entry_count = source_entries; for (i = 0; i < source->entries.length; ++i) { git_tree_entry *entry_src = source->entries.contents[i]; git_tree_entry *entry = git__calloc(1, sizeof(git_tree_entry)); if (entry == NULL) { git_treebuilder_free(bld); return GIT_ENOMEM; } entry->filename = git__strdup(entry_src->filename); if (entry->filename == NULL) { free(entry); git_treebuilder_free(bld); return GIT_ENOMEM; } entry->filename_len = entry_src->filename_len; git_oid_cpy(&entry->oid, &entry_src->oid); entry->attr = entry_src->attr; git_vector_insert(&bld->entries, entry); } } *builder_p = bld; return GIT_SUCCESS; }
int git_odb_new(git_odb **out) { git_odb *db = (git_odb*) git__calloc(1, sizeof(*db)); GITERR_CHECK_ALLOC(db); if (git_cache_init(&db->own_cache) < 0 || git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) { git__free(db); return -1; } *out = db; GIT_REFCOUNT_INC(db); return 0; }
static int create_internal(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch) { git_remote *remote; git_buf fetchbuf = GIT_BUF_INIT; int error = -1; /* name is optional */ assert(out && repo && url); remote = git__calloc(1, sizeof(git_remote)); GITERR_CHECK_ALLOC(remote); remote->repo = repo; remote->update_fetchhead = 1; if (get_check_cert(&remote->check_cert, repo) < 0) goto on_error; if (git_vector_init(&remote->refs, 32, NULL) < 0) goto on_error; remote->url = git__strdup(url); GITERR_CHECK_ALLOC(remote->url); if (name != NULL) { remote->name = git__strdup(name); GITERR_CHECK_ALLOC(remote->name); } if (fetch != NULL) { if (add_refspec(remote, fetch, true) < 0) goto on_error; } if (!name) /* A remote without a name doesn't download tags */ remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; *out = remote; git_buf_free(&fetchbuf); return 0; on_error: git_remote_free(remote); git_buf_free(&fetchbuf); return error; }
static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end) { int error = GIT_SUCCESS; if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < GIT_SUCCESS) return GIT_ENOMEM; while (buffer < buffer_end) { git_tree_entry *entry; entry = git__calloc(1, sizeof(git_tree_entry)); if (entry == NULL) { error = GIT_ENOMEM; break; } if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS) return GIT_ENOMEM; if (git__strtol32((long *)&entry->attr, buffer, &buffer, 8) < GIT_SUCCESS) return GIT_EOBJCORRUPTED; if (*buffer++ != ' ') { error = GIT_EOBJCORRUPTED; break; } if (memchr(buffer, 0, buffer_end - buffer) == NULL) { error = GIT_EOBJCORRUPTED; break; } entry->filename = git__strdup(buffer); entry->filename_len = strlen(buffer); while (buffer < buffer_end && *buffer != 0) buffer++; buffer++; git_oid_mkraw(&entry->oid, (const unsigned char *)buffer); buffer += GIT_OID_RAWSZ; } return error; }