11#include " ../subcommand/push_subcommand.hpp"
22
3+ #include < algorithm>
34#include < iostream>
45#include < optional>
5- #include < unordered_map >
6+ #include < string >
67#include < string_view>
78
89#include < git2.h>
@@ -17,7 +18,6 @@ push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app)
1718 auto * sub = app.add_subcommand (" push" , " Update remote refs along with associated objects" );
1819
1920 sub->add_option (" <remote>" , m_remote_name, " The remote to push to" )->default_val (" origin" );
20- sub->add_option (" <branch>" , m_branch_name, " The branch to push" );
2121 sub->add_option (" <refspec>" , m_refspecs, " The refspec(s) to push" );
2222 sub->add_flag (
2323 " --all,--branches" ,
@@ -71,22 +71,15 @@ void push_subcommand::run()
7171 else if (m_refspecs.empty ())
7272 {
7373 std::string branch;
74- if (!m_branch_name. empty ())
74+ try
7575 {
76- branch = m_branch_name;
76+ auto head_ref = repo.head ();
77+ branch = head_ref.short_name ();
7778 }
78- else
79+ catch (...)
7980 {
80- try
81- {
82- auto head_ref = repo.head ();
83- branch = head_ref.short_name ();
84- }
85- catch (...)
86- {
87- std::cerr << " Could not determine current branch to push." << std::endl;
88- return ;
89- }
81+ std::cerr << " Could not determine current branch to push." << std::endl;
82+ return ;
9083 }
9184 std::string refspec = " refs/heads/" + branch;
9285 m_refspecs.push_back (refspec);
@@ -95,20 +88,34 @@ void push_subcommand::run()
9588 git_strarray* refspecs_ptr = nullptr ;
9689 refspecs_ptr = refspecs_wrapper;
9790
98- // Take a snapshot of remote branches to check which ones are new after push
99- git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
100- callbacks.credentials = user_credentials;
101- credentials_payload creds_payload;
102- callbacks.payload = &creds_payload;
103- push_opts.callbacks .payload = &creds_payload;
104-
105- auto remote_heads = remote.list_heads (&callbacks);
91+ // // Take a snapshot of remote branches to check which ones are new after push
92+ // git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
93+ // callbacks.credentials = user_credentials;
94+ // credentials_payload creds_payload;
95+ // callbacks.payload = &creds_payload;
96+ // push_opts.callbacks.payload = &creds_payload;
10697
98+ // auto remote_heads = remote.list_heads(&callbacks);
99+ //
100+ //
107101 // Map with names of branches and their oids before push
108- std::unordered_map<std::string, git_oid> remote_heads_map;
109- for (const auto & h : remote_heads)
102+ // std::unordered_map<std::string, git_oid> remote_heads_map;
103+ // for (const auto& h : remote_heads)
104+ // {
105+ // remote_heads_map.emplace(h.name, h.oid);
106+ // }
107+
108+ // Take a snapshot of repo's references to check which ones are new after push
109+ auto repo_refs = repo.reference_list ();
110+ std::vector<std::string> repo_refs_remote;
111+ for (int i = 0 ; i < repo_refs.size (); ++i)
110112 {
111- remote_heads_map.emplace (h.name , h.oid );
113+ std::string prefix_remote = " refs/remote/" ;
114+ if (repo_refs[i].substr (0 , prefix_remote.size ()) == prefix_remote)
115+ {
116+ std::string remote_short_name = repo_refs[i].substr (prefix_remote.size ());
117+ repo_refs_remote.push_back (remote_short_name);
118+ }
112119 }
113120
114121 remote.push (refspecs_ptr, &push_opts);
@@ -117,11 +124,11 @@ void push_subcommand::run()
117124 for (const auto & refspec : m_refspecs)
118125 {
119126 std::string_view ref_view (refspec);
120- std::string_view prefix = " refs/heads/" ;
127+ std::string_view prefix_local = " refs/heads/" ;
121128 std::string local_short_name;
122- if (ref_view.substr (0 , prefix .size ()) == prefix )
129+ if (ref_view.substr (0 , prefix_local .size ()) == prefix_local )
123130 {
124- local_short_name = ref_view.substr (prefix .size ());
131+ local_short_name = ref_view.substr (prefix_local .size ());
125132 }
126133 else
127134 {
@@ -148,14 +155,14 @@ void push_subcommand::run()
148155 }
149156 }
150157
151- auto iter = remote_heads_map. find (remote_ref);
152- if (iter == remote_heads_map .end ())
158+ auto iter = std:: find (repo_refs_remote. begin (), repo_refs_remote. end (), remote_ref);
159+ if (iter == repo_refs_remote .end ())
153160 {
154161 std::cout << " * [new branch] " << local_short_name << " -> " << remote_branch << std::endl;
155162 continue ;
156163 }
157164
158- git_oid remote_oid = iter-> second ;
165+ git_oid remote_oid = repo. ref_name_to_id (* iter) ;
159166
160167 std::optional<git_oid> local_oid_opt;
161168 if (auto ref_opt = repo.find_reference_dwim ((" refs/heads/" + local_short_name)))
0 commit comments