int Repository_contains(Repository *self, PyObject *value) { git_oid oid; git_odb *odb; int err, len, exists; len = py_str_to_git_oid(value, &oid); if (len < 0) return -1; err = git_repository_odb(&odb, self->repo); if (err < 0) { Error_set(err); return -1; } if (len < GIT_OID_HEXSZ) { git_odb_object *obj = NULL; err = git_odb_read_prefix(&obj, odb, &oid, len); if (err < 0 && err != GIT_ENOTFOUND) { Error_set(err); exists = -1; } else { exists = (err == 0); if (obj) git_odb_object_free(obj); } } else { exists = git_odb_exists(odb, &oid); } git_odb_free(odb); return exists; }
static int filter_ref__cb(git_remote_head *head, void *payload) { struct filter_payload *p = payload; int error; if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0) { p->found_head = 1; } else { /* If it doesn't match the refpec, we don't want it */ error = git_refspec_src_match(p->spec, head->name); if (error == GIT_ENOMATCH) return GIT_SUCCESS; if (error < GIT_SUCCESS) return git__rethrow(error, "Error matching remote ref name"); } /* If we have the object, mark it so we don't ask for it */ if (git_odb_exists(p->odb, &head->oid)) head->local = 1; else p->remote->need_pack = 1; return git_vector_insert(&p->remote->refs, head); }
static int maybe_want(git_remote *remote, git_remote_head *head, git_odb *odb, git_refspec *tagspec) { int match = 0; if (!git_reference_is_valid_name(head->name)) return 0; if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { /* * If tagopt is --tags, always request tags * in addition to the remote's refspecs */ if (git_refspec_src_matches(tagspec, head->name)) match = 1; } if (!match && git_remote__matching_refspec(remote, head->name)) match = 1; if (!match) return 0; /* If we have the object, mark it so we don't ask for it */ if (git_odb_exists(odb, &head->oid)) { head->local = 1; } else remote->need_pack = 1; return git_vector_insert(&remote->refs, head); }
static int filter_ref__cb(git_remote_head *head, void *payload) { struct filter_payload *p = payload; int match = 0; if (!git_reference_is_valid_name(head->name)) return 0; if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0) p->found_head = 1; else if (p->remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { /* * If tagopt is --tags, then we only use the default * tags refspec and ignore the remote's */ if (git_refspec_src_matches(p->tagspec, head->name)) match = 1; else return 0; } else if (git_remote__matching_refspec(p->remote, head->name)) match = 1; if (!match) return 0; /* If we have the object, mark it so we don't ask for it */ if (git_odb_exists(p->odb, &head->oid)) head->local = 1; else p->remote->need_pack = 1; return git_vector_insert(&p->remote->refs, head); }
static ERL_NIF_TERM geef_object_exists(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { char path[MAXBUFLEN]; char sha[MAXBUFLEN]; (void)memset(&path, '\0', sizeof(path)); (void)memset(&sha, '\0', sizeof(sha)); if (enif_get_string(env, argv[0], path, sizeof(path), ERL_NIF_LATIN1) < 1) return enif_make_badarg(env); if (enif_get_string(env, argv[1], sha, sizeof(sha), ERL_NIF_LATIN1) < 1) return enif_make_badarg(env); git_odb *odb; git_odb_open(&odb, path); git_oid oid; git_oid_fromstr(&oid, sha); int exists = git_odb_exists(odb, &oid); if(exists == 1) { return enif_make_atom(env, "true"); } return enif_make_atom(env, "false"); }
int git_odb_exists_prefix( git_oid *out, git_odb *db, const git_oid *short_id, size_t len) { int error = GIT_ENOTFOUND, num_found = 0; size_t i; git_oid key = {{0}}, last_found = {{0}}, found; assert(db && short_id); if (len < GIT_OID_MINPREFIXLEN) return git_odb__error_ambiguous("prefix length too short"); if (len > GIT_OID_HEXSZ) len = GIT_OID_HEXSZ; if (len == GIT_OID_HEXSZ) { if (git_odb_exists(db, short_id)) { if (out) git_oid_cpy(out, short_id); return 0; } else { return git_odb__error_notfound("no match for id prefix", short_id); } } /* just copy valid part of short_id */ memcpy(&key.id, short_id->id, (len + 1) / 2); if (len & 1) key.id[len / 2] &= 0xF0; for (i = 0; i < db->backends.length; ++i) { backend_internal *internal = (backend_internal *) git_vector_get(&db->backends, i); git_odb_backend *b = internal->backend; if (!b->exists_prefix) continue; error = b->exists_prefix(&found, b, &key, len); if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) continue; if (error) return error; /* make sure found item doesn't introduce ambiguity */ if (num_found) { if (git_oid__cmp(&last_found, &found)) return git_odb__error_ambiguous("multiple matches for prefix"); } else { git_oid_cpy(&last_found, &found); num_found++; } } if (!num_found) return git_odb__error_notfound("no match for id prefix", &key); if (out) git_oid_cpy(out, &last_found); return 0; }
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; }
void test_odb_loose__exists(void) { git_oid id, id2; git_odb *odb; write_object_files(&one); cl_git_pass(git_odb_open(&odb, "test-objects")); cl_git_pass(git_oid_fromstr(&id, one.id)); cl_assert(git_odb_exists(odb, &id)); /* Test for a non-existant object */ cl_git_pass(git_oid_fromstr(&id2, "8b137891791fe96927ad78e64b0aad7bded08baa")); cl_assert(!git_odb_exists(odb, &id2)); git_odb_free(odb); }
void test_odb_backend_simple__exists_fails_for_nonexisting_object(void) { const fake_object objs[] = { { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, { NULL, NULL } }; setup_backend(objs); cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633")); cl_assert(git_odb_exists(_odb, &_oid) == 0); }
void test_odb_backend_simple__exists_succeeds(void) { const fake_object objs[] = { { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, { NULL, NULL } }; setup_backend(objs); cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); cl_assert(git_odb_exists(_odb, &_oid)); }
int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream) { if (stream->received_bytes != stream->declared_size) return git_odb_stream__invalid_length(stream, "stream_finalize_write()"); git_hash_final(out, (git_hash_ctx*) stream->hash_ctx); if (git_odb_exists(stream->backend->odb, out)) return 0; return stream->finalize_write(stream, out); }
void test_odb_packed__mass_read(void) { unsigned int i; for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) { git_oid id; git_odb_object *obj; cl_git_pass(git_oid_fromstr(&id, packed_objects[i])); cl_assert(git_odb_exists(_odb, &id) == 1); cl_git_pass(git_odb_read(&obj, _odb, &id)); git_odb_object_free(obj); } }
void test_odb_loose__exists(void) { git_oid id, id2; git_odb *odb; write_object_files(&one); cl_git_pass(git_odb_open(&odb, "test-objects")); cl_git_pass(git_oid_fromstr(&id, one.id)); cl_assert(git_odb_exists(odb, &id)); cl_git_pass(git_oid_fromstrp(&id, "8b137891")); cl_git_pass(git_odb_exists_prefix(&id2, odb, &id, 8)); cl_assert_equal_i(0, git_oid_streq(&id2, one.id)); /* Test for a missing object */ cl_git_pass(git_oid_fromstr(&id, "8b137891791fe96927ad78e64b0aad7bded08baa")); cl_assert(!git_odb_exists(odb, &id)); cl_git_pass(git_oid_fromstrp(&id, "8b13789a")); cl_assert_equal_i(GIT_ENOTFOUND, git_odb_exists_prefix(&id2, odb, &id, 8)); git_odb_free(odb); }
int cmd_write_tree(int argc, const char **argv) { please_git_do_it_for_me(); int verify_index = 1; if (argc == 1) verify_index = 1; else if (argc == 2 && strcmp(argv[1], "--missing-ok") == 0 ) verify_index = 0; else please_git_do_it_for_me(); char sha1buf[GIT_OID_HEXSZ + 1]; git_repository *repo = get_git_repository(); git_index *index_cur; int e = git_repository_index(&index_cur, repo); if(e != GIT_OK) libgit_error(); /* check the index */ if (verify_index) { git_odb * odb; git_repository_odb(&odb, repo); for (unsigned i = 0; i < git_index_entrycount(index_cur); i++) { git_index_entry *gie = git_index_get(index_cur, i); if (git_odb_exists(odb, &gie->oid) != 1) { printf("error: invalid object %06o %s for '%s'\n", gie->mode, git_oid_tostr(sha1buf, GIT_OID_HEXSZ+1, &gie->oid), gie->path); printf("fatal: git-write-tree: error building trees\n"); return EXIT_FAILURE; } } } /* create the tree */ git_oid oid; e = git_tree_create_fromindex(&oid, index_cur); if(e != GIT_OK) libgit_error(); printf("%s\n", git_oid_tostr(sha1buf, GIT_OID_HEXSZ+1, &oid)); return EXIT_SUCCESS; }
void test_odb_backend_simple__null_oid_is_ignored(void) { const fake_object objs[] = { { "0000000000000000000000000000000000000000", "null oid content" }, { NULL, NULL } }; git_oid null_oid = {{0}}; git_odb_object *obj; setup_backend(objs); cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)); cl_assert(!git_odb_exists(_odb, &null_oid)); cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&obj, _odb, &null_oid)); cl_assert(giterr_last() && strstr(giterr_last()->message, "null OID")); }
int git_odb_write( git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type) { size_t i; int error = GIT_ERROR; git_odb_stream *stream; assert(oid && db); git_odb_hash(oid, data, len, type); if (git_odb_exists(db, oid)) return 0; for (i = 0; i < db->backends.length && error < 0; ++i) { backend_internal *internal = (backend_internal *) git_vector_get(&db->backends, i); git_odb_backend *b = internal->backend; /* we don't write in alternates! */ if (internal->is_alternate) continue; if (b->write != NULL) error = b->write(b, oid, data, len, type); } if (!error || error == GIT_PASSTHROUGH) return 0; /* if no backends were able to write the object directly, we try a * streaming write to the backends; just write the whole object into the * stream in one push */ if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0) return error; stream->write(stream, (char*) data, len); error = stream->finalize_write(stream, oid); git_odb_stream_free(stream); return error; }
int git_repository_head_detached(git_repository *repo) { git_reference *ref; git_odb *odb = NULL; int exists; if (git_repository_odb__weakptr(&odb, repo) < 0) return -1; if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) return -1; if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { git_reference_free(ref); return 0; } exists = git_odb_exists(odb, git_reference_oid(ref)); git_reference_free(ref); return exists; }
void test_odb_packed__read_header_1(void) { unsigned int i; for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) { git_oid id; git_odb_object *obj; size_t len; git_object_t type; cl_git_pass(git_oid_fromstr(&id, loose_objects[i])); cl_assert(git_odb_exists(_odb, &id) == 1); cl_git_pass(git_odb_read(&obj, _odb, &id)); cl_git_pass(git_odb_read_header(&len, &type, _odb, &id)); cl_assert(obj->cached.size == len); cl_assert(obj->cached.type == type); git_odb_object_free(obj); } }
int git_reference_create( git_reference **ref_out, git_repository *repo, const char *name, const git_oid *oid, int force) { git_odb *odb; int error = 0; assert(repo && name && oid); /* Sanity check the reference being created - target must exist. */ if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) return error; if (!git_odb_exists(odb, oid)) { giterr_set(GITERR_REFERENCE, "Target OID for the reference doesn't exist on the repository"); return -1; } return reference__create(ref_out, repo, name, oid, NULL, force); }
static int reference__create( git_reference **ref_out, git_repository *repo, const char *name, const git_oid *oid, const char *symbolic, int force, const git_signature *signature, const char *log_message, const git_oid *old_id, const char *old_target) { git_refname_t normalized; git_refdb *refdb; git_reference *ref = NULL; int error = 0; assert(repo && name); assert(symbolic || signature); if (ref_out) *ref_out = NULL; error = reference_normalize_for_repo(normalized, repo, name); if (error < 0) return error; error = git_repository_refdb__weakptr(&refdb, repo); if (error < 0) return error; if (oid != NULL) { git_odb *odb; assert(symbolic == NULL); /* Sanity check the reference being created - target must exist. */ if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) return error; if (!git_odb_exists(odb, oid)) { giterr_set(GITERR_REFERENCE, "Target OID for the reference doesn't exist on the repository"); return -1; } ref = git_reference__alloc(normalized, oid, NULL); } else { git_refname_t normalized_target; if ((error = reference_normalize_for_repo(normalized_target, repo, symbolic)) < 0) return error; ref = git_reference__alloc_symbolic(normalized, normalized_target); } GITERR_CHECK_ALLOC(ref); if ((error = git_refdb_write(refdb, ref, force, signature, log_message, old_id, old_target)) < 0) { git_reference_free(ref); return error; } if (ref_out == NULL) git_reference_free(ref); else *ref_out = ref; return 0; }
int cmd_cat_file(int argc, const char **argv) { /* Uncomment when it passes the tests */ please_git_do_it_for_me(); char opt; if (argc != 2 && argc !=3) please_git_do_it_for_me(); if (argc == 3){ if ((strcmp(argv[1], "blob") == 0) || (strcmp(argv[1], "tree") == 0) || (strcmp(argv[1], "commit") == 0) || (strcmp(argv[1], "tag") == 0 )) opt = '0'; else opt = argv[1][1]; } else if (argc == 2 && (strcmp(argv[1], "--batch") == 0)) opt = 'a'; git_repository *repo = get_git_repository(); git_oid oid; if (git_oid_fromstr(&oid, (const char *)argv[argc-1])) please_git_do_it_for_me(); git_odb *odb; git_repository_odb(&odb, repo); git_odb_object *odb_object; if(git_odb_read(&odb_object, odb, &oid) == GIT_ENOTFOUND) libgit_error(); size_t size = git_odb_object_size(odb_object); git_otype type = git_odb_object_type(odb_object); const char *type_string = git_object_type2string(type); switch (opt) { case 'p': if (strcmp(type_string, "tree") == 0) { printf("vous etes là\n"); char ** arg = malloc(2); strcpy(arg[0], "ls-tree"); strcpy(arg[1], argv[2]); int e = cmd_ls_tree(2, (const char **)arg); if (e == 0 ) printf("succes\n"); else printf("echec\n"); } else { for(int i=0; i < (int)size; i++) printf("%c", *((char *)git_odb_object_data(odb_object)+i)); } break; case 't': printf("%s\n",type_string); break; case 's': printf("%zu\n",size); break; case 'e': if(git_odb_exists(odb, &oid) == 1) { return 0; } break; case '0' : if (strcmp(type_string, argv[1]) == 0) { for (int i=0; i < (int)size; i++) printf("%c", *((char *)git_odb_object_data(odb_object)+i)); } else please_git_do_it_for_me(); break; case 'a' : please_git_do_it_for_me(); // if (strbuf_read(&buf, 0, 4096) < 0) // die_errno("could not read from stdin"); // // git_oid id; // if (git_oid_mkstr(&id, buf.buf)) // please_git_do_it_for_me(); // // git_odb_object *odb_object1; // if(git_odb_read(&odb_object1, odb, &id) == GIT_ENOTFOUND) // libgit_error(); // size_t size1 = git_odb_object_size(odb_object1); // git_otype type1 = git_odb_object_type(odb_object1); // // printf("bla bla %s %s %zu\n",buf.buf, git_object_type2string(type1), size1); // for(int i=0; i < (int)size1; i++) // printf("%c", *((char *)git_odb_object_data(odb_object1)+i)); // fflush(stdout); break; } //git_odb_object_close(odb_object); return EXIT_SUCCESS; }