]> git.proxmox.com Git - libgit2.git/blob - tests/rebase/iterator.c
rebase: correctly finish rebasing detached heads
[libgit2.git] / tests / rebase / iterator.c
1 #include "clar_libgit2.h"
2 #include "git2/rebase.h"
3 #include "posix.h"
4
5 #include <fcntl.h>
6
7 static git_repository *repo;
8 static git_index *_index;
9 static git_signature *signature;
10
11 // Fixture setup and teardown
12 void test_rebase_iterator__initialize(void)
13 {
14 repo = cl_git_sandbox_init("rebase");
15 cl_git_pass(git_repository_index(&_index, repo));
16 cl_git_pass(git_signature_new(&signature, "Rebaser",
17 "rebaser@rebaser.rb", 1405694510, 0));
18 }
19
20 void test_rebase_iterator__cleanup(void)
21 {
22 git_signature_free(signature);
23 git_index_free(_index);
24 cl_git_sandbox_cleanup();
25 }
26
27 static void test_operations(git_rebase *rebase, size_t expected_current)
28 {
29 size_t i, expected_count = 5;
30 git_oid expected_oid[5];
31 git_rebase_operation *operation;
32
33 git_oid_fromstr(&expected_oid[0], "da9c51a23d02d931a486f45ad18cda05cf5d2b94");
34 git_oid_fromstr(&expected_oid[1], "8d1f13f93c4995760ac07d129246ac1ff64c0be9");
35 git_oid_fromstr(&expected_oid[2], "3069cc907e6294623e5917ef6de663928c1febfb");
36 git_oid_fromstr(&expected_oid[3], "588e5d2f04d49707fe4aab865e1deacaf7ef6787");
37 git_oid_fromstr(&expected_oid[4], "b146bd7608eac53d9bf9e1a6963543588b555c64");
38
39 cl_assert_equal_i(expected_count, git_rebase_operation_entrycount(rebase));
40 cl_assert_equal_i(expected_current, git_rebase_operation_current(rebase));
41
42 for (i = 0; i < expected_count; i++) {
43 operation = git_rebase_operation_byindex(rebase, i);
44 cl_assert_equal_i(GIT_REBASE_OPERATION_PICK, operation->type);
45 cl_assert_equal_oid(&expected_oid[i], &operation->id);
46 cl_assert_equal_p(NULL, operation->exec);
47 }
48 }
49
50 void test_iterator(bool inmemory)
51 {
52 git_rebase *rebase;
53 git_rebase_options opts = GIT_REBASE_OPTIONS_INIT;
54 git_reference *branch_ref, *upstream_ref;
55 git_annotated_commit *branch_head, *upstream_head;
56 git_rebase_operation *rebase_operation;
57 git_oid commit_id, expected_id;
58 int error;
59
60 opts.inmemory = inmemory;
61
62 cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
63 cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));
64
65 cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
66 cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
67
68 cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &opts));
69 test_operations(rebase, GIT_REBASE_NO_OPERATION);
70
71 if (!inmemory) {
72 git_rebase_free(rebase);
73 cl_git_pass(git_rebase_open(&rebase, repo, NULL));
74 }
75
76 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
77 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
78 NULL, NULL));
79 test_operations(rebase, 0);
80
81 git_oid_fromstr(&expected_id, "776e4c48922799f903f03f5f6e51da8b01e4cce0");
82 cl_assert_equal_oid(&expected_id, &commit_id);
83
84 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
85 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
86 NULL, NULL));
87 test_operations(rebase, 1);
88
89 git_oid_fromstr(&expected_id, "ba1f9b4fd5cf8151f7818be2111cc0869f1eb95a");
90 cl_assert_equal_oid(&expected_id, &commit_id);
91
92 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
93 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
94 NULL, NULL));
95 test_operations(rebase, 2);
96
97 git_oid_fromstr(&expected_id, "948b12fe18b84f756223a61bece4c307787cd5d4");
98 cl_assert_equal_oid(&expected_id, &commit_id);
99
100 if (!inmemory) {
101 git_rebase_free(rebase);
102 cl_git_pass(git_rebase_open(&rebase, repo, NULL));
103 }
104
105 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
106 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
107 NULL, NULL));
108 test_operations(rebase, 3);
109
110 git_oid_fromstr(&expected_id, "d9d5d59d72c9968687f9462578d79878cd80e781");
111 cl_assert_equal_oid(&expected_id, &commit_id);
112
113 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
114 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
115 NULL, NULL));
116 test_operations(rebase, 4);
117
118 git_oid_fromstr(&expected_id, "9cf383c0a125d89e742c5dec58ed277dd07588b3");
119 cl_assert_equal_oid(&expected_id, &commit_id);
120
121 cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
122 cl_assert_equal_i(GIT_ITEROVER, error);
123 test_operations(rebase, 4);
124
125 git_annotated_commit_free(branch_head);
126 git_annotated_commit_free(upstream_head);
127 git_reference_free(branch_ref);
128 git_reference_free(upstream_ref);
129 git_rebase_free(rebase);
130 }
131
132 void test_rebase_iterator__iterates(void)
133 {
134 test_iterator(false);
135 }
136
137 void test_rebase_iterator__iterates_inmemory(void)
138 {
139 test_iterator(true);
140 }