[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/QuantStack/git2cpp/0.0.12/src/subcommand/rebase_subcommand.cpp [Back]  [Original]

#include "rebase_subcommand.hpp"

#include 

#include 
#include 

#include "../utils/git_exception.hpp"
#include "../wrapper/index_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/signature_wrapper.hpp"

rebase_subcommand::rebase_subcommand(const libgit2_object&, CLI::App& app)
{
    auto* sub = app.add_subcommand("rebase", "Reapply commits on top of another base tip");

    sub->add_option("upstream", m_upstream, "Upstream branch to rebase onto");
    sub->add_option("branch", m_branch, "Working branch; defaults to HEAD");
    sub->add_option("--onto", m_onto, "Starting point at which to create the new commits");

    sub->add_flag("--abort", m_abort, "Abort the rebase operation and reset HEAD to the original branch");
    sub->add_flag("--continue", m_continue, "Restart the rebasing process after having resolved a merge conflict");
    sub->add_flag("--skip", m_skip, "Restart the rebasing process by skipping the current patch");
    sub->add_flag("--quit", m_quit, "Abort the rebase operation but HEAD is not reset back to the original branch");

    sub->callback(
        [this]()
        {
            this->run();
        }
    );
}

namespace
{
    void ensure_rebase_in_progress(git_repository_state_t state)
    {
        if (state != GIT_REPOSITORY_STATE_REBASE_INTERACTIVE && state != GIT_REPOSITORY_STATE_REBASE_MERGE)
        {
            throw std::runtime_error("No rebase in progress");
        }
    }
}

void rebase_subcommand::run()
{
    auto directory = get_current_git_path();
    auto repo = repository_wrapper::open(directory);

    git_repository_state_t state = repo.state();

    if (m_abort)
    {
        ensure_rebase_in_progress(state);
        run_abort(repo);
        return;
    }

    if (m_continue)
    {
        ensure_rebase_in_progress(state);
        run_continue(repo);
        return;
    }

    if (m_skip)
    {
        ensure_rebase_in_progress(state);
        run_skip(repo);
        return;
    }

    if (m_quit)
    {
        ensure_rebase_in_progress(state);
        run_quit(repo);
        return;
    }

    if (state == GIT_REPOSITORY_STATE_REBASE_INTERACTIVE || state == GIT_REPOSITORY_STATE_REBASE_MERGE)
    {
        throw std::runtime_error("A rebase is already in progress");
    }

    if (state != GIT_REPOSITORY_STATE_NONE)
    {
        throw std::runtime_error("Cannot rebase:  repository is in unexpected state");
    }

    run_rebase(repo);
}

annotated_commit_wrapper
rebase_subcommand::resolve_ref(const repository_wrapper& repo, const std::string& ref_name) const
{
    if (!ref_name.empty())
    {
        auto branch_commit = repo.resolve_local_ref(m_branch);
        if (!branch_commit)
        {
            throw std::runtime_error("error: could not resolve branch '" + m_branch + "'");
        }
        return std::move(branch_commit).value();
    }
    else
    {
        auto head = repo.head();
        auto branch_commit = repo.find_annotated_commit(head);
        return branch_commit;
    }
}

void rebase_subcommand::perform_rebase(repository_wrapper& repo, rebase_wrapper& rebase)
{
    auto signatures = signature_wrapper::get_default_signature_from_env(repo);

    size_t total_operations = rebase.operation_entry_count();
    std::cout 

Web Proxy Viewer  |  New URL  |  Original Page