PyObject * Repository_revparse_single(Repository *self, PyObject *py_spec) { git_object *c_obj; char *c_spec; int err; /* 1- Get the C revision spec */ c_spec = py_str_to_c_str(py_spec, NULL); if (c_spec == NULL) return NULL; /* 2- Lookup */ err = git_revparse_single(&c_obj, self->repo, c_spec); if (err < 0) { PyObject *err_obj = Error_set_str(err, c_spec); free(c_spec); return err_obj; } free(c_spec); return wrap_object(c_obj, self); }
void test_refs_reflog_messages__creating_branches_default_messages(void) { git_buf buf = GIT_BUF_INIT; git_annotated_commit *annotated; git_object *obj; git_commit *target; git_reference *branch1, *branch2; cl_git_pass(git_revparse_single(&obj, g_repo, "e90810b8df3")); cl_git_pass(git_commit_lookup(&target, g_repo, git_object_id(obj))); git_object_free(obj); cl_git_pass(git_branch_create(&branch1, g_repo, NEW_BRANCH_NAME, target, false)); cl_git_pass(git_buf_printf(&buf, "branch: Created from %s", git_oid_tostr_s(git_commit_id(target)))); cl_reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0, GIT_OID_HEX_ZERO, git_oid_tostr_s(git_commit_id(target)), g_email, git_buf_cstr(&buf)); cl_git_pass(git_reference_remove(g_repo, "refs/heads/" NEW_BRANCH_NAME)); cl_git_pass(git_annotated_commit_from_revspec(&annotated, g_repo, "e90810b8df3")); cl_git_pass(git_branch_create_from_annotated(&branch2, g_repo, NEW_BRANCH_NAME, annotated, true)); cl_reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0, GIT_OID_HEX_ZERO, git_oid_tostr_s(git_commit_id(target)), g_email, "branch: Created from e90810b8df3"); git_annotated_commit_free(annotated); git_buf_free(&buf); git_commit_free(target); git_reference_free(branch1); git_reference_free(branch2); }
void test_checkout_typechange__checkout_typechanges(void) { int i; git_object *obj; git_checkout_opts opts = {0}; opts.checkout_strategy = GIT_CHECKOUT_REMOVE_UNTRACKED | GIT_CHECKOUT_CREATE_MISSING | GIT_CHECKOUT_OVERWRITE_MODIFIED; for (i = 0; g_typechange_oids[i] != NULL; ++i) { cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i])); /* fprintf(stderr, "checking out '%s'\n", g_typechange_oids[i]); */ cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); git_object_free(obj); if (!g_typechange_empty[i]) { cl_assert(git_path_isdir("typechanges")); cl_assert(git_path_exists("typechanges/a")); cl_assert(git_path_exists("typechanges/b")); cl_assert(git_path_exists("typechanges/c")); cl_assert(git_path_exists("typechanges/d")); cl_assert(git_path_exists("typechanges/e")); } else { cl_assert(git_path_isdir("typechanges")); cl_assert(!git_path_exists("typechanges/a")); cl_assert(!git_path_exists("typechanges/b")); cl_assert(!git_path_exists("typechanges/c")); cl_assert(!git_path_exists("typechanges/d")); cl_assert(!git_path_exists("typechanges/e")); } } }
void test_clone_nonetwork__can_detached_head(void) { git_object *obj; git_repository *cloned; git_reference *cloned_head; cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); cl_git_pass(git_revparse_single(&obj, g_repo, "master~1")); cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj))); cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options)); cl_assert(git_repository_head_detached(cloned)); cl_git_pass(git_repository_head(&cloned_head, cloned)); cl_assert(!git_oid_cmp(git_object_id(obj), git_reference_target(cloned_head))); git_object_free(obj); git_reference_free(cloned_head); git_repository_free(cloned); cl_fixture_cleanup("./foo1"); }
void test_reset_hard__resetting_reverts_unmerged(void) { git_index *index; int entries; /* Ensure every permutation of non-zero stage entries results in the * path being cleaned up. */ for (entries = 1; entries < 8; entries++) { cl_git_pass(git_repository_index(&index, repo)); unmerged_index_init(index, entries); cl_git_pass(git_index_write(index)); cl_git_pass(git_revparse_single(&target, repo, "26a125e")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL, NULL)); cl_assert(git_path_exists("status/conflicting_file") == 0); git_object_free(target); target = NULL; git_index_free(index); } }
/* * $ git ls-files --cached -s --abbrev=7 -- "staged*" * 100644 55d316c 0 staged_changes * 100644 a6be623 0 staged_changes_file_deleted * ... * * $ git reset 0017bd4 -- staged_changes staged_changes_file_deleted * Unstaged changes after reset: * ... * * $ git ls-files --cached -s --abbrev=7 -- "staged*" * 100644 32504b7 0 staged_changes * 100644 061d42a 0 staged_changes_file_deleted * ... */ void test_reset_default__resetting_filepaths_replaces_their_corresponding_index_entries(void) { git_strarray before, after; char *paths[] = { "staged_changes", "staged_changes_file_deleted" }; char *before_shas[] = { "55d316c9ba708999f1918e9677d01dfcae69c6b9", "a6be623522ce87a1d862128ac42672604f7b468b" }; char *after_shas[] = { "32504b727382542f9f089e24fddac5e78533e96c", "061d42a44cacde5726057b67558821d95db96f19" }; _pathspecs.strings = paths; _pathspecs.count = 2; before.strings = before_shas; before.count = 2; after.strings = after_shas; after.count = 2; cl_git_pass(git_revparse_single(&_target, _repo, "0017bd4")); assert_content_in_index(&_pathspecs, true, &before); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, true, &after); }
void QGit::commit(QString message) { git_repository *repo = nullptr; git_signature *me = nullptr; git_commit *parent = nullptr; git_index *index = nullptr; git_tree *tree = nullptr; git_oid new_commit_id = { {0} }; git_oid parent_id = { {0} }; git_oid tree_id = { {0} }; git_object *git_obj = nullptr; git_diff *diff = nullptr; size_t _count = 0; int unborn = 0; int res = 0; QGitError error; try { res = git_repository_open(&repo, m_path.absolutePath().toUtf8().constData()); if (res) { throw QGitError("git_repository_open", res); } /// Check if somthing is staged res = git_revparse_single(&git_obj, repo, "HEAD^{tree}"); if (res) { throw QGitError("git_revparse_single(staged)", res); } res = git_tree_lookup(&tree, repo, git_object_id(git_obj)); if (res) { throw QGitError("git_tree_lookup(staged)", res); } res = git_diff_tree_to_index(&diff, repo, tree, nullptr, nullptr); if (res) { throw QGitError("git_diff_tree_to_index(staged)", res); } _count = git_diff_num_deltas(diff); if (_count == 0) { throw QGitError("Nothing staged.", res); } if (tree) { git_tree_free(tree); tree = nullptr; } /// Check end res = git_signature_default(&me, repo); if (res) { throw QGitError("git_signature_default", res); } res = git_repository_index(&index, repo); if (res) { throw QGitError("git_repository_index", res); } res = git_index_write_tree(&tree_id, index); if (res) { throw QGitError("git_index_write_tree", res); } res = git_tree_lookup(&tree, repo, &tree_id); if (res) { throw QGitError("git_tree_lookup", res); } unborn = git_repository_head_unborn(repo); if (unborn) { res = git_commit_create_v( &new_commit_id, repo, "HEAD", /* name of ref to update */ me, /* author */ me, /* committer */ nullptr, /* nullptr = UTF-8 message encoding */ message.toUtf8().constData(), /* message */ tree, /* root tree */ 0); /* parent count */ if (res) { throw QGitError("git_commit_create_v", res); } } else { res = git_reference_name_to_id(&parent_id, repo, "HEAD"); if (res) { throw QGitError("git_reference_name_to_id", res); } res = git_commit_lookup(&parent, repo, &parent_id); if (res) { throw QGitError("git_commit_lookup", res); } res = git_commit_create_v( &new_commit_id, repo, "HEAD", /* name of ref to update */ me, /* author */ me, /* committer */ nullptr, /* nullptr = UTF-8 message encoding */ message.toUtf8().constData(), /* message */ tree, /* root tree */ 1, /* parent count */ parent); /* parent */ if (res) { throw QGitError("git_commit_create_v", res); } } } catch(const QGitError &ex) { error = ex; } emit commitReply(QString::fromUtf8(reinterpret_cast<const char *>(new_commit_id.id)), error); if (parent) { git_commit_free(parent); parent = nullptr; } if (index) { git_index_free(index); index = nullptr; } if (tree) { git_tree_free(tree); tree = nullptr; } if (me) { git_signature_free(me); me = nullptr; } if (diff) { git_diff_free(diff); diff = nullptr; } if (git_obj) { git_object_free(git_obj); git_obj = nullptr; } if (repo) { git_repository_free(repo); repo = nullptr; } }
void test_checkout_tree__cannot_checkout_a_non_treeish(void) { /* blob */ cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")); cl_git_fail(git_checkout_tree(g_repo, g_object, NULL)); }
int main(int argc, char *argv[]) { const char *dir = ".", *rev = NULL; int i, action = 0, verbose = 0; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; git_threads_init(); for (i = 1; i < argc; ++i) { char *a = argv[i]; if (a[0] != '-') { if (rev != NULL) usage("Only one rev should be provided", NULL); else rev = a; } else if (!strcmp(a, "-t")) action = SHOW_TYPE; else if (!strcmp(a, "-s")) action = SHOW_SIZE; else if (!strcmp(a, "-e")) action = SHOW_NONE; else if (!strcmp(a, "-p")) action = SHOW_PRETTY; else if (!strcmp(a, "-q")) verbose = 0; else if (!strcmp(a, "-v")) verbose = 1; else if (!strcmp(a, "--help") || !strcmp(a, "-h")) usage(NULL, NULL); else if (!check_str_param(a, "--git-dir=", &dir)) usage("Unknown option", a); } if (!action || !rev) usage(NULL, NULL); check(git_repository_open_ext(&g_repo, dir, 0, NULL), "Could not open repository"); if (git_revparse_single(&obj, g_repo, rev) < 0) { fprintf(stderr, "Could not resolve '%s'\n", rev); exit(1); } if (verbose) { char oidstr[GIT_OID_HEXSZ + 1]; git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); printf("%s %s\n--\n", git_object_type2string(git_object_type(obj)), oidstr); } switch (action) { case SHOW_TYPE: printf("%s\n", git_object_type2string(git_object_type(obj))); break; case SHOW_SIZE: { git_odb *odb; git_odb_object *odbobj; check(git_repository_odb(&odb, g_repo), "Could not open ODB"); check(git_odb_read(&odbobj, odb, git_object_id(obj)), "Could not find obj"); printf("%ld\n", (long)git_odb_object_size(odbobj)); git_odb_object_free(odbobj); git_odb_free(odb); } break; case SHOW_NONE: /* just want return result */ break; case SHOW_PRETTY: switch (git_object_type(obj)) { case GIT_OBJ_BLOB: show_blob((const git_blob *)obj); break; case GIT_OBJ_COMMIT: show_commit((const git_commit *)obj); break; case GIT_OBJ_TREE: show_tree((const git_tree *)obj); break; case GIT_OBJ_TAG: show_tag((const git_tag *)obj); break; default: printf("unknown %s\n", oidstr); break; } break; } git_object_free(obj); git_repository_free(g_repo); git_threads_shutdown(); return 0; }
static void assert_invalid_spec(const char *invalid_spec) { cl_assert_equal_i( GIT_EINVALIDSPEC, git_revparse_single(&g_obj, g_repo, invalid_spec)); }
void download(KImplementation const *impl, int requested, KError *error, void *ctx) { KDictionary const *values = k_implementation_get_values(impl); char const *href = k_dictionary_lookup(values, "href"); char const *tag = k_dictionary_lookup(values, "tag"); char const *path = k_implementation_get_name(impl); git_repository *repo = NULL; git_remote *origin = NULL; git_object *object = NULL; struct progress_data progress_data = {{0}}; git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; checkout_opts.version = GIT_CHECKOUT_OPTS_VERSION; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; checkout_opts.progress_cb = checkout_progress; checkout_opts.progress_payload = &progress_data; if (git_repository_open(&repo, path) == GIT_OK) { if (git_remote_load(&origin, repo, "origin") != GIT_OK) { k_error_set(error, giterr_last()->message); goto repository_free; } if (strcmp(git_remote_url(origin), href) != 0) { k_error_set(error, "different origin"); goto remote_free; } } else { if (git_repository_init(&repo, path, 0) != GIT_OK) { k_error_set(error, giterr_last()->message); return; } if (git_remote_create(&origin, repo, "origin", href) != GIT_OK) { k_error_set(error, giterr_last()->message); goto repository_free; } } git_remote_set_update_fetchhead(origin, 0); git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); if (git_remote_connect(origin, GIT_DIRECTION_FETCH) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_free; } if (git_remote_download(origin, fetch_progress, &progress_data) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_remote_update_tips(origin) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_revparse_single(&object, repo, tag) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_repository_set_head_detached(repo, git_object_id(object)) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_checkout_tree(repo, object, &checkout_opts) != GIT_OK) { k_error_set(error, giterr_last()->message); } remote_disconnect: git_remote_disconnect(origin); remote_free: git_remote_free(origin); repository_free: git_repository_free(repo); }
void test_refs_revparse__date(void) { /* * $ git reflog HEAD --date=iso * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2 * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit: * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes * * $ git reflog HEAD --date=raw * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master * a4a7dce HEAD@{1335806617 -0900}: commit: checking in * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2 * a65fedf HEAD@{1335806603 -0900}: commit: * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour */ cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "HEAD@{10 years ago}")); test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); /* * $ git reflog master --date=iso * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src... * * $ git reflog master --date=raw * a65fedf master@{1335806603 -0800}: commit: checking in * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso */ /* * $ git reflog -1 "master@{2012-04-30 17:22:42 +0000}" * warning: Log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800. */ cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "master@{2012-04-30 17:22:42 +0000}")); cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "master@{2012-04-30 09:22:42 -0800}")); /* * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}" * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg */ test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); /* * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}" * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in */ test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); /* * $ git reflog -1 master@{2012-05-03} * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in */ test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); /* * $ git reflog -1 "master@{1335806603}" * a65fedf * * $ git reflog -1 "master@{1335806602}" * be3563a */ test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); }
static gboolean git_evtag_builtin_sign (struct EvTag *self, int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; int r; const char *tagname; git_object *obj = NULL; git_oid specified_oid; GOptionContext *optcontext; guint64 elapsed_ns; char commit_oid_hexstr[GIT_OID_HEXSZ+1]; optcontext = g_option_context_new ("TAGNAME - Create a new GPG signed tag"); if (!option_context_parse (optcontext, sign_options, &argc, &argv, cancellable, error)) goto out; if (argc < 2) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "A TAGNAME argument is required"); goto out; } else if (argc > 2) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Too many arguments"); goto out; } tagname = argv[1]; r = git_revparse_single (&obj, self->top_repo, "HEAD"); if (!handle_libgit_ret (r, error)) goto out; specified_oid = *git_object_id (obj); git_oid_fmt (commit_oid_hexstr, &specified_oid); commit_oid_hexstr[sizeof(commit_oid_hexstr)-1] = '\0'; if (!validate_at_head (self, &specified_oid, error)) goto out; if (!checksum_commit_recurse (self, &specified_oid, &elapsed_ns, cancellable, error)) goto out; if (opt_print_only) { char *stats = get_stats (self); g_print ("%s\n", stats); g_free (stats); g_print ("%s %s\n", EVTAG_SHA512, g_checksum_get_string (self->checksum)); } else { const char *editor; int tmpfd; char *temppath; char *editor_child_argv[] = { NULL, NULL, NULL }; GPtrArray *gittag_child_argv = g_ptr_array_new (); GString *buf = g_string_new ("\n\n"); gboolean have_evtag; tmpfd = g_file_open_tmp ("git-evtag-XXXXXX.md", &temppath, error); if (tmpfd < 0) goto out; (void) close (tmpfd); g_string_append_printf (buf, "# git-evtag comment: Computed checksum in %0.1fs\n", (double)(elapsed_ns) / (double) G_USEC_PER_SEC); { char *stats = get_stats (self); g_string_append (buf, stats); g_string_append_c (buf, '\n'); g_free (stats); } g_string_append (buf, EVTAG_SHA512); g_string_append_c (buf, ' '); g_string_append (buf, g_checksum_get_string (self->checksum)); g_string_append_c (buf, '\n'); if (opt_with_legacy_archive_tag) { if (!compute_and_append_legacy_archive_checksum (commit_oid_hexstr, buf, cancellable, error)) goto out; } if (!g_file_set_contents (temppath, buf->str, -1, error)) goto out; g_string_free (buf, TRUE); editor = getenv ("EDITOR"); if (!editor) editor = "vi"; editor_child_argv[0] = (char*)editor; editor_child_argv[1] = (char*)temppath; if (!spawn_sync_require_success (editor_child_argv, G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN, error)) goto out; if (!check_file_has_evtag (temppath, &have_evtag, error)) goto out; if (!have_evtag) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Aborting tag due to deleted Git-EVTag line"); goto out; } g_ptr_array_add (gittag_child_argv, "git"); g_ptr_array_add (gittag_child_argv, "tag"); if (!opt_no_signature) g_ptr_array_add (gittag_child_argv, "-s"); if (opt_keyid) { g_ptr_array_add (gittag_child_argv, "--local-user"); g_ptr_array_add (gittag_child_argv, opt_keyid); } g_ptr_array_add (gittag_child_argv, "-F"); g_ptr_array_add (gittag_child_argv, temppath); g_ptr_array_add (gittag_child_argv, (char*)tagname); g_ptr_array_add (gittag_child_argv, (char*)commit_oid_hexstr); g_ptr_array_add (gittag_child_argv, NULL); if (!spawn_sync_require_success ((char**)gittag_child_argv->pdata, G_SPAWN_SEARCH_PATH, error)) { g_printerr ("Saved tag message in: %s\n", temppath); goto out; } (void) unlink (temppath); g_ptr_array_free (gittag_child_argv, TRUE); } ret = TRUE; out: return ret; }
static void test_with_many(int expected_new) { git_index *index; git_tree *tree, *new_tree; git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/ikeepsix2.txt")); cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt")); cl_git_pass(git_index_add_bypath(index, "ikeepsix2.txt")); cl_git_pass(git_index_write(index)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, NULL, NULL, NULL, &exp)); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(expected_new + 2, exp.files); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, NULL, NULL, NULL, &exp)); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(expected_new + 1, exp.files); git_diff_free(diff); cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "yoyoyo"); cl_git_pass(git_revparse_single( (git_object **)&new_tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree, new_tree, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, NULL, NULL, NULL, &exp)); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(expected_new + 2, exp.files); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, NULL, NULL, NULL, &exp)); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(expected_new + 1, exp.files); git_diff_free(diff); git_tree_free(new_tree); git_tree_free(tree); git_index_free(index); }
int NotesModel::pull() { git_repository *repo = NULL; git_remote *remote = NULL; git_merge_head *merge_head = NULL; if (!QSettings().value("gitRemoteUrl").isValid()) { qDebug() << "gitRemoteUrl setting invalid"; return false; } if (QSettings().value("gitRemoteUrl") == "") return false; try { //Open Repo e(git_repository_open(&repo, notesFolder().absolutePath().toUtf8().constData())); //List repository and add one from Settings if none exists or doesn t have same URI than settings git_strarray remotes = {0}; git_remote_list(&remotes, repo); if (remotes.count >= 1) { for (size_t i = 0; i < remotes.count; i++) { e(git_remote_load(&remote, repo, remotes.strings[i])); qDebug() << git_remote_url(remote); if (QSettings().value("gitRemoteUrl").isValid() && git_remote_url(remote)) { if (strcmp(git_remote_url(remote),QSettings().value("gitRemoteUrl").toString().toUtf8().constData()) != 0) { e(git_remote_delete(remote)); e(git_remote_create(&remote, repo, "upstream", QSettings().value("gitRemoteUrl").toString().toUtf8().constData())); e(git_remote_save(remote)); } } } } else if (remotes.count == 0) { e(git_remote_create(&remote, repo, "upstream", QSettings().value("gitRemoteUrl").toString().toUtf8().constData())); e(git_remote_save(remote)); } e(git_remote_load(&remote, repo, "upstream")); git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; callbacks.credentials = cred_acquire_cb; e(git_remote_set_callbacks(remote, &callbacks)); e(git_remote_connect(remote, GIT_DIRECTION_FETCH)); int connected = git_remote_connected(remote); if (connected) e(git_remote_fetch(remote, NULL, NULL)); git_checkout_options checkout_opt = GIT_CHECKOUT_OPTIONS_INIT; checkout_opt.checkout_strategy = GIT_CHECKOUT_FORCE; checkout_opt.file_mode = 0644; git_merge_options merge_opt = GIT_MERGE_OPTIONS_INIT; merge_opt.file_favor = GIT_MERGE_FILE_FAVOR_UNION; const git_remote_head **head = NULL; size_t size = 0; e(git_remote_ls(&head, &size, remote)); if (size <= 0) e(-1); git_oid oid = head[0]->oid; e(git_merge_head_from_fetchhead(&merge_head, repo, "master", git_remote_url(remote), &oid)); e(git_merge(repo, (const git_merge_head **)(&merge_head), 1, &merge_opt, &checkout_opt)); //TRY TO COMMIT /* Create signature */ git_signature *me = NULL; e(git_signature_now(&me, "sparkleNotes", "*****@*****.**")); //Tree Lookup git_object *tree_obj; e(git_revparse_single(&tree_obj, repo, "HEAD^{tree}")); // Get parent commit git_oid parentCommitId; git_commit *parent; git_oid remoteParentCommitId; git_commit *remoteParent; e(git_reference_name_to_id( &parentCommitId, repo, "ORIG_HEAD" )); e(git_commit_lookup( &parent, repo, &parentCommitId )); e(git_reference_name_to_id( &remoteParentCommitId, repo, "MERGE_HEAD" )); e(git_commit_lookup( &remoteParent, repo, &remoteParentCommitId )); if (remoteParent == parent) { //Same commit ... nothing to merge e(git_repository_state_cleanup(repo)); git_merge_head_free(merge_head); git_remote_free(remote); git_repository_free(repo); return false; } const git_commit *parents [2] = { parent, remoteParent }; git_oid new_commit_id; e(git_commit_create( &new_commit_id, repo, "HEAD", /* name of ref to update */ me, /* author */ me, /* committer */ "UTF-8", /* message encoding */ "Merge remote upstream/master", /* message */ (git_tree *) tree_obj, /* root tree */ 2, /* parent count */ parents)); /* parents */ git_merge_head_free(merge_head); git_remote_free(remote); e(git_repository_state_cleanup(repo)); git_repository_free(repo); } catch (int error) { const git_error *err = giterr_last(); if (err != NULL) qDebug() << QString::number(err->klass) + "\t" + QString(err->message); emit this->error(QString(err->message)); giterr_clear(); git_merge_head_free(merge_head); git_remote_free(remote); git_repository_free(repo); } return true; }
/** * Find object specified by revision * * @param repo S4 class git_repository * @param revision The revision string, see * http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions * @return S4 object of class git_commit, git_tag or git_tree. */ SEXP git2r_revparse_single(SEXP repo, SEXP revision) { int err; SEXP result = R_NilValue; git_repository *repository = NULL; git_object *treeish = NULL; if (git2r_arg_check_string(revision)) git2r_error(git2r_err_string_arg, __func__, "revision"); repository = git2r_repository_open(repo); if (!repository) git2r_error(git2r_err_invalid_repository, __func__, NULL); err = git_revparse_single( &treeish, repository, CHAR(STRING_ELT(revision, 0))); if (GIT_OK != err) goto cleanup; switch (git_object_type(treeish)) { case GIT_OBJ_COMMIT: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_commit"))); git2r_commit_init((git_commit*)treeish, repo, result); break; case GIT_OBJ_TAG: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_tag"))); git2r_tag_init((git_tag*)treeish, repo, result); break; case GIT_OBJ_TREE: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_tree"))); git2r_tree_init((git_tree*)treeish, repo, result); break; default: giterr_set_str(GITERR_NONE, git2r_err_revparse_single); err = GIT_ERROR; break; } cleanup: if (treeish) git_object_free(treeish); if (repository) git_repository_free(repository); if (R_NilValue != result) UNPROTECT(1); if (GIT_OK != err) { if (GIT_ENOTFOUND == err) { git2r_error( git2r_err_from_libgit2, __func__, "Requested object could not be found"); } else { git2r_error( git2r_err_from_libgit2, __func__, giterr_last()->message); } } return result; }
void QGit::commitDiff(QString commitId) { const git_diff_delta *delta = nullptr; git_tree *commit_tree = nullptr; git_tree *parent_tree = nullptr; git_repository *repo = nullptr; git_commit *parent = nullptr; git_commit *commit = nullptr; git_object *obj = nullptr; git_diff *diff = nullptr; QList<QGitCommitDiffParent> parents; QGitSignature commitAuthor, commitCommiter; QDateTime commitTime; QString commitMessage; QGitCommit commitDiff; unsigned int parentCount; int res = 0; QGitError error; try { res = git_repository_open(&repo, m_path.absolutePath().toUtf8().constData()); if (res) { throw QGitError("git_repository_open", res); } if (commitId.isEmpty()) { commitId = QStringLiteral("HEAD"); } res = git_revparse_single(&obj, repo, commitId.toLatin1()); if (res) { throw QGitError("git_revparse_single", res); } res = git_commit_lookup(&commit, repo, git_object_id(obj)); if (res) { throw QGitError("git_commit_lookup", res); } res = git_commit_tree(&commit_tree, commit); if (res) { throw QGitError("git_commit_tree", res); } commitAuthor = QGitSignature(QString::fromUtf8(git_commit_author(commit)->name), QString::fromUtf8(git_commit_author(commit)->email), QDateTime::fromMSecsSinceEpoch(git_commit_author(commit)->when.time * 1000)); commitCommiter = QGitSignature(QString::fromUtf8(git_commit_committer(commit)->name), QString::fromUtf8(git_commit_committer(commit)->email), QDateTime::fromMSecsSinceEpoch(git_commit_committer(commit)->when.time * 1000)); auto time = git_commit_time(commit); auto timeOffset = git_commit_time_offset(commit); commitTime = QDateTime::fromMSecsSinceEpoch(time * 1000); commitTime.setOffsetFromUtc(timeOffset * 60); commitMessage = QString::fromUtf8(git_commit_message(commit)); parentCount = git_commit_parentcount(commit); if (parentCount == 0) { QGitCommitDiffParent item; res = git_diff_tree_to_tree(&diff, repo, nullptr, commit_tree, nullptr); if (res) { throw QGitError("git_diff_tree_to_tree", res); } size_t _count = git_diff_num_deltas(diff); for(size_t c = 0; c < _count; c++) { delta = git_diff_get_delta(diff, c); item.addFile(delta); } git_diff_free(diff); diff = nullptr; parents.append(item); } else { for(unsigned int c = 0; c < parentCount; c++) { res = git_commit_parent(&parent, commit, c); if (res) { throw QGitError("git_commit_parent", res); } char commit_id[41] = {0, }; const git_oid *oid = git_commit_id(parent); git_oid_tostr(commit_id, 41, oid); QGitCommitDiffParent item(commit_id); res = git_commit_tree(&parent_tree, parent); if (res) { throw QGitError("git_commit_tree", res); } res = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, nullptr); if (res) { throw QGitError("git_diff_tree_to_tree", res); } size_t _count = git_diff_num_deltas(diff); for(size_t c = 0; c < _count; c++) { delta = git_diff_get_delta(diff, c); item.addFile(delta); } git_diff_free(diff); diff = nullptr; git_tree_free(parent_tree); parent_tree = nullptr; git_commit_free(parent); parent = nullptr; parents.append(item); } } } catch(const QGitError &ex) { error = ex; } commitDiff = QGitCommit(commitId, parents, commitTime, commitAuthor, commitCommiter, commitMessage); emit commitDiffReply(commitId, commitDiff, error); if (diff) { git_diff_free(diff); diff = nullptr; } if (parent_tree) { git_tree_free(parent_tree); parent_tree = nullptr; } if (parent) { git_commit_free(parent); parent = nullptr; } if (commit_tree) { git_tree_free(commit_tree); commit_tree = nullptr; } if(commit) { git_commit_free(commit); commit = nullptr; } if(obj) { git_object_free(obj); obj = nullptr; } if (repo) { git_repository_free(repo); repo = nullptr; } }
void test_refs_revparse__nonexistant_object(void) { cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "this doesn't exist")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist^1")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist~2")); }
void QGit::commitDiffContent(QString first, QString second, QList<QString> files) { git_commit *first_commit = nullptr, *second_commit = nullptr; git_object *first_obj = nullptr, *second_obj = nullptr; git_tree *first_tree = nullptr, *second_tree = nullptr; const git_diff_delta *delta = nullptr; git_repository *repo = nullptr; git_patch *patch = nullptr; QList<QGitDiffFile> items; git_diff *diff = nullptr; QGitError error; int res = 0; try { res = git_repository_open(&repo, m_path.absolutePath().toUtf8().constData()); if (res) { throw QGitError("git_repository_open", res); } if (!first.isEmpty()) { res = git_revparse_single(&first_obj, repo, first.toLatin1()); if (res) { throw QGitError("git_revparse_single(first)", res); } res = git_commit_lookup(&first_commit, repo, git_object_id(first_obj)); if (res) { throw QGitError("git_commit_lookup(first)", res); } res = git_commit_tree(&first_tree, first_commit); if (res) { throw QGitError("git_commit_tree(first)", res); } } if (second == "staged") { git_diff_options options; res = git_revparse_single(&first_obj, repo, "HEAD^{tree}"); if (res) { throw QGitError("git_revparse_single(staged)", res); } res = git_tree_lookup(&first_tree, repo, git_object_id(first_obj)); if (res) { throw QGitError("git_tree_lookup(staged)", res); } memset(&options, 0, sizeof(options)); options.version = GIT_DIFF_OPTIONS_VERSION; options.flags = GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_SHOW_UNTRACKED_CONTENT; options.context_lines = 3; res = git_diff_tree_to_index(&diff, repo, first_tree, nullptr, &options); if (res) { throw QGitError("git_diff_tree_to_index(staged)", res); } } else if (second == "unstaged") { git_diff_options options; memset(&options, 0, sizeof(options)); options.version = GIT_DIFF_OPTIONS_VERSION; options.flags = GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_SHOW_UNTRACKED_CONTENT; options.context_lines = 3; res = git_diff_index_to_workdir(&diff, repo, nullptr, &options); if (res) { throw QGitError("git_diff_index_to_workdir", res); } } else { res = git_revparse_single(&second_obj, repo, second.toLatin1()); if (res) { throw QGitError("git_revparse_single(second)", res); } res = git_commit_lookup(&second_commit, repo, git_object_id(second_obj)); if (res) { throw QGitError("git_commit_lookup(second)", res); } res = git_commit_tree(&second_tree, second_commit); if (res) { throw QGitError("git_commit_tree(second)", res); } res = git_diff_tree_to_tree(&diff, repo, first_tree, second_tree, nullptr); if (res) { throw QGitError("git_diff_tree_to_tree", res); } } size_t _count = git_diff_num_deltas(diff); for(size_t c = 0; c < _count; c++) { delta = git_diff_get_delta(diff, c); QGitDiffFile item(delta); if (files.contains(item.new_file().path())) { res = git_patch_from_diff(&patch, diff, c); if (res) { throw QGitError("git_patch_from_diff", res); } size_t hunks = git_patch_num_hunks(patch); for(size_t hunk_cnt = 0; hunk_cnt < hunks; hunk_cnt++) { const git_diff_hunk *hunk = nullptr; res = git_patch_get_hunk(&hunk, nullptr, patch, hunk_cnt); if (res) { throw QGitError("git_patch_get_hunk", res); } QGitDiffHunk hunkObj(hunk); const git_diff_line *line = nullptr; int lines = git_patch_num_lines_in_hunk(patch, hunk_cnt); if (lines > 0) { for(int line_cnt = 0; line_cnt < lines; line_cnt++) { res = git_patch_get_line_in_hunk(&line, patch, hunk_cnt, static_cast<size_t>(line_cnt)); if (res) { throw QGitError("git_patch_get_line_in_hunk", res); } hunkObj.addLine(line); } } item.addHunk(hunkObj); } items.append(item); } } } catch(const QGitError &ex) { error = ex; } emit commitDiffContentReply(first, second, items, error); }
/* * $ git blame -n 359fc2d -- include/git2.h * orig line no final line no * commit orig path V author timestamp V * d12299fe src/git.h 1 (Vicent Martí 2010-12-03 22:22:10 +0200 1 * 359fc2d2 include/git2.h 2 (Edward Thomson 2013-01-08 17:07:25 -0600 2 * d12299fe src/git.h 5 (Vicent Martí 2010-12-03 22:22:10 +0200 3 * bb742ede include/git2.h 4 (Vicent Martí 2011-09-19 01:54:32 +0300 4 * bb742ede include/git2.h 5 (Vicent Martí 2011-09-19 01:54:32 +0300 5 * d12299fe src/git.h 24 (Vicent Martí 2010-12-03 22:22:10 +0200 6 * d12299fe src/git.h 25 (Vicent Martí 2010-12-03 22:22:10 +0200 7 * d12299fe src/git.h 26 (Vicent Martí 2010-12-03 22:22:10 +0200 8 * d12299fe src/git.h 27 (Vicent Martí 2010-12-03 22:22:10 +0200 9 * d12299fe src/git.h 28 (Vicent Martí 2010-12-03 22:22:10 +0200 10 * 96fab093 include/git2.h 11 (Sven Strickroth 2011-10-09 18:37:41 +0200 11 * 9d1dcca2 src/git2.h 33 (Vicent Martí 2011-02-07 10:35:58 +0200 12 * 44908fe7 src/git2.h 29 (Vicent Martí 2010-12-06 23:03:16 +0200 13 * a15c550d include/git2.h 14 (Vicent Martí 2011-11-16 14:09:44 +0100 14 * 44908fe7 src/git2.h 30 (Vicent Martí 2010-12-06 23:03:16 +0200 15 * d12299fe src/git.h 32 (Vicent Martí 2010-12-03 22:22:10 +0200 16 * 44908fe7 src/git2.h 33 (Vicent Martí 2010-12-06 23:03:16 +0200 17 * d12299fe src/git.h 34 (Vicent Martí 2010-12-03 22:22:10 +0200 18 * 44908fe7 src/git2.h 35 (Vicent Martí 2010-12-06 23:03:16 +0200 19 * 638c2ca4 src/git2.h 36 (Vicent Martí 2010-12-18 02:10:25 +0200 20 * 44908fe7 src/git2.h 36 (Vicent Martí 2010-12-06 23:03:16 +0200 21 * d12299fe src/git.h 37 (Vicent Martí 2010-12-03 22:22:10 +0200 22 * 44908fe7 src/git2.h 38 (Vicent Martí 2010-12-06 23:03:16 +0200 23 * 44908fe7 src/git2.h 39 (Vicent Martí 2010-12-06 23:03:16 +0200 24 * bf787bd8 include/git2.h 25 (Carlos Martín Nieto 2012-04-08 18:56:50 +0200 25 * 0984c876 include/git2.h 26 (Scott J. Goldman 2012-11-28 18:27:43 -0800 26 * 2f8a8ab2 src/git2.h 41 (Vicent Martí 2011-01-29 01:56:25 +0200 27 * 27df4275 include/git2.h 47 (Michael Schubert 2011-06-28 14:13:12 +0200 28 * a346992f include/git2.h 28 (Ben Straub 2012-05-10 09:47:14 -0700 29 * d12299fe src/git.h 40 (Vicent Martí 2010-12-03 22:22:10 +0200 30 * 44908fe7 src/git2.h 41 (Vicent Martí 2010-12-06 23:03:16 +0200 31 * 44908fe7 src/git2.h 42 (Vicent Martí 2010-12-06 23:03:16 +0200 32 * 44908fe7 src/git2.h 43 (Vicent Martí 2010-12-06 23:03:16 +0200 33 * 44908fe7 src/git2.h 44 (Vicent Martí 2010-12-06 23:03:16 +0200 34 * 44908fe7 src/git2.h 45 (Vicent Martí 2010-12-06 23:03:16 +0200 35 * 65b09b1d include/git2.h 33 (Russell Belfer 2012-02-02 18:03:43 -0800 36 * d12299fe src/git.h 46 (Vicent Martí 2010-12-03 22:22:10 +0200 37 * 44908fe7 src/git2.h 47 (Vicent Martí 2010-12-06 23:03:16 +0200 38 * 5d4cd003 include/git2.h 55 (Carlos Martín Nieto 2011-03-28 17:02:45 +0200 39 * 41fb1ca0 include/git2.h 39 (Philip Kelley 2012-10-29 13:41:14 -0400 40 * 2dc31040 include/git2.h 56 (Carlos Martín Nieto 2011-06-20 18:58:57 +0200 41 * 764df57e include/git2.h 40 (Ben Straub 2012-06-15 13:14:43 -0700 42 * 5280f4e6 include/git2.h 41 (Ben Straub 2012-07-31 19:39:06 -0700 43 * 613d5eb9 include/git2.h 43 (Philip Kelley 2012-11-28 11:42:37 -0500 44 * d12299fe src/git.h 48 (Vicent Martí 2010-12-03 22:22:10 +0200 45 * 111ee3fe include/git2.h 41 (Vicent Martí 2012-07-11 14:37:26 +0200 46 * f004c4a8 include/git2.h 44 (Russell Belfer 2012-08-21 17:26:39 -0700 47 * 111ee3fe include/git2.h 42 (Vicent Martí 2012-07-11 14:37:26 +0200 48 * 9c82357b include/git2.h 58 (Carlos Martín Nieto 2011-06-17 18:13:14 +0200 49 * d6258deb include/git2.h 61 (Carlos Martín Nieto 2011-06-25 15:10:09 +0200 50 * b311e313 include/git2.h 63 (Julien Miotte 2011-07-27 18:31:13 +0200 51 * 3412391d include/git2.h 63 (Carlos Martín Nieto 2011-07-07 11:47:31 +0200 52 * bfc9ca59 include/git2.h 43 (Russell Belfer 2012-03-28 16:45:36 -0700 53 * bf477ed4 include/git2.h 44 (Michael Schubert 2012-02-15 00:33:38 +0100 54 * edebceff include/git2.h 46 (nulltoken 2012-05-01 13:57:45 +0200 55 * 743a4b3b include/git2.h 48 (nulltoken 2012-06-15 22:24:59 +0200 56 * 0a32dca5 include/git2.h 54 (Michael Schubert 2012-08-19 22:26:32 +0200 57 * 590fb68b include/git2.h 55 (nulltoken 2012-10-04 13:47:45 +0200 58 * bf477ed4 include/git2.h 45 (Michael Schubert 2012-02-15 00:33:38 +0100 59 * d12299fe src/git.h 49 (Vicent Martí 2010-12-03 22:22:10 +0200 60 */ void test_blame_simple__trivial_libgit2(void) { git_blame_options opts = GIT_BLAME_OPTIONS_INIT; git_object *obj; /* If we can't open the libgit2 repo or if it isn't a full repo * with proper history, just skip this test */ if (git_repository_open(&g_repo, cl_fixture("../..")) < 0) cl_skip(); if (git_repository_is_shallow(g_repo)) cl_skip(); if (git_revparse_single(&obj, g_repo, "359fc2d") < 0) cl_skip(); git_oid_cpy(&opts.newest_commit, git_object_id(obj)); git_object_free(obj); cl_git_pass(git_blame_file(&g_blame, g_repo, "include/git2.h", &opts)); check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "359fc2d2", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 2, 3, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 3, 4, 2, 0, "bb742ede", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 4, 6, 5, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 5, 11, 1, 0, "96fab093", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 6, 12, 1, 0, "9d1dcca2", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 7, 13, 1, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 8, 14, 1, 0, "a15c550d", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 9, 15, 1, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 10, 16, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 11, 17, 1, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 12, 18, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 13, 19, 1, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 14, 20, 1, 0, "638c2ca4", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 15, 21, 1, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 16, 22, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 17, 23, 2, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 18, 25, 1, 0, "bf787bd8", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 19, 26, 1, 0, "0984c876", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 20, 27, 1, 0, "2f8a8ab2", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 21, 28, 1, 0, "27df4275", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 22, 29, 1, 0, "a346992f", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 23, 30, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 24, 31, 5, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 25, 36, 1, 0, "65b09b1d", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 26, 37, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 27, 38, 1, 0, "44908fe7", "src/git2.h"); check_blame_hunk_index(g_repo, g_blame, 28, 39, 1, 0, "5d4cd003", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 29, 40, 1, 0, "41fb1ca0", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 30, 41, 1, 0, "2dc31040", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 31, 42, 1, 0, "764df57e", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 32, 43, 1, 0, "5280f4e6", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 33, 44, 1, 0, "613d5eb9", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 34, 45, 1, 0, "d12299fe", "src/git.h"); check_blame_hunk_index(g_repo, g_blame, 35, 46, 1, 0, "111ee3fe", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 36, 47, 1, 0, "f004c4a8", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 37, 48, 1, 0, "111ee3fe", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 38, 49, 1, 0, "9c82357b", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 39, 50, 1, 0, "d6258deb", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 40, 51, 1, 0, "b311e313", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 41, 52, 1, 0, "3412391d", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 42, 53, 1, 0, "bfc9ca59", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 43, 54, 1, 0, "bf477ed4", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 44, 55, 1, 0, "edebceff", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 45, 56, 1, 0, "743a4b3b", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 46, 57, 1, 0, "0a32dca5", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 47, 58, 1, 0, "590fb68b", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 48, 59, 1, 0, "bf477ed4", "include/git2.h"); check_blame_hunk_index(g_repo, g_blame, 49, 60, 1, 0, "d12299fe", "src/git.h"); }
QStringList KateProjectWorker::filesFromGit(const QDir &dir, bool recursive) { // init libgit2, we require at least 0.22 which has this function! // do this here to have init in this thread done, shutdown afterwards again! git_libgit2_init(); QStringList files; git_repository *repo = nullptr; git_object *root_tree = nullptr, *tree = nullptr; // check if the repo can be opened. // git_repository_open_ext() will return 0 if everything is OK; // if not, return an empty files list const QByteArray repoPathUtf8 = dir.path().toUtf8(); if (git_repository_open_ext(&repo, repoPathUtf8.constData(), 0, NULL)) { git_libgit2_shutdown(); return files; } // get the working directory of the repo // if none was found, return an empty files list const char *working_dir = nullptr; if ((working_dir = git_repository_workdir(repo)) == nullptr) { git_repository_free(repo); git_libgit2_shutdown(); return files; } if (git_revparse_single(&root_tree, repo, "HEAD^{tree}")) { git_repository_free(repo); git_libgit2_shutdown(); return files; } QDir workdir; workdir.setPath(QString::fromUtf8(working_dir)); const QByteArray relpathUtf8 = workdir.relativeFilePath(dir.path()).toUtf8(); if (relpathUtf8.isEmpty() || relpathUtf8 == ".") { // git_object_lookup_bypath is not able to resolv "." as path tree = root_tree; } else { if (git_object_lookup_bypath(&tree, root_tree, relpathUtf8.constData(), GIT_OBJ_TREE)) { git_object_free(root_tree); git_repository_free(repo); git_libgit2_shutdown(); return files; } } QString path = workdir.absolutePath() + QDir::separator(); files.append(gitSearchTree(tree, path, recursive)); if (recursive && relpathUtf8.isEmpty()) { files.append(gitSearchSubmodules(repo, path)); } files.append(gitSearchStatusList(repo, path)); if (tree != root_tree) { git_object_free(tree); } git_object_free(root_tree); git_repository_free(repo); git_libgit2_shutdown(); return files; }
static int handle(KDriver const *driver, KImplementation const *impl, int requested) { char const *url = k_dictionary_get(k_implementation_get_values(impl), "href"); char const *tag = k_dictionary_get(k_implementation_get_values(impl), "tag"); char const *path = k_dictionary_get(k_implementation_get_meta(impl), "name"); git_repository* repo = NULL; git_remote* origin = NULL; if (git_repository_open(&repo, path) == GIT_OK) { if (git_remote_load(&origin, repo, "origin") != GIT_OK) { git_repository_free(repo); return -1; } if (strcmp(url, git_remote_url(origin)) != 0) { giterr_set_str(GITERR_INVALID, "different origin"); git_repository_free(repo); return -1; } } else { if (git_repository_init(&repo, path, 0) != GIT_OK) { return -1; } if (git_remote_create(&origin, repo, "origin", url) != GIT_OK) { git_repository_free(repo); return -1; } } git_remote_set_update_fetchhead(origin, 0); git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); if (git_remote_connect(origin, GIT_DIRECTION_FETCH) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } if (git_remote_download(origin, transfer_progress, NULL) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } if (git_remote_update_tips(origin) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } git_object* object = NULL; if (git_revparse_single(&object, repo, tag) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } if (git_repository_set_head_detached(repo, git_object_id(object)) != GIT_OK) { git_object_free(object); git_remote_free(origin); git_repository_free(repo); return -1; } git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; checkout_opts.version = GIT_CHECKOUT_OPTS_VERSION; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; checkout_opts.progress_cb = checkout_progress; checkout_opts.progress_payload = NULL; if (git_checkout_tree(repo, object, &checkout_opts) != GIT_OK) { git_object_free(object); git_remote_free(origin); git_repository_free(repo); return -1; } git_object_free(object); git_remote_free(origin); git_repository_free(repo); return 0; }
/** Entry point for this command */ int main(int argc, char *argv[]) { git_repository *repo; struct opts o = { ".", NULL, 0, 0 }; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; git_libgit2_init(); parse_opts(&o, argc, argv); check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL), "Could not open repository", NULL); check_lg2(git_revparse_single(&obj, repo, o.rev), "Could not resolve", o.rev); if (o.verbose) { char oidstr[GIT_OID_HEXSZ + 1]; git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); printf("%s %s\n--\n", git_object_type2string(git_object_type(obj)), oidstr); } switch (o.action) { case SHOW_TYPE: printf("%s\n", git_object_type2string(git_object_type(obj))); break; case SHOW_SIZE: { git_odb *odb; git_odb_object *odbobj; check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL); check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)), "Could not find obj", NULL); printf("%ld\n", (long)git_odb_object_size(odbobj)); git_odb_object_free(odbobj); git_odb_free(odb); } break; case SHOW_NONE: /* just want return result */ break; case SHOW_PRETTY: switch (git_object_type(obj)) { case GIT_OBJ_BLOB: show_blob((const git_blob *)obj); break; case GIT_OBJ_COMMIT: show_commit((const git_commit *)obj); break; case GIT_OBJ_TREE: show_tree((const git_tree *)obj); break; case GIT_OBJ_TAG: show_tag((const git_tag *)obj); break; default: printf("unknown %s\n", oidstr); break; } break; } git_object_free(obj); git_repository_free(repo); git_libgit2_shutdown(); return 0; }
bool CatCommand::Execute() { if (!GitAdminDir::IsWorkingTreeOrBareRepo(g_Git.m_CurrentDir)) { CMessageBox::Show(GetExplorerHWND(), IDS_NOGITREPO, IDS_APPNAME, MB_ICONERROR); return false; } CString savepath = CPathUtils::GetLongPathname(parser.GetVal(L"savepath")); CString revision = parser.GetVal(L"revision"); if (g_Git.UsingLibGit2(CGit::GIT_CMD_GETONEFILE)) { CAutoRepository repo(g_Git.GetGitRepository()); if (!repo) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), g_Git.GetLibGit2LastErr(L"Could not open repository."), L"TortoiseGit", MB_ICONERROR); return false; } CAutoObject obj; if (git_revparse_single(obj.GetPointer(), repo, CUnicodeUtils::GetUTF8(revision))) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), g_Git.GetLibGit2LastErr(L"Could not parse revision."), L"TortoiseGit", MB_ICONERROR); return false; } if (git_object_type(obj) == GIT_OBJECT_BLOB) { CAutoFILE file = _wfsopen(savepath, L"wb", SH_DENYRW); if (file == nullptr) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), L"Could not open file for writing.", L"TortoiseGit", MB_ICONERROR); return false; } CAutoBuf buf; if (git_blob_filtered_content(buf, reinterpret_cast<git_blob*>(static_cast<git_object*>(obj)), CUnicodeUtils::GetUTF8(cmdLinePath.GetGitPathString()), 0)) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), g_Git.GetLibGit2LastErr(L"Could not get filtered content."), L"TortoiseGit", MB_ICONERROR); return false; } if (fwrite(buf->ptr, sizeof(char), buf->size, file) != buf->size) { ::DeleteFile(savepath); CString err = CFormatMessageWrapper(); CMessageBox::Show(GetExplorerHWND(), L"Could not write to file: " + err, L"TortoiseGit", MB_ICONERROR); return false; } return true; } if (g_Git.GetOneFile(revision, cmdLinePath, savepath)) { MessageBox(GetExplorerHWND(), g_Git.GetGitLastErr(L"Could get file.", CGit::GIT_CMD_GETONEFILE), L"TortoiseGit", MB_ICONERROR); return false; } return true; } CString cmd, output, err; cmd.Format(L"git.exe cat-file -t %s", static_cast<LPCTSTR>(revision)); if (g_Git.Run(cmd, &output, &err, CP_UTF8)) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), output + L'\n' + err, L"TortoiseGit", MB_ICONERROR); return false; } if (CStringUtils::StartsWith(output, L"blob")) cmd.Format(L"git.exe cat-file -p %s", static_cast<LPCTSTR>(revision)); else cmd.Format(L"git.exe show %s -- \"%s\"", static_cast<LPCTSTR>(revision), static_cast<LPCTSTR>(this->cmdLinePath.GetWinPathString())); if (g_Git.RunLogFile(cmd, savepath, &err)) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), L"Cat file failed:\n" + err, L"TortoiseGit", MB_ICONERROR); return false; } return true; }
/** * Create tag targeting HEAD commit in repository. * * @param repo S4 class git_repository * @param name Name for the tag. * @param message The tag message. * @param tagger The tagger (author) of the tag * @return S4 object of class git_tag */ SEXP git2r_tag_create(SEXP repo, SEXP name, SEXP message, SEXP tagger) { SEXP result = R_NilValue; int err; git_oid oid; git_repository *repository = NULL; git_signature *sig_tagger = NULL; git_tag *tag = NULL; git_object *target = NULL; if (git2r_arg_check_string(name)) git2r_error(__func__, NULL, "'name'", git2r_err_string_arg); if (git2r_arg_check_string(message)) git2r_error(__func__, NULL, "'message'", git2r_err_string_arg); if (git2r_arg_check_signature(tagger)) git2r_error(__func__, NULL, "'tagger'", git2r_err_signature_arg); repository = git2r_repository_open(repo); if (!repository) git2r_error(__func__, NULL, git2r_err_invalid_repository, NULL); err = git2r_signature_from_arg(&sig_tagger, tagger); if (err) goto cleanup; err = git_revparse_single(&target, repository, "HEAD^{commit}"); if (err) goto cleanup; err = git_tag_create( &oid, repository, CHAR(STRING_ELT(name, 0)), target, sig_tagger, CHAR(STRING_ELT(message, 0)), 0); if (err) goto cleanup; err = git_tag_lookup(&tag, repository, &oid); if (err) goto cleanup; PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_tag"))); git2r_tag_init(tag, repo, result); cleanup: if (tag) git_tag_free(tag); if (sig_tagger) git_signature_free(sig_tagger); if (target) git_object_free(target); if (repository) git_repository_free(repository); if (R_NilValue != result) UNPROTECT(1); if (err) git2r_error(__func__, giterr_last(), NULL, NULL); return result; }
void test_refs_revparse__a_too_short_objectid_returns_EAMBIGUOUS(void) { cl_assert_equal_i( GIT_EAMBIGUOUS, git_revparse_single(&g_obj, g_repo, "e90")); }
bool ResetProgressCommand::Run(CGitProgressList* list, CString& sWindowTitle, int& /*m_itemCountTotal*/, int& /*m_itemCount*/) { if (!g_Git.UsingLibGit2(CGit::GIT_CMD_RESET)) { // should never run to here ASSERT(0); return false; } list->SetWindowTitle(IDS_PROGRS_TITLE_RESET, g_Git.m_CurrentDir, sWindowTitle); list->SetBackgroundImage(IDI_UPDATE_BKG); int resetTypesResource[] = { IDS_RESET_SOFT, IDS_RESET_MIXED, IDS_RESET_HARD }; list->ReportCmd(CString(MAKEINTRESOURCE(IDS_PROGRS_TITLE_RESET)) + L' ' + CString(MAKEINTRESOURCE(resetTypesResource[m_resetType])) + L' ' + m_revision); list->ShowProgressBar(); CAutoRepository repo(g_Git.GetGitRepository()); if (!repo) { list->ReportGitError(); return false; } CGitProgressList::Payload cbpayload = { list, repo }; git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT; CBlockCacheForPath block(g_Git.m_CurrentDir); CAutoObject target; if (git_revparse_single(target.GetPointer(), repo, CUnicodeUtils::GetUTF8(m_revision))) goto error; checkout_options.progress_payload = &cbpayload; checkout_options.progress_cb = [](const char*, size_t completed_steps, size_t total_steps, void* payload) { CGitProgressList::Payload* cbpayload = (CGitProgressList::Payload*)payload; cbpayload->list->m_itemCountTotal = (int)total_steps; cbpayload->list->m_itemCount = (int)completed_steps; }; checkout_options.notify_cb = [](git_checkout_notify_t, const char* pPath, const git_diff_file*, const git_diff_file*, const git_diff_file*, void* payload) -> int { CGitProgressList* list = (CGitProgressList*)payload; CString path(CUnicodeUtils::GetUnicode(pPath)); if (DWORD(CRegDWORD(L"Software\\TortoiseGit\\RevertWithRecycleBin", TRUE))) { if (!CTGitPath(g_Git.CombinePath(path)).Delete(true, true)) { list->ReportError(L"Could move \"" + path + L"\" to recycle bin"); return GIT_EUSER; } } list->AddNotify(new CGitProgressList::WC_File_NotificationData(path, CGitProgressList::WC_File_NotificationData::git_wc_notify_checkout)); return 0; }; checkout_options.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED; checkout_options.notify_payload = list; if (git_reset(repo, target, (git_reset_t)(m_resetType + 1), &checkout_options)) goto error; // Not setting m_PostCmdCallback here, as clone is only called from AppUtils.cpp return true; error: list->ReportGitError(); return false; }
//-----------------------------------------------------------------------------------------// int AdVersion::getGitVersion(QString gitDirectory, QString &version) { int ierr; QFile dir(gitDirectory+"/.git"); QByteArray tempData; git_repository *repo; git_describe_result *description; git_describe_options options; git_describe_format_options format; git_object *headObject; git_buf buffer = { 0 }; version = QString(); if(!dir.exists()) return -1; tempData = gitDirectory.toLatin1(); const char *cgitDirectory = tempData.data(); git_libgit2_init(); ierr = git_repository_open(&repo,cgitDirectory); if(ierr<0) { git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_init_options(&options,GIT_DESCRIBE_OPTIONS_VERSION); options.show_commit_oid_as_fallback = 1; if(ierr<0) { git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_init_format_options(&format,GIT_DESCRIBE_FORMAT_OPTIONS_VERSION); if(ierr<0) { git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_revparse_single(&headObject,repo,"HEAD"); if(ierr<0) { git_object_free(headObject); git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_commit(&description,headObject,&options); if(ierr<0) { git_object_free(headObject); git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_format(&buffer,description,&format); if(ierr<0) { git_object_free(headObject); git_describe_result_free(description); git_repository_free(repo); git_libgit2_shutdown(); return ierr; } version.sprintf("%s",buffer.ptr); git_object_free(headObject); git_describe_result_free(description); git_repository_free(repo); git_libgit2_shutdown(); return ERROR_NOERROR; }