summaryrefslogtreecommitdiff
path: root/tests/reset
diff options
context:
space:
mode:
Diffstat (limited to 'tests/reset')
-rw-r--r--tests/reset/default.c38
-rw-r--r--tests/reset/hard.c52
-rw-r--r--tests/reset/mixed.c42
-rw-r--r--tests/reset/reset_helpers.c18
-rw-r--r--tests/reset/reset_helpers.h3
-rw-r--r--tests/reset/soft.c61
6 files changed, 174 insertions, 40 deletions
diff --git a/tests/reset/default.c b/tests/reset/default.c
index e29e63550..c76f14813 100644
--- a/tests/reset/default.c
+++ b/tests/reset/default.c
@@ -21,7 +21,6 @@ static void initialize(const char *repo_name)
void test_reset_default__initialize(void)
{
- initialize("status");
}
void test_reset_default__cleanup(void)
@@ -57,7 +56,7 @@ static void assert_content_in_index(
if (!expected_shas)
continue;
- cl_git_pass(git_oid_streq(&entry->oid, expected_shas->strings[i]));
+ cl_git_pass(git_oid_streq(&entry->id, expected_shas->strings[i]));
} else
cl_assert_equal_i(should_exist, error != GIT_ENOTFOUND);
}
@@ -67,6 +66,8 @@ void test_reset_default__resetting_filepaths_against_a_null_target_removes_them_
{
char *paths[] = { "staged_changes", "staged_new_file" };
+ initialize("status");
+
_pathspecs.strings = paths;
_pathspecs.count = 2;
@@ -102,6 +103,8 @@ void test_reset_default__resetting_filepaths_replaces_their_corresponding_index_
char *after_shas[] = { "32504b727382542f9f089e24fddac5e78533e96c",
"061d42a44cacde5726057b67558821d95db96f19" };
+ initialize("status");
+
_pathspecs.strings = paths;
_pathspecs.count = 2;
before.strings = before_shas;
@@ -139,7 +142,6 @@ void test_reset_default__resetting_filepaths_clears_previous_conflicts(void)
char *paths[] = { "conflicts-one.txt" };
char *after_shas[] = { "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" };
- test_reset_default__cleanup();
initialize("mergedrepo");
_pathspecs.strings = paths;
@@ -168,6 +170,8 @@ void test_reset_default__resetting_unknown_filepaths_does_not_fail(void)
{
char *paths[] = { "I_am_not_there.txt", "me_neither.txt" };
+ initialize("status");
+
_pathspecs.strings = paths;
_pathspecs.count = 2;
@@ -178,3 +182,31 @@ void test_reset_default__resetting_unknown_filepaths_does_not_fail(void)
assert_content_in_index(&_pathspecs, false, NULL);
}
+
+void test_reset_default__staged_rename_reset_delete(void)
+{
+ git_index_entry entry;
+ const git_index_entry *existing;
+ char *paths[] = { "new.txt" };
+
+ initialize("testrepo2");
+
+ existing = git_index_get_bypath(_index, "new.txt", 0);
+ cl_assert(existing);
+ memcpy(&entry, existing, sizeof(entry));
+
+ cl_git_pass(git_index_remove_bypath(_index, "new.txt"));
+
+ entry.path = "renamed.txt";
+ cl_git_pass(git_index_add(_index, &entry));
+
+ _pathspecs.strings = paths;
+ _pathspecs.count = 1;
+
+ 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, true, NULL);
+}
diff --git a/tests/reset/hard.c b/tests/reset/hard.c
index 1c0c84135..c6bf7a8ac 100644
--- a/tests/reset/hard.c
+++ b/tests/reset/hard.c
@@ -69,10 +69,9 @@ void test_reset_hard__resetting_reverts_modified_files(void)
cl_assert_equal_s(before[i], content.ptr);
}
- retrieve_target_from_oid(
- &target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f");
+ cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
- cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
for (i = 0; i < 4; ++i) {
cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
@@ -95,9 +94,9 @@ void test_reset_hard__cannot_reset_in_a_bare_repository(void)
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_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
- cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD));
+ cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD, NULL, NULL));
git_repository_free(bare);
}
@@ -111,7 +110,7 @@ static void index_entry_init(git_index *index, int side, git_oid *oid)
entry.path = "conflicting_file";
entry.flags = (side << GIT_IDXENTRY_STAGESHIFT);
entry.mode = 0100644;
- git_oid_cpy(&entry.oid, oid);
+ git_oid_cpy(&entry.id, oid);
cl_git_pass(git_index_add(index, &entry));
}
@@ -152,8 +151,8 @@ void test_reset_hard__resetting_reverts_unmerged(void)
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_git_pass(git_revparse_single(&target, repo, "26a125e"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
cl_assert(git_path_exists("status/conflicting_file") == 0);
@@ -183,8 +182,8 @@ void test_reset_hard__cleans_up_merge(void)
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_git_pass(git_revparse_single(&target, repo, "0017bd4"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path)));
cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path)));
@@ -198,3 +197,36 @@ void test_reset_hard__cleans_up_merge(void)
git_buf_free(&merge_mode_path);
git_buf_free(&orig_head_path);
}
+
+void test_reset_hard__reflog_is_correct(void)
+{
+ const char *exp_msg = "commit: Add a file which name should appear before the "
+ "\"subdir/\" folder while being dealt with by the treewalker";
+
+ reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
+
+ /* Branch not moving, no reflog entry */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
+ reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
+
+ git_object_free(target);
+
+ /* Moved branch, expect default message */
+ exp_msg = "reset: moving";
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
+ reflog_check(repo, "HEAD", 4, NULL, exp_msg);
+ reflog_check(repo, "refs/heads/master", 4, NULL, exp_msg);
+
+ git_object_free(target);
+
+ /* Moved branch, expect custom message */
+ exp_msg = "message1";
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, "message1"));
+ reflog_check(repo, "HEAD", 5, NULL, exp_msg);
+ reflog_check(repo, "refs/heads/master", 5, NULL, exp_msg);
+}
diff --git a/tests/reset/mixed.c b/tests/reset/mixed.c
index 7b90c23f1..55b8a2f88 100644
--- a/tests/reset/mixed.c
+++ b/tests/reset/mixed.c
@@ -27,9 +27,9 @@ void test_reset_mixed__cannot_reset_in_a_bare_repository(void)
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_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
- cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED));
+ cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED, NULL, NULL));
git_repository_free(bare);
}
@@ -40,10 +40,44 @@ void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void)
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_revparse_single(&target, repo, "605812a"));
- cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_WT_NEW);
}
+
+void test_reset_mixed__reflog_is_correct(void)
+{
+ const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
+
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Branch not moving, no reflog entry */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ git_object_free(target);
+ target = NULL;
+
+ /* Moved branch, expect default message */
+ exp_msg = "reset: moving";
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
+ reflog_check(repo, "HEAD", 10, NULL, exp_msg);
+ reflog_check(repo, "refs/heads/master", 10, NULL, exp_msg);
+
+ git_object_free(target);
+ target = NULL;
+
+ /* Moved branch, expect custom message */
+ exp_msg = "message1";
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, "message1"));
+ reflog_check(repo, "HEAD", 11, NULL, exp_msg);
+ reflog_check(repo, "refs/heads/master", 11, NULL, exp_msg);
+}
diff --git a/tests/reset/reset_helpers.c b/tests/reset/reset_helpers.c
index 17edca4e9..7a335a600 100644
--- a/tests/reset/reset_helpers.c
+++ b/tests/reset/reset_helpers.c
@@ -1,10 +1,20 @@
#include "clar_libgit2.h"
#include "reset_helpers.h"
-void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha)
+void reflog_check(git_repository *repo, const char *refname,
+ size_t exp_count, const char *exp_email, const char *exp_msg)
{
- git_oid oid;
+ git_reflog *log;
+ const git_reflog_entry *entry;
- cl_git_pass(git_oid_fromstr(&oid, sha));
- cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY));
+ cl_git_pass(git_reflog_read(&log, repo, refname));
+ cl_assert_equal_i(exp_count, git_reflog_entrycount(log));
+ entry = git_reflog_entry_byindex(log, 0);
+
+ if (exp_email)
+ cl_assert_equal_s(exp_email, git_reflog_entry_committer(entry)->email);
+ if (exp_msg)
+ cl_assert_equal_s(exp_msg, git_reflog_entry_message(entry));
+
+ git_reflog_free(log);
}
diff --git a/tests/reset/reset_helpers.h b/tests/reset/reset_helpers.h
index 5dbe9d2c7..e7e048514 100644
--- a/tests/reset/reset_helpers.h
+++ b/tests/reset/reset_helpers.h
@@ -3,4 +3,5 @@
#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);
+void reflog_check(git_repository *repo, const char *refname,
+ size_t exp_count, const char *exp_email, const char *exp_msg);
diff --git a/tests/reset/soft.c b/tests/reset/soft.c
index bd6fcc205..c889c0355 100644
--- a/tests/reset/soft.c
+++ b/tests/reset/soft.c
@@ -26,12 +26,11 @@ static void assert_reset_soft(bool should_be_detached)
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_git_pass(git_revparse_single(&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_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert(git_repository_head_detached(repo) == should_be_detached);
@@ -46,7 +45,7 @@ void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(vo
void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
{
- git_repository_detach_head(repo);
+ git_repository_detach_head(repo, NULL, NULL);
assert_reset_soft(true);
}
@@ -60,9 +59,9 @@ void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_ch
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_revparse_single(&target, repo, raw_head_oid));
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_pass(git_oid_streq(&oid, raw_head_oid));
@@ -73,9 +72,9 @@ 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_revparse_single(&target, repo, "b25fa35"));
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert(git_repository_head_detached(repo) == false);
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
@@ -85,27 +84,27 @@ void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void
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_pass(git_revparse_single(&target, repo, "53fc32d"));
- cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
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));
+ cl_git_pass(git_revparse_single(&target, repo, "521d87c"));
+ cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
}
void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void)
{
git_reference *head;
- retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+ cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_assert_equal_i(true, git_repository_head_unborn(repo));
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert_equal_i(false, git_repository_head_unborn(repo));
@@ -119,13 +118,13 @@ 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_repository_detach_head(repo, NULL, NULL));
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_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
- cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT));
+ cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
git_buf_free(&merge_head_path);
@@ -153,5 +152,31 @@ void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE
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));
+ cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+}
+
+void test_reset_soft_reflog_is_correct(void)
+{
+ const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
+
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Branch not moving, no reflog entry */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Moved branch, expect default message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving");
+
+ /* Moved branch, expect custom message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, "message1"));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 11, NULL, "message1");
}