Skip to content

Commit 59ea2c5

Browse files
committed
examples: add merge
1 parent bb9353c commit 59ea2c5

File tree

1 file changed

+391
-0
lines changed

1 file changed

+391
-0
lines changed

examples/merge.c

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

0 commit comments

Comments
 (0)