-
Notifications
You must be signed in to change notification settings - Fork 3
Add merge subcommand (fastforward)
#48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #include <cassert> | ||
| #include <git2/types.h> | ||
|
|
||
| #include "merge_subcommand.hpp" | ||
| #include "../wrapper/repository_wrapper.hpp" | ||
|
|
||
|
|
||
| merge_subcommand::merge_subcommand(const libgit2_object&, CLI::App& app) | ||
| { | ||
| auto *sub = app.add_subcommand("merge", "Join two or more development histories together"); | ||
|
|
||
| sub->add_option("<branch>", m_branches_to_merge, "Branch(es) to merge"); | ||
|
|
||
| sub->callback([this]() { this->run(); }); | ||
| } | ||
|
|
||
| annotated_commit_list_wrapper resolve_heads(const repository_wrapper& repo, std::vector<std::string> m_branches_to_merge) | ||
| { | ||
| std::vector<annotated_commit_wrapper> commits_to_merge; | ||
| commits_to_merge.reserve(m_branches_to_merge.size()); | ||
|
|
||
| for (const auto branch_name:m_branches_to_merge) | ||
| { | ||
| std::optional<annotated_commit_wrapper> commit = repo.resolve_local_ref(branch_name); | ||
| if (commit.has_value()) | ||
| { | ||
| commits_to_merge.push_back(std::move(commit).value()); | ||
ianthomas23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| return annotated_commit_list_wrapper(std::move(commits_to_merge)); | ||
| } | ||
|
|
||
| void perform_fastforward(repository_wrapper& repo, const git_oid* target_oid, int is_unborn) | ||
JohanMabille marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| const git_checkout_options ff_checkout_options = GIT_CHECKOUT_OPTIONS_INIT; | ||
|
|
||
| auto lambda_get_target_ref = [] (auto repo, auto is_unborn) | ||
| { | ||
| if (!is_unborn) | ||
| { | ||
| return repo->head(); | ||
| } | ||
| else | ||
| { | ||
| return repo->find_reference("HEAD"); | ||
| } | ||
| }; | ||
| auto target_ref = lambda_get_target_ref(&repo, is_unborn); | ||
|
|
||
| auto target = repo.find_object(target_oid, GIT_OBJECT_COMMIT); | ||
JohanMabille marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| repo.checkout_tree(target, &ff_checkout_options); | ||
|
|
||
| auto new_target_ref = target_ref.new_ref(target_oid); | ||
ianthomas23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| void merge_subcommand::run() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
| auto bare = false; | ||
| auto repo = repository_wrapper::open(directory); | ||
|
|
||
| auto state = repo.state(); | ||
| if (state != GIT_REPOSITORY_STATE_NONE) | ||
| { | ||
| std::cout << "repository is in unexpected state " << state <<std::endl; | ||
| } | ||
|
|
||
| git_merge_analysis_t analysis; | ||
| git_merge_preference_t preference; | ||
| annotated_commit_list_wrapper commits_to_merge = resolve_heads(repo, m_branches_to_merge); | ||
| size_t num_commits_to_merge = commits_to_merge.size(); | ||
| git_annotated_commit** c_commits_to_merge = commits_to_merge; | ||
| auto commits_to_merge_const = const_cast<const git_annotated_commit**>(c_commits_to_merge); | ||
|
|
||
| throw_if_error(git_merge_analysis(&analysis, &preference, repo, commits_to_merge_const, num_commits_to_merge)); | ||
|
|
||
| if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) | ||
| { | ||
| std::cout << "Already up-to-date" << std::endl; | ||
| } | ||
| else if (analysis & GIT_MERGE_ANALYSIS_UNBORN || | ||
| (analysis & GIT_MERGE_ANALYSIS_FASTFORWARD && | ||
| !(preference & GIT_MERGE_PREFERENCE_NO_FASTFORWARD))) | ||
| { | ||
| const git_oid* target_oid; | ||
| if (analysis & GIT_MERGE_ANALYSIS_UNBORN) | ||
| { | ||
| std::cout << "Unborn" << std::endl; | ||
| } | ||
| else | ||
| { | ||
| std::cout << "Fast-forward" << std::endl; | ||
| } | ||
| const annotated_commit_wrapper& commit = commits_to_merge.front(); | ||
| target_oid = &commit.oid(); | ||
| assert(num_commits_to_merge == 1); | ||
ianthomas23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #pragma once | ||
|
|
||
| #include <CLI/CLI.hpp> | ||
|
|
||
| #include "../utils/common.hpp" | ||
|
|
||
| class merge_subcommand | ||
| { | ||
| public: | ||
|
|
||
| explicit merge_subcommand(const libgit2_object&, CLI::App& app); | ||
| void run(); | ||
|
|
||
| private: | ||
| std::vector<std::string> m_branches_to_merge; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #include "../wrapper/annotated_commit_wrapper.hpp" | ||
| #include <iostream> | ||
|
|
||
| annotated_commit_wrapper::annotated_commit_wrapper(git_annotated_commit* commit) | ||
| : base_type(commit) | ||
|
|
@@ -21,3 +22,38 @@ std::string_view annotated_commit_wrapper::reference_name() const | |
| const char* res = git_annotated_commit_ref(*this); | ||
| return res ? res : std::string_view{}; | ||
| } | ||
|
|
||
| annotated_commit_list_wrapper::annotated_commit_list_wrapper(std::vector<annotated_commit_wrapper> annotated_commit_list) | ||
|
||
| : m_annotated_commit_list(std::move(annotated_commit_list)) | ||
| { | ||
| p_resource = new git_annotated_commit*[m_annotated_commit_list.size()]; | ||
| for (size_t i=0; i<m_annotated_commit_list.size(); ++i) | ||
| { | ||
| p_resource[i] = m_annotated_commit_list[i]; | ||
| } | ||
| } | ||
|
|
||
| annotated_commit_list_wrapper::~annotated_commit_list_wrapper() | ||
| { | ||
| delete[] p_resource; | ||
| p_resource = nullptr; | ||
| } | ||
|
|
||
| size_t annotated_commit_list_wrapper::size() const | ||
| { | ||
| return m_annotated_commit_list.size(); | ||
| } | ||
|
|
||
| annotated_commit_wrapper annotated_commit_list_wrapper::front() | ||
| { | ||
| return annotated_commit_wrapper(std::move(m_annotated_commit_list.front())); | ||
| } | ||
|
|
||
| void annotated_commit_list_wrapper::print() const | ||
| { | ||
| std::cout << "aclw - p_resource = " << p_resource <<std::endl; | ||
| for (size_t i=0; i<m_annotated_commit_list.size(); ++i) | ||
| { | ||
| std::cout << "aclw - p_resource[i] = :" << p_resource[i] << std::endl; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.