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