PyObject * Repository_create_blob_fromdisk(Repository *self, PyObject *args) { git_oid oid; const char* path; int err; if (!PyArg_ParseTuple(args, "s", &path)) return NULL; err = git_blob_create_fromdisk(&oid, self->repo, path); if (err < 0) return Error_set(err); return git_oid_to_python(&oid); }
/* * call-seq: * Blob.from_disk(repository, file_path) -> oid * * Write the file specified in +file_path+ to a blob in +repository+. * The repository can be bare or not. * * Example: * * Blob.from_disk(repo, '/var/repos/blob.h') #=> '5b5b025afb0b4c913b4c338a42934a3863bf3643' */ static VALUE rb_git_blob_from_disk(VALUE self, VALUE rb_repo, VALUE rb_path) { int error; git_oid oid; git_repository *repo; Check_Type(rb_path, T_STRING); rugged_check_repo(rb_repo); Data_Get_Struct(rb_repo, git_repository, repo); error = git_blob_create_fromdisk(&oid, repo, StringValueCStr(rb_path)); rugged_exception_check(error); return rugged_create_oid(&oid); }
int create_repo(const char *repo_name) { int r; git_repository *repo; git_oid blob_id; git_oid oid; git_oid tree_id; git_signature *author; git_time_t time; git_config *config; git_index *index; git_tree *tree; git_treebuilder *tree_builder; git_treebuilder *empty_tree_builder; char global_config_path[GIT_PATH_MAX]; // create the repository r = git_repository_init(&repo, repo_name, 1); if (r) printf("error in creating repository\n"); printf("Repo created\n"); // set the repository config git_config_new(&config); git_config_find_global(global_config_path, GIT_PATH_MAX); git_config_add_file_ondisk(config, global_config_path, 1); //git_config_set_string(config, "name", "Varun Agrawal"); //git_config_set_string(config, "email", "*****@*****.**"); git_repository_set_config(repo, config); printf("Repo config set\n"); // create a treebuilder r = git_treebuilder_create(&tree_builder, NULL); if (r) printf("error in creting treebuilder\n"); printf("Tree builder created\n"); // ADDING FIRST FILE // create a blob r = git_blob_create_fromdisk(&blob_id, repo, "test1"); if (r) printf("error in creating blob from disk\n"); printf("Blob created\n"); // insert into tree r = git_treebuilder_insert(NULL, tree_builder, "test1", &blob_id, 0100644); if (r) printf("error in inserting into treebuilder\n"); printf("Insert into treebuilder successful\n"); // ADDING SECOND FILE // create a blob r = git_blob_create_fromdisk(&blob_id, repo, "test2"); if (r) printf("error in creating blob from disk\n"); printf("Blob created\n"); // insert into tree r = git_treebuilder_insert(NULL, tree_builder, "test2", &blob_id, 0100644); if (r) printf("error in inserting into treebuilder\n"); printf("Insert into treebuilder successful\n"); // ADDING A EMPTY FOLDER // create a empty tree r = git_treebuilder_create(&empty_tree_builder, NULL); if (r) printf("error in creting empty treebuilder\n"); printf("Empty Tree builder created\n"); // write the empty tree to the repo r = git_treebuilder_write(&tree_id, repo, empty_tree_builder); if (r) printf("error in writing the empty tree to the repo\n"); printf("Writing the empty tree to repo successful\n"); // insert empty tree into the tree r = git_treebuilder_insert(NULL, tree_builder, "test_dir", &tree_id, 0040000); if (r) printf("error in inserting into treebuilder\n"); printf("Insert into treebuilder successful\n"); // write the tree to the repo r = git_treebuilder_write(&oid, repo, tree_builder); if (r) printf("error in writing the tree to the repo\n"); printf("Writing the tree to repo successful\n"); // tree lookup r = git_tree_lookup(&tree, repo, &oid); if (r) printf("error in tree lookup\n"); printf("Tree lookup done\n"); // create a author time = get_time(); r = git_signature_new(&author, "Varun Agrawal", "*****@*****.**", time, -300); if (r) printf("error in creating signature\n"); printf("Author signature created\n"); // create a commit r = git_commit_create( &oid, // object id repo, // repository "HEAD", // update reference, this will update the HEAD to this commit author, // author author, // committer NULL, // message encoding, by default UTF-8 is used "first commit", // message for the commit tree, // the git_tree object which will be used as the tree for this commit. don't know if NULL is valid 0, // number of parents. Don't know what value should be used here NULL); // array of pointers to the parents(git_commit *parents[]) if (r) printf("error in creating a commit\n"); printf("Commit created\n"); git_repository_free(repo); return 0; }
int edit_repo(const char *repo_name) { int r; git_repository *repo; git_oid blob_id; git_oid oid; git_oid tree_id; git_signature *author; git_time_t time; git_config *config; git_index *index; git_tree *tree; git_treebuilder *tree_builder; git_treebuilder *empty_tree_builder; git_reference *head; git_commit *commit_parents[1]; char global_config_path[GIT_PATH_MAX]; char out[41]; out[40] = '\0'; // create the repository r = git_repository_open(&repo, repo_name); if (r) printf("error in opening repository\n"); printf("Repo opened\n"); // create a treebuilder r = git_treebuilder_create(&tree_builder, NULL); if (r) printf("error in creting treebuilder\n"); printf("Tree builder created\n"); // ADDING FIRST FILE // create a blob r = git_blob_create_fromdisk(&blob_id, repo, "test2"); if (r) printf("error in creating blob from disk\n"); printf("Blob created\n"); // insert into tree r = git_treebuilder_insert(NULL, tree_builder, "test1", &blob_id, 0100644); if (r) printf("error in inserting into treebuilder\n"); printf("Insert into treebuilder successful\n"); // write the tree to the repo r = git_treebuilder_write(&oid, repo, tree_builder); if (r) printf("error in writing the tree to the repo\n"); printf("Writing the tree to repo successful\n"); // tree lookup r = git_tree_lookup(&tree, repo, &oid); if (r) printf("error in tree lookup\n"); printf("Tree lookup done\n"); // create a author time = get_time(); r = git_signature_new(&author, "Varun Agrawal", "*****@*****.**", time, -300); if (r) printf("error in creating signature\n"); printf("Author signature created\n"); // obtaining the head r = git_repository_head(&head, repo); if (r) printf("error in obtaining the head\n"); r = git_reference_name_to_oid(&oid, repo, git_reference_name(head)); if (r) printf("error in obtaining the ref id of head\n"); printf("Obtained the head id %s\n", git_oid_tostr(out, 41, &oid)); git_commit_lookup(&commit_parents[0], repo, &oid); // create a commit r = git_commit_create( &oid, // object id repo, // repository "HEAD", // update reference, this will update the HEAD to this commit author, // author author, // committer NULL, // message encoding, by default UTF-8 is used "second commit", // message for the commit tree, // the git_tree object which will be used as the tree for this commit. don't know if NULL is valid 1, // number of parents. Don't know what value should be used here commit_parents); // array of pointers to the parents(git_commit *parents[]) if (r) printf("error in creating a commit\n"); printf("Commit created\n"); git_repository_free(repo); return 0; }