2 * libgit2 "merge" example - shows how to perform merges
4 * Written by the libgit2 contributors
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
17 /** The following example demonstrates how to do merges with libgit2.
19 * It will merge whatever commit-ish you pass in into the current branch.
21 * Recognized options are :
22 * --no-commit: don't actually commit the merge.
26 struct merge_options
{
30 git_annotated_commit
**annotated
;
31 size_t annotated_count
;
36 static void print_usage(void)
38 fprintf(stderr
, "usage: merge [--no-commit] <commit...>\n");
42 static void merge_options_init(struct merge_options
*opts
)
44 memset(opts
, 0, sizeof(*opts
));
47 opts
->heads_count
= 0;
48 opts
->annotated
= NULL
;
49 opts
->annotated_count
= 0;
52 static void opts_add_refish(struct merge_options
*opts
, const char *refish
)
58 sz
= ++opts
->heads_count
* sizeof(opts
->heads
[0]);
59 opts
->heads
= xrealloc((void *) opts
->heads
, sz
);
60 opts
->heads
[opts
->heads_count
- 1] = refish
;
63 static void parse_options(const char **repo_path
, struct merge_options
*opts
, int argc
, char **argv
)
65 struct args_info args
= ARGS_INFO_INIT
;
70 for (args
.pos
= 1; args
.pos
< argc
; ++args
.pos
) {
71 const char *curr
= argv
[args
.pos
];
74 opts_add_refish(opts
, curr
);
75 } else if (!strcmp(curr
, "--no-commit")) {
77 } else if (match_str_arg(repo_path
, &args
, "--git-dir")) {
85 static int resolve_heads(git_repository
*repo
, struct merge_options
*opts
)
87 git_annotated_commit
**annotated
= calloc(opts
->heads_count
, sizeof(git_annotated_commit
*));
88 size_t annotated_count
= 0, i
;
91 for (i
= 0; i
< opts
->heads_count
; i
++) {
92 err
= resolve_refish(&annotated
[annotated_count
++], repo
, opts
->heads
[i
]);
94 fprintf(stderr
, "failed to resolve refish %s: %s\n", opts
->heads
[i
], git_error_last()->message
);
100 if (annotated_count
!= opts
->heads_count
) {
101 fprintf(stderr
, "unable to parse some refish\n");
106 opts
->annotated
= annotated
;
107 opts
->annotated_count
= annotated_count
;
111 static int perform_fastforward(git_repository
*repo
, const git_oid
*target_oid
, int is_unborn
)
113 git_checkout_options ff_checkout_options
= GIT_CHECKOUT_OPTIONS_INIT
;
114 git_reference
*target_ref
;
115 git_reference
*new_target_ref
;
116 git_object
*target
= NULL
;
120 const char *symbolic_ref
;
121 git_reference
*head_ref
;
123 /* HEAD reference is unborn, lookup manually so we don't try to resolve it */
124 err
= git_reference_lookup(&head_ref
, repo
, "HEAD");
126 fprintf(stderr
, "failed to lookup HEAD ref\n");
130 /* Grab the reference HEAD should be pointing to */
131 symbolic_ref
= git_reference_symbolic_target(head_ref
);
133 /* Create our master reference on the target OID */
134 err
= git_reference_create(&target_ref
, repo
, symbolic_ref
, target_oid
, 0, NULL
);
136 fprintf(stderr
, "failed to create master reference\n");
140 git_reference_free(head_ref
);
142 /* HEAD exists, just lookup and resolve */
143 err
= git_repository_head(&target_ref
, repo
);
145 fprintf(stderr
, "failed to get HEAD reference\n");
150 /* Lookup the target object */
151 err
= git_object_lookup(&target
, repo
, target_oid
, GIT_OBJECT_COMMIT
);
153 fprintf(stderr
, "failed to lookup OID %s\n", git_oid_tostr_s(target_oid
));
157 /* Checkout the result so the workdir is in the expected state */
158 ff_checkout_options
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
159 err
= git_checkout_tree(repo
, target
, &ff_checkout_options
);
161 fprintf(stderr
, "failed to checkout HEAD reference\n");
165 /* Move the target reference to the target OID */
166 err
= git_reference_set_target(&new_target_ref
, target_ref
, target_oid
, NULL
);
168 fprintf(stderr
, "failed to move HEAD reference\n");
172 git_reference_free(target_ref
);
173 git_reference_free(new_target_ref
);
174 git_object_free(target
);
179 static void output_conflicts(git_index
*index
)
181 git_index_conflict_iterator
*conflicts
;
182 const git_index_entry
*ancestor
;
183 const git_index_entry
*our
;
184 const git_index_entry
*their
;
187 check_lg2(git_index_conflict_iterator_new(&conflicts
, index
), "failed to create conflict iterator", NULL
);
189 while ((err
= git_index_conflict_next(&ancestor
, &our
, &their
, conflicts
)) == 0) {
190 fprintf(stderr
, "conflict: a:%s o:%s t:%s\n",
191 ancestor
? ancestor
->path
: "NULL",
192 our
->path
? our
->path
: "NULL",
193 their
->path
? their
->path
: "NULL");
196 if (err
!= GIT_ITEROVER
) {
197 fprintf(stderr
, "error iterating conflicts\n");
200 git_index_conflict_iterator_free(conflicts
);
203 static int create_merge_commit(git_repository
*repo
, git_index
*index
, struct merge_options
*opts
)
205 git_oid tree_oid
, commit_oid
;
208 git_reference
*merge_ref
= NULL
;
209 git_annotated_commit
*merge_commit
;
210 git_reference
*head_ref
;
211 git_commit
**parents
= calloc(opts
->annotated_count
+ 1, sizeof(git_commit
*));
212 const char *msg_target
= NULL
;
218 /* Grab our needed references */
219 check_lg2(git_repository_head(&head_ref
, repo
), "failed to get repo HEAD", NULL
);
220 if (resolve_refish(&merge_commit
, repo
, opts
->heads
[0])) {
221 fprintf(stderr
, "failed to resolve refish %s", opts
->heads
[0]);
226 /* Maybe that's a ref, so DWIM it */
227 err
= git_reference_dwim(&merge_ref
, repo
, opts
->heads
[0]);
228 check_lg2(err
, "failed to DWIM reference", git_error_last()->message
);
230 /* Grab a signature */
231 check_lg2(git_signature_now(&sign
, "Me", "me@example.com"), "failed to create signature", NULL
);
233 #define MERGE_COMMIT_MSG "Merge %s '%s'"
234 /* Prepare a standard merge commit message */
235 if (merge_ref
!= NULL
) {
236 check_lg2(git_branch_name(&msg_target
, merge_ref
), "failed to get branch name of merged ref", NULL
);
238 msg_target
= git_oid_tostr_s(git_annotated_commit_id(merge_commit
));
241 msglen
= snprintf(NULL
, 0, MERGE_COMMIT_MSG
, (merge_ref
? "branch" : "commit"), msg_target
);
242 if (msglen
> 0) msglen
++;
243 msg
= malloc(msglen
);
244 err
= snprintf(msg
, msglen
, MERGE_COMMIT_MSG
, (merge_ref
? "branch" : "commit"), msg_target
);
246 /* This is only to silence the compiler */
247 if (err
< 0) goto cleanup
;
249 /* Setup our parent commits */
250 err
= git_reference_peel((git_object
**)&parents
[0], head_ref
, GIT_OBJECT_COMMIT
);
251 check_lg2(err
, "failed to peel head reference", NULL
);
252 for (i
= 0; i
< opts
->annotated_count
; i
++) {
253 git_commit_lookup(&parents
[i
+ 1], repo
, git_annotated_commit_id(opts
->annotated
[i
]));
256 /* Prepare our commit tree */
257 check_lg2(git_index_write_tree(&tree_oid
, index
), "failed to write merged tree", NULL
);
258 check_lg2(git_tree_lookup(&tree
, repo
, &tree_oid
), "failed to lookup tree", NULL
);
261 err
= git_commit_create(&commit_oid
,
262 repo
, git_reference_name(head_ref
),
266 opts
->annotated_count
+ 1, (const git_commit
**)parents
);
267 check_lg2(err
, "failed to create commit", NULL
);
269 /* We're done merging, cleanup the repository state */
270 git_repository_state_cleanup(repo
);
277 int lg2_merge(git_repository
*repo
, int argc
, char **argv
)
279 struct merge_options opts
;
281 git_repository_state_t state
;
282 git_merge_analysis_t analysis
;
283 git_merge_preference_t preference
;
284 const char *path
= ".";
287 merge_options_init(&opts
);
288 parse_options(&path
, &opts
, argc
, argv
);
290 state
= git_repository_state(repo
);
291 if (state
!= GIT_REPOSITORY_STATE_NONE
) {
292 fprintf(stderr
, "repository is in unexpected state %d\n", state
);
296 err
= resolve_heads(repo
, &opts
);
300 err
= git_merge_analysis(&analysis
, &preference
,
302 (const git_annotated_commit
**)opts
.annotated
,
303 opts
.annotated_count
);
304 check_lg2(err
, "merge analysis failed", NULL
);
306 if (analysis
& GIT_MERGE_ANALYSIS_UP_TO_DATE
) {
307 printf("Already up-to-date\n");
309 } else if (analysis
& GIT_MERGE_ANALYSIS_UNBORN
||
310 (analysis
& GIT_MERGE_ANALYSIS_FASTFORWARD
&&
311 !(preference
& GIT_MERGE_PREFERENCE_NO_FASTFORWARD
))) {
312 const git_oid
*target_oid
;
313 if (analysis
& GIT_MERGE_ANALYSIS_UNBORN
) {
316 printf("Fast-forward\n");
319 /* Since this is a fast-forward, there can be only one merge head */
320 target_oid
= git_annotated_commit_id(opts
.annotated
[0]);
321 assert(opts
.annotated_count
== 1);
323 return perform_fastforward(repo
, target_oid
, (analysis
& GIT_MERGE_ANALYSIS_UNBORN
));
324 } else if (analysis
& GIT_MERGE_ANALYSIS_NORMAL
) {
325 git_merge_options merge_opts
= GIT_MERGE_OPTIONS_INIT
;
326 git_checkout_options checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
328 merge_opts
.flags
= 0;
329 merge_opts
.file_flags
= GIT_MERGE_FILE_STYLE_DIFF3
;
331 checkout_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
|GIT_CHECKOUT_ALLOW_CONFLICTS
;
333 if (preference
& GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
) {
334 printf("Fast-forward is preferred, but only a merge is possible\n");
338 err
= git_merge(repo
,
339 (const git_annotated_commit
**)opts
.annotated
, opts
.annotated_count
,
340 &merge_opts
, &checkout_opts
);
341 check_lg2(err
, "merge failed", NULL
);
344 /* If we get here, we actually performed the merge above */
346 check_lg2(git_repository_index(&index
, repo
), "failed to get repository index", NULL
);
348 if (git_index_has_conflicts(index
)) {
349 /* Handle conflicts */
350 output_conflicts(index
);
351 } else if (!opts
.no_commit
) {
352 create_merge_commit(repo
, index
, &opts
);
353 printf("Merge made\n");
357 free((char **)opts
.heads
);
358 free(opts
.annotated
);