Commit 503b30d52679c85c762fe69020383e5a3fa71faf

Etienne Samson 2018-01-17T02:25:36

examples: hoist the merge analysis back into main

diff --git a/examples/merge.c b/examples/merge.c
index 6d516ca..6bcae7a 100644
--- a/examples/merge.c
+++ b/examples/merge.c
@@ -36,7 +36,6 @@ typedef struct {
 	size_t annotated_count;
 
 	int no_commit : 1;
-	int did_merge : 1;
 } merge_options;
 
 static void print_usage(void)
@@ -203,23 +202,39 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid, 
 	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_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
+	git_repository *repo = NULL;
+	merge_options opts;
+	git_index *index;
+	git_repository_state_t state;
 	git_merge_analysis_t analysis;
 	git_merge_preference_t preference;
+	const char *path = ".";
 	int err = 0;
 
-	merge_opts.flags = 0;
-	merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3;
+	merge_options_init(&opts);
+	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,
 	                         repo,
-	                         (const git_annotated_commit **)opts->annotated,
-	                         opts->annotated_count);
+	                         (const git_annotated_commit **)opts.annotated,
+	                         opts.annotated_count);
 	check_lg2(err, "merge analysis failed", NULL);
 
 	if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
@@ -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 */
-		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));
 	} 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) {
 			printf("Fast-forward is preferred, but only a merge is possible\n");
 			return -1;
 		}
 
 		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);
-		if (err != 0)
-			return -1;
-
-		/* Inform that a merge was done */
-		opts->did_merge = 1;
-
-		return 0;
+		check_lg2(err, "merge failed", NULL);
 	}
 
-	return -1;
-}
-
-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;
-	}
+	/* If we get here, we actually performed the merge above */
 
 	check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL);
 
@@ -382,6 +361,8 @@ int main(int argc, char **argv)
 
 		/* We're done merging, cleanup the repository state */
 		git_repository_state_cleanup(repo);
+
+		printf("Merge made\n");
 	}
 cleanup:
 	free(opts.heads);