Skip to content

Commit 503b30d

Browse files
committed
examples: hoist the merge analysis back into main
1 parent 60c6547 commit 503b30d

File tree

1 file changed

+39
-58
lines changed

1 file changed

+39
-58
lines changed

examples/merge.c

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ typedef struct {
3636
size_t annotated_count;
3737

3838
int no_commit : 1;
39-
int did_merge : 1;
4039
} merge_options;
4140

4241
static void print_usage(void)
@@ -203,23 +202,39 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid,
203202
return 0;
204203
}
205204

206-
static int analyze_merge(git_repository *repo, merge_options *opts)
205+
int main(int argc, char **argv)
207206
{
208-
git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
209-
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
207+
git_repository *repo = NULL;
208+
merge_options opts;
209+
git_index *index;
210+
git_repository_state_t state;
210211
git_merge_analysis_t analysis;
211212
git_merge_preference_t preference;
213+
const char *path = ".";
212214
int err = 0;
213215

214-
merge_opts.flags = 0;
215-
merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3;
216+
merge_options_init(&opts);
217+
parse_options(&path, &opts, argc, argv);
216218

217-
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS;
219+
git_libgit2_init();
220+
221+
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
222+
"Could not open repository", NULL);
223+
224+
state = git_repository_state(repo);
225+
if (state != GIT_REPOSITORY_STATE_NONE) {
226+
fprintf(stderr, "repository is in unexpected state %d\n", state);
227+
goto cleanup;
228+
}
229+
230+
err = resolve_heads(repo, &opts);
231+
if (err != 0)
232+
goto cleanup;
218233

219234
err = git_merge_analysis(&analysis, &preference,
220235
repo,
221-
(const git_annotated_commit **)opts->annotated,
222-
opts->annotated_count);
236+
(const git_annotated_commit **)opts.annotated,
237+
opts.annotated_count);
223238
check_lg2(err, "merge analysis failed", NULL);
224239

225240
if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
@@ -236,67 +251,31 @@ static int analyze_merge(git_repository *repo, merge_options *opts)
236251
}
237252

238253
/* Since this is a fast-forward, there can be only one merge head */
239-
target_oid = git_annotated_commit_id(opts->annotated[0]);
254+
target_oid = git_annotated_commit_id(opts.annotated[0]);
255+
assert(opts.annotated_count == 1);
240256

241257
return perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN));
242258
} else if (analysis & GIT_MERGE_ANALYSIS_NORMAL) {
259+
git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
260+
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
261+
262+
merge_opts.flags = 0;
263+
merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3;
264+
265+
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS;
266+
243267
if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) {
244268
printf("Fast-forward is preferred, but only a merge is possible\n");
245269
return -1;
246270
}
247271

248272
err = git_merge(repo,
249-
(const git_annotated_commit **)opts->annotated, opts->annotated_count,
273+
(const git_annotated_commit **)opts.annotated, opts.annotated_count,
250274
&merge_opts, &checkout_opts);
251-
if (err != 0)
252-
return -1;
253-
254-
/* Inform that a merge was done */
255-
opts->did_merge = 1;
256-
257-
return 0;
275+
check_lg2(err, "merge failed", NULL);
258276
}
259277

260-
return -1;
261-
}
262-
263-
int main(int argc, char **argv)
264-
{
265-
git_repository *repo = NULL;
266-
merge_options opts;
267-
git_index *index;
268-
git_repository_state_t state;
269-
const char *path = ".";
270-
int err = 0;
271-
272-
merge_options_init(&opts);
273-
parse_options(&path, &opts, argc, argv);
274-
275-
git_libgit2_init();
276-
277-
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
278-
"Could not open repository", NULL);
279-
280-
state = git_repository_state(repo);
281-
if (state != GIT_REPOSITORY_STATE_NONE) {
282-
fprintf(stderr, "repository is in unexpected state %d\n", state);
283-
goto cleanup;
284-
}
285-
286-
err = resolve_heads(repo, &opts);
287-
if (err != 0)
288-
goto cleanup;
289-
290-
err = analyze_merge(repo, &opts);
291-
if (err != 0) {
292-
fprintf(stderr, "merge failed\n");
293-
goto cleanup;
294-
}
295-
296-
if (!opts.did_merge) {
297-
/* Was either up-to-date, unborn, or a fast-forward, nothing left to do */
298-
goto cleanup;
299-
}
278+
/* If we get here, we actually performed the merge above */
300279

301280
check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL);
302281

@@ -382,6 +361,8 @@ int main(int argc, char **argv)
382361

383362
/* We're done merging, cleanup the repository state */
384363
git_repository_state_cleanup(repo);
364+
365+
printf("Merge made\n");
385366
}
386367
cleanup:
387368
free(opts.heads);

0 commit comments

Comments
 (0)