-
Notifications
You must be signed in to change notification settings - Fork 5
Add branches to push subcommand #126
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 3 commits
Commits
Show all changes
4 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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,21 +1,29 @@ | ||||||
| #include "../subcommand/push_subcommand.hpp" | ||||||
|
|
||||||
| #include <iostream> | ||||||
| #include <unordered_map> | ||||||
| #include <utility> | ||||||
|
|
||||||
| #include <git2/remote.h> | ||||||
|
|
||||||
| #include "../utils/ansi_code.hpp" | ||||||
| #include "../utils/common.hpp" | ||||||
| #include "../utils/credentials.hpp" | ||||||
| #include "../utils/progress.hpp" | ||||||
| #include "../wasm/scope.hpp" | ||||||
| #include "../wrapper/repository_wrapper.hpp" | ||||||
|
|
||||||
| push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app) | ||||||
| { | ||||||
| auto* sub = app.add_subcommand("push", "Update remote refs along with associated objects"); | ||||||
|
|
||||||
| sub->add_option("<remote>", m_remote_name, "The remote to push to")->default_val("origin"); | ||||||
|
|
||||||
| sub->add_option("<refspec>", m_refspecs, "The refspec(s) to push"); | ||||||
| sub->add_option("<refspec>", m_refspecs, "The refspec(s) to push")->expected(0, -1); | ||||||
| sub->add_flag( | ||||||
| "--all,--branches", | ||||||
| m_branches_flag, | ||||||
| "Push all branches (i.e. refs under " + ansi_code::bold + "refs/heads/" + ansi_code::reset | ||||||
| + "); cannot be used with other <refspec>." | ||||||
| ); | ||||||
|
|
||||||
| sub->callback( | ||||||
| [this]() | ||||||
|
|
@@ -25,6 +33,131 @@ push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app) | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| void push_subcommand::fill_refspec(repository_wrapper& repo) | ||||||
| { | ||||||
| const std::string prefix = std::string("refs/heads/"); | ||||||
| if (m_branches_flag) | ||||||
| { | ||||||
| auto iter = repo.iterate_branches(GIT_BRANCH_LOCAL); | ||||||
| auto br = iter.next(); | ||||||
| while (br) | ||||||
| { | ||||||
| std::string refspec = std::string(br->name()); | ||||||
| if (refspec.starts_with(prefix)) | ||||||
| { | ||||||
| refspec = refspec.substr(prefix.size()); | ||||||
| } | ||||||
| m_refspecs.push_back(refspec); | ||||||
| br = iter.next(); | ||||||
| } | ||||||
| } | ||||||
| else if (m_refspecs.empty()) | ||||||
| { | ||||||
| std::string branch; | ||||||
| try | ||||||
| { | ||||||
| auto head_ref = repo.head(); | ||||||
| branch = head_ref.short_name(); | ||||||
| } | ||||||
| catch (...) | ||||||
| { | ||||||
| std::cerr << "Could not determine current branch to push." << std::endl; | ||||||
| return; | ||||||
| } | ||||||
| m_refspecs.push_back(branch); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| for (auto& refspec : m_refspecs) | ||||||
| { | ||||||
| if (refspec.starts_with(prefix)) | ||||||
| { | ||||||
| refspec = refspec.substr(prefix.size()); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| std::unordered_map<std::string, git_oid> get_remotes(repository_wrapper& repo, std::string remote_name) | ||||||
| { | ||||||
| std::vector<std::string> repo_refs = repo.refs_list(); | ||||||
| std::unordered_map<std::string, git_oid> remotes_oids; | ||||||
| const std::string prefix = std::string("refs/remotes/") + remote_name + "/"; | ||||||
| for (const auto& r : repo_refs) | ||||||
| { | ||||||
| if (r.size() > prefix.size() && r.compare(0, prefix.size(), prefix) == 0) | ||||||
| { | ||||||
| // r is like "refs/remotes/origin/main" | ||||||
| std::string short_name = r.substr(prefix.size()); // "main" or "feature/x" | ||||||
|
|
||||||
| git_oid oid = repo.ref_name_to_id(r); | ||||||
| remotes_oids.emplace(short_name, oid); | ||||||
| } | ||||||
| } | ||||||
| return remotes_oids; | ||||||
| } | ||||||
|
|
||||||
| std::unordered_map<std::string, git_oid> diff_branches( | ||||||
| std::unordered_map<std::string, git_oid> remotes_before_push, | ||||||
| std::unordered_map<std::string, git_oid> remotes_after_push | ||||||
| ) | ||||||
| { | ||||||
| std::unordered_map<std::string, git_oid> new_branches; | ||||||
| for (const auto& br : remotes_after_push) | ||||||
| { | ||||||
| const std::string name = br.first; | ||||||
| const git_oid& oid = br.second; | ||||||
| if (remotes_before_push.find(name) == remotes_before_push.end()) | ||||||
| { | ||||||
| new_branches.emplace(name, oid); | ||||||
| } | ||||||
| } | ||||||
| return new_branches; | ||||||
| } | ||||||
|
|
||||||
| std::pair<std::vector<std::string>, std::vector<std::string>> | ||||||
| split_refspecs(std::vector<std::string> refspecs, std::unordered_map<std::string, git_oid> new_branches) | ||||||
| { | ||||||
| std::vector<std::string> new_pushed_refspecs; | ||||||
| std::vector<std::string> existing_refspecs; | ||||||
|
|
||||||
| for (const auto refspec : refspecs) | ||||||
| { | ||||||
| if (new_branches.find(refspec) == new_branches.end()) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| existing_refspecs.push_back(refspec); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| new_pushed_refspecs.push_back(refspec); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return std::make_pair(new_pushed_refspecs, existing_refspecs); | ||||||
| } | ||||||
|
|
||||||
| std::pair<std::string, std::string> | ||||||
| get_branch_names(repository_wrapper& repo, std::string remote_name, std::string refspec) | ||||||
| { | ||||||
| std::optional<std::string> upstream_opt = repo.branch_upstream_name(refspec); | ||||||
| std::string remote_branch = refspec; | ||||||
| if (upstream_opt.has_value()) | ||||||
| { | ||||||
| const std::string up_name = upstream_opt.value(); | ||||||
| auto pos = up_name.find('/'); | ||||||
| if (pos != std::string::npos && pos + 1 < up_name.size()) | ||||||
| { | ||||||
| std::string up_remote = up_name.substr(0, pos); | ||||||
| std::string up_branch = up_name.substr(pos + 1); | ||||||
| if (up_remote == remote_name) | ||||||
| { | ||||||
| remote_branch = up_branch; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return std::make_pair(refspec, remote_branch); | ||||||
| } | ||||||
|
|
||||||
| void push_subcommand::run() | ||||||
| { | ||||||
| wasm_http_transport_scope transport; // Enables wasm http(s) transport. | ||||||
|
|
@@ -40,25 +173,52 @@ void push_subcommand::run() | |||||
| push_opts.callbacks.push_transfer_progress = push_transfer_progress; | ||||||
| push_opts.callbacks.push_update_reference = push_update_reference; | ||||||
|
|
||||||
| if (m_refspecs.empty()) | ||||||
| fill_refspec(repo); | ||||||
| std::vector<std::string> refspecs_push; | ||||||
| for (auto refspec : m_refspecs) | ||||||
| { | ||||||
| try | ||||||
| refspecs_push.push_back("refs/heads/" + refspec); | ||||||
| } | ||||||
| git_strarray_wrapper refspecs_wrapper(refspecs_push); | ||||||
| git_strarray* refspecs_ptr = refspecs_wrapper; | ||||||
|
|
||||||
| auto remotes_before_push = get_remotes(repo, remote_name); | ||||||
|
|
||||||
| remote.push(refspecs_ptr, &push_opts); | ||||||
|
|
||||||
| auto remotes_after_push = get_remotes(repo, remote_name); | ||||||
|
|
||||||
| auto new_branches = diff_branches(remotes_before_push, remotes_after_push); | ||||||
| auto [new_pushed_refspecs, existing_refspecs] = split_refspecs(m_refspecs, new_branches); | ||||||
|
|
||||||
| std::cout << "To " << remote.url() << std::endl; | ||||||
| for (const auto& refspec : new_pushed_refspecs) | ||||||
| { | ||||||
| auto [local_short_name, remote_branch] = get_branch_names(repo, remote_name, refspec); | ||||||
|
|
||||||
| std::cout << " * [new branch] " << local_short_name << " -> " << remote_branch << std::endl; | ||||||
| } | ||||||
|
|
||||||
| for (const auto& refspec : existing_refspecs) | ||||||
| { | ||||||
| auto [local_short_name, remote_branch] = get_branch_names(repo, remote_name, refspec); | ||||||
|
|
||||||
| git_oid remote_oid = remotes_before_push[refspec]; | ||||||
|
|
||||||
| git_oid local_oid; | ||||||
| if (auto ref_opt = repo.find_reference_dwim(("refs/heads/" + local_short_name))) | ||||||
| { | ||||||
| auto head_ref = repo.head(); | ||||||
| std::string short_name = head_ref.short_name(); | ||||||
| std::string refspec = "refs/heads/" + short_name; | ||||||
| m_refspecs.push_back(refspec); | ||||||
| const git_oid* target = ref_opt->target(); | ||||||
| local_oid = *target; | ||||||
| } | ||||||
| catch (...) | ||||||
|
|
||||||
| if (!git_oid_equal(&remote_oid, &local_oid)) | ||||||
| { | ||||||
| std::cerr << "Could not determine current branch to push." << std::endl; | ||||||
| return; | ||||||
| std::string old_hex = oid_to_hex(remote_oid); | ||||||
| std::string new_hex = oid_to_hex(local_oid); | ||||||
| // TODO: check order of hex codes | ||||||
| std::cout << " " << old_hex.substr(0, 7) << ".." << new_hex.substr(0, 7) << " " | ||||||
| << local_short_name << " -> " << remote_branch << std::endl; | ||||||
| } | ||||||
| } | ||||||
| git_strarray_wrapper refspecs_wrapper(m_refspecs); | ||||||
| git_strarray* refspecs_ptr = nullptr; | ||||||
| refspecs_ptr = refspecs_wrapper; | ||||||
|
|
||||||
| remote.push(refspecs_ptr, &push_opts); | ||||||
| std::cout << "Pushed to " << remote_name << std::endl; | ||||||
| } | ||||||
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
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 |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| #include <vector> | ||
|
|
||
| #include <git2.h> | ||
| #include <git2/remote.h> | ||
|
|
||
| #include "../wrapper/wrapper_base.hpp" | ||
|
|
||
|
|
||
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.
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.