]>
Commit | Line | Data |
---|---|---|
edebceff | 1 | #include "clar_libgit2.h" |
2 | #include "reset_helpers.h" | |
3 | ||
4 | static git_repository *repo; | |
5 | static git_object *target; | |
6 | ||
7 | void test_reset_soft__initialize(void) | |
8 | { | |
9 | repo = cl_git_sandbox_init("testrepo.git"); | |
10 | } | |
11 | ||
12 | void test_reset_soft__cleanup(void) | |
13 | { | |
14 | git_object_free(target); | |
15 | cl_git_sandbox_cleanup(); | |
16 | } | |
17 | ||
18 | static void assert_reset_soft(bool should_be_detached) | |
19 | { | |
20 | git_oid oid; | |
21 | ||
22 | cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); | |
23 | cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); | |
24 | ||
25 | retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); | |
26 | ||
27 | cl_assert(git_repository_head_detached(repo) == should_be_detached); | |
28 | ||
29 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); | |
30 | ||
31 | cl_assert(git_repository_head_detached(repo) == should_be_detached); | |
32 | ||
33 | cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); | |
34 | cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); | |
35 | } | |
36 | ||
37 | void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void) | |
38 | { | |
39 | assert_reset_soft(false); | |
40 | } | |
41 | ||
edebceff | 42 | void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) |
43 | { | |
209e34fa | 44 | git_repository_detach_head(repo); |
edebceff | 45 | |
46 | assert_reset_soft(true); | |
47 | } | |
48 | ||
49 | void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) | |
50 | { | |
51 | git_oid oid; | |
52 | char raw_head_oid[GIT_OID_HEXSZ + 1]; | |
53 | ||
54 | cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); | |
55 | git_oid_fmt(raw_head_oid, &oid); | |
56 | raw_head_oid[GIT_OID_HEXSZ] = '\0'; | |
57 | ||
58 | retrieve_target_from_oid(&target, repo, raw_head_oid); | |
59 | ||
60 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); | |
61 | ||
62 | cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); | |
63 | cl_git_pass(git_oid_streq(&oid, raw_head_oid)); | |
64 | } | |
65 | ||
66 | void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void) | |
67 | { | |
68 | git_oid oid; | |
69 | ||
70 | /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ | |
71 | retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); | |
72 | ||
73 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); | |
74 | ||
75 | cl_assert(git_repository_head_detached(repo) == false); | |
76 | cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); | |
77 | cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); | |
78 | } | |
79 | ||
80 | void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) | |
81 | { | |
82 | /* 53fc32d is the tree of commit e90810b */ | |
83 | retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016"); | |
84 | ||
85 | cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); | |
86 | git_object_free(target); | |
87 | ||
88 | /* 521d87c is an annotated tag pointing to a blob */ | |
89 | retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"); | |
90 | cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); | |
91 | } |