examples: hoist the merge analysis back into main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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);