тянуть с помощью libgit2, c ++ - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь перейти от удаленного мастера к локальному. В удаленном мастере только одна несинхронизированная фиксация.

Ошибка в методе git_annotated_commit_lookup ():

Git Ошибка -3: объект не найден - нет совпадения для идентификатора (08f4a8cc00400100f083caccd755000020299210)

В обратном вызове fetchhead_ref_cb никогда не выполняйте код в блоке «если».


    int fetchhead_ref_cb(const char *name, const char *url,
       const git_oid *oid, unsigned int is_merge, void *payload)
    
    {
       qDebug() << "fetchhead_ref_cb";
       if (is_merge)
       {
            qDebug() << "Is merge";
            git_oid_cpy((git_oid *)payload, oid);
       }
       return 0;
    }

    bool pullBranch()
    {
        int error;
        git_remote *remote;
        git_oid branchOidToMerge;
            
        /* lookup the remote */
        error = git_remote_lookup(&remote, repo, "origin");
        if (!checkForError(error, "Remote lookup")) {
            git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
            options.callbacks.credentials = cred_acquire_cb;
            error = git_remote_fetch(remote,
                                     NULL, /* refspecs, NULL to use the configured ones */
                                     &options, /* options, empty for defaults */
                                     "pull"); /* reflog mesage, usually "fetch" or "pull", you can leave it NULL for "fetch" */
            if (!checkForError(error, "Remote fetch")) {
    
                git_repository_fetchhead_foreach(repo, fetchhead_ref_cb, &branchOidToMerge);
    
                git_merge_options merge_options = GIT_MERGE_OPTIONS_INIT;
                git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
                git_annotated_commit *commit;
    
                error = git_annotated_commit_lookup(&commit, repo, &branchOidToMerge);
                if (!checkForError(error, "Annotated commit lookup")) {
                    error = git_merge(repo, (const git_annotated_commit **)commit, 1, &merge_options, &checkout_options);
                    if (!checkForError(error, "Merge")) {
                        git_annotated_commit_free(commit);
                        git_repository_state_cleanup(repo);
                        git_remote_free(remote);
                        return true;
                    }
                }
            }
        }
        git_remote_free(remote);
        return false;
    }

     

1 Ответ

0 голосов
/ 20 июля 2020

Решение для быстрого слияния:

GitPullStatus GitWizard::pullBranch()
{
    git_remote *remote;

    int error = git_remote_lookup(&remote, repo, "origin");
    if (!checkForError(error, "Remote lookup")) {
        git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
        options.callbacks.credentials = cred_acquire_cb;
        error = git_remote_fetch(remote,
                                 NULL, /* refspecs, NULL to use the configured ones */
                                 &options, /* options, empty for defaults */
                                 "pull"); /* reflog mesage, usually "fetch" or "pull", you can leave it NULL for "fetch" */
        if (!checkForError(error, "Remote fetch")) {

            git_oid branchOidToMerge;

            git_repository_fetchhead_foreach(repo, fetchhead_ref_cb, &branchOidToMerge);
            git_annotated_commit *their_heads[1];

            error = git_annotated_commit_lookup(&their_heads[0], repo, &branchOidToMerge);
            checkForError(error, "Annotated commit lookup");

                git_merge_analysis_t anout;
                git_merge_preference_t pout;

                qDebug() << "Try analysis";

                error = git_merge_analysis(&anout, &pout, repo, (const git_annotated_commit **) their_heads, 1);

                checkForError(error, "Merge analysis");

                if (anout & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
                    qDebug() << "up to date";
                    git_annotated_commit_free(their_heads[0]);
                    git_repository_state_cleanup(repo);
                    git_remote_free(remote);
                    return GitPullStatus::GIT_UP_TO_DATE;
                } else if (anout & GIT_MERGE_ANALYSIS_FASTFORWARD) {
                    qDebug() << "fast-forwarding";

                    git_reference *ref;
                    git_reference *newref;

                    const char *name = QString("refs/heads/").append(mCurrentBranch).toLocal8Bit().data();

                    if (git_reference_lookup(&ref, repo, name) == 0)
                        git_reference_set_target(&newref, ref, &branchOidToMerge, "pull: Fast-forward");

                    git_reset_from_annotated(repo, their_heads[0], GIT_RESET_HARD, NULL);

                    git_reference_free(ref);
                    git_repository_state_cleanup(repo);
                }

                git_annotated_commit_free(their_heads[0]);
                git_repository_state_cleanup(repo);
                git_remote_free(remote);
                return GitPullStatus::GIT_PULL_OK;
        }
    }
    git_remote_free(remote);
    return GitPullStatus::GIT_PULL_ERROR;
}
...