Commit 503b30d5 by Etienne Samson

examples: hoist the merge analysis back into main

parent 60c6547c
...@@ -36,7 +36,6 @@ typedef struct { ...@@ -36,7 +36,6 @@ typedef struct {
size_t annotated_count; size_t annotated_count;
int no_commit : 1; int no_commit : 1;
int did_merge : 1;
} merge_options; } merge_options;
static void print_usage(void) static void print_usage(void)
...@@ -203,23 +202,39 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid, ...@@ -203,23 +202,39 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid,
return 0; return 0;
} }
static int analyze_merge(git_repository *repo, merge_options *opts) int main(int argc, char **argv)
{ {
git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; git_repository *repo = NULL;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; merge_options opts;
git_index *index;
git_repository_state_t state;
git_merge_analysis_t analysis; git_merge_analysis_t analysis;
git_merge_preference_t preference; git_merge_preference_t preference;
const char *path = ".";
int err = 0; int err = 0;
merge_opts.flags = 0; merge_options_init(&opts);
merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3; parse_options(&path, &opts, argc, argv);
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS; git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
"Could not open repository", NULL);
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state);
goto cleanup;
}
err = resolve_heads(repo, &opts);
if (err != 0)
goto cleanup;
err = git_merge_analysis(&analysis, &preference, err = git_merge_analysis(&analysis, &preference,
repo, repo,
(const git_annotated_commit **)opts->annotated, (const git_annotated_commit **)opts.annotated,
opts->annotated_count); opts.annotated_count);
check_lg2(err, "merge analysis failed", NULL); check_lg2(err, "merge analysis failed", NULL);
if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) { if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
...@@ -236,67 +251,31 @@ static int analyze_merge(git_repository *repo, merge_options *opts) ...@@ -236,67 +251,31 @@ static int analyze_merge(git_repository *repo, merge_options *opts)
} }
/* Since this is a fast-forward, there can be only one merge head */ /* Since this is a fast-forward, there can be only one merge head */
target_oid = git_annotated_commit_id(opts->annotated[0]); target_oid = git_annotated_commit_id(opts.annotated[0]);
assert(opts.annotated_count == 1);
return perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN)); return perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN));
} else if (analysis & GIT_MERGE_ANALYSIS_NORMAL) { } else if (analysis & GIT_MERGE_ANALYSIS_NORMAL) {
git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
merge_opts.flags = 0;
merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3;
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS;
if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) { if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) {
printf("Fast-forward is preferred, but only a merge is possible\n"); printf("Fast-forward is preferred, but only a merge is possible\n");
return -1; return -1;
} }
err = git_merge(repo, err = git_merge(repo,
(const git_annotated_commit **)opts->annotated, opts->annotated_count, (const git_annotated_commit **)opts.annotated, opts.annotated_count,
&merge_opts, &checkout_opts); &merge_opts, &checkout_opts);
if (err != 0) check_lg2(err, "merge failed", NULL);
return -1;
/* Inform that a merge was done */
opts->did_merge = 1;
return 0;
} }
return -1; /* If we get here, we actually performed the merge above */
}
int main(int argc, char **argv)
{
git_repository *repo = NULL;
merge_options opts;
git_index *index;
git_repository_state_t state;
const char *path = ".";
int err = 0;
merge_options_init(&opts);
parse_options(&path, &opts, argc, argv);
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
"Could not open repository", NULL);
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state);
goto cleanup;
}
err = resolve_heads(repo, &opts);
if (err != 0)
goto cleanup;
err = analyze_merge(repo, &opts);
if (err != 0) {
fprintf(stderr, "merge failed\n");
goto cleanup;
}
if (!opts.did_merge) {
/* Was either up-to-date, unborn, or a fast-forward, nothing left to do */
goto cleanup;
}
check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL); check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL);
...@@ -382,6 +361,8 @@ int main(int argc, char **argv) ...@@ -382,6 +361,8 @@ int main(int argc, char **argv)
/* We're done merging, cleanup the repository state */ /* We're done merging, cleanup the repository state */
git_repository_state_cleanup(repo); git_repository_state_cleanup(repo);
printf("Merge made\n");
} }
cleanup: cleanup:
free(opts.heads); free(opts.heads);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment