/* * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt * 100644 1f85ca5 1 conflicts-one.txt * 100644 6aea5f2 2 conflicts-one.txt * 100644 516bd85 3 conflicts-one.txt * * $ git reset 9a05ccb -- conflicts-one.txt * Unstaged changes after reset: * ... * * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt * 100644 1f85ca5 0 conflicts-one.txt * */ void test_reset_default__resetting_filepaths_clears_previous_conflicts(void) { git_index_entry *conflict_entry[3]; git_strarray after; char *paths[] = { "conflicts-one.txt" }; char *after_shas[] = { "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" }; test_reset_default__cleanup(); initialize("mergedrepo"); _pathspecs.strings = paths; _pathspecs.count = 1; after.strings = after_shas; after.count = 1; cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt")); cl_git_pass(git_revparse_single(&_target, _repo, "9a05ccb")); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, true, &after); cl_assert_equal_i(GIT_ENOTFOUND, git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt")); }
void test_reset_default__resetting_filepaths_against_a_null_target_removes_them_from_the_index(void) { char *paths[] = { "staged_changes", "staged_new_file" }; _pathspecs.strings = paths; _pathspecs.count = 2; assert_content_in_index(&_pathspecs, true, NULL); cl_git_pass(git_reset_default(_repo, NULL, &_pathspecs)); assert_content_in_index(&_pathspecs, false, NULL); }
int cmd_reset(git_repository *repo, int argc, char **argv) { int err = GIT_OK; int rc = EXIT_FAILURE; const char *spec; git_object *treeobj = NULL; int mode = -1; int spec_pos; if (argc < 3) { fprintf (stderr, "USAGE: %s <treeish> <paths>\n", argv[0]); fprintf (stderr, "USAGE: %s [--soft|--mixed|--hard] <commit>\n", argv[0]); return -1; } if (!strcmp("--soft", argv[1])) mode = GIT_RESET_SOFT; else if (!strcmp("--mixed", argv[1])) mode = GIT_RESET_MIXED; else if (!strcmp("--hard", argv[1])) mode = GIT_RESET_HARD; if (mode != -1) spec_pos = 2; else spec_pos = 1; spec = argv[spec_pos]; if ((err = git_revparse_single(&treeobj, repo, spec))) goto out; if (mode == -1) { git_strarray strarray; strarray.count = argc-1-spec_pos; strarray.strings = argv+1+spec_pos; if ((err = git_reset_default(repo, treeobj, &strarray))) goto out; } else { if ((err = git_reset(repo, treeobj, mode, NULL, NULL, NULL))) goto out; } out: if (treeobj) git_object_free(treeobj); if (err != GIT_OK) libgit_error(); return rc; }
/* $ git reset HEAD -- "I_am_not_there.txt" "me_neither.txt" Unstaged changes after reset: ... */ void test_reset_default__resetting_unknown_filepaths_does_not_fail(void) { char *paths[] = { "I_am_not_there.txt", "me_neither.txt" }; _pathspecs.strings = paths; _pathspecs.count = 2; assert_content_in_index(&_pathspecs, false, NULL); cl_git_pass(git_revparse_single(&_target, _repo, "HEAD")); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, false, NULL); }
/** * Updates some entries in the index from the HEAD commit tree. * * @param repo S3 class git_repository * @param path The paths to reset * @return R_NilValue */ SEXP git2r_reset_default(SEXP repo, SEXP path) { int error = 0; git_strarray pathspec = {0}; git_reference *head = NULL; git_object *head_commit = NULL; git_repository *repository = NULL; if (git2r_arg_check_string_vec(path)) git2r_error(__func__, NULL, "'path'", git2r_err_string_vec_arg); repository = git2r_repository_open(repo); if (!repository) git2r_error(__func__, NULL, git2r_err_invalid_repository, NULL); error = git2r_copy_string_vec(&pathspec, path); if (error || !pathspec.count) goto cleanup; error = git_repository_head(&head, repository); if (error) goto cleanup; error = git_reference_peel(&head_commit, head, GIT2R_OBJECT_COMMIT); if (error) goto cleanup; error = git_reset_default(repository, head_commit, &pathspec); cleanup: git_reference_free(head); git_object_free(head_commit); free(pathspec.strings); git_repository_free(repository); if (error) git2r_error(__func__, GIT2R_ERROR_LAST(), NULL, NULL); return R_NilValue; }
/* * $ 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::unstageFiles(QStringList items) { QVector<QByteArray> tmpStrList; git_repository *repo = nullptr; git_reference *head = nullptr; git_object *head_commit = nullptr; git_strarray paths = {nullptr, 0}; int res = 0; QGitError error; try { if (items.count() == 0) { throw QGitError(); } res = git_repository_open(&repo, m_path.absolutePath().toUtf8().constData()); if (res) { throw QGitError("git_repository_open", res); } paths.count = static_cast<size_t>(items.count()); paths.strings = static_cast<char **>(malloc(sizeof(char *) * static_cast<size_t>(items.count()))); if (paths.strings == nullptr) { throw QGitError("malloc", 0); } tmpStrList.reserve(items.count()); for(int c = 0; c < items.count(); c++) { tmpStrList.append(items.at(c).toUtf8()); paths.strings[c] = const_cast<char *>(tmpStrList.at(c).data()); } res = git_repository_head(&head, repo); if (res == 0) { res = git_reference_peel(&head_commit, head, GIT_OBJ_COMMIT); if (res) { throw QGitError("git_reference_peel", res); } } res = git_reset_default(repo, head_commit, &paths); if (res) { throw QGitError("git_reset_default", res); } } catch(const QGitError &ex) { error = ex; } emit unstageFilesReply(error); if (paths.strings) { free(paths.strings); paths.strings = nullptr; } paths.count = 0; if (head_commit) { git_object_free(head_commit); head_commit = nullptr; } if (head) { git_reference_free(head); head = nullptr; } if (repo) { git_repository_free(repo); repo = nullptr; } }