examples: move support code into static functions
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
diff --git a/examples/merge.c b/examples/merge.c
index 6bcae7a..15df7d0 100644
--- a/examples/merge.c
+++ b/examples/merge.c
@@ -202,6 +202,100 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid,
return 0;
}
+static void output_conflicts(git_index *index)
+{
+ /* Handle conflicts */
+ git_index_conflict_iterator *conflicts;
+ const git_index_entry *ancestor;
+ const git_index_entry *our;
+ const git_index_entry *their;
+ int err = 0;
+
+ check_lg2(git_index_conflict_iterator_new(&conflicts, index), "failed to create conflict iterator", NULL);
+
+ while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) {
+ fprintf(stderr, "conflict: a:%s o:%s t:%s\n",
+ ancestor ? ancestor->path : "", our->path, their->path);
+ }
+
+ if (err != GIT_ITEROVER) {
+ fprintf(stderr, "error iterating conflicts\n");
+ }
+
+ git_index_conflict_iterator_free(conflicts);
+}
+
+static int create_merge_commit(git_repository *repo, git_index *index, merge_options *opts)
+{
+ git_oid tree_oid, commit_oid;
+ git_tree *tree;
+ git_signature *sign;
+ git_reference *merge_ref = NULL;
+ git_annotated_commit *merge_commit;
+ git_reference *head_ref;
+ git_commit **parents = calloc(opts->annotated_count + 1, sizeof(git_commit *));
+ const char *msg_target = NULL;
+ size_t msglen = 0;
+ char *msg;
+ size_t i;
+ int err;
+
+ /* Grab our needed references */
+ check_lg2(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL);
+ if (resolve_refish(&merge_commit, repo, opts->heads[0])) {
+ fprintf(stderr, "failed to resolve refish %s", opts->heads[0]);
+ }
+
+ /* Maybe that's a ref, so DWIM it */
+ err = git_reference_dwim(&merge_ref, repo, opts->heads[0]);
+ check_lg2(err, "failed to DWIM reference", giterr_last()->message);
+
+ /* Grab a signature */
+ check_lg2(git_signature_now(&sign, "Me", "me@example.com"), "failed to create signature", NULL);
+
+#define MERGE_COMMIT_MSG "Merge %s '%s'"
+ /* Prepare a standard merge commit message */
+ if (merge_ref != NULL) {
+ check_lg2(git_branch_name(&msg_target, merge_ref), "failed to get branch name of merged ref", NULL);
+ } else {
+ msg_target = git_oid_tostr_s(git_annotated_commit_id(merge_commit));
+ }
+
+ msglen = snprintf(NULL, 0, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
+ if (msglen > 0) msglen++;
+ msg = malloc(msglen);
+ err = snprintf(msg, msglen, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
+
+ /* This is only to silence the compiler */
+ if (err < 0) goto cleanup;
+
+ /* Setup our parent commits */
+ err = git_reference_peel((git_object **)&parents[0], head_ref, GIT_OBJ_COMMIT);
+ check_lg2(err, "failed to peel head reference", NULL);
+ for (i = 0; i < opts->annotated_count; i++) {
+ git_commit_lookup(&parents[i + 1], repo, git_annotated_commit_id(opts->annotated[i]));
+ }
+
+ /* Prepare our commit tree */
+ check_lg2(git_index_write_tree(&tree_oid, index), "failed to write merged tree", NULL);
+ check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "failed to lookup tree", NULL);
+
+ /* Commit time ! */
+ err = git_commit_create(&commit_oid,
+ repo, git_reference_name(head_ref),
+ sign, sign,
+ NULL, msg,
+ tree,
+ opts->annotated_count + 1, (const git_commit **)parents);
+ check_lg2(err, "failed to create commit", NULL);
+
+ /* We're done merging, cleanup the repository state */
+ git_repository_state_cleanup(repo);
+
+cleanup:
+ return err;
+}
+
int main(int argc, char **argv)
{
git_repository *repo = NULL;
@@ -280,90 +374,12 @@ int main(int argc, char **argv)
check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL);
if (git_index_has_conflicts(index)) {
- /* Handle conflicts */
- git_index_conflict_iterator *conflicts;
- const git_index_entry *ancestor;
- const git_index_entry *our;
- const git_index_entry *their;
-
- check_lg2(git_index_conflict_iterator_new(&conflicts, index), "failed to create conflict iterator", NULL);
-
- while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) {
- fprintf(stderr, "conflict: a:%s o:%s t:%s\n",
- ancestor ? ancestor->path : "", our->path, their->path);
- }
-
- if (err != GIT_ITEROVER) {
- fprintf(stderr, "error iterating conflicts\n");
- }
-
- git_index_conflict_iterator_free(conflicts);
+ output_conflicts(index);
} else if (!opts.no_commit) {
- git_oid tree_oid, commit_oid;
- git_tree *tree;
- git_signature *sign;
- git_reference *merge_ref = NULL;
- git_annotated_commit *merge_commit;
- git_reference *head_ref;
- git_commit **parents = calloc(opts.annotated_count + 1, sizeof(git_commit *));
- const char *msg_target = NULL;
- size_t msglen = 0;
- char *msg;
- size_t i;
- int err;
-
- /* Grab our needed references */
- check_lg2(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL);
- if (resolve_refish(&merge_commit, repo, opts.heads[0])) {
- fprintf(stderr, "failed to resolve refish %s", opts.heads[0]);
- }
-
- /* Maybe that's a ref, so DWIM it */
- git_reference_dwim(&merge_ref, repo, opts.heads[0]);
-
- /* Grab a signature */
- check_lg2(git_signature_now(&sign, "Me", "me@example.com"), "failed to create signature", NULL);
-
-#define MERGE_COMMIT_MSG "Merge %s '%s'\n"
- /* Prepare a standard merge commit message */
- if (merge_ref != NULL) {
- check_lg2(git_branch_name(&msg_target, merge_ref), "failed to get branch name of merged ref", NULL);
- } else {
- msg_target = git_oid_tostr_s(git_annotated_commit_id(merge_commit));
- }
-
- msglen = snprintf(NULL, 0, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
- if (msglen > 0) msglen++;
- msg = malloc(msglen);
- err = snprintf(msg, msglen, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
-
- /* This is only to silence the compiler */
- if (err < 0) goto cleanup;
-
- /* Setup our parent commits */
- check_lg2(git_reference_peel((git_object **)&parents[0], head_ref, GIT_OBJ_COMMIT), "failed to peel head reference", NULL);
- for (i = 0; i < opts.annotated_count; i++) {
- git_commit_lookup(&parents[i + 1], repo, git_annotated_commit_id(opts.annotated[i]));
- }
-
- /* Prepare our commit tree */
- check_lg2(git_index_write_tree(&tree_oid, index), "failed to write merged tree", NULL);
- check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "failed to lookup tree", NULL);
-
- /* Commit time ! */
- err = git_commit_create(&commit_oid,
- repo, git_reference_name(head_ref),
- sign, sign,
- NULL, msg,
- tree,
- opts.annotated_count + 1, (const git_commit **)parents);
- check_lg2(err, "failed to create commit", NULL);
-
- /* We're done merging, cleanup the repository state */
- git_repository_state_cleanup(repo);
-
+ create_merge_commit(repo, index, &opts);
printf("Merge made\n");
}
+
cleanup:
free(opts.heads);
free(opts.annotated);