-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 merge_subcommand::resolve_heads(const repository_wrapper& repo) | ||
| { | ||
| 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()); | ||
| } | ||
| } | ||
| return annotated_commit_list_wrapper(std::move(commits_to_merge)); | ||
| } | ||
|
|
||
| void perform_fastforward(repository_wrapper& repo, const git_oid target_oid, int is_unborn) | ||
| { | ||
| 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"); | ||
| } | ||
| }; | ||
| reference_wrapper target_ref = lambda_get_target_ref(&repo, is_unborn); | ||
|
|
||
| object_wrapper target = repo.find_object(target_oid, GIT_OBJECT_COMMIT); | ||
|
|
||
| repo.checkout_tree(target, ff_checkout_options); | ||
|
|
||
| target_ref.write_new_ref(target_oid); | ||
| } | ||
|
|
||
| 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); | ||
| 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))) | ||
| { | ||
| 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(); | ||
| const git_oid target_oid = commit.oid(); | ||
| // Since this is a fast-forward, there can be only one merge head. | ||
| 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)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #pragma once | ||
|
|
||
| #include <CLI/CLI.hpp> | ||
|
|
||
| #include "../utils/common.hpp" | ||
| #include "../wrapper/repository_wrapper.hpp" | ||
|
|
||
| class merge_subcommand | ||
| { | ||
| public: | ||
|
|
||
| explicit merge_subcommand(const libgit2_object&, CLI::App& app); | ||
| void run(); | ||
|
|
||
| private: | ||
|
|
||
| annotated_commit_list_wrapper resolve_heads(const repository_wrapper& repo); | ||
|
|
||
| std::vector<std::string> m_branches_to_merge; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.