|
| 1 | +#include <cassert> |
| 2 | +#include <git2/types.h> |
| 3 | + |
| 4 | +#include "merge_subcommand.hpp" |
| 5 | +// #include "../wrapper/repository_wrapper.hpp" |
| 6 | + |
| 7 | + |
| 8 | +merge_subcommand::merge_subcommand(const libgit2_object&, CLI::App& app) |
| 9 | +{ |
| 10 | + auto *sub = app.add_subcommand("merge", "Join two or more development histories together"); |
| 11 | + |
| 12 | + sub->add_option("<branch>", m_branches_to_merge, "Branch(es) to merge"); |
| 13 | + |
| 14 | + sub->callback([this]() { this->run(); }); |
| 15 | +} |
| 16 | + |
| 17 | +annotated_commit_list_wrapper merge_subcommand::resolve_heads(const repository_wrapper& repo) |
| 18 | +{ |
| 19 | + std::vector<annotated_commit_wrapper> commits_to_merge; |
| 20 | + commits_to_merge.reserve(m_branches_to_merge.size()); |
| 21 | + |
| 22 | + for (const auto branch_name:m_branches_to_merge) |
| 23 | + { |
| 24 | + std::optional<annotated_commit_wrapper> commit = repo.resolve_local_ref(branch_name); |
| 25 | + if (commit.has_value()) |
| 26 | + { |
| 27 | + commits_to_merge.push_back(std::move(commit).value()); |
| 28 | + } |
| 29 | + } |
| 30 | + return annotated_commit_list_wrapper(std::move(commits_to_merge)); |
| 31 | +} |
| 32 | + |
| 33 | +void perform_fastforward(repository_wrapper& repo, const git_oid target_oid, int is_unborn) |
| 34 | +{ |
| 35 | + const git_checkout_options ff_checkout_options = GIT_CHECKOUT_OPTIONS_INIT; |
| 36 | + |
| 37 | + auto lambda_get_target_ref = [] (auto repo, auto is_unborn) |
| 38 | + { |
| 39 | + if (!is_unborn) |
| 40 | + { |
| 41 | + return repo->head(); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + return repo->find_reference("HEAD"); |
| 46 | + } |
| 47 | + }; |
| 48 | + reference_wrapper target_ref = lambda_get_target_ref(&repo, is_unborn); |
| 49 | + |
| 50 | + object_wrapper target = repo.find_object(target_oid, GIT_OBJECT_COMMIT); |
| 51 | + |
| 52 | + repo.checkout_tree(target, ff_checkout_options); |
| 53 | + |
| 54 | + target_ref.write_new_ref(target_oid); |
| 55 | +} |
| 56 | + |
| 57 | +void merge_subcommand::run() |
| 58 | +{ |
| 59 | + auto directory = get_current_git_path(); |
| 60 | + auto bare = false; |
| 61 | + auto repo = repository_wrapper::open(directory); |
| 62 | + |
| 63 | + auto state = repo.state(); |
| 64 | + if (state != GIT_REPOSITORY_STATE_NONE) |
| 65 | + { |
| 66 | + std::cout << "repository is in unexpected state " << state <<std::endl; |
| 67 | + } |
| 68 | + |
| 69 | + git_merge_analysis_t analysis; |
| 70 | + git_merge_preference_t preference; |
| 71 | + annotated_commit_list_wrapper commits_to_merge = resolve_heads(repo); |
| 72 | + size_t num_commits_to_merge = commits_to_merge.size(); |
| 73 | + git_annotated_commit** c_commits_to_merge = commits_to_merge; |
| 74 | + auto commits_to_merge_const = const_cast<const git_annotated_commit**>(c_commits_to_merge); |
| 75 | + |
| 76 | + throw_if_error(git_merge_analysis(&analysis, &preference, repo, commits_to_merge_const, num_commits_to_merge)); |
| 77 | + |
| 78 | + if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) |
| 79 | + { |
| 80 | + std::cout << "Already up-to-date" << std::endl; |
| 81 | + } |
| 82 | + else if (analysis & GIT_MERGE_ANALYSIS_UNBORN || |
| 83 | + (analysis & GIT_MERGE_ANALYSIS_FASTFORWARD && |
| 84 | + !(preference & GIT_MERGE_PREFERENCE_NO_FASTFORWARD))) |
| 85 | + { |
| 86 | + if (analysis & GIT_MERGE_ANALYSIS_UNBORN) |
| 87 | + { |
| 88 | + std::cout << "Unborn" << std::endl; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + std::cout << "Fast-forward" << std::endl; |
| 93 | + } |
| 94 | + const annotated_commit_wrapper& commit = commits_to_merge.front(); |
| 95 | + const git_oid target_oid = commit.oid(); |
| 96 | + // Since this is a fast-forward, there can be only one merge head. |
| 97 | + assert(num_commits_to_merge == 1); |
| 98 | + perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN)); |
| 99 | + } |
| 100 | +} |
0 commit comments