/* Test that reading and writing a tree is a no-op */ void test_index_read_tree__read_write_involution(void) { git_repository *repo; git_index *index; git_oid tree_oid; git_tree *tree; git_oid expected; p_mkdir("read_tree", 0700); cl_git_pass(git_repository_init(&repo, "./read_tree", 0)); cl_git_pass(git_repository_index(&index, repo)); cl_assert(git_index_entrycount(index) == 0); p_mkdir("./read_tree/abc", 0700); /* Sort order: '-' < '/' < '_' */ cl_git_mkfile("./read_tree/abc-d", NULL); cl_git_mkfile("./read_tree/abc/d", NULL); cl_git_mkfile("./read_tree/abc_d", NULL); cl_git_pass(git_index_add(index, "abc-d", 0)); cl_git_pass(git_index_add(index, "abc_d", 0)); cl_git_pass(git_index_add(index, "abc/d", 0)); /* write-tree */ cl_git_pass(git_tree_create_fromindex(&expected, index)); /* read-tree */ git_tree_lookup(&tree, repo, &expected); cl_git_pass(git_index_read_tree(index, tree, NULL)); git_tree_free(tree); cl_git_pass(git_tree_create_fromindex(&tree_oid, index)); cl_assert(git_oid_cmp(&expected, &tree_oid) == 0); git_index_free(index); git_repository_free(repo); cl_fixture_cleanup("read_tree"); }
PyObject * Index_write_tree(Index *self) { git_oid oid; int err; err = git_tree_create_fromindex(&oid, self->index); if (err < 0) return Error_set(err); return git_oid_to_python(oid.id); }
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; }
int main(int argc, char **argv) { git_repository *repo; git_odb *odb; git_oid oid, tree_id, commit; git_index *index; char hash[41]; int error; char *folder; int folder_size; git_signature *author, *committer; git_tree *tree; git_reference *master; hash[40] = '\0'; if(argc != 2) { printf("usage: migit <path-to-git-repo>\n"); return 1; } folder_size = strlen(argv[1]) + 6; folder = malloc(folder_size); sprintf(folder, "%s/.git", argv[1]); folder[folder_size - 1] = '\0'; create_sample_file(argv[1]); error = git_repository_open(&repo, folder); if(error) { die("could not open repository", error); } odb = git_repository_database(repo); git_odb_write(&oid, odb, content, strlen(content) * sizeof(char), GIT_OBJ_BLOB); error = git_repository_index(&index, repo); if(error) { die("could not open index", error); } error = git_index_add(index, fname, 0); if(error) { die("could not add file to index", error); } assert(git_index_entrycount(index) == 1); error = git_tree_create_fromindex(&tree_id, index); if(error) { die("could not create tree from index", error); } error = git_tree_lookup(&tree, repo, &tree_id); if(error) { die("could not create tree from id", error); } error = git_signature_new(&author, "helino", "*****@*****.**", 12345, 0); if(error) { die("could not create signature for author", error); } error = git_signature_new(&committer, "irock", "*****@*****.**", 12345, 0); if(error) { die("could not create signature for committer", error); } error = git_commit_create(&commit, repo, NULL, author, committer, NULL, "hello git", tree, 0, NULL); if(error) { die("could not commit", error); } git_oid_fmt(hash, &commit); printf("commit: %s\n", hash); error = git_reference_create_oid(&master, repo, "refs/heads/master", &commit, 1); if(error) { die("could not create master reference", error); } git_repository_free(repo); free(folder); return 0; }
/* Close an open tree */ //## @Native void GitTree.close(); KMETHOD GitTree_close(CTX ctx, ksfp_t *sfp _RIX) { kGitTree_free(ctx, sfp[0].p); RETURNvoid_(); } /* Write a tree to the ODB from the index file */ //## @Native @Static GitOid GitTree.createFromIndex(GitIndex index); KMETHOD GitTree_createFromIndex(CTX ctx, ksfp_t *sfp _RIX) { git_oid *oid = (git_oid *)KNH_MALLOC(ctx, sizeof(git_oid)); git_index *index = RawPtr_to(git_index *, sfp[1]); int error = git_tree_create_fromindex(oid, index); if (error < GIT_SUCCESS) { TRACE_ERROR(ctx, "git_tree_create_fromindex", error); KNH_FREE(ctx, oid, sizeof(git_oid)); RETURN_(KNH_NULL); } RETURN_(new_ReturnRawPtr(ctx, sfp, oid)); } /* Get the UNIX file attributes of a tree entry */ //## @Native int GitTreeEntry.attributes(); KMETHOD GitTreeEntry_attributes(CTX ctx, ksfp_t *sfp _RIX) { const git_tree_entry *entry = RawPtr_to(const git_tree_entry *, sfp[0]); unsigned int i = git_tree_entry_attributes(entry); RETURNi_(i);
git_object_close(obj); git_tree_close(tree); git_repository_free(repo); END_TEST #if 0 BEGIN_TEST(write0, "write a tree from an index") git_repository *repo; git_index *index; git_oid tree_oid; must_pass(git_repository_open(&repo, "/tmp/redtmp/.git")); must_pass(git_repository_index(&index, repo)); must_pass(git_tree_create_fromindex(&tree_oid, index)); must_pass(print_tree(repo, &tree_oid, 0)); git_repository_free(repo); END_TEST #endif BEGIN_TEST(write2, "write a tree from a memory") git_repository *repo; git_treebuilder *builder; git_tree *tree; git_oid id, bid, rid, id2; must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); git_oid_mkstr(&id, first_tree); git_oid_mkstr(&id2, second_tree);