format code

This commit is contained in:
Gerhard Hoffmann 2024-04-05 12:44:34 +02:00
parent a3cbb20a53
commit fe46da5417

View File

@ -388,8 +388,7 @@ static void opts_add_refish(struct merge_options *opts, const char *refish) {
} }
} }
static int resolve_heads(git_repository *repo, struct merge_options *opts) static int resolve_heads(git_repository *repo, struct merge_options *opts) {
{
git_annotated_commit **annotated = (git_annotated_commit **)calloc(opts->heads_count, sizeof(git_annotated_commit *)); git_annotated_commit **annotated = (git_annotated_commit **)calloc(opts->heads_count, sizeof(git_annotated_commit *));
size_t annotated_count = 0, i; size_t annotated_count = 0, i;
int err = 0; int err = 0;
@ -414,8 +413,7 @@ static int resolve_heads(git_repository *repo, struct merge_options *opts)
return 0; return 0;
} }
static int perform_fastforward(git_repository *repo, const git_oid *target_oid, int is_unborn) static int perform_fastforward(git_repository *repo, const git_oid *target_oid, int is_unborn) {
{
// git_checkout_options ff_checkout_options = GIT_CHECKOUT_OPTIONS_INIT; // git_checkout_options ff_checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
git_checkout_options ff_checkout_options; git_checkout_options ff_checkout_options;
@ -433,158 +431,173 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid,
const char *symbolic_ref; const char *symbolic_ref;
git_reference *head_ref; git_reference *head_ref;
/* HEAD reference is unborn, lookup manually so we don't try to resolve it */ /* HEAD reference is unborn, lookup manually so we don't try to resolve it */
err = git_reference_lookup(&head_ref, repo, "HEAD"); err = git_reference_lookup(&head_ref, repo, "HEAD");
if (err != 0) { if (err != 0) {
fprintf(stderr, "failed to lookup HEAD ref\n"); qCritical() << HEADER << "failed to lookup HEAD ref";
return -1; return -1;
}
/* Grab the reference HEAD should be pointing to */
symbolic_ref = git_reference_symbolic_target(head_ref);
/* Create our master reference on the target OID */
err = git_reference_create(&target_ref, repo, symbolic_ref, target_oid, 0, NULL);
if (err != 0) {
qCritical() << HEADER << "failed to create master reference";
return -1;
}
git_reference_free(head_ref);
} else {
/* HEAD exists, just lookup and resolve */
err = git_repository_head(&target_ref, repo);
if (err != 0) {
qCritical() << HEADER << "failed to get HEAD reference";
return -1;
}
} }
/* Grab the reference HEAD should be pointing to */ /* Lookup the target object */
symbolic_ref = git_reference_symbolic_target(head_ref); err = git_object_lookup(&target, repo, target_oid, GIT_OBJECT_COMMIT);
/* Create our master reference on the target OID */
err = git_reference_create(&target_ref, repo, symbolic_ref, target_oid, 0, NULL);
if (err != 0) { if (err != 0) {
fprintf(stderr, "failed to create master reference\n"); qCritical() << HEADER << QString("failed to lookup OID %s").arg(git_oid_tostr_s(target_oid));
return -1; return -1;
} }
git_reference_free(head_ref); /* Checkout the result so the workdir is in the expected state */
} else { ff_checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
/* HEAD exists, just lookup and resolve */ err = git_checkout_tree(repo, target, &ff_checkout_options);
err = git_repository_head(&target_ref, repo);
if (err != 0) { if (err != 0) {
fprintf(stderr, "failed to get HEAD reference\n"); qCritical() << HEADER << "failed to checkout HEAD reference";
return -1; return -1;
} }
}
/* Lookup the target object */ /* Move the target reference to the target OID */
err = git_object_lookup(&target, repo, target_oid, GIT_OBJECT_COMMIT); err = git_reference_set_target(&new_target_ref, target_ref, target_oid, NULL);
if (err != 0) { if (err != 0) {
fprintf(stderr, "failed to lookup OID %s\n", git_oid_tostr_s(target_oid)); qCritical() << HEADER << "failed to move HEAD reference";
return -1; return -1;
} }
/* Checkout the result so the workdir is in the expected state */ git_reference_free(target_ref);
ff_checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE; git_reference_free(new_target_ref);
err = git_checkout_tree(repo, target, &ff_checkout_options); git_object_free(target);
if (err != 0) {
fprintf(stderr, "failed to checkout HEAD reference\n");
return -1;
}
/* Move the target reference to the target OID */ return 0;
err = git_reference_set_target(&new_target_ref, target_ref, target_oid, NULL);
if (err != 0) {
fprintf(stderr, "failed to move HEAD reference\n");
return -1;
}
git_reference_free(target_ref);
git_reference_free(new_target_ref);
git_object_free(target);
return 0;
} }
static void output_conflicts(git_index *index) static void output_conflicts(git_index *index) {
{ git_index_conflict_iterator *conflicts;
git_index_conflict_iterator *conflicts; const git_index_entry *ancestor;
const git_index_entry *ancestor; const git_index_entry *our;
const git_index_entry *our; const git_index_entry *their;
const git_index_entry *their; int err = 0;
int err = 0;
check(git_index_conflict_iterator_new(&conflicts, index), "failed to create conflict iterator", NULL); check(git_index_conflict_iterator_new(&conflicts, index), "failed to create conflict iterator", NULL);
while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) { while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) {
fprintf(stderr, "conflict: a:%s o:%s t:%s\n", qCritical() << HEADER
ancestor ? ancestor->path : "NULL", << QString("conflict: a:%s o:%s t:%s")
our->path ? our->path : "NULL", .arg(ancestor ? ancestor->path : "NULL")
their->path ? their->path : "NULL"); .arg(our->path ? our->path : "NULL")
} .arg(their->path ? their->path : "NULL");
}
if (err != GIT_ITEROVER) { if (err != GIT_ITEROVER) {
fprintf(stderr, "error iterating conflicts\n"); qCritical() << HEADER << "error iterating conflicts";
} }
git_index_conflict_iterator_free(conflicts); git_index_conflict_iterator_free(conflicts);
} }
static int create_merge_commit(git_repository *repo, git_index *index, struct merge_options *opts) static int create_merge_commit(git_repository *repo, git_index *index, struct merge_options *opts) {
{ git_oid tree_oid, commit_oid;
git_oid tree_oid, commit_oid; git_tree *tree;
git_tree *tree; git_signature *sign;
git_signature *sign; git_reference *merge_ref = NULL;
git_reference *merge_ref = NULL; git_annotated_commit *merge_commit;
git_annotated_commit *merge_commit; git_reference *head_ref;
git_reference *head_ref; git_commit **parents = (git_commit **)calloc(opts->annotated_count + 1, sizeof(git_commit *));
git_commit **parents = (git_commit **)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 */ if (!parents) {
check(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL); qCritical() << HEADER << 'calloc failed';
if (resolve_refish(&merge_commit, repo, opts->heads[0])) { return -1;
fprintf(stderr, "failed to resolve refish %s", opts->heads[0]); }
free(parents);
return -1; const char *msg_target = NULL;
} size_t msglen = 0;
char *msg;
size_t i;
int err;
/* Maybe that's a ref, so DWIM it */ /* Grab our needed references */
err = git_reference_dwim(&merge_ref, repo, opts->heads[0]); check(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL);
check(err, "failed to DWIM reference", git_error_last()->message); if (resolve_refish(&merge_commit, repo, opts->heads[0])) {
qCritical() << HEADER << QString("failed to resolve refish %s").args(opts->heads[0]);
free(parents);
return -1;
}
/* Grab a signature */ /* Maybe that's a ref, so DWIM it */
check(git_signature_now(&sign, "Me", "me@example.com"), "failed to create signature", NULL); err = git_reference_dwim(&merge_ref, repo, opts->heads[0]);
check(err, "failed to DWIM reference", git_error_last()->message);
/* Grab a signature */
check(git_signature_now(&sign, "Me", "me@example.com"), "failed to create signature", NULL);
#define MERGE_COMMIT_MSG "Merge %s '%s'" #define MERGE_COMMIT_MSG "Merge %s '%s'"
/* Prepare a standard merge commit message */ /* Prepare a standard merge commit message */
if (merge_ref != NULL) { if (merge_ref != NULL) {
check(git_branch_name(&msg_target, merge_ref), "failed to get branch name of merged ref", NULL); check(git_branch_name(&msg_target, merge_ref), "failed to get branch name of merged ref", NULL);
} else { } else {
msg_target = git_oid_tostr_s(git_annotated_commit_id(merge_commit)); 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); msglen = snprintf(NULL, 0, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
if (msglen > 0) msglen++; if (msglen > 0) {
msg = (char *)malloc(msglen); msglen++;
err = snprintf(msg, msglen, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target); }
if ((msg = (char *)malloc(msglen)) == nullptr) {
qCritical() << HEADER << 'malloc failed';
goto cleanup;
}
err = snprintf(msg, msglen, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target);
/* This is only to silence the compiler */ /* This is only to silence the compiler */
if (err < 0) goto cleanup; if (err < 0) {
goto cleanup;
}
/* Setup our parent commits */ /* Setup our parent commits */
err = git_reference_peel((git_object **)&parents[0], head_ref, GIT_OBJECT_COMMIT); err = git_reference_peel((git_object **)&parents[0], head_ref, GIT_OBJECT_COMMIT);
check(err, "failed to peel head reference", NULL); check(err, "failed to peel head reference", NULL);
for (i = 0; i < opts->annotated_count; i++) { for (i = 0; i < opts->annotated_count; i++) {
git_commit_lookup(&parents[i + 1], repo, git_annotated_commit_id(opts->annotated[i])); git_commit_lookup(&parents[i + 1], repo, git_annotated_commit_id(opts->annotated[i]));
} }
/* Prepare our commit tree */ /* Prepare our commit tree */
check(git_index_write_tree(&tree_oid, index), "failed to write merged tree", NULL); check(git_index_write_tree(&tree_oid, index), "failed to write merged tree", NULL);
check(git_tree_lookup(&tree, repo, &tree_oid), "failed to lookup tree", NULL); check(git_tree_lookup(&tree, repo, &tree_oid), "failed to lookup tree", NULL);
/* Commit time ! */ /* Commit time ! */
err = git_commit_create(&commit_oid, err = git_commit_create(&commit_oid,
repo, git_reference_name(head_ref), repo, git_reference_name(head_ref),
sign, sign, sign, sign,
NULL, msg, NULL, msg,
tree, tree,
opts->annotated_count + 1, (const git_commit **)parents); opts->annotated_count + 1, (const git_commit **)parents);
check(err, "failed to create commit", NULL);
check(err, "failed to create commit", NULL);
/* 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);
cleanup: cleanup:
free(parents); free(parents);
return err; return err;
} }
static int merge(git_repository *repo) { static int merge(git_repository *repo) {