void test_object_tree_write__subtree(void) { /* write a hierarchical tree from a memory */ git_treebuilder *builder; git_tree *tree; git_oid id, bid, subtree_id, id2, id3; git_oid id_hiearar; git_oid_fromstr(&id, first_tree); git_oid_fromstr(&id2, second_tree); git_oid_fromstr(&id3, third_tree); git_oid_fromstr(&bid, blob_oid); /* create subtree */ cl_git_pass(git_treebuilder_create(&builder, NULL)); cl_git_pass(git_treebuilder_insert( NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); /* -V536 */ cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder)); git_treebuilder_free(builder); /* create parent tree */ cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); cl_git_pass(git_treebuilder_create(&builder, tree)); cl_git_pass(git_treebuilder_insert( NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); /* -V536 */ cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder)); git_treebuilder_free(builder); git_tree_free(tree); cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0); /* check data is correct */ cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar)); cl_assert(2 == git_tree_entrycount(tree)); git_tree_free(tree); }
int cmd_read_tree(int argc, const char **argv) { please_git_do_it_for_me(); if (argc != 2) please_git_do_it_for_me(); /* Find the current repository */ repo = get_git_repository(); /*Find the tree*/ git_tree *tree; git_oid oid_tree; switch (git_oid_fromstr(&oid_tree, (const char *)argv[argc-1])) { case GIT_ENOTOID: please_git_do_it_for_me(); break; case GIT_SUCCESS: break; default: libgit_error(); } e = git_tree_lookup(&tree, repo, &oid_tree); if (e) { if (e == GIT_EINVALIDTYPE || e == GIT_ENOTFOUND) { error("Tree object not found"); } else { libgit_error(); } } /* Open the index */ if (git_repository_index(&index_cur, repo) < 0) libgit_error(); /* Clear the index */ git_index_clear(index_cur); /* add objects of the tree to the index */ add_tree_to_index(tree, ""); /* write the index */ git_index_write(index_cur); return EXIT_SUCCESS; }
/* git reset --hard 72333f47d4e83616630ff3b0ffe4c0faebcc3c45 * git revert --no-commit d1d403d22cbe24592d725f442835cf46fe60c8ac */ void test_revert_workdir__again_after_automerge(void) { git_commit *head, *commit; git_tree *reverted_tree; git_oid head_oid, revert_oid, reverted_tree_oid, reverted_commit_oid; git_signature *signature; struct merge_index_entry merge_index_entries[] = { { 0100644, "caf99de3a49827117bb66721010eac461b06a80c", 0, "file1.txt" }, { 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" }, { 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" }, { 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" }, }; struct merge_index_entry second_revert_entries[] = { { 0100644, "3a3ef367eaf3fe79effbfb0a56b269c04c2b59fe", 1, "file1.txt" }, { 0100644, "caf99de3a49827117bb66721010eac461b06a80c", 2, "file1.txt" }, { 0100644, "747726e021bc5f44b86de60e3032fd6f9f1b8383", 3, "file1.txt" }, { 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" }, { 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" }, { 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" }, }; git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); cl_git_pass(git_revert(repo, commit, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 4)); cl_git_pass(git_index_write_tree(&reverted_tree_oid, repo_index)); cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid)); cl_git_pass(git_signature_new(&signature, "Reverter", "*****@*****.**", time(NULL), 0)); cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&head)); cl_git_pass(git_revert(repo, commit, NULL)); cl_assert(merge_test_index(repo_index, second_revert_entries, 6)); git_signature_free(signature); git_tree_free(reverted_tree); git_commit_free(commit); git_commit_free(head); }
void test_object_tree_write__filtering(void) { git_treebuilder *builder; int i; git_oid entry_oid, tree_oid; git_tree *tree; git_oid_fromstr(&entry_oid, blob_oid); cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL)); for (i = 0; _entries[i].filename; ++i) cl_git_pass(git_treebuilder_insert(NULL, builder, _entries[i].filename, &entry_oid, _entries[i].attr)); cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder)); cl_assert(git_treebuilder_get(builder, "apple") != NULL); cl_assert(git_treebuilder_get(builder, "aardvark") != NULL); cl_assert(git_treebuilder_get(builder, "last") != NULL); git_treebuilder_filter(builder, treebuilder_filter_prefixed, "apple"); cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder)); cl_assert(git_treebuilder_get(builder, "apple") == NULL); cl_assert(git_treebuilder_get(builder, "aardvark") != NULL); cl_assert(git_treebuilder_get(builder, "last") != NULL); git_treebuilder_filter(builder, treebuilder_filter_prefixed, "a"); cl_assert_equal_i(2, (int)git_treebuilder_entrycount(builder)); cl_assert(git_treebuilder_get(builder, "aardvark") == NULL); cl_assert(git_treebuilder_get(builder, "last") != NULL); cl_git_pass(git_treebuilder_write(&tree_oid, builder)); git_treebuilder_free(builder); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid)); cl_assert_equal_i(2, (int)git_tree_entrycount(tree)); git_tree_free(tree); }
void test_index_read_index__maintains_stat_cache(void) { git_index *new_index; git_oid index_id; git_index_entry new_entry; const git_index_entry *e; git_tree *tree; size_t i; cl_assert_equal_i(4, git_index_entrycount(_index)); /* write-tree */ cl_git_pass(git_index_write_tree(&index_id, _index)); /* read-tree, then read index */ git_tree_lookup(&tree, _repo, &index_id); cl_git_pass(git_index_new(&new_index)); cl_git_pass(git_index_read_tree(new_index, tree)); git_tree_free(tree); /* add a new entry that will not have stat data */ memset(&new_entry, 0, sizeof(git_index_entry)); new_entry.path = "Hello"; git_oid_fromstr(&new_entry.id, "0123456789012345678901234567890123456789"); new_entry.file_size = 1234; new_entry.mode = 0100644; cl_git_pass(git_index_add(new_index, &new_entry)); cl_assert_equal_i(5, git_index_entrycount(new_index)); cl_git_pass(git_index_read_index(_index, new_index)); git_index_free(new_index); cl_assert_equal_i(5, git_index_entrycount(_index)); for (i = 0; i < git_index_entrycount(_index); i++) { e = git_index_get_byindex(_index, i); if (strcmp(e->path, "Hello") == 0) { cl_assert_equal_i(0, e->ctime.seconds); cl_assert_equal_i(0, e->mtime.seconds); } else { cl_assert(0 != e->ctime.seconds); cl_assert(0 != e->mtime.seconds); } } }
void add_tree_to_index(git_tree * tree, const char * prefix) { for (size_t i = 0; i < git_tree_entrycount(tree); i++) { /* Get the tree entry */ const git_tree_entry *tree_entry; tree_entry = git_tree_entry_byindex(tree,(unsigned int)i); if (tree_entry == NULL) { printf("Tree entry not found"); libgit_error(); } /* Get the oid of a tree entry */ const git_oid *entry_oid = git_tree_entry_id (tree_entry); /* is a sub directory ? */ git_tree * subtree; if (git_tree_lookup(&subtree, repo, entry_oid) == 0) { char * subprefix = xmalloc(sizeof(char)*1024); const char * dirname = git_tree_entry_name (tree_entry); git2__joinpath(subprefix, prefix, dirname); add_tree_to_index(subtree, subprefix); continue; } char * completename = xmalloc(sizeof(char)*1024); git2__joinpath(completename, prefix, git_tree_entry_name (tree_entry)); git_index_entry source_entry = { {0,0},//git_index_time ctime {0,0},//git_index_time mtime 0,//unsigned int dev 0,//unsigned int ino git_tree_entry_attributes(tree_entry),//unsigned int mode 0,//unsigned int uid 0,//unsigned int gid 0,//git_off_t file_size *entry_oid, 0, 0,//unsigned short flags_extended completename }; git_index_append2(index_cur, &source_entry); } }
static bool roundtrip_with_read_index(const char *tree_idstr) { git_oid tree_id, new_tree_id; git_tree *tree; git_index *tree_index; cl_git_pass(git_oid_fromstr(&tree_id, tree_idstr)); cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id)); cl_git_pass(git_index_new(&tree_index)); cl_git_pass(git_index_read_tree(tree_index, tree)); cl_git_pass(git_index_read_index(_index, tree_index)); cl_git_pass(git_index_write_tree(&new_tree_id, _index)); git_tree_free(tree); git_index_free(tree_index); return git_oid_equal(&tree_id, &new_tree_id); }
void test_clone_nonetwork__clone_tag_to_tree(void) { git_repository *stage; git_index_entry entry; git_index *index; git_odb *odb; git_oid tree_id; git_tree *tree; git_reference *tag; git_tree_entry *tentry; const char *file_path = "some/deep/path.txt"; const char *file_content = "some content\n"; const char *tag_name = "refs/tags/tree-tag"; stage = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_odb(&odb, stage)); cl_git_pass(git_index_new(&index)); memset(&entry, 0, sizeof(git_index_entry)); entry.path = file_path; entry.mode = GIT_FILEMODE_BLOB; cl_git_pass(git_odb_write(&entry.id, odb, file_content, strlen(file_content), GIT_OBJ_BLOB)); cl_git_pass(git_index_add(index, &entry)); cl_git_pass(git_index_write_tree_to(&tree_id, index, stage)); cl_git_pass(git_reference_create(&tag, stage, tag_name, &tree_id, 0, NULL)); git_reference_free(tag); git_odb_free(odb); git_index_free(index); g_options.local = GIT_CLONE_NO_LOCAL; cl_git_pass(git_clone(&g_repo, cl_git_path_url(git_repository_path(stage)), "./foo", &g_options)); git_repository_free(stage); cl_git_pass(git_reference_lookup(&tag, g_repo, tag_name)); cl_git_pass(git_tree_lookup(&tree, g_repo, git_reference_target(tag))); git_reference_free(tag); cl_git_pass(git_tree_entry_bypath(&tentry, tree, file_path)); git_tree_entry_free(tentry); git_tree_free(tree); cl_fixture_cleanup("testrepo.git"); }
/* * revert the same commit twice (when the first reverts cleanly): * * git revert 2d440f2 * git revert 2d440f2 */ void test_revert_workdir__again_after_edit(void) { git_reference *head_ref; git_commit *orig_head, *commit; git_tree *reverted_tree; git_oid orig_head_oid, revert_oid, reverted_tree_oid, reverted_commit_oid; git_signature *signature; struct merge_index_entry merge_index_entries[] = { { 0100644, "3721552e06c4bdc7d478e0674e6304888545d5fd", 0, "file1.txt" }, { 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" }, { 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" }, { 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" }, }; cl_git_pass(git_repository_head(&head_ref, repo)); cl_git_pass(git_oid_fromstr(&orig_head_oid, "399fb3aba3d9d13f7d40a9254ce4402067ef3149")); cl_git_pass(git_commit_lookup(&orig_head, repo, &orig_head_oid)); cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD, NULL)); cl_git_pass(git_oid_fromstr(&revert_oid, "2d440f2b3147d3dc7ad1085813478d6d869d5a4d")); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); cl_git_pass(git_revert(repo, commit, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 4)); cl_git_pass(git_index_write_tree(&reverted_tree_oid, repo_index)); cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid)); cl_git_pass(git_signature_new(&signature, "Reverter", "*****@*****.**", time(NULL), 0)); cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head)); cl_git_pass(git_revert(repo, commit, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 4)); git_signature_free(signature); git_tree_free(reverted_tree); git_commit_free(commit); git_commit_free(orig_head); git_reference_free(head_ref); }
void test_object_tree_attributes__normalize_600(void) { git_oid id; git_tree *tree; git_repository *repo; const git_tree_entry *entry; repo = cl_git_sandbox_init("deprecated-mode.git"); git_oid_fromstr(&id, "0810fb7818088ff5ac41ee49199b51473b1bd6c7"); cl_git_pass(git_tree_lookup(&tree, repo, &id)); entry = git_tree_entry_byname(tree, "ListaTeste.xml"); cl_assert_equal_i(git_tree_entry_filemode(entry), GIT_FILEMODE_BLOB); cl_assert_equal_i(git_tree_entry_filemode_raw(entry), 0100600); git_tree_free(tree); cl_git_sandbox_cleanup(); }
/* 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_bypath(index, "abc-d")); cl_git_pass(git_index_add_bypath(index, "abc_d")); cl_git_pass(git_index_add_bypath(index, "abc/d")); /* write-tree */ cl_git_pass(git_index_write_tree(&expected, index)); /* read-tree */ git_tree_lookup(&tree, repo, &expected); cl_git_pass(git_index_read_tree(index, tree)); git_tree_free(tree); cl_git_pass(git_index_write_tree(&tree_oid, index)); cl_assert_equal_oid(&expected, &tree_oid); git_index_free(index); git_repository_free(repo); cl_fixture_cleanup("read_tree"); }
void test_object_tree_attributes__group_writable_tree_entries_created_with_an_antique_git_version_can_still_be_accessed(void) { git_repository *repo; git_oid tid; git_tree *tree; const git_tree_entry *entry; cl_git_pass(git_repository_open(&repo, cl_fixture("deprecated-mode.git"))); cl_git_pass(git_oid_fromstr(&tid, tree_oid)); cl_git_pass(git_tree_lookup(&tree, repo, &tid)); entry = git_tree_entry_byname(tree, "old_mode.txt"); cl_assert_equal_i( GIT_FILEMODE_BLOB, git_tree_entry_filemode(entry)); git_tree_free(tree); git_repository_free(repo); }
void test_object_tree_read__loaded(void) { /* acces randomly the entries on a loaded tree */ git_oid id; git_tree *tree; git_oid_fromstr(&id, tree_oid); cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); cl_assert(git_tree_entry_byname(tree, "README") != NULL); cl_assert(git_tree_entry_byname(tree, "NOTEXISTS") == NULL); cl_assert(git_tree_entry_byname(tree, "") == NULL); cl_assert(git_tree_entry_byindex(tree, 0) != NULL); cl_assert(git_tree_entry_byindex(tree, 2) != NULL); cl_assert(git_tree_entry_byindex(tree, 3) == NULL); cl_assert(git_tree_entry_byindex(tree, (unsigned int)-1) == NULL); git_tree_free(tree); }
// Fixture setup and teardown void test_rebase_submodule__initialize(void) { git_index *index; git_oid tree_oid, commit_id; git_tree *tree; git_commit *parent; git_object *obj; git_reference *master_ref; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; repo = cl_git_sandbox_init("rebase-submodule"); cl_git_pass(git_signature_new(&signature, "Rebaser", "*****@*****.**", 1405694510, 0)); rewrite_gitmodules(git_repository_workdir(repo)); cl_git_pass(git_submodule_set_url(repo, "my-submodule", git_repository_path(repo))); /* We have to commit the rewritten .gitmodules file */ cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_add_bypath(index, ".gitmodules")); cl_git_pass(git_index_write_tree(&tree_oid, index)); cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); cl_git_pass(git_repository_head(&master_ref, repo)); cl_git_pass(git_commit_lookup(&parent, repo, git_reference_target(master_ref))); cl_git_pass(git_commit_create_v(&commit_id, repo, git_reference_name(master_ref), signature, signature, NULL, "Fixup .gitmodules", tree, 1, parent)); /* And a final reset, for good measure */ cl_git_pass(git_object_lookup(&obj, repo, &commit_id, GIT_OBJ_COMMIT)); cl_git_pass(git_reset(repo, obj, GIT_RESET_HARD, &opts)); git_index_free(index); git_object_free(obj); git_commit_free(parent); git_reference_free(master_ref); git_tree_free(tree); }
static int find_subtree_r(git_tree **out, git_tree *root, git_repository *repo, const char *target, int *fanout) { int error; git_tree *subtree = NULL; *out = NULL; error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); if (error == GIT_EEXISTS) return git_tree_lookup(out, repo, git_tree_id(root)); if (error < 0) return error; *fanout += 2; error = find_subtree_r(out, subtree, repo, target, fanout); git_tree_free(subtree); return error; }
void test_object_tree_write__from_memory(void) { /* write a tree from a memory */ git_treebuilder *builder; git_tree *tree; git_oid id, bid, rid, id2; git_oid_fromstr(&id, first_tree); git_oid_fromstr(&id2, second_tree); git_oid_fromstr(&bid, blob_oid); /* create a second tree from first tree using `git_treebuilder_insert` * on REPOSITORY_FOLDER. */ cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); cl_git_pass(git_treebuilder_new(&builder, g_repo, tree)); cl_git_fail(git_treebuilder_insert(NULL, builder, "", &bid, GIT_FILEMODE_BLOB)); cl_git_fail(git_treebuilder_insert(NULL, builder, "/", &bid, GIT_FILEMODE_BLOB)); cl_git_fail(git_treebuilder_insert(NULL, builder, ".git", &bid, GIT_FILEMODE_BLOB)); cl_git_fail(git_treebuilder_insert(NULL, builder, "..", &bid, GIT_FILEMODE_BLOB)); cl_git_fail(git_treebuilder_insert(NULL, builder, ".", &bid, GIT_FILEMODE_BLOB)); cl_git_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt", &bid, GIT_FILEMODE_BLOB)); cl_git_pass(git_treebuilder_insert( NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); cl_git_pass(git_treebuilder_write(&rid, builder)); cl_assert(git_oid_cmp(&rid, &id2) == 0); git_treebuilder_free(builder); git_tree_free(tree); }
void GitRepository::init() { git_repository* repo = NULL; git_repository_init_options initopts = GIT_REPOSITORY_INIT_OPTIONS_INIT; initopts.flags = GIT_REPOSITORY_INIT_MKPATH; git_eval(git_repository_init_ext(&repo, m_local_dir_path.absolutePath().toLocal8Bit(), &initopts)); git_auto<git_index> index; git_eval(git_repository_index(&index, repo)); git_oid tree_id; git_eval(git_index_write_tree(&tree_id, index)); git_auto<git_tree> tree; git_eval(git_tree_lookup(&tree, repo, &tree_id)); git_oid commit_id; git_eval(git_commit_create_v(&commit_id, repo, "HEAD", signature(), signature(), NULL, "Initial commit", tree, 0)); setRepository(repo); }
void test_index_read_index__read_and_writes(void) { git_oid tree_id, new_tree_id; git_tree *tree; git_index *tree_index, *new_index; cl_git_pass(git_oid_fromstr(&tree_id, "ae90f12eea699729ed24555e40b9fd669da12a12")); cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id)); cl_git_pass(git_index_new(&tree_index)); cl_git_pass(git_index_read_tree(tree_index, tree)); cl_git_pass(git_index_read_index(_index, tree_index)); cl_git_pass(git_index_write(_index)); cl_git_pass(git_index_open(&new_index, git_index_path(_index))); cl_git_pass(git_index_write_tree_to(&new_tree_id, new_index, _repo)); cl_assert_equal_oid(&tree_id, &new_tree_id); git_tree_free(tree); git_index_free(tree_index); git_index_free(new_index); }
void GitWrapper::initializeGitRepository(QString folder) { GIT_RETURN_IF_DISABLED() QMutexLocker l(&gitMutex); // this is not thread safe, we use locking elsewhere git_repository *repo = NULL; QByteArray ba = folder.toUtf8(); const char *cString = ba.data(); int error = git_repository_init(&repo, cString, false); if (error < 0) { const git_error *e = giterr_last(); kDebug() << e->message; } git_signature *sig = NULL; git_index *index = NULL; git_oid tree_id; git_oid commit_id; git_tree *tree = NULL; // no error handling at the moment git_signature_now(&sig, "AutoGit", "auto@localhost"); git_repository_index(&index, repo); git_index_write_tree(&tree_id, index); git_tree_lookup(&tree, repo, &tree_id); git_commit_create_v(&commit_id, repo, "HEAD", sig, sig, NULL, "Initial commit", tree, 0); git_signature_free(sig); git_index_free(index); git_tree_free(tree); //first commit commitPattern(repo, "*", "Initial full commit"); git_repository_free(repo); }
static int lookup_object_by_name(request_rec *r, git_repository *repo, const git_oid *tree_oid, const char *name, const git_oid **oid) { const git_tree_entry *entry; int ret; git_tree *tree; char *extname; int type = WIKI_NOTFOUND; ret = git_tree_lookup(&tree, repo, tree_oid); extname = apr_pstrcat(r->pool, name, ".md", NULL); entry = git_tree_entry_byname(tree, extname); //printf("extname: %s\n", extname); if (entry) { type = WIKI_MARKDOWN; }else{ entry = git_tree_entry_byname(tree, name); if (entry) { int attr = git_tree_entry_attributes(entry); if(S_ISDIR(attr)){ type = WIKI_DIR; }else{ type = WIKI_FOUND; } } } git_tree_free(tree); if (entry == NULL) { return WIKI_NOTFOUND; } *oid = git_tree_entry_id(entry); // is this safe? return type; }
/* * revert the same commit twice (when the first reverts cleanly): * * git revert 2d440f2 * git revert 2d440f2 */ void test_revert_workdir__again(void) { git_reference *head_ref; git_commit *orig_head; git_tree *reverted_tree; git_oid reverted_tree_oid, reverted_commit_oid; git_signature *signature; struct merge_index_entry merge_index_entries[] = { { 0100644, "7731926a337c4eaba1e2187d90ebfa0a93659382", 0, "file1.txt" }, { 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" }, { 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" }, { 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" }, }; cl_git_pass(git_repository_head(&head_ref, repo)); cl_git_pass(git_reference_peel((git_object **)&orig_head, head_ref, GIT_OBJ_COMMIT)); cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD, NULL)); cl_git_pass(git_revert(repo, orig_head, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 4)); cl_git_pass(git_index_write_tree(&reverted_tree_oid, repo_index)); cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid)); cl_git_pass(git_signature_new(&signature, "Reverter", "*****@*****.**", time(NULL), 0)); cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head)); cl_git_pass(git_revert(repo, orig_head, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 4)); git_signature_free(signature); git_tree_free(reverted_tree); git_commit_free(orig_head); git_reference_free(head_ref); }
void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void) { git_index *index = NULL; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid tree_id, commit_id; git_tree *tree = NULL; git_commit *commit = NULL; git_repository_index(&index, g_repo); opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_reference_name_to_id(&commit_id, g_repo, "refs/heads/master")); cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &opts)); cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777)); cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n"); cl_git_pass(git_index_add_bypath(index, "this-is-dir/contained_file")); git_index_write_tree(&tree_id, index); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file")); opts.checkout_strategy = GIT_CHECKOUT_SAFE; opts.checkout_strategy = 1; git_checkout_tree(g_repo, (git_object *)tree, &opts); git_tree_free(tree); git_commit_free(commit); git_index_free(index); }
void test_repo_init__init_with_initial_commit(void) { git_index *index; cl_set_cleanup(&cleanup_repository, "committed"); /* Initialize the repository */ cl_git_pass(git_repository_init(&_repo, "committed", 0)); /* Index will be automatically created when requested for a new repo */ cl_git_pass(git_repository_index(&index, _repo)); /* Create a file so we can commit it * * If you are writing code outside the test suite, you can create this * file any way that you like, such as: * FILE *fp = fopen("committed/file.txt", "w"); * fputs("some stuff\n", fp); * fclose(fp); * We like to use the help functions because they do error detection * in a way that's easily compatible with our test suite. */ cl_git_mkfile("committed/file.txt", "some stuff\n"); /* Add file to the index */ cl_git_pass(git_index_add_bypath(index, "file.txt")); cl_git_pass(git_index_write(index)); /* Intentionally not using cl_repo_commit_from_index here so this code * can be used as an example of how an initial commit is typically * made to a repository... */ /* Make sure we're ready to use git_signature_default :-) */ { git_config *cfg, *local; cl_git_pass(git_repository_config(&cfg, _repo)); cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL)); cl_git_pass(git_config_set_string(local, "user.name", "Test User")); cl_git_pass(git_config_set_string(local, "user.email", "*****@*****.**")); git_config_free(local); git_config_free(cfg); } /* Create a commit with the new contents of the index */ { git_signature *sig; git_oid tree_id, commit_id; git_tree *tree; cl_git_pass(git_signature_default(&sig, _repo)); cl_git_pass(git_index_write_tree(&tree_id, index)); cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id)); cl_git_pass(git_commit_create_v( &commit_id, _repo, "HEAD", sig, sig, NULL, "First", tree, 0)); git_tree_free(tree); git_signature_free(sig); } git_index_free(index); }
int git_commit_tree(git_tree **tree_out, const git_commit *commit) { assert(commit); return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id); }
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; }
int main (int argc, char** argv) { // ### Opening the Repository // There are a couple of methods for opening a repository, this being the simplest. // There are also [methods][me] for specifying the index file and work tree locations, here // we are assuming they are in the normal places. // // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository git_repository *repo; if (argc > 1) { git_repository_open(&repo, argv[1]); } else { git_repository_open(&repo, "/opt/libgit2-test/.git"); } // ### SHA-1 Value Conversions // For our first example, we will convert a 40 character hex value to the 20 byte raw SHA1 value. printf("*Hex to Raw*\n"); char hex[] = "fd6e612585290339ea8bf39c692a7ff6a29cb7c3"; // The `git_oid` is the structure that keeps the SHA value. We will use this throughout the example // for storing the value of the current SHA key we're working with. git_oid oid; git_oid_fromstr(&oid, hex); // Once we've converted the string into the oid value, we can get the raw value of the SHA. printf("Raw 20 bytes: [%.20s]\n", (&oid)->id); // Next we will convert the 20 byte raw SHA1 value to a human readable 40 char hex value. printf("\n*Raw to Hex*\n"); char out[41]; out[40] = '\0'; // If you have a oid, you can easily get the hex value of the SHA as well. git_oid_fmt(out, &oid); printf("SHA hex string: %s\n", out); // ### Working with the Object Database // **libgit2** provides [direct access][odb] to the object database. // The object database is where the actual objects are stored in Git. For // working with raw objects, we'll need to get this structure from the // repository. // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb git_odb *odb; git_repository_odb(&odb, repo); // #### Raw Object Reading printf("\n*Raw Object Read*\n"); git_odb_object *obj; git_otype otype; const unsigned char *data; const char *str_type; int error; // We can read raw objects directly from the object database if we have the oid (SHA) // of the object. This allows us to access objects without knowing thier type and inspect // the raw bytes unparsed. error = git_odb_read(&obj, odb, &oid); // A raw object only has three properties - the type (commit, blob, tree or tag), the size // of the raw data and the raw, unparsed data itself. For a commit or tag, that raw data // is human readable plain ASCII text. For a blob it is just file contents, so it could be // text or binary data. For a tree it is a special binary format, so it's unlikely to be // hugely helpful as a raw object. data = (const unsigned char *)git_odb_object_data(obj); otype = git_odb_object_type(obj); // We provide methods to convert from the object type which is an enum, to a string // representation of that value (and vice-versa). str_type = git_object_type2string(otype); printf("object length and type: %d, %s\n", (int)git_odb_object_size(obj), str_type); // For proper memory management, close the object when you are done with it or it will leak // memory. git_odb_object_free(obj); // #### Raw Object Writing printf("\n*Raw Object Write*\n"); // You can also write raw object data to Git. This is pretty cool because it gives you // direct access to the key/value properties of Git. Here we'll write a new blob object // that just contains a simple string. Notice that we have to specify the object type as // the `git_otype` enum. git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); // Now that we've written the object, we can check out what SHA1 was generated when the // object was written to our database. git_oid_fmt(out, &oid); printf("Written Object: %s\n", out); // ### Object Parsing // libgit2 has methods to parse every object type in Git so you don't have to work directly // with the raw data. This is much faster and simpler than trying to deal with the raw data // yourself. // #### Commit Parsing // [Parsing commit objects][pco] is simple and gives you access to all the data in the commit // - the // author (name, email, datetime), committer (same), tree, message, encoding and parent(s). // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit printf("\n*Commit Parsing*\n"); git_commit *commit; git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1"); error = git_commit_lookup(&commit, repo, &oid); const git_signature *author, *cmtter; const char *message; time_t ctime; unsigned int parents, p; // Each of the properties of the commit object are accessible via methods, including commonly // needed variations, such as `git_commit_time` which returns the author time and `_message` // which gives you the commit message. message = git_commit_message(commit); author = git_commit_author(commit); cmtter = git_commit_committer(commit); ctime = git_commit_time(commit); // The author and committer methods return [git_signature] structures, which give you name, email // and `when`, which is a `git_time` structure, giving you a timestamp and timezone offset. printf("Author: %s (%s)\n", author->name, author->email); // Commits can have zero or more parents. The first (root) commit will have no parents, most commits // will have one, which is the commit it was based on, and merge commits will have two or more. // Commits can technically have any number, though it's pretty rare to have more than two. parents = git_commit_parentcount(commit); for (p = 0;p < parents;p++) { git_commit *parent; git_commit_parent(&parent, commit, p); git_oid_fmt(out, git_commit_id(parent)); printf("Parent: %s\n", out); git_commit_free(parent); } // Don't forget to close the object to prevent memory leaks. You will have to do this for // all the objects you open and parse. git_commit_free(commit); // #### Writing Commits // // libgit2 provides a couple of methods to create commit objects easily as well. There are four // different create signatures, we'll just show one of them here. You can read about the other // ones in the [commit API docs][cd]. // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit printf("\n*Commit Writing*\n"); git_oid tree_id, parent_id, commit_id; git_tree *tree; git_commit *parent; // Creating signatures for an authoring identity and time is pretty simple - you will need to have // this to create a commit in order to specify who created it and when. Default values for the name // and email should be found in the `user.name` and `user.email` configuration options. See the `config` // section of this example file to see how to access config values. git_signature_new((git_signature **)&author, "Scott Chacon", "*****@*****.**", 123456789, 60); git_signature_new((git_signature **)&cmtter, "Scott A Chacon", "*****@*****.**", 987654321, 90); // Commit objects need a tree to point to and optionally one or more parents. Here we're creating oid // objects to create the commit with, but you can also use git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac"); git_tree_lookup(&tree, repo, &tree_id); git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1"); git_commit_lookup(&parent, repo, &parent_id); // Here we actually create the commit object with a single call with all the values we need to create // the commit. The SHA key is written to the `commit_id` variable here. git_commit_create_v( &commit_id, /* out id */ repo, NULL, /* do not update the HEAD */ author, cmtter, NULL, /* use default message encoding */ "example commit", tree, 1, parent); // Now we can take a look at the commit SHA we've generated. git_oid_fmt(out, &commit_id); printf("New Commit: %s\n", out); // #### Tag Parsing // You can parse and create tags with the [tag management API][tm], which functions very similarly // to the commit lookup, parsing and creation methods, since the objects themselves are very similar. // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag printf("\n*Tag Parsing*\n"); git_tag *tag; const char *tmessage, *tname; git_otype ttype; // We create an oid for the tag object if we know the SHA and look it up in the repository the same // way that we would a commit (or any other) object. git_oid_fromstr(&oid, "bc422d45275aca289c51d79830b45cecebff7c3a"); error = git_tag_lookup(&tag, repo, &oid); // Now that we have the tag object, we can extract the information it generally contains: the target // (usually a commit object), the type of the target object (usually 'commit'), the name ('v1.0'), // the tagger (a git_signature - name, email, timestamp), and the tag message. git_tag_target((git_object **)&commit, tag); tname = git_tag_name(tag); // "test" ttype = git_tag_type(tag); // GIT_OBJ_COMMIT (otype enum) tmessage = git_tag_message(tag); // "tag message\n" printf("Tag Message: %s\n", tmessage); git_commit_free(commit); // #### Tree Parsing // [Tree parsing][tp] is a bit different than the other objects, in that we have a subtype which is the // tree entry. This is not an actual object type in Git, but a useful structure for parsing and // traversing tree entries. // // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree printf("\n*Tree Parsing*\n"); const git_tree_entry *entry; git_object *objt; // Create the oid and lookup the tree object just like the other objects. git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"); git_tree_lookup(&tree, repo, &oid); // Getting the count of entries in the tree so you can iterate over them if you want to. int cnt = git_tree_entrycount(tree); // 3 printf("tree entries: %d\n", cnt); entry = git_tree_entry_byindex(tree, 0); printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" // You can also access tree entries by name if you know the name of the entry you're looking for. entry = git_tree_entry_byname(tree, "hello.c"); git_tree_entry_name(entry); // "hello.c" // Once you have the entry object, you can access the content or subtree (or commit, in the case // of submodules) that it points to. You can also get the mode if you want. git_tree_entry_to_object(&objt, repo, entry); // blob // Remember to close the looked-up object once you are done using it git_object_free(objt); // #### Blob Parsing // // The last object type is the simplest and requires the least parsing help. Blobs are just file // contents and can contain anything, there is no structure to it. The main advantage to using the // [simple blob api][ba] is that when you're creating blobs you don't have to calculate the size // of the content. There is also a helper for reading a file from disk and writing it to the db and // getting the oid back so you don't have to do all those steps yourself. // // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob printf("\n*Blob Parsing*\n"); git_blob *blob; git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0"); git_blob_lookup(&blob, repo, &oid); // You can access a buffer with the raw contents of the blob directly. // Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files): // do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to // find out its exact size in bytes printf("Blob Size: %ld\n", git_blob_rawsize(blob)); // 8 git_blob_rawcontent(blob); // "content" // ### Revwalking // // The libgit2 [revision walking api][rw] provides methods to traverse the directed graph created // by the parent pointers of the commit objects. Since all commits point back to the commit that // came directly before them, you can walk this parentage as a graph and find all the commits that // were ancestors of (reachable from) a given starting point. This can allow you to create `git log` // type functionality. // // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk printf("\n*Revwalking*\n"); git_revwalk *walk; git_commit *wcommit; git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1"); // To use the revwalker, create a new walker, tell it how you want to sort the output and then push // one or more starting points onto the walker. If you want to emulate the output of `git log` you // would push the SHA of the commit that HEAD points to into the walker and then start traversing them. // You can also 'hide' commits that you want to stop at or not see any of their ancestors. So if you // want to emulate `git log branch1..branch2`, you would push the oid of `branch2` and hide the oid // of `branch1`. git_revwalk_new(&walk, repo); git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); git_revwalk_push(walk, &oid); const git_signature *cauth; const char *cmsg; // Now that we have the starting point pushed onto the walker, we can start asking for ancestors. It // will return them in the sorting order we asked for as commit oids. // We can then lookup and parse the commited pointed at by the returned OID; // note that this operation is specially fast since the raw contents of the commit object will // be cached in memory while ((git_revwalk_next(&oid, walk)) == 0) { error = git_commit_lookup(&wcommit, repo, &oid); cmsg = git_commit_message(wcommit); cauth = git_commit_author(wcommit); printf("%s (%s)\n", cmsg, cauth->email); git_commit_free(wcommit); } // Like the other objects, be sure to free the revwalker when you're done to prevent memory leaks. // Also, make sure that the repository being walked it not deallocated while the walk is in // progress, or it will result in undefined behavior git_revwalk_free(walk); // ### Index File Manipulation // // The [index file API][gi] allows you to read, traverse, update and write the Git index file // (sometimes thought of as the staging area). // // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index printf("\n*Index Walking*\n"); git_index *index; unsigned int i, ecount; // You can either open the index from the standard location in an open repository, as we're doing // here, or you can open and manipulate any index file with `git_index_open_bare()`. The index // for the repository will be located and loaded from disk. git_repository_index(&index, repo); // For each entry in the index, you can get a bunch of information including the SHA (oid), path // and mode which map to the tree objects that are written out. It also has filesystem properties // to help determine what to inspect for changes (ctime, mtime, dev, ino, uid, gid, file_size and flags) // All these properties are exported publicly in the `git_index_entry` struct ecount = git_index_entrycount(index); for (i = 0; i < ecount; ++i) { git_index_entry *e = git_index_get(index, i); printf("path: %s\n", e->path); printf("mtime: %d\n", (int)e->mtime.seconds); printf("fs: %d\n", (int)e->file_size); } git_index_free(index); // ### References // // The [reference API][ref] allows you to list, resolve, create and update references such as // branches, tags and remote references (everything in the .git/refs directory). // // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference printf("\n*Reference Listing*\n"); // Here we will implement something like `git for-each-ref` simply listing out all available // references and the object SHA they resolve to. git_strarray ref_list; git_reference_list(&ref_list, repo, GIT_REF_LISTALL); const char *refname; git_reference *ref; // Now that we have the list of reference names, we can lookup each ref one at a time and // resolve them to the SHA, then print both values out. for (i = 0; i < ref_list.count; ++i) { refname = ref_list.strings[i]; git_reference_lookup(&ref, repo, refname); switch (git_reference_type(ref)) { case GIT_REF_OID: git_oid_fmt(out, git_reference_oid(ref)); printf("%s [%s]\n", refname, out); break; case GIT_REF_SYMBOLIC: printf("%s => %s\n", refname, git_reference_target(ref)); break; default: fprintf(stderr, "Unexpected reference type\n"); exit(1); } } git_strarray_free(&ref_list); // ### Config Files // // The [config API][config] allows you to list and updatee config values in // any of the accessible config file locations (system, global, local). // // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config printf("\n*Config Listing*\n"); const char *email; int32_t j; git_config *cfg; // Open a config object so we can read global values from it. git_config_open_ondisk(&cfg, "~/.gitconfig"); git_config_get_int32(cfg, "help.autocorrect", &j); printf("Autocorrect: %d\n", j); git_config_get_string(cfg, "user.email", &email); printf("Email: %s\n", email); // Finally, when you're done with the repository, you can free it as well. git_repository_free(repo); return 0; }
void MainWindow::on_pushButtonCommit_clicked() { if (repo != NULL) { QString filepath = ui->textRepoLocation->text(); QFile myfile(filepath+ "/test.txt"); if (myfile.open(QFile::WriteOnly)) { QTextStream out(&myfile); out << ui->textContent->toPlainText(); } //add to index git_strarray array = {0}; git_index* index; struct print_payload payload; git_repository_index(&index, repo); git_index_add_all(index, &array, 0, NULL, &payload); git_index_write(index); //note2self: the above simply writes //now, do a commit! //random: list references git_strarray refs = {0}; int error = git_reference_list(&refs, repo); for (int i=0; i<refs.count; i++) { qDebug() << "ref " <<i<<" = "<< refs.strings[i]; } //end random: list references //for a commit, we need: // -repo (obviously) // - name of ref (usually "HEAD") // - author commiter sig // - encoding+ message // - root tree (pointer // - parent count+parents (why? can have multiple parents) git_tree *tree; git_oid tree_id, parent_id, commit_id; error = git_index_write_tree(&tree_id, index); //we need to put index into a tree object for commit error = git_tree_lookup(&tree, repo, &tree_id); //tree_id = *git_object_id((git_object*)tree); git_commit *parent; //get HEAD and use it as parent of commit, put it in parent error = git_reference_name_to_id(&parent_id, repo, "HEAD"); error = git_commit_lookup(&parent, repo, &parent_id); //do commit git_signature* sig; git_signature_now(&sig, "johnty", "*****@*****.**"); const char* msg = "Test Commit Message"; git_commit_create_v( &commit_id, repo, "HEAD", sig, sig, NULL, msg, tree, 1, parent ); git_index_free(index); git_signature_free(sig); git_tree_free(tree); git_commit_free(parent); //QMessageBox::information(this, tr("commit"), tr("commit successful")); } }
static int try_to_git_merge(git_repository *repo, git_reference *local, git_reference *remote, git_oid *base, const git_oid *local_id, const git_oid *remote_id) { git_tree *local_tree, *remote_tree, *base_tree; git_commit *local_commit, *remote_commit, *base_commit; git_index *merged_index; git_merge_options merge_options; if (verbose) { char outlocal[41], outremote[41]; outlocal[40] = outremote[40] = 0; git_oid_fmt(outlocal, local_id); git_oid_fmt(outremote, remote_id); fprintf(stderr, "trying to merge local SHA %s remote SHA %s\n", outlocal, outremote); } git_merge_init_options(&merge_options, GIT_MERGE_OPTIONS_VERSION); #ifdef USE_LIBGIT23_API merge_options.tree_flags = GIT_MERGE_TREE_FIND_RENAMES; #else merge_options.flags = GIT_MERGE_TREE_FIND_RENAMES; #endif merge_options.file_favor = GIT_MERGE_FILE_FAVOR_UNION; merge_options.rename_threshold = 100; if (git_commit_lookup(&local_commit, repo, local_id)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: can't get commit (%s)"), giterr_last()->message); if (git_commit_tree(&local_tree, local_commit)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: failed local tree lookup (%s)"), giterr_last()->message); if (git_commit_lookup(&remote_commit, repo, remote_id)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: can't get commit (%s)"), giterr_last()->message); if (git_commit_tree(&remote_tree, remote_commit)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: failed remote tree lookup (%s)"), giterr_last()->message); if (git_commit_lookup(&base_commit, repo, base)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: can't get commit: (%s)"), giterr_last()->message); if (git_commit_tree(&base_tree, base_commit)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: failed base tree lookup: (%s)"), giterr_last()->message); if (git_merge_trees(&merged_index, repo, base_tree, local_tree, remote_tree, &merge_options)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: merge failed (%s)"), giterr_last()->message); if (git_index_has_conflicts(merged_index)) { int error; const git_index_entry *ancestor = NULL, *ours = NULL, *theirs = NULL; git_index_conflict_iterator *iter = NULL; error = git_index_conflict_iterator_new(&iter, merged_index); while (git_index_conflict_next(&ancestor, &ours, &theirs, iter) != GIT_ITEROVER) { /* Mark this conflict as resolved */ fprintf(stderr, "conflict in %s / %s / %s -- ", ours ? ours->path : "-", theirs ? theirs->path : "-", ancestor ? ancestor->path : "-"); if ((!ours && theirs && ancestor) || (ours && !theirs && ancestor)) { // the file was removed on one side or the other - just remove it fprintf(stderr, "looks like a delete on one side; removing the file from the index\n"); error = git_index_remove(merged_index, ours ? ours->path : theirs->path, GIT_INDEX_STAGE_ANY); } else { error = git_index_conflict_remove(merged_index, ours ? ours->path : theirs ? theirs->path : ancestor->path); } if (error) { fprintf(stderr, "error at conflict resplution (%s)", giterr_last()->message); } } git_index_conflict_cleanup(merged_index); git_index_conflict_iterator_free(iter); report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: merge conflict - manual intervention needed")); } git_oid merge_oid, commit_oid; git_tree *merged_tree; git_signature *author; git_commit *commit; if (git_index_write_tree_to(&merge_oid, merged_index, repo)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: writing the tree failed (%s)"), giterr_last()->message); if (git_tree_lookup(&merged_tree, repo, &merge_oid)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: tree lookup failed (%s)"), giterr_last()->message); if (git_signature_default(&author, repo) < 0) return report_error(translate("gettextFromC", "Failed to get author: (%s)"), giterr_last()->message); if (git_commit_create_v(&commit_oid, repo, NULL, author, author, NULL, "automatic merge", merged_tree, 2, local_commit, remote_commit)) return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: git commit create failed (%s)"), giterr_last()->message); if (git_commit_lookup(&commit, repo, &commit_oid)) return report_error(translate("gettextFromC", "Error: could not lookup the merge commit I just created (%s)"), giterr_last()->message); if (git_branch_is_head(local) && !git_repository_is_bare(repo)) { git_object *parent; git_reference_peel(&parent, local, GIT_OBJ_COMMIT); if (update_git_checkout(repo, parent, merged_tree)) { report_error("Warning: checked out branch is inconsistent with git data"); } } if (git_reference_set_target(&local, local, &commit_oid, "Subsurface merge event")) return report_error("Error: failed to update branch (%s)", giterr_last()->message); set_git_id(&commit_oid); git_signature_free(author); return 0; }
static int rebase_commit__create( git_commit **out, git_rebase *rebase, git_index *index, git_commit *parent_commit, const git_signature *author, const git_signature *committer, const char *message_encoding, const char *message) { git_rebase_operation *operation; git_commit *current_commit = NULL, *commit = NULL; git_tree *parent_tree = NULL, *tree = NULL; git_oid tree_id, commit_id; int error; operation = git_array_get(rebase->operations, rebase->current); if (git_index_has_conflicts(index)) { giterr_set(GITERR_REBASE, "conflicts have not been resolved"); error = GIT_EUNMERGED; goto done; } if ((error = git_commit_lookup(¤t_commit, rebase->repo, &operation->id)) < 0 || (error = git_commit_tree(&parent_tree, parent_commit)) < 0 || (error = git_index_write_tree_to(&tree_id, index, rebase->repo)) < 0 || (error = git_tree_lookup(&tree, rebase->repo, &tree_id)) < 0) goto done; if (git_oid_equal(&tree_id, git_tree_id(parent_tree))) { giterr_set(GITERR_REBASE, "this patch has already been applied"); error = GIT_EAPPLIED; goto done; } if (!author) author = git_commit_author(current_commit); if (!message) { message_encoding = git_commit_message_encoding(current_commit); message = git_commit_message(current_commit); } if ((error = git_commit_create(&commit_id, rebase->repo, NULL, author, committer, message_encoding, message, tree, 1, (const git_commit **)&parent_commit)) < 0 || (error = git_commit_lookup(&commit, rebase->repo, &commit_id)) < 0) goto done; *out = commit; done: if (error < 0) git_commit_free(commit); git_commit_free(current_commit); git_tree_free(parent_tree); git_tree_free(tree); return error; }