-
Notifications
You must be signed in to change notification settings - Fork 3
Add merge commit #53
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
+166
−5
Merged
Add merge commit #53
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,17 @@ | |
| #include <git2/types.h> | ||
|
|
||
| #include "merge_subcommand.hpp" | ||
| // #include "../wrapper/repository_wrapper.hpp" | ||
| #include <iostream> | ||
|
|
||
|
|
||
| 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->add_flag("--no-ff", m_no_ff, ""); | ||
| // sub->add_flag("--commit", m_commit, "Perform the merge and commit the result. This option can be used to override --no-commit."); | ||
| sub->add_flag("--no-commit", m_no_commit, "With --no-commit perform the merge and stop just before creating a merge commit, to give the user a chance to inspect and further tweak the merge result before committing. \nNote that fast-forward updates do not create a merge commit and therefore there is no way to stop those merges with --no-commit. Thus, if you want to ensure your branch is not changed or updated by the merge command, use --no-ff with --no-commit."); | ||
|
|
||
| sub->callback([this]() { this->run(); }); | ||
| } | ||
|
|
@@ -54,6 +57,56 @@ void perform_fastforward(repository_wrapper& repo, const git_oid target_oid, int | |
| target_ref.write_new_ref(target_oid); | ||
| } | ||
|
|
||
| void create_merge_commit(repository_wrapper& repo, const index_wrapper& index, std::vector<std::string> m_branches_to_merge, | ||
| const annotated_commit_list_wrapper& commits_to_merge, size_t num_commits_to_merge) | ||
| { | ||
| auto head_ref = repo.head(); | ||
| auto merge_ref = repo.find_reference_dwim(m_branches_to_merge.front()); | ||
| auto merge_commit = repo.resolve_local_ref(m_branches_to_merge.front()).value(); | ||
|
|
||
| std::vector<commit_wrapper> parents_list; | ||
| parents_list.reserve(num_commits_to_merge + 1); | ||
| parents_list.push_back(std::move(head_ref.peel<commit_wrapper>())); | ||
| for (size_t i=0; i<num_commits_to_merge; ++i) | ||
| { | ||
| parents_list.push_back(repo.find_commit(commits_to_merge[i].oid())); | ||
| } | ||
| auto parents = commit_list_wrapper(std::move(parents_list)); | ||
|
|
||
| auto author_committer_sign = signature_wrapper::get_default_signature_from_env(repo); | ||
| std::string author_name; | ||
| author_name = author_committer_sign.first.name(); | ||
| std::string author_email; | ||
| author_email = author_committer_sign.first.email(); | ||
| auto author_committer_sign_now = signature_wrapper::signature_now(author_name, author_email, author_name, author_email); | ||
|
|
||
| // TODO: add a prompt to edit the merge message | ||
| std::string msg_target = ""; | ||
| if (merge_ref) | ||
| { | ||
| msg_target = merge_ref->short_name(); | ||
| } | ||
| else | ||
| { | ||
| msg_target = git_oid_tostr_s(&(merge_commit.oid())); | ||
| } | ||
|
|
||
| std::string msg = "Merge "; | ||
| if (merge_ref) | ||
| { | ||
| msg.append("branch "); | ||
| } | ||
| else | ||
| { | ||
| msg.append("commit "); | ||
| } | ||
|
||
| msg.append(msg_target); | ||
|
|
||
| repo.create_commit(author_committer_sign_now, msg, std::optional<commit_list_wrapper>(std::move(parents))); | ||
|
|
||
| repo.state_cleanup(); | ||
| } | ||
|
|
||
| void merge_subcommand::run() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
|
|
@@ -78,6 +131,7 @@ void merge_subcommand::run() | |
| if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) | ||
| { | ||
| std::cout << "Already up-to-date" << std::endl; | ||
| return; | ||
| } | ||
| else if (analysis & GIT_MERGE_ANALYSIS_UNBORN || | ||
| (analysis & GIT_MERGE_ANALYSIS_FASTFORWARD && | ||
|
|
@@ -97,4 +151,39 @@ void merge_subcommand::run() | |
| assert(num_commits_to_merge == 1); | ||
| perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN)); | ||
| } | ||
| else if (analysis & GIT_MERGE_ANALYSIS_NORMAL) | ||
| { | ||
| git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; | ||
| git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; | ||
|
|
||
| merge_opts.flags = 0; | ||
| merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3; | ||
|
|
||
| checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS; | ||
|
|
||
| if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) | ||
| { | ||
| std::cout << "Fast-forward is preferred, but only a merge is possible\n" << std::endl; | ||
| } | ||
|
|
||
| throw_if_error(git_merge(repo, | ||
| (const git_annotated_commit**)c_commits_to_merge, | ||
| num_commits_to_merge, | ||
| &merge_opts, | ||
| &checkout_opts)); | ||
| } | ||
|
|
||
| index_wrapper index = repo.make_index(); | ||
|
|
||
| if (git_index_has_conflicts(index)) | ||
| { | ||
| std::cout << "Conflict. To be implemented" << std::endl; | ||
| /* Handle conflicts */ | ||
| // output_conflicts(index); | ||
| } | ||
| else if (!m_no_commit) | ||
| { | ||
| create_merge_commit(repo, index, m_branches_to_merge, commits_to_merge, num_commits_to_merge); | ||
| printf("Merge made\n"); | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_merge_commitcould be a method of the merge_subcommand class; this way it would have access to them_changes_to_mergemember, and this parameter can be removed. If we prefer to keep it as a free function, then the parameter should be renamed inbranches_to_merge(them_prefix should be reserved for class members).