]> git.proxmox.com Git - libgit2.git/blame - tests-clar/reset/soft.c
Merge pull request #1106 from frasertweedale/fix/freebsd-build
[libgit2.git] / tests-clar / reset / soft.c
CommitLineData
edebceff 1#include "clar_libgit2.h"
632d8b23 2#include "posix.h"
edebceff 3#include "reset_helpers.h"
632d8b23 4#include "path.h"
c436ed26 5#include "repo/repo_helpers.h"
edebceff 6
7static git_repository *repo;
8static git_object *target;
9
10void test_reset_soft__initialize(void)
11{
12 repo = cl_git_sandbox_init("testrepo.git");
13}
14
15void test_reset_soft__cleanup(void)
16{
17 git_object_free(target);
9094d30b
SC
18 target = NULL;
19
edebceff 20 cl_git_sandbox_cleanup();
21}
22
23static void assert_reset_soft(bool should_be_detached)
24{
25 git_oid oid;
26
27 cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
28 cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
29
30 retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
31
32 cl_assert(git_repository_head_detached(repo) == should_be_detached);
33
34 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
35
36 cl_assert(git_repository_head_detached(repo) == should_be_detached);
37
38 cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
39 cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
40}
41
42void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void)
43{
44 assert_reset_soft(false);
45}
46
edebceff 47void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
48{
209e34fa 49 git_repository_detach_head(repo);
edebceff 50
51 assert_reset_soft(true);
52}
53
54void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void)
55{
56 git_oid oid;
57 char raw_head_oid[GIT_OID_HEXSZ + 1];
58
59 cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
60 git_oid_fmt(raw_head_oid, &oid);
61 raw_head_oid[GIT_OID_HEXSZ] = '\0';
62
63 retrieve_target_from_oid(&target, repo, raw_head_oid);
64
65 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
66
67 cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
68 cl_git_pass(git_oid_streq(&oid, raw_head_oid));
69}
70
71void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void)
72{
73 git_oid oid;
74
75 /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */
76 retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
77
78 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
79
80 cl_assert(git_repository_head_detached(repo) == false);
81 cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
82 cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
83}
84
85void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
86{
87 /* 53fc32d is the tree of commit e90810b */
88 retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016");
89
90 cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
91 git_object_free(target);
92
93 /* 521d87c is an annotated tag pointing to a blob */
94 retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
95 cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
96}
c436ed26 97
98void test_reset_soft__resetting_against_an_orphaned_head_repo_makes_the_head_no_longer_orphaned(void)
99{
100 git_reference *head;
101
102 retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
103
104 make_head_orphaned(repo, NON_EXISTING_HEAD);
105
106 cl_assert_equal_i(true, git_repository_head_orphan(repo));
107
108 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
109
110 cl_assert_equal_i(false, git_repository_head_orphan(repo));
111
112 cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD));
113 cl_assert_equal_i(0, git_oid_streq(git_reference_oid(head), KNOWN_COMMIT_IN_BARE_REPO));
114
115 git_reference_free(head);
116}
632d8b23
ET
117
118void test_reset_soft__fails_when_merging(void)
119{
120 git_buf merge_head_path = GIT_BUF_INIT;
121
31966d20 122 cl_git_pass(git_repository_detach_head(repo));
632d8b23
ET
123 cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
124 cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");
125
03bdb2ad
ET
126 retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
127
128 cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT));
632d8b23 129 cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
00e161b9 130
131 git_buf_free(&merge_head_path);
632d8b23 132}