1 #include "clar_libgit2.h"
5 #include "merge_helpers.h"
7 #include "git2/merge.h"
8 #include "git2/sys/index.h"
9 #include "git2/annotated_commit.h"
11 int merge_trees_from_branches(
12 git_index
**index
, git_repository
*repo
,
13 const char *ours_name
, const char *theirs_name
,
14 git_merge_options
*opts
)
16 git_commit
*our_commit
, *their_commit
, *ancestor_commit
= NULL
;
17 git_tree
*our_tree
, *their_tree
, *ancestor_tree
= NULL
;
18 git_oid our_oid
, their_oid
, ancestor_oid
;
19 git_buf branch_buf
= GIT_BUF_INIT
;
22 git_buf_printf(&branch_buf
, "%s%s", GIT_REFS_HEADS_DIR
, ours_name
);
23 cl_git_pass(git_reference_name_to_id(&our_oid
, repo
, branch_buf
.ptr
));
24 cl_git_pass(git_commit_lookup(&our_commit
, repo
, &our_oid
));
26 git_buf_clear(&branch_buf
);
27 git_buf_printf(&branch_buf
, "%s%s", GIT_REFS_HEADS_DIR
, theirs_name
);
28 cl_git_pass(git_reference_name_to_id(&their_oid
, repo
, branch_buf
.ptr
));
29 cl_git_pass(git_commit_lookup(&their_commit
, repo
, &their_oid
));
31 error
= git_merge_base(&ancestor_oid
, repo
, git_commit_id(our_commit
), git_commit_id(their_commit
));
33 if (error
!= GIT_ENOTFOUND
) {
36 cl_git_pass(git_commit_lookup(&ancestor_commit
, repo
, &ancestor_oid
));
37 cl_git_pass(git_commit_tree(&ancestor_tree
, ancestor_commit
));
40 cl_git_pass(git_commit_tree(&our_tree
, our_commit
));
41 cl_git_pass(git_commit_tree(&their_tree
, their_commit
));
43 cl_git_pass(git_merge_trees(index
, repo
, ancestor_tree
, our_tree
, their_tree
, opts
));
45 git_buf_free(&branch_buf
);
46 git_tree_free(our_tree
);
47 git_tree_free(their_tree
);
48 git_tree_free(ancestor_tree
);
49 git_commit_free(our_commit
);
50 git_commit_free(their_commit
);
51 git_commit_free(ancestor_commit
);
56 int merge_commits_from_branches(
57 git_index
**index
, git_repository
*repo
,
58 const char *ours_name
, const char *theirs_name
,
59 git_merge_options
*opts
)
61 git_commit
*our_commit
, *their_commit
;
62 git_oid our_oid
, their_oid
;
63 git_buf branch_buf
= GIT_BUF_INIT
;
65 git_buf_printf(&branch_buf
, "%s%s", GIT_REFS_HEADS_DIR
, ours_name
);
66 cl_git_pass(git_reference_name_to_id(&our_oid
, repo
, branch_buf
.ptr
));
67 cl_git_pass(git_commit_lookup(&our_commit
, repo
, &our_oid
));
69 git_buf_clear(&branch_buf
);
70 git_buf_printf(&branch_buf
, "%s%s", GIT_REFS_HEADS_DIR
, theirs_name
);
71 cl_git_pass(git_reference_name_to_id(&their_oid
, repo
, branch_buf
.ptr
));
72 cl_git_pass(git_commit_lookup(&their_commit
, repo
, &their_oid
));
74 cl_git_pass(git_merge_commits(index
, repo
, our_commit
, their_commit
, opts
));
76 git_buf_free(&branch_buf
);
77 git_commit_free(our_commit
);
78 git_commit_free(their_commit
);
83 int merge_branches(git_repository
*repo
,
84 const char *ours_branch
, const char *theirs_branch
,
85 git_merge_options
*merge_opts
, git_checkout_options
*checkout_opts
)
87 git_reference
*head_ref
, *theirs_ref
;
88 git_annotated_commit
*theirs_head
;
89 git_checkout_options head_checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
91 head_checkout_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
93 cl_git_pass(git_reference_symbolic_create(&head_ref
, repo
, "HEAD", ours_branch
, 1, NULL
));
94 cl_git_pass(git_checkout_head(repo
, &head_checkout_opts
));
96 cl_git_pass(git_reference_lookup(&theirs_ref
, repo
, theirs_branch
));
97 cl_git_pass(git_annotated_commit_from_ref(&theirs_head
, repo
, theirs_ref
));
99 cl_git_pass(git_merge(repo
, (const git_annotated_commit
**)&theirs_head
, 1, merge_opts
, checkout_opts
));
101 git_reference_free(head_ref
);
102 git_reference_free(theirs_ref
);
103 git_annotated_commit_free(theirs_head
);
108 void merge__dump_index_entries(git_vector
*index_entries
)
111 const git_index_entry
*index_entry
;
113 printf ("\nINDEX [%"PRIuZ
"]:\n", index_entries
->length
);
114 for (i
= 0; i
< index_entries
->length
; i
++) {
115 index_entry
= index_entries
->contents
[i
];
117 printf("%o ", index_entry
->mode
);
118 printf("%s ", git_oid_allocfmt(&index_entry
->id
));
119 printf("%d ", git_index_entry_stage(index_entry
));
120 printf("%s ", index_entry
->path
);
126 void merge__dump_names(git_index
*index
)
129 const git_index_name_entry
*conflict_name
;
131 for (i
= 0; i
< git_index_name_entrycount(index
); i
++) {
132 conflict_name
= git_index_name_get_byindex(index
, i
);
134 printf("%s %s %s\n", conflict_name
->ancestor
, conflict_name
->ours
, conflict_name
->theirs
);
139 void merge__dump_reuc(git_index
*index
)
142 const git_index_reuc_entry
*reuc
;
144 printf ("\nREUC:\n");
145 for (i
= 0; i
< git_index_reuc_entrycount(index
); i
++) {
146 reuc
= git_index_reuc_get_byindex(index
, i
);
148 printf("%s ", reuc
->path
);
149 printf("%o ", reuc
->mode
[0]);
150 printf("%s\n", git_oid_allocfmt(&reuc
->oid
[0]));
151 printf(" %o ", reuc
->mode
[1]);
152 printf(" %s\n", git_oid_allocfmt(&reuc
->oid
[1]));
153 printf(" %o ", reuc
->mode
[2]);
154 printf(" %s ", git_oid_allocfmt(&reuc
->oid
[2]));
160 static int index_entry_eq_merge_index_entry(const struct merge_index_entry
*expected
, const git_index_entry
*actual
)
162 git_oid expected_oid
;
165 if (strlen(expected
->oid_str
) != 0) {
166 cl_git_pass(git_oid_fromstr(&expected_oid
, expected
->oid_str
));
171 if (actual
->mode
!= expected
->mode
||
172 (test_oid
&& git_oid_cmp(&actual
->id
, &expected_oid
) != 0) ||
173 git_index_entry_stage(actual
) != expected
->stage
)
176 if (actual
->mode
== 0 && (actual
->path
!= NULL
|| strlen(expected
->path
) > 0))
179 if (actual
->mode
!= 0 && (strcmp(actual
->path
, expected
->path
) != 0))
185 static int name_entry_eq(const char *expected
, const char *actual
)
187 if (strlen(expected
) == 0)
188 return (actual
== NULL
) ? 1 : 0;
190 return (strcmp(expected
, actual
) == 0) ? 1 : 0;
193 static int name_entry_eq_merge_name_entry(const struct merge_name_entry
*expected
, const git_index_name_entry
*actual
)
195 if (name_entry_eq(expected
->ancestor_path
, actual
->ancestor
) == 0 ||
196 name_entry_eq(expected
->our_path
, actual
->ours
) == 0 ||
197 name_entry_eq(expected
->their_path
, actual
->theirs
) == 0)
203 static int index_conflict_data_eq_merge_diff(const struct merge_index_conflict_data
*expected
, git_merge_diff
*actual
)
205 if (!index_entry_eq_merge_index_entry(&expected
->ancestor
.entry
, &actual
->ancestor_entry
) ||
206 !index_entry_eq_merge_index_entry(&expected
->ours
.entry
, &actual
->our_entry
) ||
207 !index_entry_eq_merge_index_entry(&expected
->theirs
.entry
, &actual
->their_entry
))
210 if (expected
->ours
.status
!= actual
->our_status
||
211 expected
->theirs
.status
!= actual
->their_status
)
217 int merge_test_merge_conflicts(git_vector
*conflicts
, const struct merge_index_conflict_data expected
[], size_t expected_len
)
219 git_merge_diff
*actual
;
222 if (conflicts
->length
!= expected_len
)
225 for (i
= 0; i
< expected_len
; i
++) {
226 actual
= conflicts
->contents
[i
];
228 if (!index_conflict_data_eq_merge_diff(&expected
[i
], actual
))
235 int merge_test_index(git_index
*index
, const struct merge_index_entry expected
[], size_t expected_len
)
238 const git_index_entry
*index_entry
;
241 dump_index_entries(&index->entries);
244 if (git_index_entrycount(index
) != expected_len
)
247 for (i
= 0; i
< expected_len
; i
++) {
248 if ((index_entry
= git_index_get_byindex(index
, i
)) == NULL
)
251 if (!index_entry_eq_merge_index_entry(&expected
[i
], index_entry
))
258 int merge_test_names(git_index
*index
, const struct merge_name_entry expected
[], size_t expected_len
)
261 const git_index_name_entry
*name_entry
;
267 if (git_index_name_entrycount(index
) != expected_len
)
270 for (i
= 0; i
< expected_len
; i
++) {
271 if ((name_entry
= git_index_name_get_byindex(index
, i
)) == NULL
)
274 if (! name_entry_eq_merge_name_entry(&expected
[i
], name_entry
))
281 int merge_test_reuc(git_index
*index
, const struct merge_reuc_entry expected
[], size_t expected_len
)
284 const git_index_reuc_entry
*reuc_entry
;
285 git_oid expected_oid
;
291 if (git_index_reuc_entrycount(index
) != expected_len
)
294 for (i
= 0; i
< expected_len
; i
++) {
295 if ((reuc_entry
= git_index_reuc_get_byindex(index
, i
)) == NULL
)
298 if (strcmp(reuc_entry
->path
, expected
[i
].path
) != 0 ||
299 reuc_entry
->mode
[0] != expected
[i
].ancestor_mode
||
300 reuc_entry
->mode
[1] != expected
[i
].our_mode
||
301 reuc_entry
->mode
[2] != expected
[i
].their_mode
)
304 if (expected
[i
].ancestor_mode
> 0) {
305 cl_git_pass(git_oid_fromstr(&expected_oid
, expected
[i
].ancestor_oid_str
));
307 if (git_oid_cmp(&reuc_entry
->oid
[0], &expected_oid
) != 0)
311 if (expected
[i
].our_mode
> 0) {
312 cl_git_pass(git_oid_fromstr(&expected_oid
, expected
[i
].our_oid_str
));
314 if (git_oid_cmp(&reuc_entry
->oid
[1], &expected_oid
) != 0)
318 if (expected
[i
].their_mode
> 0) {
319 cl_git_pass(git_oid_fromstr(&expected_oid
, expected
[i
].their_oid_str
));
321 if (git_oid_cmp(&reuc_entry
->oid
[2], &expected_oid
) != 0)
329 int dircount(void *payload
, git_buf
*pathbuf
)
331 size_t *entries
= payload
;
332 size_t len
= git_buf_len(pathbuf
);
334 if (len
< 5 || strcmp(pathbuf
->ptr
+ (git_buf_len(pathbuf
) - 5), "/.git") != 0)
340 int merge_test_workdir(git_repository
*repo
, const struct merge_index_entry expected
[], size_t expected_len
)
342 size_t actual_len
= 0, i
;
343 git_oid actual_oid
, expected_oid
;
344 git_buf wd
= GIT_BUF_INIT
;
346 git_buf_puts(&wd
, repo
->workdir
);
347 git_path_direach(&wd
, 0, dircount
, &actual_len
);
349 if (actual_len
!= expected_len
)
352 for (i
= 0; i
< expected_len
; i
++) {
353 git_blob_create_fromworkdir(&actual_oid
, repo
, expected
[i
].path
);
354 git_oid_fromstr(&expected_oid
, expected
[i
].oid_str
);
356 if (git_oid_cmp(&actual_oid
, &expected_oid
) != 0)