diff options
Diffstat (limited to 'tests-clar/reset')
-rw-r--r-- | tests-clar/reset/default.c | 180 | ||||
-rw-r--r-- | tests-clar/reset/hard.c | 200 | ||||
-rw-r--r-- | tests-clar/reset/mixed.c | 49 | ||||
-rw-r--r-- | tests-clar/reset/reset_helpers.c | 10 | ||||
-rw-r--r-- | tests-clar/reset/reset_helpers.h | 6 | ||||
-rw-r--r-- | tests-clar/reset/soft.c | 157 |
6 files changed, 602 insertions, 0 deletions
diff --git a/tests-clar/reset/default.c b/tests-clar/reset/default.c new file mode 100644 index 000000000..506d971ff --- /dev/null +++ b/tests-clar/reset/default.c @@ -0,0 +1,180 @@ +#include "clar_libgit2.h" +#include "posix.h" +#include "reset_helpers.h" +#include "path.h" + +static git_repository *_repo; +static git_object *_target; +static git_strarray _pathspecs; +static git_index *_index; + +static void initialize(const char *repo_name) +{ + _repo = cl_git_sandbox_init(repo_name); + cl_git_pass(git_repository_index(&_index, _repo)); + + _target = NULL; + + _pathspecs.strings = NULL; + _pathspecs.count = 0; +} + +void test_reset_default__initialize(void) +{ + initialize("status"); +} + +void test_reset_default__cleanup(void) +{ + git_object_free(_target); + _target = NULL; + + git_index_free(_index); + _index = NULL; + + cl_git_sandbox_cleanup(); +} + +static void assert_content_in_index( + git_strarray *pathspecs, + bool should_exist, + git_strarray *expected_shas) +{ + size_t i, pos; + int error; + + for (i = 0; i < pathspecs->count; i++) { + error = git_index_find(&pos, _index, pathspecs->strings[i]); + + if (should_exist) { + const git_index_entry *entry; + + cl_assert(error != GIT_ENOTFOUND); + + entry = git_index_get_byindex(_index, pos); + cl_assert(entry != NULL); + + if (!expected_shas) + continue; + + cl_git_pass(git_oid_streq(&entry->oid, expected_shas->strings[i])); + } else + cl_assert_equal_i(should_exist, error != GIT_ENOTFOUND); + } +} + +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); +} + +/* + * $ 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); +} + +/* + * $ 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")); +} + +/* +$ 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); +} diff --git a/tests-clar/reset/hard.c b/tests-clar/reset/hard.c new file mode 100644 index 000000000..62371f83f --- /dev/null +++ b/tests-clar/reset/hard.c @@ -0,0 +1,200 @@ +#include "clar_libgit2.h" +#include "posix.h" +#include "reset_helpers.h" +#include "path.h" +#include "fileops.h" + +static git_repository *repo; +static git_object *target; + +void test_reset_hard__initialize(void) +{ + repo = cl_git_sandbox_init("status"); + target = NULL; +} + +void test_reset_hard__cleanup(void) +{ + if (target != NULL) { + git_object_free(target); + target = NULL; + } + + cl_git_sandbox_cleanup(); +} + +static int strequal_ignore_eol(const char *exp, const char *str) +{ + while (*exp && *str) { + if (*exp != *str) { + while (*exp == '\r' || *exp == '\n') ++exp; + while (*str == '\r' || *str == '\n') ++str; + if (*exp != *str) + return false; + } else { + exp++; str++; + } + } + return (!*exp && !*str); +} + +void test_reset_hard__resetting_reverts_modified_files(void) +{ + git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT; + int i; + static const char *files[4] = { + "current_file", + "modified_file", + "staged_new_file", + "staged_changes_modified_file" }; + static const char *before[4] = { + "current_file\n", + "modified_file\nmodified_file\n", + "staged_new_file\n", + "staged_changes_modified_file\nstaged_changes_modified_file\nstaged_changes_modified_file\n" + }; + static const char *after[4] = { + "current_file\n", + "modified_file\n", + NULL, + "staged_changes_modified_file\n" + }; + const char *wd = git_repository_workdir(repo); + + cl_assert(wd); + + for (i = 0; i < 4; ++i) { + cl_git_pass(git_buf_joinpath(&path, wd, files[i])); + cl_git_pass(git_futils_readbuffer(&content, path.ptr)); + cl_assert_equal_s(before[i], content.ptr); + } + + retrieve_target_from_oid( + &target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); + + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + + for (i = 0; i < 4; ++i) { + cl_git_pass(git_buf_joinpath(&path, wd, files[i])); + if (after[i]) { + cl_git_pass(git_futils_readbuffer(&content, path.ptr)); + cl_assert(strequal_ignore_eol(after[i], content.ptr)); + } else { + cl_assert(!git_path_exists(path.ptr)); + } + } + + git_buf_free(&content); + git_buf_free(&path); +} + +void test_reset_hard__cannot_reset_in_a_bare_repository(void) +{ + git_repository *bare; + + cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); + cl_assert(git_repository_is_bare(bare) == true); + + retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); + + cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD)); + + git_repository_free(bare); +} + +static void index_entry_init(git_index *index, int side, git_oid *oid) +{ + git_index_entry entry; + + memset(&entry, 0x0, sizeof(git_index_entry)); + + entry.path = "conflicting_file"; + entry.flags = (side << GIT_IDXENTRY_STAGESHIFT); + entry.mode = 0100644; + git_oid_cpy(&entry.oid, oid); + + cl_git_pass(git_index_add(index, &entry)); +} + +static void unmerged_index_init(git_index *index, int entries) +{ + int write_ancestor = 1; + int write_ours = 2; + int write_theirs = 4; + git_oid ancestor, ours, theirs; + + git_oid_fromstr(&ancestor, "6bb0d9f700543ba3d318ba7075fc3bd696b4287b"); + git_oid_fromstr(&ours, "b19a1e93bec1317dc6097229e12afaffbfa74dc2"); + git_oid_fromstr(&theirs, "950b81b7eee953d050aa05a641f8e056c85dd1bd"); + + cl_git_rewritefile("status/conflicting_file", "conflicting file\n"); + + if (entries & write_ancestor) + index_entry_init(index, 1, &ancestor); + + if (entries & write_ours) + index_entry_init(index, 2, &ours); + + if (entries & write_theirs) + index_entry_init(index, 3, &theirs); +} + +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)); + + retrieve_target_from_oid(&target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + + cl_assert(git_path_exists("status/conflicting_file") == 0); + + git_object_free(target); + target = NULL; + + git_index_free(index); + } +} + +void test_reset_hard__cleans_up_merge(void) +{ + git_buf merge_head_path = GIT_BUF_INIT, + merge_msg_path = GIT_BUF_INIT, + merge_mode_path = GIT_BUF_INIT, + orig_head_path = GIT_BUF_INIT; + + cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); + cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); + + cl_git_pass(git_buf_joinpath(&merge_msg_path, git_repository_path(repo), "MERGE_MSG")); + cl_git_mkfile(git_buf_cstr(&merge_msg_path), "Merge commit 0017bd4ab1ec30440b17bae1680cff124ab5f1f6\n"); + + cl_git_pass(git_buf_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE")); + cl_git_mkfile(git_buf_cstr(&merge_mode_path), ""); + + cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD")); + cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); + + retrieve_target_from_oid(&target, repo, "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + + cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path))); + cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path))); + cl_assert(!git_path_exists(git_buf_cstr(&merge_mode_path))); + + cl_assert(git_path_exists(git_buf_cstr(&orig_head_path))); + cl_git_pass(p_unlink(git_buf_cstr(&orig_head_path))); + + git_buf_free(&merge_head_path); + git_buf_free(&merge_msg_path); + git_buf_free(&merge_mode_path); + git_buf_free(&orig_head_path); +} diff --git a/tests-clar/reset/mixed.c b/tests-clar/reset/mixed.c new file mode 100644 index 000000000..7b90c23f1 --- /dev/null +++ b/tests-clar/reset/mixed.c @@ -0,0 +1,49 @@ +#include "clar_libgit2.h" +#include "posix.h" +#include "reset_helpers.h" +#include "path.h" + +static git_repository *repo; +static git_object *target; + +void test_reset_mixed__initialize(void) +{ + repo = cl_git_sandbox_init("attr"); + target = NULL; +} + +void test_reset_mixed__cleanup(void) +{ + git_object_free(target); + target = NULL; + + cl_git_sandbox_cleanup(); +} + +void test_reset_mixed__cannot_reset_in_a_bare_repository(void) +{ + git_repository *bare; + + cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); + cl_assert(git_repository_is_bare(bare) == true); + + retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); + + cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED)); + + git_repository_free(bare); +} + +void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void) +{ + unsigned int status; + + cl_git_pass(git_status_file(&status, repo, "macro_bad")); + cl_assert(status == GIT_STATUS_CURRENT); + retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7"); + + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED)); + + cl_git_pass(git_status_file(&status, repo, "macro_bad")); + cl_assert(status == GIT_STATUS_WT_NEW); +} diff --git a/tests-clar/reset/reset_helpers.c b/tests-clar/reset/reset_helpers.c new file mode 100644 index 000000000..17edca4e9 --- /dev/null +++ b/tests-clar/reset/reset_helpers.c @@ -0,0 +1,10 @@ +#include "clar_libgit2.h" +#include "reset_helpers.h" + +void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha) +{ + git_oid oid; + + cl_git_pass(git_oid_fromstr(&oid, sha)); + cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY)); +} diff --git a/tests-clar/reset/reset_helpers.h b/tests-clar/reset/reset_helpers.h new file mode 100644 index 000000000..5dbe9d2c7 --- /dev/null +++ b/tests-clar/reset/reset_helpers.h @@ -0,0 +1,6 @@ +#include "common.h" + +#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d" +#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0" + +extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha); diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c new file mode 100644 index 000000000..884697c91 --- /dev/null +++ b/tests-clar/reset/soft.c @@ -0,0 +1,157 @@ +#include "clar_libgit2.h" +#include "posix.h" +#include "reset_helpers.h" +#include "path.h" +#include "repo/repo_helpers.h" + +static git_repository *repo; +static git_object *target; + +void test_reset_soft__initialize(void) +{ + repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_reset_soft__cleanup(void) +{ + git_object_free(target); + target = NULL; + + cl_git_sandbox_cleanup(); +} + +static void assert_reset_soft(bool should_be_detached) +{ + git_oid oid; + + cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); + cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); + + retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + + cl_assert(git_repository_head_detached(repo) == should_be_detached); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_assert(git_repository_head_detached(repo) == should_be_detached); + + cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); + cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); +} + +void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void) +{ + assert_reset_soft(false); +} + +void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) +{ + git_repository_detach_head(repo); + + assert_reset_soft(true); +} + +void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) +{ + git_oid oid; + char raw_head_oid[GIT_OID_HEXSZ + 1]; + + cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); + git_oid_fmt(raw_head_oid, &oid); + raw_head_oid[GIT_OID_HEXSZ] = '\0'; + + retrieve_target_from_oid(&target, repo, raw_head_oid); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); + cl_git_pass(git_oid_streq(&oid, raw_head_oid)); +} + +void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void) +{ + git_oid oid; + + /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ + retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_assert(git_repository_head_detached(repo) == false); + cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); + cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); +} + +void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) +{ + /* 53fc32d is the tree of commit e90810b */ + retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016"); + + cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); + git_object_free(target); + + /* 521d87c is an annotated tag pointing to a blob */ + retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"); + cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); +} + +void test_reset_soft__resetting_against_an_orphaned_head_repo_makes_the_head_no_longer_orphaned(void) +{ + git_reference *head; + + retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + + make_head_orphaned(repo, NON_EXISTING_HEAD); + + cl_assert_equal_i(true, git_repository_head_orphan(repo)); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_assert_equal_i(false, git_repository_head_orphan(repo)); + + cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD)); + cl_assert_equal_i(0, git_oid_streq(git_reference_target(head), KNOWN_COMMIT_IN_BARE_REPO)); + + git_reference_free(head); +} + +void test_reset_soft__fails_when_merging(void) +{ + git_buf merge_head_path = GIT_BUF_INIT; + + cl_git_pass(git_repository_detach_head(repo)); + cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); + cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); + + retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + + cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path))); + + git_buf_free(&merge_head_path); +} + +void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void) +{ + git_index *index; + git_reference *head; + git_buf merge_head_path = GIT_BUF_INIT; + + cl_git_sandbox_cleanup(); + + repo = cl_git_sandbox_init("mergedrepo"); + + cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); + cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path))); + git_buf_free(&merge_head_path); + + cl_git_pass(git_repository_index(&index, repo)); + cl_assert_equal_i(true, git_index_has_conflicts(index)); + git_index_free(index); + + cl_git_pass(git_repository_head(&head, repo)); + cl_git_pass(git_reference_peel(&target, head, GIT_OBJ_COMMIT)); + git_reference_free(head); + + cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT)); +} |