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