1 /*
2 * libgit2 "merge" example - shows how to perform merges
3 *
4 * Written by the libgit2 contributors
5 *
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.
9 *
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/>.
13 */
14
15 #include "common.h"
16 #include <assert.h>
17
18 #ifdef _MSC_VER
19 #define snprintf sprintf_s
20 #endif
21
22 /** The following example demonstrates how to do merges with libgit2.
23 *
24 * It will merge whatever commit-ish you pass in into the current branch.
25 *
26 * Recognized options are :
27 * --no-commit: don't actually commit the merge.
28 *
29 */
30
31 typedef struct {
32 const char **heads;
33 size_t heads_count;
34
35 git_annotated_commit **annotated;
36 size_t annotated_count;
37
38 int no_commit : 1;
39 } merge_options;
40
print_usage(void)41 static void print_usage(void)
42 {
43 fprintf(stderr, "usage: merge [--no-commit] <commit...>\n");
44 exit(1);
45 }
46
merge_options_init(merge_options * opts)47 static void merge_options_init(merge_options *opts)
48 {
49 memset(opts, 0, sizeof(*opts));
50
51 opts->heads = NULL;
52 opts->heads_count = 0;
53 opts->annotated = NULL;
54 opts->annotated_count = 0;
55 }
56
opts_add_refish(merge_options * opts,const char * refish)57 static void opts_add_refish(merge_options *opts, const char *refish)
58 {
59 size_t sz;
60
61 assert(opts != NULL);
62
63 sz = ++opts->heads_count * sizeof(opts->heads[0]);
64 opts->heads = xrealloc(opts->heads, sz);
65 opts->heads[opts->heads_count - 1] = refish;
66 }
67
parse_options(const char ** repo_path,merge_options * opts,int argc,char ** argv)68 static void parse_options(const char **repo_path, merge_options *opts, int argc, char **argv)
69 {
70 struct args_info args = ARGS_INFO_INIT;
71
72 if (argc <= 1)
73 print_usage();
74
75 for (args.pos = 1; args.pos < argc; ++args.pos) {
76 const char *curr = argv[args.pos];
77
78 if (curr[0] != '-') {
79 opts_add_refish(opts, curr);
80 } else if (!strcmp(curr, "--no-commit")) {
81 opts->no_commit = 1;
82 } else if (match_str_arg(repo_path, &args, "--git-dir")) {
83 continue;
84 } else {
85 print_usage();
86 }
87 }
88 }
89
resolve_refish(git_annotated_commit ** commit,git_repository * repo,const char * refish)90 static int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish)
91 {
92 git_reference *ref;
93 int err = 0;
94 git_oid oid;
95
96 assert(commit != NULL);
97
98 err = git_reference_dwim(&ref, repo, refish);
99 if (err == GIT_OK) {
100 git_annotated_commit_from_ref(commit, repo, ref);
101 git_reference_free(ref);
102 return 0;
103 }
104
105 err = git_oid_fromstr(&oid, refish);
106 if (err == GIT_OK) {
107 err = git_annotated_commit_lookup(commit, repo, &oid);
108 }
109
110 return err;
111 }
112
resolve_heads(git_repository * repo,merge_options * opts)113 static int resolve_heads(git_repository *repo, merge_options *opts)
114 {
115 git_annotated_commit **annotated = calloc(opts->heads_count, sizeof(git_annotated_commit *));
116 size_t annotated_count = 0, i;
117 int err = 0;
118
119 for (i = 0; i < opts->heads_count; i++) {
120 err = resolve_refish(&annotated[annotated_count++], repo, opts->heads[i]);
121 if (err != 0) {
122 fprintf(stderr, "failed to resolve refish %s: %s\n", opts->heads[i], giterr_last()->message);
123 annotated_count--;
124 continue;
125 }
126 }
127
128 if (annotated_count != opts->heads_count) {
129 fprintf(stderr, "unable to parse some refish\n");
130 free(annotated);
131 return -1;
132 }
133
134 opts->annotated = annotated;
135 opts->annotated_count = annotated_count;
136 return 0;
137 }
138
perform_fastforward(git_repository * repo,const git_oid * target_oid,int is_unborn)139 static int perform_fastforward(git_repository *repo, const git_oid *target_oid, int is_unborn)
140 {
141 git_checkout_options ff_checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
142 git_reference *target_ref;
143 git_reference *new_target_ref;
144 git_object *target = NULL;
145 int err = 0;
146
147 if (is_unborn) {
148 const char *symbolic_ref;
149 git_reference *head_ref;
150
151 /* HEAD reference is unborn, lookup manually so we don't try to resolve it */
152 err = git_reference_lookup(&head_ref, repo, "HEAD");
153 if (err != 0) {
154 fprintf(stderr, "failed to lookup HEAD ref\n");
155 return -1;
156 }
157
158 /* Grab the reference HEAD should be pointing to */
159 symbolic_ref = git_reference_symbolic_target(head_ref);
160
161 /* Create our master reference on the target OID */
162 err = git_reference_create(&target_ref, repo, symbolic_ref, target_oid, 0, NULL);
163 if (err != 0) {
164 fprintf(stderr, "failed to create master reference\n");
165 return -1;
166 }
167
168 git_reference_free(head_ref);
169 } else {
170 /* HEAD exists, just lookup and resolve */
171 err = git_repository_head(&target_ref, repo);
172 if (err != 0) {
173 fprintf(stderr, "failed to get HEAD reference\n");
174 return -1;
175 }
176 }
177
178 /* Lookup the target object */
179 err = git_object_lookup(&target, repo, target_oid, GIT_OBJ_COMMIT);
180 if (err != 0) {
181 fprintf(stderr, "failed to lookup OID %s\n", git_oid_tostr_s(target_oid));
182 return -1;
183 }
184
185 /* Checkout the result so the workdir is in the expected state */
186 ff_checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
187 err = git_checkout_tree(repo, target, &ff_checkout_options);
188 if (err != 0) {
189 fprintf(stderr, "failed to checkout HEAD reference\n");
190 return -1;
191 }
192
193 /* Move the target reference to the target OID */
194 err = git_reference_set_target(&new_target_ref, target_ref, target_oid, NULL);
195 if (err != 0) {
196 fprintf(stderr, "failed to move HEAD reference\n");
197 return -1;
198 }
199
200 git_reference_free(target_ref);
201 git_reference_free(new_target_ref);
202 git_object_free(target);
203
204 return 0;
205 }
206
output_conflicts(git_index * index)207 static void output_conflicts(git_index *index)
208 {
209 git_index_conflict_iterator *conflicts;
210 const git_index_entry *ancestor;
211 const git_index_entry *our;
212 const git_index_entry *their;
213 int err = 0;
214
215 check_lg2(git_index_conflict_iterator_new(&conflicts, index), "failed to create conflict iterator", NULL);
216
217 while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) {
218 fprintf(stderr, "conflict: a:%s o:%s t:%s\n",
219 ancestor ? ancestor->path : "NULL",
220 our->path ? our->path : "NULL",
221 their->path ? their->path : "NULL");
222 }
223
224 if (err != GIT_ITEROVER) {
225 fprintf(stderr, "error iterating conflicts\n");
226 }
227
228 git_index_conflict_iterator_free(conflicts);
229 }
230
create_merge_commit(git_repository * repo,git_index * index,merge_options * opts)231 static int create_merge_commit(git_repository *repo, git_index *index, merge_options *opts)
232 {
233 git_oid tree_oid, commit_oid;
234 git_tree *tree;
235 git_signature *sign;
236 git_reference *merge_ref = NULL;
237 git_annotated_commit *merge_commit;
238 git_reference *head_ref;
239 git_commit **parents = calloc(opts->annotated_count + 1, sizeof(git_commit *));
240 const char *msg_target = NULL;
241 size_t msglen = 0;
242 char *msg;
243 size_t i;
244 int err;
245
246 /* Grab our needed references */
247 check_lg2(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL);
248 if (resolve_refish(&merge_commit, repo, opts->heads[0])) {
249 fprintf(stderr, "failed to resolve refish %s", opts->heads[0]);
250 return -1;
251 }
252
253 /* Maybe that's a ref, so DWIM it */
254 err = git_reference_dwim(&merge_ref, repo, opts->heads[0]);
255 check_lg2(err, "failed to DWIM reference", giterr_last()->message);
256
257 /* Grab a signature */
258 check_lg2(git_signature_now(&sign, "Me", "me@example.com"), "failed to create signature", NULL);
259
260 #define MERGE_COMMIT_MSG "Merge %s '%s'"
261 /* Prepare a standard merge commit message */
262 if (merge_ref != NULL) {
263 check_lg2(git_branch_name(&msg_target, merge_ref), "failed to get branch name of merged ref", NULL);
264 } else {
265 msg_target = git_oid_tostr_s(git_annotated_commit_id(merge_commit));
266 }
267
268 msglen = snprintf(NULL, 0, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
269 if (msglen > 0) msglen++;
270 msg = malloc(msglen);
271 err = snprintf(msg, msglen, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
272
273 /* This is only to silence the compiler */
274 if (err < 0) goto cleanup;
275
276 /* Setup our parent commits */
277 err = git_reference_peel((git_object **)&parents[0], head_ref, GIT_OBJ_COMMIT);
278 check_lg2(err, "failed to peel head reference", NULL);
279 for (i = 0; i < opts->annotated_count; i++) {
280 git_commit_lookup(&parents[i + 1], repo, git_annotated_commit_id(opts->annotated[i]));
281 }
282
283 /* Prepare our commit tree */
284 check_lg2(git_index_write_tree(&tree_oid, index), "failed to write merged tree", NULL);
285 check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "failed to lookup tree", NULL);
286
287 /* Commit time ! */
288 err = git_commit_create(&commit_oid,
289 repo, git_reference_name(head_ref),
290 sign, sign,
291 NULL, msg,
292 tree,
293 opts->annotated_count + 1, (const git_commit **)parents);
294 check_lg2(err, "failed to create commit", NULL);
295
296 /* We're done merging, cleanup the repository state */
297 git_repository_state_cleanup(repo);
298
299 cleanup:
300 free(parents);
301 return err;
302 }
303
main(int argc,char ** argv)304 int main(int argc, char **argv)
305 {
306 git_repository *repo = NULL;
307 merge_options opts;
308 git_index *index;
309 git_repository_state_t state;
310 git_merge_analysis_t analysis;
311 git_merge_preference_t preference;
312 const char *path = ".";
313 int err = 0;
314
315 merge_options_init(&opts);
316 parse_options(&path, &opts, argc, argv);
317
318 git_libgit2_init();
319
320 check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
321 "Could not open repository", NULL);
322
323 state = git_repository_state(repo);
324 if (state != GIT_REPOSITORY_STATE_NONE) {
325 fprintf(stderr, "repository is in unexpected state %d\n", state);
326 goto cleanup;
327 }
328
329 err = resolve_heads(repo, &opts);
330 if (err != 0)
331 goto cleanup;
332
333 err = git_merge_analysis(&analysis, &preference,
334 repo,
335 (const git_annotated_commit **)opts.annotated,
336 opts.annotated_count);
337 check_lg2(err, "merge analysis failed", NULL);
338
339 if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
340 printf("Already up-to-date\n");
341 return 0;
342 } else if (analysis & GIT_MERGE_ANALYSIS_UNBORN ||
343 (analysis & GIT_MERGE_ANALYSIS_FASTFORWARD &&
344 !(preference & GIT_MERGE_PREFERENCE_NO_FASTFORWARD))) {
345 const git_oid *target_oid;
346 if (analysis & GIT_MERGE_ANALYSIS_UNBORN) {
347 printf("Unborn\n");
348 } else {
349 printf("Fast-forward\n");
350 }
351
352 /* Since this is a fast-forward, there can be only one merge head */
353 target_oid = git_annotated_commit_id(opts.annotated[0]);
354 assert(opts.annotated_count == 1);
355
356 return perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN));
357 } else if (analysis & GIT_MERGE_ANALYSIS_NORMAL) {
358 git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
359 git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
360
361 merge_opts.flags = 0;
362 merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3;
363
364 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS;
365
366 if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) {
367 printf("Fast-forward is preferred, but only a merge is possible\n");
368 return -1;
369 }
370
371 err = git_merge(repo,
372 (const git_annotated_commit **)opts.annotated, opts.annotated_count,
373 &merge_opts, &checkout_opts);
374 check_lg2(err, "merge failed", NULL);
375 }
376
377 /* If we get here, we actually performed the merge above */
378
379 check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL);
380
381 if (git_index_has_conflicts(index)) {
382 /* Handle conflicts */
383 output_conflicts(index);
384 } else if (!opts.no_commit) {
385 create_merge_commit(repo, index, &opts);
386 printf("Merge made\n");
387 }
388
389 cleanup:
390 free(opts.heads);
391 free(opts.annotated);
392 git_repository_free(repo);
393 git_libgit2_shutdown();
394
395 return 0;
396 }
397