Skip to content

Commit c94bdc3

Browse files
committed
backup
1 parent 339c834 commit c94bdc3

File tree

6 files changed

+110
-5
lines changed

6 files changed

+110
-5
lines changed

src/subcommand/push_subcommand.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "../subcommand/push_subcommand.hpp"
22

33
#include <iostream>
4+
#include <optional>
45

6+
#include <git2/net.h>
57
#include <git2/remote.h>
68
#include <git2/types.h>
79

@@ -33,6 +35,14 @@ push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app)
3335
);
3436
}
3537

38+
int credential_cb(git_cred **out, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload) {
39+
// Replace with your actual credentials
40+
const char* username = user_credentials;
41+
const char *password = "your_password_or_token";
42+
43+
return git_cred_userpass_plaintext_new(out, username, password);
44+
}
45+
3646
void push_subcommand::run()
3747
{
3848
auto directory = get_current_git_path();
@@ -41,6 +51,11 @@ void push_subcommand::run()
4151
std::string remote_name = m_remote_name.empty() ? "origin" : m_remote_name;
4252
auto remote = repo.find_remote(remote_name);
4353

54+
git_direction direction = GIT_DIRECTION_FETCH;
55+
56+
// remote.connect(direction, );
57+
// auto remote_branches_ante_push = remote.ls();
58+
4459
git_push_options push_opts = GIT_PUSH_OPTIONS_INIT;
4560
push_opts.callbacks.credentials = user_credentials;
4661
push_opts.callbacks.push_transfer_progress = push_transfer_progress;
@@ -85,6 +100,7 @@ void push_subcommand::run()
85100
refspecs_ptr = refspecs_wrapper;
86101

87102
remote.push(refspecs_ptr, &push_opts);
103+
auto remote_branches_post_push = remote.ls();
88104

89105
std::cout << "To " << remote.url() << std::endl;
90106
for (const auto& refspec : m_refspecs)
@@ -100,6 +116,33 @@ void push_subcommand::run()
100116
{
101117
short_name = refspec;
102118
}
103-
std::cout << " * " << short_name << " -> " << short_name << std::endl; // " * [new branch] test-"
119+
120+
// std::optional<std::string> branch_upstream_name = repo.branch_upstream_name(short_name);
121+
std::string upstream_name;
122+
upstream_name = short_name;
123+
// if (branch_upstream_name.has_value())
124+
// {
125+
// upstream_name = branch_upstream_name.value();
126+
// }
127+
// else
128+
// {
129+
// // ???
130+
// }
131+
// if (std::find(remote_branches.begin(), remote_branches.end(), short_name) == remote_branches.end())
132+
// {
133+
// std::cout << " * [new branch] " << short_name << " -> " << short_name << std::endl;
134+
// }
135+
//
136+
// if (std::find(remote_branches.begin(), remote_branches.end(), short_name) == remote_branches.end())
137+
// {
138+
// std::cout << " * [new branch] " << short_name << " -> " << short_name << std::endl;
139+
// }
140+
141+
auto ref = repo.find_reference(ref_view);
142+
if (!ref.is_remote())
143+
{
144+
std::cout << " * [new branch] " << short_name << " -> " << upstream_name << std::endl;
145+
}
146+
// std::cout << " * [new branch] " << short_name << " -> " << short_name << std::endl;
104147
}
105148
}

src/wrapper/remote_wrapper.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55

66
#include <git2/remote.h>
7+
#include <git2/types.h>
78

89
#include "../utils/git_exception.hpp"
910

@@ -53,6 +54,44 @@ std::vector<std::string> remote_wrapper::refspecs() const
5354
return result;
5455
}
5556

57+
std::vector<const git_remote_head*> remote_wrapper::ls() const
58+
{
59+
const git_remote_head** remote_heads;
60+
size_t remote_heads_size;
61+
throw_if_error(git_remote_ls(&remote_heads, &remote_heads_size, *this));
62+
63+
std::vector<const git_remote_head*> remote_heads_vec;
64+
for (size_t i = 0; i < remote_heads_size; i++)
65+
{
66+
remote_heads_vec.push_back(remote_heads[i]);
67+
}
68+
return remote_heads_vec;
69+
}
70+
71+
72+
// std::vector<std::string> remote_wrapper::ls() const
73+
// {
74+
// const git_remote_head** remote_heads;
75+
// size_t remote_heads_size;
76+
// throw_if_error(git_remote_ls(&remote_heads, &remote_heads_size, *this));
77+
78+
// std::vector<std::string> remote_branches;
79+
// for (size_t i = 0; i < remote_heads_size; i++)
80+
// {
81+
// const git_remote_head* head = remote_heads[i];
82+
// if (!head->local)
83+
// {
84+
// remote_branches.push_back(head->name);
85+
// }
86+
// }
87+
// return remote_branches;
88+
// }
89+
90+
void remote_wrapper::connect(git_direction direction, const git_remote_callbacks* callbacks)
91+
{
92+
throw_if_error(git_remote_connect(*this, direction, callbacks, NULL, NULL));
93+
}
94+
5695
void remote_wrapper::fetch(const git_strarray* refspecs, const git_fetch_options* opts, const char* reflog_message)
5796
{
5897
throw_if_error(git_remote_fetch(*this, refspecs, opts, reflog_message));

src/wrapper/remote_wrapper.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class remote_wrapper : public wrapper_base<git_remote>
2525

2626
std::vector<std::string> refspecs() const;
2727

28+
std::vector<const git_remote_head*> ls() const;
29+
// std::vector<std::string> ls() const;
30+
31+
void connect(git_direction direction, const git_remote_callbacks* callbacks);
2832
void fetch(const git_strarray* refspecs, const git_fetch_options* opts, const char* reflog_message);
2933
void push(const git_strarray* refspecs, const git_push_options* opts);
3034

src/wrapper/repository_wrapper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <algorithm>
44
#include <fstream>
55
#include <iostream>
6+
#include <optional>
7+
#include <string_view>
8+
#include <git2/buffer.h>
69

710
#include "../utils/git_exception.hpp"
811
#include "../wrapper/commit_wrapper.hpp"
@@ -194,6 +197,21 @@ std::optional<reference_wrapper> repository_wrapper::upstream() const
194197
}
195198
}
196199

200+
std::optional<std::string> repository_wrapper::branch_upstream_name(std::string local_branch) const
201+
{
202+
git_buf buf;
203+
int error = git_branch_upstream_name(&buf, *this, local_branch.c_str());
204+
if (error != 0)
205+
{
206+
return std::nullopt;
207+
}
208+
209+
std::optional<std::string> result;
210+
result = std::string_view(buf.ptr);
211+
git_buf_dispose(&buf);
212+
return result;
213+
}
214+
197215
branch_tracking_info repository_wrapper::get_tracking_info() const
198216
{
199217
branch_tracking_info info;

src/wrapper/repository_wrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <git2.h>
88

9-
#include "../utils/common.hpp"
109
#include "../utils/git_exception.hpp"
1110
#include "../wrapper/annotated_commit_wrapper.hpp"
1211
#include "../wrapper/branch_wrapper.hpp"
@@ -74,6 +73,7 @@ class repository_wrapper : public wrapper_base<git_repository>
7473
branch_wrapper find_branch(std::string_view name) const;
7574
branch_iterator iterate_branches(git_branch_t type) const;
7675
std::optional<reference_wrapper> upstream() const;
76+
std::optional<std::string> branch_upstream_name(std::string local_branch) const;
7777
branch_tracking_info get_tracking_info() const;
7878

7979
// Commits

test/test_push.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_push_private_repo(
6161
assert p_push.returncode == 0
6262
assert p_push.stdout.count("Username:") == 2
6363
assert p_push.stdout.count("Password:") == 2
64-
assert " * test-" in p_push.stdout # " * [new branch] test-"
64+
# assert " * [new branch] test-" in p_push.stdout
6565
print(p_push.stdout)
6666

6767

@@ -107,7 +107,7 @@ def test_push_branch_private_repo(
107107
push_cmd = [git2cpp_path, "push", "origin", branch_name]
108108
p_push = subprocess.run(push_cmd, cwd=repo_path, capture_output=True, text=True, input=input)
109109
assert p_push.returncode == 0
110-
# assert " * [new branch] test-" in p_push.stdout
110+
assert " * [new branch] test-" in p_push.stdout
111111
print("\n\n", p_push.stdout)
112112

113113

@@ -156,5 +156,6 @@ def test_push_branches_flag_private_repo(
156156
push_cmd = [git2cpp_path, "push", "origin", "--branches"]
157157
p_push = subprocess.run(push_cmd, cwd=repo_path, capture_output=True, text=True, input=input)
158158
assert p_push.returncode == 0
159-
# assert " * [new branch] test-" in p_push.stdout
159+
assert " * [new branch] test-" in p_push.stdout
160+
# assert "main" not in p_push.stdout
160161
print("\n\n", p_push.stdout)

0 commit comments

Comments
 (0)